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' => join(', ',
78 'cust_main.custnum as cust_main_custnum',
79 FS::UI::Web::cust_sql_fields(),
82 'extra_sql' => "$search ORDER BY _date",
83 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
88 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
91 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
94 $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay ".
95 "WHERE payinfo = '$payinfo' AND payby = '$payby'";
98 'table' => 'cust_pay',
99 'hashref' => { 'payinfo' => $payinfo,
101 'extra_sql' => "ORDER BY _date",
107 my $cust_pay = shift;
108 $cust_pay->cust_main_custnum
109 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
113 %><%= include( 'elements/search.html',
115 'name' => 'payments',
116 'query' => $sql_query,
117 'count_query' => $count_query,
118 'count_addl' => [ '$%.2f total paid', ],
119 'header' => [ 'Payment',
122 FS::UI::Web::cust_header(),
126 my $cust_pay = shift;
127 if ( $cust_pay->payby eq 'CARD' ) {
128 'Card #'. $cust_pay->payinfo_masked;
129 } elsif ( $cust_pay->payby eq 'CHEK' ) {
130 'E-check acct#'. $cust_pay->payinfo;
131 } elsif ( $cust_pay->payby eq 'BILL' ) {
132 'Check #'. $cust_pay->payinfo;
133 } elsif ( $cust_pay->payby eq 'PREP' ) {
134 'Prepaid card #'. $cust_pay->payinfo;
136 $cust_pay->payby. ' '. $cust_pay->payinfo;
139 sub { sprintf('$%.2f', shift->paid ) },
140 sub { time2str('%b %d %Y', shift->_date ) },
141 \&FS::UI::Web::cust_fields,
143 #'align' => 'lrrrll',
149 ( map { $link } FS::UI::Web::cust_header() ),