1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#!/usr/bin/perl -w
use strict;
use Getopt::Std;
use Date::Parse;
use FS::UID qw(adminsuidsetup);
use FS::Record qw( qsearch );
use FS::cust_pay;
&untaint_argv; #what it sounds like (eww)
use vars qw(%opt);
getopts("p:a:b:e:", \%opt);
my $user = shift or die &usage;
my $dbh = adminsuidsetup $user;
my @where = ();
push @where, 'agentnum = '. $dbh->quote($opt{a}) if $opt{a};
push @where, 'cust_pay.payby = '. $dbh->quote($opt{p}) if $opt{p};
push @where, 'cust_pay._date > '. $dbh->quote(str2time($opt{b})) if $opt{b};
push @where, 'cust_pay._date < '. $dbh->quote(str2time($opt{e})) if $opt{e};
my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
my $addl_from = 'LEFT JOIN cust_main USING( custnum )';
my @payrow = qsearch( { table => 'cust_pay',
hashref => {},
select => 'count(*) AS quantity, paid',
addl_from => $addl_from,
extra_sql => $extra_sql,
order_by => 'GROUP BY paid',
}
);
my $max = 0;
my $sum = 0;
foreach (@payrow) {
$sum += $_->quantity;
$max = $_->quantity if $_->quantity > $max;
}
my $scale = int($max/60) + 1;
print "\n PAYMENTS RECEIVED";
print " AFTER $opt{b}" if $opt{b};
print " UNTIL $opt{e}" if $opt{e};
print " VIA $opt{p}" if $opt{p};
print " BY AGENT $opt{a}" if $opt{a};
print "\n\n";
print "Total number of payments: $sum\n\n";
print "(each * represents $scale)\n\n" if $scale > 1;
foreach my $payrow ( @payrow ) {
print sprintf("%10.2f", $payrow->paid),
": ",
sprintf("%6d", $payrow->quantity),
"| ",
'*' x($payrow->quantity/$scale),
"\n";
}
print "\n";
###
# subroutines
###
sub untaint_argv {
foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
#$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
# Date::Parse
$ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
$ARGV[$_]=$1;
}
}
sub usage {
die "Usage:\n\n cust_pay_histogram [ -b 'begin_date' ] [ -e 'end_date' ] [ -p 'payby' ] [ -a agentnum ] user\n";
}
###
# documentation
###
=head1 NAME
cust_pay_histogram - Show a histogram of payments made for a date range.
=head1 SYNOPSIS
freeside-daily [ -b 'begin_date' ] [ -e 'end_date'] [ -p 'payby' ] [ -a agentnum ] user
=head1 DESCRIPTION
Displays a histogram of cust_pay records in the database.
-b: Include only payments since 'begin_date'. Date is in any format Date::Parse is happy with, but be careful.
-e: Include only payments before 'end_date'. Date is in any format Date::Parse is happy with, but be careful.
-p: Only process payments with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
-a: Only process payments of customers with the specified agentnum
user: From the mapsecrets file - see config.html from the base documentation
=head1 BUGS
=head1 SEE ALSO
L<FS::cust_pay>
=cut
|