1 <% include( 'elements/search.html',
5 'count_query' => $count_query,
6 'count_addl' => [ '$%.2f total paid', ],
7 'header' => [ 'Payment',
10 FS::UI::Web::cust_header(),
15 if ( $cust_pay->payby eq 'CARD' ) {
16 'Card #'. $cust_pay->paymask;
17 } elsif ( $cust_pay->payby eq 'CHEK' ) {
18 'E-check acct#'. $cust_pay->payinfo;
19 } elsif ( $cust_pay->payby eq 'BILL' ) {
20 'Check #'. $cust_pay->payinfo;
21 } elsif ( $cust_pay->payby eq 'PREP' ) {
22 'Prepaid card #'. $cust_pay->payinfo;
23 } elsif ( $cust_pay->payby eq 'CASH' ) {
24 'Cash '. $cust_pay->payinfo;
25 } elsif ( $cust_pay->payby eq 'WEST' ) {
26 'Western Union'; #. $cust_pay->payinfo;
27 } elsif ( $cust_pay->payby eq 'MCRD' ) {
28 'Manual credit card'; #. $cust_pay->payinfo;
30 $cust_pay->payby. ' '. $cust_pay->payinfo;
33 sub { sprintf('$%.2f', shift->paid ) },
34 sub { time2str('%b %d %Y', shift->_date ) },
35 \&FS::UI::Web::cust_fields,
38 'align' => 'rrr'.FS::UI::Web::cust_aligns(),
43 ( map { $_ ne 'Cust. Status' ? $link : '' }
44 FS::UI::Web::cust_header()
51 FS::UI::Web::cust_colors(),
57 FS::UI::Web::cust_styles(),
64 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
66 my $title = 'Payment Search Results';
67 my( $count_query, $sql_query );
68 if ( $cgi->param('magic') ) {
72 if ( $cgi->param('magic') eq '_date' ) {
75 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
76 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
77 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
78 die "unknown agentnum $1" unless $agent;
79 $title = $agent->agent. " $title";
82 if ( $cgi->param('payby') ) {
83 $cgi->param('payby') =~
84 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
85 or die "illegal payby ". $cgi->param('payby');
86 push @search, "cust_pay.payby = '$1'";
92 if ( $cardtype eq 'VisaMC' ) {
93 #avoid posix regexes for portability
95 " ( ( substring(cust_pay.payinfo from 1 for 1) = '4' ".
96 " AND substring(cust_pay.payinfo from 1 for 4) != '4936' ".
97 " AND substring(cust_pay.payinfo from 1 for 6) ".
98 " NOT SIMILAR TO '49030[2-9]' ".
99 " AND substring(cust_pay.payinfo from 1 for 6) ".
100 " NOT SIMILAR TO '49033[5-9]' ".
101 " AND substring(cust_pay.payinfo from 1 for 6) ".
102 " NOT SIMILAR TO '49110[1-2]' ".
103 " AND substring(cust_pay.payinfo from 1 for 6) ".
104 " NOT SIMILAR TO '49117[4-9]' ".
105 " AND substring(cust_pay.payinfo from 1 for 6) ".
106 " NOT SIMILAR TO '49118[1-2]' ".
108 " OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
109 " OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
110 " OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
111 " OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
112 " OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
113 " OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
114 " OR substring(cust_pay.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
116 } elsif ( $cardtype eq 'Amex' ) {
118 " ( substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
119 " OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
121 } elsif ( $cardtype eq 'Discover' ) {
123 " ( substring(cust_pay.payinfo from 1 for 4 ) = '6011' ".
124 " OR substring(cust_pay.payinfo from 1 for 2 ) = '65' ".
125 " OR substring(cust_pay.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
127 } elsif ( $cardtype eq 'Maestro' ) {
129 " ( substring(cust_pay.payinfo from 1 for 2 ) = '63' ".
130 " OR substring(cust_pay.payinfo from 1 for 2 ) = '67' ".
131 " OR substring(cust_pay.payinfo from 1 for 6 ) = '564182' ".
132 " OR substring(cust_pay.payinfo from 1 for 4 ) = '4936' ".
133 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
134 " SIMILAR TO '49030[2-9]' ".
135 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
136 " SIMILAR TO '49033[5-9]' ".
137 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
138 " SIMILAR TO '49110[1-2]' ".
139 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
140 " SIMILAR TO '49117[4-9]' ".
141 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
142 " SIMILAR TO '49118[1-2]' ".
145 die "unknown card type $cardtype";
148 my $masksearch = $search;
149 $masksearch =~ s/cust_pay\.payinfo/cust_pay.paymask/gi;
152 "( $search OR ( cust_pay.paymask IS NOT NULL AND $masksearch ) )";
157 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
158 push @search, "_date >= $beginning ",
161 push @search, FS::UI::Web::parse_lt_gt($cgi, 'paid' );
165 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
167 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
168 or die "illegal paybatch: ". $cgi->param('paybatch');
170 push @search, "paybatch = '$1'";
172 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
175 die "unknown search magic: ". $cgi->param('magic');
178 #here is the agent virtualization
179 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
181 my $search = ' WHERE '. join(' AND ', @search);
183 $count_query = "SELECT COUNT(*), SUM(paid) ".
184 "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
188 'table' => 'cust_pay',
189 'select' => join(', ',
191 'cust_main.custnum as cust_main_custnum',
192 FS::UI::Web::cust_sql_fields(),
195 'extra_sql' => "$search ORDER BY $orderby",
196 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
201 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
204 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
207 $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay".
208 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
209 " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql;
212 'table' => 'cust_pay',
213 'hashref' => { 'payinfo' => $payinfo,
215 'extra_sql' => $FS::CurrentUser::CurrentUser->agentnums_sql.
222 my $cust_pay = shift;
223 $cust_pay->cust_main_custnum
224 ? [ "${p}view/cust_main.cgi?", 'custnum' ]