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,
32 include( 'elements/cust_pay_or_refund.html',
33 'table' => 'h_cust_pay',
34 'amount_field' => 'paid',
35 'name_singular' => 'payment',
36 'name_verb' => 'paid',
37 'pre_header' => [ 'Transaction', 'By' ],
38 'pre_fields' => [ 'history_action', 'history_user' ],
43 'title' => $title, # XXX: translate
44 'name_singular' => $name_singular,
45 'query' => $sql_query,
46 'count_query' => $count_query,
47 'count_addl' => \@count_addl,
48 'redirect_empty' => $opt{'redirect_empty'},
51 'sort_fields' => \@sort_fields,
54 'link_onclicks' => \@link_onclicks,
62 my $curuser = $FS::CurrentUser::CurrentUser;
65 unless $curuser->access_right('Financial reports');
67 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
69 my $amount_field = $opt{'amount_field'};
70 my $name_singular = $opt{'name_singular'};
72 my $unapplied = $cgi->param('unapplied');
74 $title = 'Unapplied ' if $unapplied;
75 $title .= "\u$name_singular Search Results";
78 if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
79 || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
80 || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
82 && ! $opt{'disable_link'}
88 if ( $table eq 'cust_pay_void' ) {
91 } elsif ( $table eq /^cust_(\w+)$/ ) {
97 $link = [ "${p}view/$table.html?$q", $key ]
101 my $cust_link = sub {
102 my $cust_thing = shift;
103 $cust_thing->cust_main_custnum
104 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
108 # only valid for $table == 'cust_pay' atm
110 if ( $cgi->param('tax_names') ) {
111 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
118 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
119 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
120 WHERE cust_bill_pkg.pkgnum = 0
121 AND cust_bill_pay.paynum = cust_pay.paynum
126 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
128 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
132 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
133 "omitting tax name information from report.";
144 if ( $opt{'pre_header'} ) {
145 push @header, @{ $opt{'pre_header'} };
146 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
147 push @links, map '', @{ $opt{'pre_header'} };
148 push @fields, @{ $opt{'pre_fields'} };
149 push @sort_fields, @{ $opt{'pre_fields'} };
152 my $sub_receipt = sub {
154 my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
156 include('/elements/popup_link_onclick.html',
157 'action' => $p.'view/cust_pay.html?link=popup;'.$objnum,
158 'actionlabel' => emt('Payment Receipt'),
162 push @header, "\u$name_singular",
167 push @fields, 'payby_payinfo_pretty',
168 sub { sprintf('$%.2f', shift->$amount_field() ) },
170 push @link_onclicks, $sub_receipt, '';
171 push @sort_fields, '', $amount_field;
174 push @header, emt('Unapplied');
177 push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) };
178 push @sort_fields, '';
181 push @header, emt('Date');
184 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
185 push @sort_fields, '_date';
187 unless ( $opt{'disable_by'} ) {
188 push @header, emt('By');
191 push @fields, sub { my $o = shift->otaker;
192 $o = 'auto billing' if $o eq 'fs_daily';
193 $o = 'customer self-service' if $o eq 'fs_selfservice';
199 push @header, (emt('Tax names'), emt('Tax province'));
201 push @links, ('','');
202 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
203 split('\|', shift->tax_names)
206 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
209 split('\|', shift->tax_names)
214 push @header, FS::UI::Web::cust_header();
215 $align .= FS::UI::Web::cust_aligns();
216 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
217 FS::UI::Web::cust_header();
218 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
219 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
220 push @fields, \&FS::UI::Web::cust_fields;
222 push @header, @{ $opt{'addl_header'} }
223 if $opt{'addl_header'};
224 push @fields, @{ $opt{'addl_fields'} }
225 if $opt{'addl_fields'};
227 my( $count_query, $sql_query, @count_addl );
228 if ( $cgi->param('magic') ) {
233 FS::UI::Web::cust_sql_fields(),
234 'cust_main.custnum AS cust_main_custnum',
236 push @select, $tax_names if $tax_names;
239 if ( $cgi->param('magic') eq '_date' ) {
241 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
242 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
243 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
244 die "unknown agentnum $1" unless $agent;
245 $title = $agent->agent. " $title";
248 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
249 push @search, "refnum = $1";
250 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
251 die "unknown refnum $1" unless $part_referral;
252 $title = $part_referral->referral. " $title";
255 # cust_classnum (false laziness w/ elements/cust_main_dayranges.html, prepaid_income.html, cust_bill_pay.html, cust_bill_pkg.html cust_bill_pkg_referral.html, unearned_detail.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
256 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
257 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
258 push @search, 'COALESCE( cust_main.classnum, 0) IN ( '.
259 join(',', map { $_ || '0' } @classnums ).
264 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
265 push @search, "custnum = $1";
268 if ( $cgi->param('payby') ) {
270 my @all_payby_search = ();
271 foreach my $payby ( $cgi->param('payby') ) {
274 /^(CARD|CHEK|BILL|CASH|PPAL|APPL|ANRD|PREP|WIRE|WEST|EDI|MCRD)(-(VisaMC|Amex|Discover|Maestro|Tokenized))?$/
275 or die "illegal payby $payby";
277 my $payby_search = "$table.payby = '$1'";
283 my $similar_to = dbh->{Driver}->{Name} =~ /^mysql/i
284 ? 'REGEXP' #doesn't behave exactly the same, but
285 #should work for our patterns
289 if ( $cardtype eq 'VisaMC' ) {
291 #avoid posix regexes for portability
293 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
294 " AND substring($table.payinfo from 1 for 4) != '4936' ".
295 " AND substring($table.payinfo from 1 for 6) ".
296 " NOT $similar_to '49030[2-9]' ".
297 " AND substring($table.payinfo from 1 for 6) ".
298 " NOT $similar_to '49033[5-9]' ".
299 " AND substring($table.payinfo from 1 for 6) ".
300 " NOT $similar_to '49110[1-2]' ".
301 " AND substring($table.payinfo from 1 for 6) ".
302 " NOT $similar_to '49117[4-9]' ".
303 " AND substring($table.payinfo from 1 for 6) ".
304 " NOT $similar_to '49118[1-2]' ".
306 " OR substring($table.payinfo from 1 for 2) = '51' ".
307 " OR substring($table.payinfo from 1 for 2) = '52' ".
308 " OR substring($table.payinfo from 1 for 2) = '53' ".
309 " OR substring($table.payinfo from 1 for 2) = '54' ".
310 " OR substring($table.payinfo from 1 for 2) = '54' ".
311 " OR substring($table.payinfo from 1 for 2) = '55' ".
312 # " OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l was processed as Visa/MC inside US, now Discover
315 } elsif ( $cardtype eq 'Amex' ) {
318 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
319 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
322 } elsif ( $cardtype eq 'Discover' ) {
324 my $conf = new FS::Conf;
325 my $country = $conf->config('countrydefault') || 'US';
328 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
329 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
330 " OR substring($table.payinfo from 1 for 3 ) = '300' ".
331 " OR substring($table.payinfo from 1 for 3 ) = '301' ".
332 " OR substring($table.payinfo from 1 for 3 ) = '302' ".
333 " OR substring($table.payinfo from 1 for 3 ) = '303' ".
334 " OR substring($table.payinfo from 1 for 3 ) = '304' ".
335 " OR substring($table.payinfo from 1 for 3 ) = '305' ".
336 " OR substring($table.payinfo from 1 for 4 ) = '3095' ".
337 " OR substring($table.payinfo from 1 for 2 ) = '36' ".
338 " OR substring($table.payinfo from 1 for 2 ) = '38' ".
339 " OR substring($table.payinfo from 1 for 2 ) = '39' ".
340 " OR substring($table.payinfo from 1 for 3 ) = '644' ".
341 " OR substring($table.payinfo from 1 for 3 ) = '645' ".
342 " OR substring($table.payinfo from 1 for 3 ) = '646' ".
343 " OR substring($table.payinfo from 1 for 3 ) = '647' ".
344 " OR substring($table.payinfo from 1 for 3 ) = '648' ".
345 " OR substring($table.payinfo from 1 for 3 ) = '649' ".
346 ( $country =~ /^(US|CA)$/
347 ?" OR substring($table.payinfo from 1 for 4 ) = '3528' ". # JCB cards in the 3528-3589 range identified as Discover inside US/CA
348 " OR substring($table.payinfo from 1 for 4 ) = '3529' ".
349 " OR substring($table.payinfo from 1 for 3 ) = '353' ".
350 " OR substring($table.payinfo from 1 for 3 ) = '354' ".
351 " OR substring($table.payinfo from 1 for 3 ) = '355' ".
352 " OR substring($table.payinfo from 1 for 3 ) = '356' ".
353 " OR substring($table.payinfo from 1 for 3 ) = '357' ".
354 " OR substring($table.payinfo from 1 for 3 ) = '358' "
357 " OR substring($table.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
360 } elsif ( $cardtype eq 'Maestro' ) {
363 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
364 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
365 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
366 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
367 " OR substring($table.payinfo from 1 for 6 ) ".
368 " $similar_to '49030[2-9]' ".
369 " OR substring($table.payinfo from 1 for 6 ) ".
370 " $similar_to '49033[5-9]' ".
371 " OR substring($table.payinfo from 1 for 6 ) ".
372 " $similar_to '49110[1-2]' ".
373 " OR substring($table.payinfo from 1 for 6 ) ".
374 " $similar_to '49117[4-9]' ".
375 " OR substring($table.payinfo from 1 for 6 ) ".
376 " $similar_to '49118[1-2]' ".
379 } elsif ( $cardtype eq 'Tokenized' ) {
381 $search = " substring($table.payinfo from 1 for 2 ) = '99' ";
384 die "unknown card type $cardtype";
387 my $masksearch = $search;
388 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
390 $payby_search = "( $payby_search AND ( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) ) )";
394 push @all_payby_search, $payby_search;
398 push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
402 if ( $cgi->param('payinfo') ) {
403 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
404 or die "illegal payinfo ". $cgi->param('payinfo');
405 push @search, "$table.payinfo = '$1'";
408 if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
409 # I think that's all the characters we need to allow.
410 # To avoid confusion, this parameter searches both auth and order_number.
411 push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
412 push @fields, 'auth', 'order_number';
413 push @header, 'Auth #', 'Transaction #';
418 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
419 push @search, "$table.usernum = $1";
422 #for cust_pay_pending... statusNOT=done
423 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
424 push @search, "status != '$1'";
427 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
429 push @search, "_date >= $beginning ",
432 if ( $table eq 'cust_pay_void' ) {
433 my($v_beginning, $v_ending) =
434 FS::UI::Web::parse_beginning_ending($cgi, 'void');
435 push @search, "void_date >= $v_beginning ",
436 "void_date <= $v_ending";
439 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
443 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
445 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
446 or die "illegal paybatch: ". $cgi->param('paybatch');
448 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
450 } elsif ( $cgi->param('magic') eq 'batchnum' ) {
452 $cgi->param('batchnum') =~ /^(\d+)$/
453 or die "illegal batchnum: ".$cgi->param('batchnum');
455 push @search, "batchnum = $1";
457 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
460 die "unknown search magic: ". $cgi->param('magic');
463 if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
464 push @search, "paybatch = '$1'";
467 #unapplied payment/refund
469 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
470 push @search, "FS::$table"->unapplied_sql . ' > 0';
474 #for the history search
475 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
476 my @history_action = split(/,/, $1);
477 push @search, 'history_action IN ('.
478 join(',', map "'$_'", @history_action ). ')';
481 if ( $cgi->param('history_date_beginning')
482 || $cgi->param('history_date_ending') ) {
483 my($h_beginning, $h_ending) =
484 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
485 push @search, "history_date >= $h_beginning ",
486 "history_date <= $h_ending";
489 #here is the agent virtualization
490 push @search, $curuser->agentnums_sql;
492 my $addl_from = FS::UI::Web::join_cust_main($table);
495 if ( $cgi->param('tax_names') ) {
496 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
500 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
502 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
503 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
504 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
505 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
506 FS::UI::Web::cust_sql_fields();
508 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
512 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
513 "omitting tax name information from report.";
518 my $search = ' WHERE '. join(' AND ', @search);
520 $count_query = "SELECT COUNT(*), SUM($amount_field) ";
521 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
523 $count_query .= "FROM $table $addl_from".
526 @count_addl = ( '$%.2f total '.$opt{name_verb} );
527 push @count_addl, '$%.2f unapplied' if $unapplied;
531 'select' => join(', ', @select),
533 'extra_sql' => "$search $group_by",
534 'order_by' => "ORDER BY $orderby",
535 'addl_from' => $addl_from,
540 #hmm... is this still used?
542 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
545 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
548 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
549 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
550 " AND ". $curuser->agentnums_sql;
551 @count_addl = ( '$%.2f total '.$opt{name_verb} );
555 'hashref' => { 'payinfo' => $payinfo,
557 'extra_sql' => $curuser->agentnums_sql.
564 $title = join('',map {ucfirst} split(/\b/,$title));