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";
95 if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
96 || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
97 || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
99 && ! $opt{'disable_link'}
105 if ( $table eq 'cust_pay_void' ) {
108 } elsif ( $table eq /^cust_(\w+)$/ ) {
114 $link = [ "${p}view/$table.html?$q", $key ]
118 my $cust_link = sub {
119 my $cust_thing = shift;
120 $cust_thing->cust_main_custnum
121 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
125 # only valid for $table == 'cust_pay' atm
127 if ( $cgi->param('tax_names') ) {
128 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
135 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
136 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
137 WHERE cust_bill_pkg.pkgnum = 0
138 AND cust_bill_pay.paynum = cust_pay.paynum
143 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
145 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
149 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
150 "omitting tax name information from report.";
161 if ( $opt{'pre_header'} ) {
162 push @header, @{ $opt{'pre_header'} };
163 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
164 push @links, map '', @{ $opt{'pre_header'} };
165 push @fields, @{ $opt{'pre_fields'} };
166 push @sort_fields, @{ $opt{'pre_fields'} };
169 my $sub_receipt = sub {
171 my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
173 include('/elements/popup_link_onclick.html',
174 'action' => $p.'view/cust_pay.html?link=popup;'.$objnum,
175 'actionlabel' => emt('Payment Receipt'),
179 push @header, "\u$name_singular",
184 push @fields, 'payby_payinfo_pretty',
185 sub { sprintf($money, shift->$amount_field() ) },
187 push @link_onclicks, $sub_receipt, '';
188 push @sort_fields, 'paysort', $amount_field;
190 if ($opt{'show_card_type'}) {
191 push @header, emt('Card Type');
195 (($_[0]->payby eq 'CARD') && ($_[0]->paymask !~ /N\/A/)) ? cardtype($_[0]->paymask) : ''
197 push @sort_fields, '';
201 push @header, emt('Unapplied');
204 push @fields, sub { sprintf($money, shift->unapplied_amount) };
205 push @sort_fields, '';
208 push @header, emt('Date');
211 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
212 push @sort_fields, '_date';
214 if ($opt{'show_order_number'}) {
215 push @header, emt('Order Number');
218 push @fields, 'order_number';
219 push @sort_fields, 'order_number';
222 unless ( $opt{'disable_by'} ) {
223 push @header, emt('By');
226 push @fields, sub { my $o = shift->otaker;
227 $o = 'auto billing' if $o eq 'fs_daily';
228 $o = 'customer self-service' if $o eq 'fs_selfservice';
231 push @sort_fields, '';
235 push @header, (emt('Tax names'), emt('Tax province'));
237 push @links, ('','');
238 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
239 split('\|', shift->tax_names)
242 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
245 split('\|', shift->tax_names)
248 push @sort_fields, '', '';
251 push @header, FS::UI::Web::cust_header();
252 $align .= FS::UI::Web::cust_aligns();
253 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
254 FS::UI::Web::cust_header();
255 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
256 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
257 push @fields, \&FS::UI::Web::cust_fields;
258 push @sort_fields, FS::UI::Web::cust_sort_fields;
260 push @header, @{ $opt{'addl_header'} }
261 if $opt{'addl_header'};
262 push @fields, @{ $opt{'addl_fields'} }
263 if $opt{'addl_fields'};
264 push @sort_fields, @{ $opt{'addl_sort_fields'} }
265 if $opt{'addl_sort_fields'};
267 my( $count_query, $sql_query, @count_addl );
268 if ( $cgi->param('magic') ) {
273 "( $table.payby || ' ' || coalesce($table.paymask, $table.payinfo) ) AS paysort",
274 FS::UI::Web::cust_sql_fields(),
275 'cust_main.custnum AS cust_main_custnum',
277 push @select, $tax_names if $tax_names;
280 if ( $cgi->param('magic') eq '_date' ) {
282 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
283 push @search, "cust_main.agentnum = $1"; # $search{'agentnum'} = $1;
284 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
285 die "unknown agentnum $1" unless $agent;
286 $title = $agent->agent. " $title";
289 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
290 push @search, "cust_main.refnum = $1";
291 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
292 die "unknown refnum $1" unless $part_referral;
293 $title = $part_referral->referral. " $title";
296 # cust_classnum - standard matching
297 push @search, $m->comp('match-classnum',
298 param => 'cust_classnum', field => 'cust_main.classnum'
301 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
302 push @search, "$table.custnum = $1";
305 if ( $cgi->param('payby') ) {
307 my @all_payby_search = ();
308 foreach my $payby ( $cgi->param('payby') ) {
311 /^(CARD|CHEK|BILL|CASH|PPAL|APPL|ANRD|PREP|WIRE|WEST|IDTP|EDI|MCRD|MCHK)(-(VisaMC|Amex|Discover|Maestro|Tokenized))?$/
312 or die "illegal payby $payby";
314 my $payby_search = "$table.payby = '$1'";
320 my $similar_to = dbh->{Driver}->{Name} =~ /^mysql/i
321 ? 'REGEXP' #doesn't behave exactly the same, but
322 #should work for our patterns
326 if ( $cardtype eq 'VisaMC' ) {
328 #avoid posix regexes for portability
331 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
333 " AND substring($table.payinfo from 1 for 4) != '4936' ".
334 " AND substring($table.payinfo from 1 for 6) ".
335 " NOT $similar_to '49030[2-9]' ".
336 " AND substring($table.payinfo from 1 for 6) ".
337 " NOT $similar_to '49033[5-9]' ".
338 " AND substring($table.payinfo from 1 for 6) ".
339 " NOT $similar_to '49110[1-2]' ".
340 " AND substring($table.payinfo from 1 for 6) ".
341 " NOT $similar_to '49117[4-9]' ".
342 " AND substring($table.payinfo from 1 for 6) ".
343 " NOT $similar_to '49118[1-2]' ".
346 " OR substring($table.payinfo from 1 for 2) = '51' ".
347 " OR substring($table.payinfo from 1 for 2) = '52' ".
348 " OR substring($table.payinfo from 1 for 2) = '53' ".
349 " OR substring($table.payinfo from 1 for 2) = '54' ".
350 " OR substring($table.payinfo from 1 for 2) = '54' ".
351 " OR substring($table.payinfo from 1 for 2) = '55' ".
352 " OR substring($table.payinfo from 1 for 4) $similar_to '222[1-9]' ".
353 " OR substring($table.payinfo from 1 for 3) $similar_to '22[3-9]' ".
354 " OR substring($table.payinfo from 1 for 2) $similar_to '2[3-6]' ".
355 " OR substring($table.payinfo from 1 for 3) $similar_to '27[0-1]' ".
356 " OR substring($table.payinfo from 1 for 4) = '2720' ".
359 } elsif ( $cardtype eq 'Amex' ) {
362 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
363 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
366 } elsif ( $cardtype eq 'Discover' ) {
368 my $country = $conf->config('countrydefault') || 'US';
371 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
372 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
373 " OR substring($table.payinfo from 1 for 3 ) = '300' ". # diner's 300-305
374 " OR substring($table.payinfo from 1 for 3 ) = '301' ".
375 " OR substring($table.payinfo from 1 for 3 ) = '302' ".
376 " OR substring($table.payinfo from 1 for 3 ) = '303' ".
377 " OR substring($table.payinfo from 1 for 3 ) = '304' ".
378 " OR substring($table.payinfo from 1 for 3 ) = '305' ".
379 " OR substring($table.payinfo from 1 for 4 ) = '3095' ". # diner's 3095
380 " OR substring($table.payinfo from 1 for 2 ) = '36' ". # diner's 36, 38, 39
381 " OR substring($table.payinfo from 1 for 2 ) = '38' ".
382 " OR substring($table.payinfo from 1 for 2 ) = '39' ".
383 " OR substring($table.payinfo from 1 for 3 ) = '644' ".
384 " OR substring($table.payinfo from 1 for 3 ) = '645' ".
385 " OR substring($table.payinfo from 1 for 3 ) = '646' ".
386 " OR substring($table.payinfo from 1 for 3 ) = '647' ".
387 " OR substring($table.payinfo from 1 for 3 ) = '648' ".
388 " OR substring($table.payinfo from 1 for 3 ) = '649' ".
389 ( $country =~ /^(US|PR|VI|MP|PW|GU)$/
390 ?" OR substring($table.payinfo from 1 for 4 ) = '3528' ". # JCB cards in the 3528-3589 range identified as Discover inside US & territories (NOT Canada)
391 " OR substring($table.payinfo from 1 for 4 ) = '3529' ".
392 " OR substring($table.payinfo from 1 for 3 ) = '353' ".
393 " OR substring($table.payinfo from 1 for 3 ) = '354' ".
394 " OR substring($table.payinfo from 1 for 3 ) = '355' ".
395 " OR substring($table.payinfo from 1 for 3 ) = '356' ".
396 " OR substring($table.payinfo from 1 for 3 ) = '357' ".
397 " OR substring($table.payinfo from 1 for 3 ) = '358' "
400 ( $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)$/
401 ?" OR substring($table.payinfo from 1 for 3 ) $similar_to '62[24-68]' " #China Union Pay processed as Discover outside CN
406 } elsif ( $cardtype eq 'Maestro' ) {
409 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
410 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
411 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
412 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
413 " OR substring($table.payinfo from 1 for 6 ) ".
414 " $similar_to '49030[2-9]' ".
415 " OR substring($table.payinfo from 1 for 6 ) ".
416 " $similar_to '49033[5-9]' ".
417 " OR substring($table.payinfo from 1 for 6 ) ".
418 " $similar_to '49110[1-2]' ".
419 " OR substring($table.payinfo from 1 for 6 ) ".
420 " $similar_to '49117[4-9]' ".
421 " OR substring($table.payinfo from 1 for 6 ) ".
422 " $similar_to '49118[1-2]' ".
425 } elsif ( $cardtype eq 'Tokenized' ) {
427 $search = " substring($table.payinfo from 1 for 2 ) = '99' ";
430 die "unknown card type $cardtype";
433 my $masksearch = $search;
434 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
436 $payby_search = "( $payby_search AND ( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) ) )";
440 push @all_payby_search, $payby_search;
444 push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
448 if ( $cgi->param('payinfo') ) {
449 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
450 or die "illegal payinfo ". $cgi->param('payinfo');
451 my $regexp = regexp_sql();
452 push @search, "$table.payinfo $regexp '^0*$1\$'";
455 if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
456 # I think that's all the characters we need to allow.
457 # To avoid confusion, this parameter searches both auth and order_number.
458 push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
459 push @fields, 'auth', 'order_number';
460 push @header, 'Auth #', 'Transaction #';
461 push @sort_fields, '', '';
466 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
467 push @search, "$table.usernum = $1";
470 #for cust_pay_pending... statusNOT=done
471 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
472 push @search, "$table.status != '$1'";
475 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
477 push @search, "$table._date >= $beginning ",
478 "$table._date <= $ending";
480 if ( $table eq 'cust_pay_void' ) {
481 my($v_beginning, $v_ending) =
482 FS::UI::Web::parse_beginning_ending($cgi, 'void');
483 push @search, "$table.void_date >= $v_beginning ",
484 "$table.void_date <= $v_ending";
487 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field, $table);
491 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
493 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
494 or die "illegal paybatch: ". $cgi->param('paybatch');
496 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
498 } elsif ( $cgi->param('magic') eq 'batchnum' ) {
500 $cgi->param('batchnum') =~ /^(\d+)$/
501 or die "illegal batchnum: ".$cgi->param('batchnum');
503 push @search, "batchnum = $1";
505 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
508 die "unknown search magic: ". $cgi->param('magic');
511 if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
512 push @search, "paybatch = '$1'";
515 #unapplied payment/refund
517 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
518 push @search, "FS::$table"->unapplied_sql . ' > 0';
522 #for the history search
523 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
524 my @history_action = split(/,/, $1);
525 push @search, 'history_action IN ('.
526 join(',', map "'$_'", @history_action ). ')';
529 if ( $cgi->param('history_date_beginning')
530 || $cgi->param('history_date_ending') ) {
531 my($h_beginning, $h_ending) =
532 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
533 push @search, "history_date >= $h_beginning ",
534 "history_date <= $h_ending";
537 #here is the agent virtualization
538 push @search, $curuser->agentnums_sql;
540 my $addl_from = FS::UI::Web::join_cust_main($table);
543 # reasons, for refunds and voided payments
545 push @select, "reason.reason";
546 $addl_from .= " LEFT JOIN reason USING (reasonnum)\n";
547 push @fields, 'reason';
548 push @sort_fields, 'reason.reason';
549 push @header, emt('Reason');
550 if ( $cgi->param('reasonnum') =~ /^(\d+)$/ ) {
551 push @search, "COALESCE(reasonnum, 0) = $1";
555 if ( $cgi->param('tax_names') ) {
556 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
560 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
562 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
563 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
564 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
565 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
566 FS::UI::Web::cust_sql_fields();
568 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
572 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
573 "omitting tax name information from report.";
578 my $search = ' WHERE '. join(' AND ', @search);
580 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) ";
581 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
583 $count_query .= "FROM $table $addl_from".
586 @count_addl = ( '$%.2f total '.$opt{name_verb} );
587 push @count_addl, '$%.2f unapplied' if $unapplied;
591 'select' => join(', ', @select),
593 'extra_sql' => "$search $group_by",
594 'order_by' => "ORDER BY $orderby",
595 'addl_from' => $addl_from,
600 #hmm... is this still used?
601 warn "undefined search magic";
603 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
606 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
609 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) FROM $table".
610 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
611 " AND ". $curuser->agentnums_sql;
612 @count_addl = ( '$%.2f total '.$opt{name_verb} );
616 'hashref' => { 'payinfo' => $payinfo,
618 'extra_sql' => $curuser->agentnums_sql.
625 $title = join('',map {ucfirst} split(/\b/,$title));