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