2 my $title = 'Payment Search Results';
3 my( $count_query, $sql_query );
4 if ( $cgi->param('magic') && $cgi->param('magic') eq '_date' ) {
8 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
9 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
10 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
11 die "unknown agentnum $1" unless $agent;
12 $title = $agent->agent. " $title";
15 if ( $cgi->param('payby') ) {
16 $cgi->param('payby') =~ /^(CARD|CHEK|BILL)(-(VisaMC|Amex|Discover))?$/
17 or die "illegal payby ". $cgi->param('payby');
18 push @search, "cust_pay.payby = '$1'";
20 if ( $3 eq 'VisaMC' ) {
21 #avoid posix regexes for portability
23 " ( substring(cust_pay.payinfo from 1 for 1) = '4' ".
24 " OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
25 " OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
26 " OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
27 " OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
28 " OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
29 " OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
31 } elsif ( $3 eq 'Amex' ) {
33 " ( substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
34 " OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
36 } elsif ( $3 eq 'Discover' ) {
38 " substring(cust_pay.payinfo from 1 for 4 ) = '6011' ";
40 die "unknown card type $3";
45 #false laziness with cust_pkg.cgi
46 if ( $cgi->param('beginning')
47 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
48 my $beginning = str2time($1);
49 push @search, "_date >= $beginning ";
51 if ( $cgi->param('ending')
52 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
53 my $ending = str2time($1) + 86399;
54 push @search, " _date <= $ending ";
56 if ( $cgi->param('begin')
57 && $cgi->param('begin') =~ /^(\d+)$/ ) {
58 push @search, "_date >= $1 ";
60 if ( $cgi->param('end')
61 && $cgi->param('end') =~ /^(\d+)$/ ) {
62 push @search, " _date < $1 ";
67 $search = ' WHERE '. join(' AND ', @search);
70 $count_query = "SELECT COUNT(*), SUM(paid) ".
71 "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
75 'table' => 'cust_pay',
76 'select' => 'cust_pay.*, cust_main.last, cust_main.first, cust_main.company',
78 'extra_sql' => "$search ORDER BY _date",
79 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
84 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
87 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
90 $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay ".
91 "WHERE payinfo = '$payinfo' AND payby = '$payby'";
94 'table' => 'cust_pay',
95 'hashref' => { 'payinfo' => $payinfo,
97 'extra_sql' => "ORDER BY _date",
102 my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
104 %><%= include( 'elements/search.html',
106 'name' => 'payments',
107 'query' => $sql_query,
108 'count_query' => $count_query,
109 'count_addl' => [ '$%.2f total paid', ],
111 [ qw(Payment Amount Date), 'Cust #', 'Contact name',
115 my $cust_pay = shift;
116 if ( $cust_pay->payby eq 'CARD' ) {
117 'Card #'. $cust_pay->payinfo_masked;
118 } elsif ( $cust_pay->payby eq 'CHEK' ) {
119 'E-check acct#'. $cust_pay->payinfo;
120 } elsif ( $cust_pay->payby eq 'BILL' ) {
121 'Check #'. $cust_pay->payinfo;
123 $cust_pay->payby. ' '. $cust_pay->payinfo;
126 sub { sprintf('$%.2f', shift->paid ) },
127 sub { time2str('%b %d %Y', shift->_date ) },
129 sub { $_[0]->get('last'). ', '. $_[0]->first; },