6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw( qsearch );
10 &untaint_argv; #what it sounds like (eww)
12 getopts("p:a:b:e:", \%opt);
14 my $user = shift or die &usage;
15 my $dbh = adminsuidsetup $user;
19 push @where, 'agentnum = '. $dbh->quote($opt{a}) if $opt{a};
20 push @where, 'cust_pay.payby = '. $dbh->quote($opt{p}) if $opt{p};
21 push @where, 'cust_pay._date > '. $dbh->quote(str2time($opt{b})) if $opt{b};
22 push @where, 'cust_pay._date < '. $dbh->quote(str2time($opt{e})) if $opt{e};
24 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
25 my $addl_from = 'LEFT JOIN cust_main USING( custnum )';
27 my @payrow = qsearch( { table => 'cust_pay',
29 select => 'count(*) AS quantity, paid',
30 addl_from => $addl_from,
31 extra_sql => $extra_sql,
32 order_by => 'GROUP BY paid',
40 $max = $_->quantity if $_->quantity > $max;
42 my $scale = int($max/60) + 1;
44 print "\n PAYMENTS RECEIVED";
45 print " AFTER $opt{b}" if $opt{b};
46 print " UNTIL $opt{e}" if $opt{e};
47 print " VIA $opt{p}" if $opt{p};
48 print " BY AGENT $opt{a}" if $opt{a};
50 print "Total number of payments: $sum\n\n";
51 print "(each * represents $scale)\n\n" if $scale > 1;
53 foreach my $payrow ( @payrow ) {
54 print sprintf("%10.2f", $payrow->paid),
56 sprintf("%6d", $payrow->quantity),
58 '*' x($payrow->quantity/$scale),
70 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
71 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
73 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
79 die "Usage:\n\n cust_pay_histogram [ -b 'begin_date' ] [ -e 'end_date' ] [ -p 'payby' ] [ -a agentnum ] user\n";
88 cust_pay_histogram - Show a histogram of payments made for a date range.
92 freeside-daily [ -b 'begin_date' ] [ -e 'end_date'] [ -p 'payby' ] [ -a agentnum ] user
96 Displays a histogram of cust_pay records in the database.
98 -b: Include only payments since 'begin_date'. Date is in any format Date::Parse is happy with, but be careful.
100 -e: Include only payments before 'end_date'. Date is in any format Date::Parse is happy with, but be careful.
102 -p: Only process payments with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
104 -a: Only process payments of customers with the specified agentnum
106 user: Freeside username