RT-Client-Tools/scripts/rt-bulk-prep-pay.plx
Bradley M. Kuhn 8ac2663124 Improve totals and subtotals output amounts
Various changes that I did a while ago to better support totals and
subtotals output.
2020-06-22 10:18:30 -07:00

141 lines
5.6 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright © 2018, Bradley M. Kuhn
# License: AGPL-3.0-or-later
use strict;
use warnings;
use autodie qw(:all);
use Getopt::Long;
use File::Spec;
use Date::Manip qw(ParseDate UnixDate);
my($VERBOSE, $INTERACTIVE, $RT_CMD, $SVN_CMD, %AMOUNTS, $SHOW_AMOUNTS, $QUERY_STRING, $SUB_TOTALS);
###############################################################################
sub TicketIDsReadyForPayment () {
my @ticketSpecs;
my $queryString = $QUERY_STRING;
if (not defined $queryString or $queryString =~ /^\s*$/) {
$queryString = "Status = 'ready-for-payment' and Owner = 'bkuhn' and Queue = 'outreachy-interns' and Subject like '%2019-12%'";
$queryString = "Status = 'ready-for-payment' and Owner = 'bkuhn' and Due < '2020-04-30'";
$queryString = "Status = 'ready-for-payment'";
$queryString = "Status = 'ready-for-payment' and 'CF.{payment-method}' = 'USD PayPal'";
$queryString = "Status = 'ready-for-payment'";
}
print "Using $queryString\n";
open(my $rtLsFH, "-|", "$RT_CMD", "ls", "-i", $queryString);
print "Running: rt ls -i Status=ready-for-payment\n" if ($VERBOSE >= 10);
while (my $lsLine = <$rtLsFH>) {
print "rt ls line from Ready for payment search: $lsLine" if ($VERBOSE >= 10);
chomp $lsLine;
next if $lsLine =~ /^\s*$/; # There are sometimes blank lines in the rt ls -i output,
push(@ticketSpecs, $lsLine); # particularly with 0 results, and no empty strings go on this list
}
print "Done: rt ls -i Status=ready-for-payment\n" if ($VERBOSE >= 10);
close $rtLsFH;
return @ticketSpecs;
}
###############################################################################
sub FindMostRecentPaymentMethodForTicket ($) {
my($ticketSpec) = @_;
my $paymentMethod;
$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
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;
}
}
close $rtShowFH;
return ($value eq 'FX Wire' or $value eq 'NO-CF-SET') ? "$value: $paymentMethod" : $value;
}
###############################################################################
GetOptions("verbose=i" => \$VERBOSE, "interactive" => \$INTERACTIVE, 'showAmounts' => \$SHOW_AMOUNTS,
'subTotals' => \$SUB_TOTALS,
"rtCommand=s" => \$RT_CMD, "svnCommand=s" => \$SVN_CMD, 'queryString=s' => \$QUERY_STRING);
$SHOW_AMOUNTS = 1 if $SUB_TOTALS;
$RT_CMD = '/usr/bin/rt' unless defined $RT_CMD;
$SVN_CMD = '/usr/bin/svn' unless defined $SVN_CMD;
$INTERACTIVE = 0 if not defined $INTERACTIVE;
my @ticketSpecs = TicketIDsReadyForPayment();
if (@ticketSpecs <= 0) {
print "No tickets found that are ready for payment at this time\n";
exit 0;
}
my %payments;
print "Total tickets ready for payment is: ", scalar(@ticketSpecs), "\n\n";
foreach my $ticketSpec (@ticketSpecs) {
my $paymentMethod = FindMostRecentPaymentMethodForTicket($ticketSpec);
print STDERR "$ticketSpec has payment method of $paymentMethod\n" if ($VERBOSE > 6);
if (not defined $paymentMethod) {
warn "Cannot find payment method for ticket, $ticketSpec";
$paymentMethod = "UNKNOWN PAYMENT METHOD";
}
push(@{$payments{$paymentMethod}}, $ticketSpec);
}
print "#" x 70, "\n";
my ($overallTotal, $overallTotalOk) = (0.00, 1);
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);
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; }
}
if ($subTotalOk) {
print sprintf("%30s: \$%.2f\n\n", "SUBTOTAL for $paymentMethod", $subTotal);
$overallTotal += $subTotal;
} else {
$overallTotalOk = 0;
}
}
}
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; }
}
}
print sprintf("%30s: \$%.2f\n", "TOTAL AMOUNT", $overallTotal) if $overallTotalOk and $SHOW_AMOUNTS;
###############################################################################
#
# Local variables:
# compile-command: "perl -c rt-bulk-prep-pay.plx"
# perl-indent-level: 2
# End: