5 include( 'elements/cust_pay_or_refund.html',
7 'amount_field' => 'paid',
8 'name_singular' => 'payment',
12 include( 'elements/cust_pay_or_refund.html',
14 'amount_field' => 'refund',
15 'name_singular' => 'refund',
16 'name_verb' => 'refunded',
19 include( 'elements/cust_pay_or_refund.html',
20 'thing' => 'pay_pending',
21 'amount_field' => 'paid',
22 'name_singular' => 'pending payment',
23 'name_verb' => 'pending',
29 'redirect_empty' => $redirect_empty,
33 <% include( 'search.html',
35 'name_singular' => $name_singular,
36 'query' => $sql_query,
37 'count_query' => $count_query,
38 'count_addl' => [ '$%.2f total '.$opt{name_verb}, ],
39 'redirect_empty' => $opt{'redirect_empty'},
40 'header' => [ "\u$name_singular",
44 FS::UI::Web::cust_header(),
47 'payby_payinfo_pretty',
48 sub { sprintf('$%.2f', shift->$amount_field() ) },
49 sub { time2str('%b %d %Y', shift->_date ) },
51 \&FS::UI::Web::cust_fields,
55 join('', map 'c', @fields ).
56 FS::UI::Web::cust_aligns(),
62 ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
63 FS::UI::Web::cust_header()
71 FS::UI::Web::cust_colors(),
78 FS::UI::Web::cust_styles(),
86 my $curuser = $FS::CurrentUser::CurrentUser;
89 unless $curuser->access_right('Financial reports');
91 my $thing = $opt{'thing'};
92 my $amount_field = $opt{'amount_field'};
93 my $name_singular = $opt{'name_singular'};
95 my $title = "\u$name_singular Search Results";
99 unless ( $opt{'disable_by'} ) {
101 push @fields, sub { my $o = shift->otaker;
102 $o = 'auto billing' if $o eq 'fs_daily';
103 $o = 'customer self-service' if $o eq 'fs_selfservice';
108 push @header, @{ $opt{'addl_header'} }
109 if $opt{'addl_header'};
110 push @fields, @{ $opt{'addl_fields'} }
111 if $opt{'addl_fields'};
113 my( $count_query, $sql_query );
114 if ( $cgi->param('magic') ) {
118 if ( $cgi->param('magic') eq '_date' ) {
121 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
122 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
123 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
124 die "unknown agentnum $1" unless $agent;
125 $title = $agent->agent. " $title";
128 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
129 push @search, "custnum = $1";
132 if ( $cgi->param('payby') ) {
133 $cgi->param('payby') =~
134 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
135 or die "illegal payby ". $cgi->param('payby');
136 push @search, "cust_$thing.payby = '$1'";
142 if ( $cardtype eq 'VisaMC' ) {
143 #avoid posix regexes for portability
145 " ( ( substring(cust_$thing.payinfo from 1 for 1) = '4' ".
146 " AND substring(cust_$thing.payinfo from 1 for 4) != '4936' ".
147 " AND substring(cust_$thing.payinfo from 1 for 6) ".
148 " NOT SIMILAR TO '49030[2-9]' ".
149 " AND substring(cust_$thing.payinfo from 1 for 6) ".
150 " NOT SIMILAR TO '49033[5-9]' ".
151 " AND substring(cust_$thing.payinfo from 1 for 6) ".
152 " NOT SIMILAR TO '49110[1-2]' ".
153 " AND substring(cust_$thing.payinfo from 1 for 6) ".
154 " NOT SIMILAR TO '49117[4-9]' ".
155 " AND substring(cust_$thing.payinfo from 1 for 6) ".
156 " NOT SIMILAR TO '49118[1-2]' ".
158 " OR substring(cust_$thing.payinfo from 1 for 2) = '51' ".
159 " OR substring(cust_$thing.payinfo from 1 for 2) = '52' ".
160 " OR substring(cust_$thing.payinfo from 1 for 2) = '53' ".
161 " OR substring(cust_$thing.payinfo from 1 for 2) = '54' ".
162 " OR substring(cust_$thing.payinfo from 1 for 2) = '54' ".
163 " OR substring(cust_$thing.payinfo from 1 for 2) = '55' ".
164 " OR substring(cust_$thing.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
166 } elsif ( $cardtype eq 'Amex' ) {
168 " ( substring(cust_$thing.payinfo from 1 for 2 ) = '34' ".
169 " OR substring(cust_$thing.payinfo from 1 for 2 ) = '37' ".
171 } elsif ( $cardtype eq 'Discover' ) {
173 " ( substring(cust_$thing.payinfo from 1 for 4 ) = '6011' ".
174 " OR substring(cust_$thing.payinfo from 1 for 2 ) = '65' ".
175 " OR substring(cust_$thing.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
177 } elsif ( $cardtype eq 'Maestro' ) {
179 " ( substring(cust_$thing.payinfo from 1 for 2 ) = '63' ".
180 " OR substring(cust_$thing.payinfo from 1 for 2 ) = '67' ".
181 " OR substring(cust_$thing.payinfo from 1 for 6 ) = '564182' ".
182 " OR substring(cust_$thing.payinfo from 1 for 4 ) = '4936' ".
183 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
184 " SIMILAR TO '49030[2-9]' ".
185 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
186 " SIMILAR TO '49033[5-9]' ".
187 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
188 " SIMILAR TO '49110[1-2]' ".
189 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
190 " SIMILAR TO '49117[4-9]' ".
191 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
192 " SIMILAR TO '49118[1-2]' ".
195 die "unknown card type $cardtype";
198 my $masksearch = $search;
199 $masksearch =~ s/cust_$thing\.payinfo/cust_$thing.paymask/gi;
202 "( $search OR ( cust_$thing.paymask IS NOT NULL AND $masksearch ) )";
207 if ( $cgi->param('payinfo') ) {
208 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
209 or die "illegal payinfo ". $cgi->param('payinfo');
210 push @search, "cust_$thing.payinfo = '$1'";
213 #for cust_pay_pending... statusNOT=done
214 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
215 push @search, "status != '$1'";
218 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
219 push @search, "_date >= $beginning ",
222 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
226 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
228 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
229 or die "illegal paybatch: ". $cgi->param('paybatch');
231 push @search, "paybatch = '$1'";
233 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
236 die "unknown search magic: ". $cgi->param('magic');
239 #here is the agent virtualization
240 push @search, $curuser->agentnums_sql;
242 my $search = ' WHERE '. join(' AND ', @search);
244 $count_query = "SELECT COUNT(*), SUM($amount_field) ".
245 "FROM cust_$thing LEFT JOIN cust_main USING ( custnum )".
249 'table' => "cust_$thing",
250 'select' => join(', ',
252 'cust_main.custnum as cust_main_custnum',
253 FS::UI::Web::cust_sql_fields(),
256 'extra_sql' => "$search ORDER BY $orderby",
257 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
262 #hmm... is this still used?
264 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
267 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
270 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM cust_$thing".
271 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
272 " AND ". $curuser->agentnums_sql;
275 'table' => "cust_$thing",
276 'hashref' => { 'payinfo' => $payinfo,
278 'extra_sql' => $curuser->agentnums_sql.
285 if ( ( $curuser->access_right('View invoices') #XXX for now
286 || $curuser->access_right('View customer payments')
288 && ! $opt{'disable_link'}
291 $link = [ "${p}view/cust_$thing.html?${thing}num=", $thing.'num' ]
294 my $cust_link = sub {
295 my $cust_thing = shift;
296 $cust_thing->cust_main_custnum
297 ? [ "${p}view/cust_main.cgi?", 'custnum' ]