Rework to handle new format of custom fields for payment methods.

This commit is contained in:
Bradley M. Kuhn 2020-07-21 13:50:43 -07:00
parent 880da9360c
commit 6c2d685116

View file

@ -46,31 +46,26 @@ sub FindMostRecentPaymentMethodForTicket ($) {
$ticketSpec =~ s%^\s*ticket\s*/\s*%%;
open(my $rtShowFH, "-|", "$RT_CMD", "show", '-t', 'ticket', '-f', 'CF.{payment-method}', $ticketSpec);
my($ticketDataAgain, $value) = <$rtShowFH>;
close $rtShowFH;
chomp $value; $value =~ s/^\s*cf.{payment-method}\s*:\s+(.*)$/$1/i
chomp $value; $value =~ s/^\s*cf.\{payment-method\}\s*:\s+(.*)$/$1/i
or warn "$ticketSpec: Unknown value for payment-method of \"$value\"";
if (not defined $value or $value =~ /^\s*$/) {
$value = "NO-CF-SET";
} elsif ($value ne 'FX Wire') {
return $value unless $SHOW_AMOUNTS;
}
$ticketSpec =~ s%^\s*ticket\s*/\s*%%;
open($rtShowFH, "-|", "$RT_CMD", "show", $ticketSpec);
while (my $showLine = <$rtShowFH>) {
print STDERR "rt show line for $ticketSpec: $showLine" if ($VERBOSE >= 10);
$paymentMethod = lc($1)
if ($showLine =~ /^\s*PAYMENT\s+METHOD\s*:\s*(.*?)\s*$/);
# don't 'last' when found as we want the last one.
if ($showLine =~ /^\s*(?:TOTAL|AMOUNT)(?:\s*TO\s*PAY)?\s*:\s*(.*?)\s*$/i) {
$AMOUNTS{$ticketSpec} = $1;
$AMOUNTS{$ticketSpec} =~ s/\$//g; $AMOUNTS{$ticketSpec} =~ s/,//g; $AMOUNTS{$ticketSpec} =~ s/\s//g;
$AMOUNTS{$ticketSpec} =~ s/\s*U\s*sS\s*D\s*$//g;
}
}
if ($SHOW_AMOUNTS) {
open(my $rtShowFH, "-|", "$RT_CMD", "show", '-t', 'ticket', '-f', 'CF.{payment-amount}', $ticketSpec);
my($ticketDataYetAgain, $curAndAmount) = <$rtShowFH>;
close $rtShowFH;
return ($value eq 'FX Wire' or $value eq 'NO-CF-SET') ? "$value: $paymentMethod" : $value;
chomp $curAndAmount; $curAndAmount =~ s/^\s*cf.\{payment-amount\}\s*:\s+(.*)$/$1/i;
$ticketSpec =~ s%^\s*ticket\s*/\s*%%;
$curAndAmount = "USD -32767.69" if not defined $curAndAmount;
$curAndAmount =~ /^\s*([A-Z]{3,3})\s+([\-\d\,\.]+)\s*$/ or die "Ticket, $ticketSpec, has amount of $curAndAmount";
($AMOUNTS{$ticketSpec}{currency}, $AMOUNTS{$ticketSpec}{amount}) = ($1, $2);
$AMOUNTS{$ticketSpec}{amount} =~ s/\$//g; $AMOUNTS{$ticketSpec}{amount} =~ s/,//g;
$AMOUNTS{$ticketSpec}{currency} =~ s/\s//g;
}
return $value;
}
###############################################################################
@ -104,21 +99,28 @@ foreach my $ticketSpec (@ticketSpecs) {
push(@{$payments{$paymentMethod}}, $ticketSpec);
}
print "#" x 70, "\n";
my ($overallTotal, $overallTotalOk) = (0.00, 1);
my $overallTotalOk = 1;
my %overallTotals;
foreach my $paymentMethod (sort { $a cmp $b } keys %payments) {
print "$paymentMethod: Count: ", scalar(@{$payments{$paymentMethod}}), "\n";
print " Tickets: ", join(" ", map { s%^\s*ticket\s*/\s*%%; $_; } @{$payments{$paymentMethod}}), "\n";
my($subTotal, $subTotalOk) = (0.00, 1);
my $subTotalOk = 1;
my %subTotals;
if ($SUB_TOTALS) {
foreach my $tix (@{$payments{$paymentMethod}}) {
$tix =~ s%^\s*ticket\s*/\s*%%;
my $val = $AMOUNTS{$tix};
print sprintf(" %5d: \$%.2f\n", $tix, $AMOUNTS{$tix});
if ($val =~ /^[\d\.]+$/) { $subTotal += $val; } else { $subTotalOk = 0; }
my($currency, $val) = ($AMOUNTS{$tix}{currency}, $AMOUNTS{$tix}{amount});
$subTotals{$currency} = 0.00 unless defined $subTotals{$currency};
print sprintf(" %5d: %.2f %3.3s\n", $tix, $val, $currency);
if (defined $val and $val =~ /^[\d\.]+$/) { $subTotals{$currency} += $val; } else { $subTotalOk = 0; }
}
if ($subTotalOk) {
print sprintf("%30s: \$%.2f\n\n", "SUBTOTAL for $paymentMethod", $subTotal);
$overallTotal += $subTotal;
print sprintf("%30s", "SUBTOTAL for $paymentMethod\n");
foreach my $currency (keys %subTotals) {
print sprintf("%30s %.2f %3.3s\n\n", " ", $subTotals{$currency}, $currency);
$overallTotals{$currency} = 0.00 unless defined $overallTotals{$currency};
$overallTotals{$currency} += $subTotals{$currency};
}
} else {
$overallTotalOk = 0;
}
@ -127,12 +129,20 @@ foreach my $paymentMethod (sort { $a cmp $b } keys %payments) {
if ($SHOW_AMOUNTS and not $SUB_TOTALS) {
print "Unable to find amounts for all tickets\n" if (scalar(@ticketSpecs) != scalar(keys %AMOUNTS));
foreach my $tix (sort { $a <=> $b} (keys %AMOUNTS)) {
my $val = $AMOUNTS{$tix};
print sprintf("%5d: \$%.2f\n", $tix, $AMOUNTS{$tix});
if ($val =~ /^[\d\.]+$/) { $overallTotal += $val; } else { $overallTotalOk = 0; }
foreach my $currency (keys %overallTotals) {
my $val = $AMOUNTS{$tix}{$currency};
print sprintf("%5d: %.2f %3.3d\n", $tix, $val, $currency);
$overallTotals{$currency} = 0.00 unless defined $overallTotals{$currency};
if ($val =~ /^[\d\.]+$/) { $overallTotals{$currency} += $val; } else { $overallTotalOk = 0; }
}
}
}
if ($overallTotalOk and $SHOW_AMOUNTS) {
print sprintf("%30s:\n", "TOTAL AMOUNT");
foreach my $currency (keys %overallTotals) {
print sprintf("%30s %.2f %3.3s\n\n", " ", $overallTotals{$currency}, $currency);
}
}
print sprintf("%30s: \$%.2f\n", "TOTAL AMOUNT", $overallTotal) if $overallTotalOk and $SHOW_AMOUNTS;
###############################################################################
#
# Local variables: