1 <% include( 'elements/search.html',
5 'count_query' => $count_query,
6 'count_addl' => [ '$%.2f total paid', ],
7 'header' => [ 'Payment',
11 FS::UI::Web::cust_header(),
16 if ( $cust_pay->payby eq 'CARD' ) {
17 'Card #'. $cust_pay->paymask;
18 } elsif ( $cust_pay->payby eq 'CHEK' ) {
19 'E-check acct#'. $cust_pay->payinfo;
20 } elsif ( $cust_pay->payby eq 'BILL' ) {
21 'Check #'. $cust_pay->payinfo;
22 } elsif ( $cust_pay->payby eq 'PREP' ) {
23 'Prepaid card #'. $cust_pay->payinfo;
24 } elsif ( $cust_pay->payby eq 'CASH' ) {
25 'Cash '. $cust_pay->payinfo;
26 } elsif ( $cust_pay->payby eq 'WEST' ) {
27 'Western Union'; #. $cust_pay->payinfo;
28 } elsif ( $cust_pay->payby eq 'MCRD' ) {
29 'Manual credit card'; #. $cust_pay->payinfo;
31 $cust_pay->payby. ' '. $cust_pay->payinfo;
34 sub { sprintf('$%.2f', shift->paid ) },
35 sub { time2str('%b %d %Y', shift->_date ) },
36 sub { my $o = shift->otaker;
37 $o = 'auto billing' if $o eq 'fs_daily';
38 $o = 'customer self-service' if $o eq 'fs_selfservice';
41 \&FS::UI::Web::cust_fields,
44 'align' => 'rrrc'.FS::UI::Web::cust_aligns(),
50 ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
51 FS::UI::Web::cust_header()
59 FS::UI::Web::cust_colors(),
66 FS::UI::Web::cust_styles(),
73 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
75 my $title = 'Payment Search Results';
76 my( $count_query, $sql_query );
77 if ( $cgi->param('magic') ) {
81 if ( $cgi->param('magic') eq '_date' ) {
84 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
85 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
86 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
87 die "unknown agentnum $1" unless $agent;
88 $title = $agent->agent. " $title";
91 if ( $cgi->param('payby') ) {
92 $cgi->param('payby') =~
93 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
94 or die "illegal payby ". $cgi->param('payby');
95 push @search, "cust_pay.payby = '$1'";
101 if ( $cardtype eq 'VisaMC' ) {
102 #avoid posix regexes for portability
104 " ( ( substring(cust_pay.payinfo from 1 for 1) = '4' ".
105 " AND substring(cust_pay.payinfo from 1 for 4) != '4936' ".
106 " AND substring(cust_pay.payinfo from 1 for 6) ".
107 " NOT SIMILAR TO '49030[2-9]' ".
108 " AND substring(cust_pay.payinfo from 1 for 6) ".
109 " NOT SIMILAR TO '49033[5-9]' ".
110 " AND substring(cust_pay.payinfo from 1 for 6) ".
111 " NOT SIMILAR TO '49110[1-2]' ".
112 " AND substring(cust_pay.payinfo from 1 for 6) ".
113 " NOT SIMILAR TO '49117[4-9]' ".
114 " AND substring(cust_pay.payinfo from 1 for 6) ".
115 " NOT SIMILAR TO '49118[1-2]' ".
117 " OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
118 " OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
119 " OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
120 " OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
121 " OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
122 " OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
123 " OR substring(cust_pay.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
125 } elsif ( $cardtype eq 'Amex' ) {
127 " ( substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
128 " OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
130 } elsif ( $cardtype eq 'Discover' ) {
132 " ( substring(cust_pay.payinfo from 1 for 4 ) = '6011' ".
133 " OR substring(cust_pay.payinfo from 1 for 2 ) = '65' ".
134 " OR substring(cust_pay.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
136 } elsif ( $cardtype eq 'Maestro' ) {
138 " ( substring(cust_pay.payinfo from 1 for 2 ) = '63' ".
139 " OR substring(cust_pay.payinfo from 1 for 2 ) = '67' ".
140 " OR substring(cust_pay.payinfo from 1 for 6 ) = '564182' ".
141 " OR substring(cust_pay.payinfo from 1 for 4 ) = '4936' ".
142 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
143 " SIMILAR TO '49030[2-9]' ".
144 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
145 " SIMILAR TO '49033[5-9]' ".
146 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
147 " SIMILAR TO '49110[1-2]' ".
148 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
149 " SIMILAR TO '49117[4-9]' ".
150 " OR substring(cust_pay.payinfo from 1 for 6 ) ".
151 " SIMILAR TO '49118[1-2]' ".
154 die "unknown card type $cardtype";
157 my $masksearch = $search;
158 $masksearch =~ s/cust_pay\.payinfo/cust_pay.paymask/gi;
161 "( $search OR ( cust_pay.paymask IS NOT NULL AND $masksearch ) )";
166 if ( $cgi->param('payinfo') ) {
167 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
168 or die "illegal payinfo ". $cgi->param('payinfo');
169 push @search, "cust_pay.payinfo = '$1'";
172 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
173 push @search, "_date >= $beginning ",
176 push @search, FS::UI::Web::parse_lt_gt($cgi, 'paid' );
180 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
182 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
183 or die "illegal paybatch: ". $cgi->param('paybatch');
185 push @search, "paybatch = '$1'";
187 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
190 die "unknown search magic: ". $cgi->param('magic');
193 #here is the agent virtualization
194 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
196 my $search = ' WHERE '. join(' AND ', @search);
198 $count_query = "SELECT COUNT(*), SUM(paid) ".
199 "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
203 'table' => 'cust_pay',
204 'select' => join(', ',
206 'cust_main.custnum as cust_main_custnum',
207 FS::UI::Web::cust_sql_fields(),
210 'extra_sql' => "$search ORDER BY $orderby",
211 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
216 #hmm... is this still used?
218 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
221 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
224 $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay".
225 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
226 " AND ". $FS::CurrentUser::CurrentUser->agentnums_sql;
229 'table' => 'cust_pay',
230 'hashref' => { 'payinfo' => $payinfo,
232 'extra_sql' => $FS::CurrentUser::CurrentUser->agentnums_sql.
239 $link = [ "${p}view/cust_pay.html?paynum=", 'paynum' ]
240 if $FS::CurrentUser::CurrentUser->access_right('View invoices'); #XXX for now
241 #later# if $FS::CurrentUser::CurrentUser->access_right('View customer payments');
243 my $cust_link = sub {
244 my $cust_pay = shift;
245 $cust_pay->cust_main_custnum
246 ? [ "${p}view/cust_main.cgi?", 'custnum' ]