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 'addl_sort_fields' => [],
30 'redirect_empty' => $redirect_empty,
33 include( 'elements/cust_pay_or_refund.html',
34 'table' => 'h_cust_pay',
35 'amount_field' => 'paid',
36 'name_singular' => 'payment',
37 'name_verb' => 'paid',
38 'pre_header' => [ 'Transaction', 'By' ],
39 'pre_fields' => [ 'history_action', 'history_user' ],
43 <& grouped-search.html,
44 'title' => $title, # XXX: translate
45 'name_singular' => $name_singular,
46 'query' => $sql_query,
47 'count_query' => $count_query,
48 'count_addl' => \@count_addl,
49 'redirect_empty' => $opt{'redirect_empty'},
52 'sort_fields' => \@sort_fields,
55 'link_onclicks' => \@link_onclicks,
59 'group_column' => 'payby',
60 'group_label' => 'payby_name',
61 'subtotal' => { $opt{amount_field} => "sum($amount_field)" },
62 'subtotal_row' => [ 'Subtotal',
63 sub { sprintf($money, $_[0]->$amount_field) },
65 'total_row' => [ '<B>Total</B>',
66 sub { sprintf("<B>$money</B>", $_[0]->$amount_field) },
74 my $curuser = $FS::CurrentUser::CurrentUser;
76 my $conf = FS::Conf->new;
77 my $money = ($conf->config('money_char') || '$') . '%.2f';
80 unless $curuser->access_right('Financial reports');
82 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
84 my $has_reason = dbdef->table($table)->column('reasonnum') ? 1 : 0;
86 my $amount_field = $opt{'amount_field'};
87 my $name_singular = $opt{'name_singular'};
89 my $unapplied = $cgi->param('unapplied');
91 $title = 'Unapplied ' if $unapplied;
92 $title .= "\u$name_singular Search Results";
96 #if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
97 # || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
98 # || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
100 # && ! $opt{'disable_link'}
106 # if ( $table eq 'cust_pay_void' ) {
109 # } elsif ( $table eq /^cust_(\w+)$/ ) {
115 # $link = [ "${p}view/$table.html?$q", $key ]
119 my $cust_link = sub {
120 my $cust_thing = shift;
121 $cust_thing->cust_main_custnum
122 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
126 # only valid for $table == 'cust_pay' atm
128 if ( $cgi->param('tax_names') ) {
129 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
136 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
137 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
138 WHERE cust_bill_pkg.pkgnum = 0
139 AND cust_bill_pay.paynum = cust_pay.paynum
144 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
146 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
150 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
151 "omitting tax name information from report.";
162 if ( $opt{'pre_header'} ) {
163 push @header, @{ $opt{'pre_header'} };
164 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
165 push @links, map '', @{ $opt{'pre_header'} };
166 push @fields, @{ $opt{'pre_fields'} };
167 push @sort_fields, @{ $opt{'pre_fields'} };
170 my $sub_receipt = $opt{'disable_link'} ? '' : sub {
172 my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
173 my $table = $obj->table;
175 if ($table eq 'cust_pay_void') {
180 include('/elements/popup_link_onclick.html',
181 'action' => $p.'view/'.$table.'.html?link=popup;'.$objnum.$void,
182 'actionlabel' => emt('Payment Receipt'),
186 push @header, "\u$name_singular",
191 push @fields, 'payby_payinfo_pretty',
192 sub { sprintf($money, shift->$amount_field() ) },
194 push @link_onclicks, $sub_receipt, '';
195 push @sort_fields, 'paysort', $amount_field;
197 if ($opt{'show_card_type'}) {
198 push @header, emt('Card Type');
202 (($_[0]->payby eq 'CARD') && ($_[0]->paymask !~ /N\/A/)) ? cardtype($_[0]->paymask) : ''
204 push @sort_fields, '';
208 push @header, emt('Unapplied');
211 push @fields, sub { sprintf($money, shift->unapplied_amount) };
212 push @sort_fields, '';
215 push @header, emt('Date');
218 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
219 push @sort_fields, '_date';
221 if ($cgi->param('show_order_number')) {
222 push @header, emt('Order Number');
225 push @fields, 'order_number';
226 push @sort_fields, 'order_number';
229 unless ( $opt{'disable_by'} ) {
230 push @header, emt('By');
233 push @fields, sub { my $o = shift->otaker;
234 $o = 'auto billing' if $o eq 'fs_daily';
235 $o = 'customer self-service' if $o eq 'fs_selfservice';
238 push @sort_fields, '';
242 push @header, (emt('Tax names'), emt('Tax province'));
244 push @links, ('','');
245 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
246 split('\|', shift->tax_names)
249 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
252 split('\|', shift->tax_names)
255 push @sort_fields, '', '';
258 push @header, FS::UI::Web::cust_header();
259 $align .= FS::UI::Web::cust_aligns();
260 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
261 FS::UI::Web::cust_header();
262 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
263 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
264 push @fields, \&FS::UI::Web::cust_fields;
265 push @sort_fields, FS::UI::Web::cust_sort_fields;
267 push @header, @{ $opt{'addl_header'} }
268 if $opt{'addl_header'};
269 push @fields, @{ $opt{'addl_fields'} }
270 if $opt{'addl_fields'};
271 push @sort_fields, @{ $opt{'addl_sort_fields'} }
272 if $opt{'addl_sort_fields'};
274 my( $count_query, $sql_query, @count_addl );
275 if ( $cgi->param('magic') ) {
280 "( $table.payby || ' ' || coalesce($table.paymask, $table.payinfo) ) AS paysort",
281 FS::UI::Web::cust_sql_fields(),
282 'cust_main.custnum AS cust_main_custnum',
284 push @select, $tax_names if $tax_names;
287 if ( $cgi->param('magic') eq '_date' ) {
289 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
290 push @search, "cust_main.agentnum = $1"; # $search{'agentnum'} = $1;
291 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
292 die "unknown agentnum $1" unless $agent;
293 $title = $agent->agent. " $title";
296 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
297 push @search, "cust_main.refnum = $1";
298 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
299 die "unknown refnum $1" unless $part_referral;
300 $title = $part_referral->referral. " $title";
303 # cust_classnum - standard matching
304 push @search, $m->comp('match-classnum',
305 param => 'cust_classnum', field => 'cust_main.classnum'
308 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
309 push @search, "$table.custnum = $1";
312 if ( $cgi->param('payby') ) {
314 my @all_payby_search = ();
315 foreach my $payby ( $cgi->param('payby') ) {
318 /^(CARD|CHEK|BILL|CASH|PPAL|APPL|ANRD|PREP|WIRE|WEST|IDTP|EDI|MCRD|MCHK)(-(VisaMC|Amex|Discover|Maestro|Tokenized))?$/
319 or die "illegal payby $payby";
321 my $payby_search = "$table.payby = '$1'";
327 my $similar_to = dbh->{Driver}->{Name} =~ /^mysql/i
328 ? 'REGEXP' #doesn't behave exactly the same, but
329 #should work for our patterns
333 if ( $cardtype eq 'VisaMC' ) {
335 #avoid posix regexes for portability
338 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
340 " AND substring($table.payinfo from 1 for 4) != '4936' ".
341 " AND substring($table.payinfo from 1 for 6) ".
342 " NOT $similar_to '49030[2-9]' ".
343 " AND substring($table.payinfo from 1 for 6) ".
344 " NOT $similar_to '49033[5-9]' ".
345 " AND substring($table.payinfo from 1 for 6) ".
346 " NOT $similar_to '49110[1-2]' ".
347 " AND substring($table.payinfo from 1 for 6) ".
348 " NOT $similar_to '49117[4-9]' ".
349 " AND substring($table.payinfo from 1 for 6) ".
350 " NOT $similar_to '49118[1-2]' ".
353 " OR substring($table.payinfo from 1 for 2) = '51' ".
354 " OR substring($table.payinfo from 1 for 2) = '52' ".
355 " OR substring($table.payinfo from 1 for 2) = '53' ".
356 " OR substring($table.payinfo from 1 for 2) = '54' ".
357 " OR substring($table.payinfo from 1 for 2) = '54' ".
358 " OR substring($table.payinfo from 1 for 2) = '55' ".
359 " OR substring($table.payinfo from 1 for 4) $similar_to '222[1-9]' ".
360 " OR substring($table.payinfo from 1 for 3) $similar_to '22[3-9]' ".
361 " OR substring($table.payinfo from 1 for 2) $similar_to '2[3-6]' ".
362 " OR substring($table.payinfo from 1 for 3) $similar_to '27[0-1]' ".
363 " OR substring($table.payinfo from 1 for 4) = '2720' ".
364 " OR substring($table.payinfo from 1 for 3) = '2[2-7]x' ".
367 } elsif ( $cardtype eq 'Amex' ) {
370 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
371 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
374 } elsif ( $cardtype eq 'Discover' ) {
376 my $country = $conf->config('countrydefault') || 'US';
379 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
380 " OR substring($table.payinfo from 1 for 3 ) = '60x' ".
381 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
383 # diner's 300-305 / 3095
384 " OR substring($table.payinfo from 1 for 3 ) = '300' ".
385 " OR substring($table.payinfo from 1 for 3 ) = '301' ".
386 " OR substring($table.payinfo from 1 for 3 ) = '302' ".
387 " OR substring($table.payinfo from 1 for 3 ) = '303' ".
388 " OR substring($table.payinfo from 1 for 3 ) = '304' ".
389 " OR substring($table.payinfo from 1 for 3 ) = '305' ".
390 " OR substring($table.payinfo from 1 for 4 ) = '3095' ".
391 " OR substring($table.payinfo from 1 for 3 ) = '30x' ".
394 " OR substring($table.payinfo from 1 for 2 ) = '36' ".
395 " OR substring($table.payinfo from 1 for 2 ) = '38' ".
396 " OR substring($table.payinfo from 1 for 2 ) = '39' ".
398 " OR substring($table.payinfo from 1 for 3 ) = '644' ".
399 " OR substring($table.payinfo from 1 for 3 ) = '645' ".
400 " OR substring($table.payinfo from 1 for 3 ) = '646' ".
401 " OR substring($table.payinfo from 1 for 3 ) = '647' ".
402 " OR substring($table.payinfo from 1 for 3 ) = '648' ".
403 " OR substring($table.payinfo from 1 for 3 ) = '649' ".
404 " OR substring($table.payinfo from 1 for 3 ) = '64x' ".
406 # JCB cards in the 3528-3589 range identified as Discover inside US & territories (NOT Canada)
407 ( $country =~ /^(US|PR|VI|MP|PW|GU)$/
408 ?" OR substring($table.payinfo from 1 for 4 ) = '3528' ".
409 " OR substring($table.payinfo from 1 for 4 ) = '3529' ".
410 " OR substring($table.payinfo from 1 for 3 ) = '353' ".
411 " OR substring($table.payinfo from 1 for 3 ) = '354' ".
412 " OR substring($table.payinfo from 1 for 3 ) = '355' ".
413 " OR substring($table.payinfo from 1 for 3 ) = '356' ".
414 " OR substring($table.payinfo from 1 for 3 ) = '357' ".
415 " OR substring($table.payinfo from 1 for 3 ) = '358' ".
416 " OR substring($table.payinfo from 1 for 3 ) = '35x' "
420 #China Union Pay processed as Discover in US, Mexico and Caribbean
421 ( $country =~ /^(US|MX|AI|AG|AW|BS|BB|BM|BQ|VG|KY|CW|DM|DO|GD|GP|JM|MQ|MS|BL|KN|LC|VC|MF|SX|TT|TC)$/
422 ?" OR substring($table.payinfo from 1 for 3 ) $similar_to '62[24-68x]' "
428 } elsif ( $cardtype eq 'Maestro' ) {
431 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
432 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
433 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
434 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
435 " OR substring($table.payinfo from 1 for 6 ) ".
436 " $similar_to '49030[2-9]' ".
437 " OR substring($table.payinfo from 1 for 6 ) ".
438 " $similar_to '49033[5-9]' ".
439 " OR substring($table.payinfo from 1 for 6 ) ".
440 " $similar_to '49110[1-2]' ".
441 " OR substring($table.payinfo from 1 for 6 ) ".
442 " $similar_to '49117[4-9]' ".
443 " OR substring($table.payinfo from 1 for 6 ) ".
444 " $similar_to '49118[1-2]' ".
447 } elsif ( $cardtype eq 'Tokenized' ) {
449 $search = " substring($table.payinfo from 1 for 2 ) = '99' ";
452 die "unknown card type $cardtype";
455 my $masksearch = $search;
456 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
458 $payby_search = "( $payby_search AND ( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) ) )";
462 push @all_payby_search, $payby_search;
466 push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
470 if ( $cgi->param('payinfo') ) {
471 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
472 or die "illegal payinfo ". $cgi->param('payinfo');
473 my $regexp = regexp_sql();
474 push @search, "$table.payinfo $regexp '^0*$1\$'";
477 if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
478 # I think that's all the characters we need to allow.
479 # To avoid confusion, this parameter searches both auth and order_number.
480 push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
481 push @fields, 'auth', 'order_number';
482 push @header, 'Auth #', 'Transaction #';
483 push @sort_fields, '', '';
488 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
489 push @search, "$table.usernum = $1";
492 #for cust_pay_pending... statusNOT=done
493 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
494 push @search, "$table.status != '$1'";
497 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
499 push @search, "$table._date >= $beginning ",
500 "$table._date <= $ending";
502 if ( $table eq 'cust_pay_void' ) {
503 my($v_beginning, $v_ending) =
504 FS::UI::Web::parse_beginning_ending($cgi, 'void');
505 push @search, "$table.void_date >= $v_beginning ",
506 "$table.void_date <= $v_ending";
509 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field, $table);
513 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
515 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
516 or die "illegal paybatch: ". $cgi->param('paybatch');
518 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
520 } elsif ( $cgi->param('magic') eq 'batchnum' ) {
522 $cgi->param('batchnum') =~ /^(\d+)$/
523 or die "illegal batchnum: ".$cgi->param('batchnum');
525 push @search, "batchnum = $1";
527 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
530 die "unknown search magic: ". $cgi->param('magic');
533 if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
534 push @search, "paybatch = '$1'";
537 #unapplied payment/refund
539 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
540 push @search, "FS::$table"->unapplied_sql . ' > 0';
544 #for the history search
545 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
546 my @history_action = split(/,/, $1);
547 push @search, 'history_action IN ('.
548 join(',', map "'$_'", @history_action ). ')';
551 if ( $cgi->param('history_date_beginning')
552 || $cgi->param('history_date_ending') ) {
553 my($h_beginning, $h_ending) =
554 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
555 push @search, "history_date >= $h_beginning ",
556 "history_date <= $h_ending";
559 #here is the agent virtualization
560 push @search, $curuser->agentnums_sql;
562 my $addl_from = FS::UI::Web::join_cust_main($table);
565 # reasons, for refunds and voided payments
567 push @select, "reason.reason";
568 $addl_from .= " LEFT JOIN reason USING (reasonnum)\n";
569 push @fields, 'reason';
570 push @sort_fields, 'reason.reason';
571 push @header, emt('Reason');
572 if ( $cgi->param('reasonnum') =~ /^(\d+)$/ ) {
573 push @search, "COALESCE(reasonnum, 0) = $1";
577 if ( $cgi->param('tax_names') ) {
578 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
582 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
584 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
585 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
586 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
587 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
588 FS::UI::Web::cust_sql_fields();
590 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
594 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
595 "omitting tax name information from report.";
600 my $search = ' WHERE '. join(' AND ', @search);
602 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) ";
603 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
605 $count_query .= "FROM $table $addl_from".
608 @count_addl = ( '$%.2f total '.$opt{name_verb} );
609 push @count_addl, '$%.2f unapplied' if $unapplied;
613 'select' => join(', ', @select),
615 'extra_sql' => "$search $group_by",
616 'order_by' => "ORDER BY $orderby",
617 'addl_from' => $addl_from,
622 #hmm... is this still used?
623 warn "undefined search magic";
625 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
628 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
631 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) FROM $table".
632 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
633 " AND ". $curuser->agentnums_sql;
634 @count_addl = ( '$%.2f total '.$opt{name_verb} );
638 'hashref' => { 'payinfo' => $payinfo,
640 'extra_sql' => $curuser->agentnums_sql.
647 $title = join('',map {ucfirst} split(/\b/,$title));