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' ],
42 <& grouped-search.html,
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,
58 'group_column' => 'payby',
59 'group_label' => 'payby_name',
60 'subtotal' => { $opt{name_verb} => "sum($amount_field)" },
61 'subtotal_row' => [ 'Subtotal',
62 sub { sprintf($money, $_[0]->$amount_field) },
64 'total_row' => [ '<B>Total</B>',
65 sub { sprintf("<B>$money</B>", $_[0]->$amount_field) },
73 my $curuser = $FS::CurrentUser::CurrentUser;
75 my $conf = FS::Conf->new;
76 my $money = ($conf->config('money_char') || '$') . '%.2f';
79 unless $curuser->access_right('Financial reports');
81 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
83 my $amount_field = $opt{'amount_field'};
84 my $name_singular = $opt{'name_singular'};
86 my $unapplied = $cgi->param('unapplied');
88 $title = 'Unapplied ' if $unapplied;
89 $title .= "\u$name_singular Search Results";
92 if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
93 || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
94 || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
96 && ! $opt{'disable_link'}
102 if ( $table eq 'cust_pay_void' ) {
105 } elsif ( $table eq /^cust_(\w+)$/ ) {
111 $link = [ "${p}view/$table.html?$q", $key ]
115 my $cust_link = sub {
116 my $cust_thing = shift;
117 $cust_thing->cust_main_custnum
118 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
122 # only valid for $table == 'cust_pay' atm
124 if ( $cgi->param('tax_names') ) {
125 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
132 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
133 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
134 WHERE cust_bill_pkg.pkgnum = 0
135 AND cust_bill_pay.paynum = cust_pay.paynum
140 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
142 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
146 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
147 "omitting tax name information from report.";
158 if ( $opt{'pre_header'} ) {
159 push @header, @{ $opt{'pre_header'} };
160 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
161 push @links, map '', @{ $opt{'pre_header'} };
162 push @fields, @{ $opt{'pre_fields'} };
163 push @sort_fields, @{ $opt{'pre_fields'} };
166 my $sub_receipt = sub {
168 my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
170 include('/elements/popup_link_onclick.html',
171 'action' => $p.'view/cust_pay.html?link=popup;'.$objnum,
172 'actionlabel' => emt('Payment Receipt'),
176 push @header, "\u$name_singular",
181 push @fields, 'payby_payinfo_pretty',
182 sub { sprintf($money, shift->$amount_field() ) },
184 push @link_onclicks, $sub_receipt, '';
185 push @sort_fields, 'paysort', $amount_field;
187 if ($opt{'show_card_type'}) {
188 push @header, emt('Card Type');
192 (($_[0]->payby eq 'CARD') && ($_[0]->paymask !~ /N\/A/)) ? cardtype($_[0]->paymask) : ''
194 push @sort_fields, '';
198 push @header, emt('Unapplied');
201 push @fields, sub { sprintf($money, shift->unapplied_amount) };
202 push @sort_fields, '';
205 push @header, emt('Date');
208 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
209 push @sort_fields, '_date';
211 unless ( $opt{'disable_by'} ) {
212 push @header, emt('By');
215 push @fields, sub { my $o = shift->otaker;
216 $o = 'auto billing' if $o eq 'fs_daily';
217 $o = 'customer self-service' if $o eq 'fs_selfservice';
223 push @header, (emt('Tax names'), emt('Tax province'));
225 push @links, ('','');
226 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
227 split('\|', shift->tax_names)
230 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
233 split('\|', shift->tax_names)
238 push @header, FS::UI::Web::cust_header();
239 $align .= FS::UI::Web::cust_aligns();
240 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
241 FS::UI::Web::cust_header();
242 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
243 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
244 push @fields, \&FS::UI::Web::cust_fields;
246 push @header, @{ $opt{'addl_header'} }
247 if $opt{'addl_header'};
248 push @fields, @{ $opt{'addl_fields'} }
249 if $opt{'addl_fields'};
251 my( $count_query, $sql_query, @count_addl );
252 if ( $cgi->param('magic') ) {
257 "( $table.payby || ' ' || coalesce($table.paymask, $table.payinfo) ) AS paysort",
258 FS::UI::Web::cust_sql_fields(),
259 'cust_main.custnum AS cust_main_custnum',
261 push @select, $tax_names if $tax_names;
264 if ( $cgi->param('magic') eq '_date' ) {
266 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
267 push @search, "cust_main.agentnum = $1"; # $search{'agentnum'} = $1;
268 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
269 die "unknown agentnum $1" unless $agent;
270 $title = $agent->agent. " $title";
273 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
274 push @search, "cust_main.refnum = $1";
275 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
276 die "unknown refnum $1" unless $part_referral;
277 $title = $part_referral->referral. " $title";
280 # cust_classnum - standard matching
281 push @search, $m->comp('match-classnum',
282 param => 'cust_classnum', field => 'cust_main.classnum'
285 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
286 push @search, "$table.custnum = $1";
289 if ( $cgi->param('payby') ) {
291 my @all_payby_search = ();
292 foreach my $payby ( $cgi->param('payby') ) {
295 /^(CARD|CHEK|BILL|CASH|PPAL|APPL|ANRD|PREP|WIRE|WEST|EDI|MCRD|MCHK)(-(VisaMC|Amex|Discover|Maestro|Tokenized))?$/
296 or die "illegal payby $payby";
298 my $payby_search = "$table.payby = '$1'";
304 my $similar_to = dbh->{Driver}->{Name} =~ /^mysql/i
305 ? 'REGEXP' #doesn't behave exactly the same, but
306 #should work for our patterns
310 if ( $cardtype eq 'VisaMC' ) {
312 #avoid posix regexes for portability
314 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
315 " AND substring($table.payinfo from 1 for 4) != '4936' ".
316 " AND substring($table.payinfo from 1 for 6) ".
317 " NOT $similar_to '49030[2-9]' ".
318 " AND substring($table.payinfo from 1 for 6) ".
319 " NOT $similar_to '49033[5-9]' ".
320 " AND substring($table.payinfo from 1 for 6) ".
321 " NOT $similar_to '49110[1-2]' ".
322 " AND substring($table.payinfo from 1 for 6) ".
323 " NOT $similar_to '49117[4-9]' ".
324 " AND substring($table.payinfo from 1 for 6) ".
325 " NOT $similar_to '49118[1-2]' ".
327 " OR substring($table.payinfo from 1 for 2) = '51' ".
328 " OR substring($table.payinfo from 1 for 2) = '52' ".
329 " OR substring($table.payinfo from 1 for 2) = '53' ".
330 " OR substring($table.payinfo from 1 for 2) = '54' ".
331 " OR substring($table.payinfo from 1 for 2) = '54' ".
332 " OR substring($table.payinfo from 1 for 2) = '55' ".
333 # " OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l was processed as Visa/MC inside US, now Discover
336 } elsif ( $cardtype eq 'Amex' ) {
339 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
340 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
343 } elsif ( $cardtype eq 'Discover' ) {
345 my $country = $conf->config('countrydefault') || 'US';
348 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
349 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
350 " OR substring($table.payinfo from 1 for 3 ) = '300' ".
351 " OR substring($table.payinfo from 1 for 3 ) = '301' ".
352 " OR substring($table.payinfo from 1 for 3 ) = '302' ".
353 " OR substring($table.payinfo from 1 for 3 ) = '303' ".
354 " OR substring($table.payinfo from 1 for 3 ) = '304' ".
355 " OR substring($table.payinfo from 1 for 3 ) = '305' ".
356 " OR substring($table.payinfo from 1 for 4 ) = '3095' ".
357 " OR substring($table.payinfo from 1 for 2 ) = '36' ".
358 " OR substring($table.payinfo from 1 for 2 ) = '38' ".
359 " OR substring($table.payinfo from 1 for 2 ) = '39' ".
360 " OR substring($table.payinfo from 1 for 3 ) = '644' ".
361 " OR substring($table.payinfo from 1 for 3 ) = '645' ".
362 " OR substring($table.payinfo from 1 for 3 ) = '646' ".
363 " OR substring($table.payinfo from 1 for 3 ) = '647' ".
364 " OR substring($table.payinfo from 1 for 3 ) = '648' ".
365 " OR substring($table.payinfo from 1 for 3 ) = '649' ".
366 ( $country =~ /^(US|CA)$/
367 ?" OR substring($table.payinfo from 1 for 4 ) = '3528' ". # JCB cards in the 3528-3589 range identified as Discover inside US/CA
368 " OR substring($table.payinfo from 1 for 4 ) = '3529' ".
369 " OR substring($table.payinfo from 1 for 3 ) = '353' ".
370 " OR substring($table.payinfo from 1 for 3 ) = '354' ".
371 " OR substring($table.payinfo from 1 for 3 ) = '355' ".
372 " OR substring($table.payinfo from 1 for 3 ) = '356' ".
373 " OR substring($table.payinfo from 1 for 3 ) = '357' ".
374 " OR substring($table.payinfo from 1 for 3 ) = '358' "
377 " OR substring($table.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
380 } elsif ( $cardtype eq 'Maestro' ) {
383 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
384 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
385 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
386 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
387 " OR substring($table.payinfo from 1 for 6 ) ".
388 " $similar_to '49030[2-9]' ".
389 " OR substring($table.payinfo from 1 for 6 ) ".
390 " $similar_to '49033[5-9]' ".
391 " OR substring($table.payinfo from 1 for 6 ) ".
392 " $similar_to '49110[1-2]' ".
393 " OR substring($table.payinfo from 1 for 6 ) ".
394 " $similar_to '49117[4-9]' ".
395 " OR substring($table.payinfo from 1 for 6 ) ".
396 " $similar_to '49118[1-2]' ".
399 } elsif ( $cardtype eq 'Tokenized' ) {
401 $search = " substring($table.payinfo from 1 for 2 ) = '99' ";
404 die "unknown card type $cardtype";
407 my $masksearch = $search;
408 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
410 $payby_search = "( $payby_search AND ( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) ) )";
414 push @all_payby_search, $payby_search;
418 push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
422 if ( $cgi->param('payinfo') ) {
423 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
424 or die "illegal payinfo ". $cgi->param('payinfo');
425 my $regexp = regexp_sql();
426 push @search, "$table.payinfo $regexp '^0*$1\$'";
429 if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
430 # I think that's all the characters we need to allow.
431 # To avoid confusion, this parameter searches both auth and order_number.
432 push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
433 push @fields, 'auth', 'order_number';
434 push @header, 'Auth #', 'Transaction #';
439 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
440 push @search, "$table.usernum = $1";
443 #for cust_pay_pending... statusNOT=done
444 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
445 push @search, "$table.status != '$1'";
448 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
450 push @search, "$table._date >= $beginning ",
451 "$table._date <= $ending";
453 if ( $table eq 'cust_pay_void' ) {
454 my($v_beginning, $v_ending) =
455 FS::UI::Web::parse_beginning_ending($cgi, 'void');
456 push @search, "$table.void_date >= $v_beginning ",
457 "$table.void_date <= $v_ending";
460 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field, $table);
464 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
466 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
467 or die "illegal paybatch: ". $cgi->param('paybatch');
469 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
471 } elsif ( $cgi->param('magic') eq 'batchnum' ) {
473 $cgi->param('batchnum') =~ /^(\d+)$/
474 or die "illegal batchnum: ".$cgi->param('batchnum');
476 push @search, "batchnum = $1";
478 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
481 die "unknown search magic: ". $cgi->param('magic');
484 if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
485 push @search, "paybatch = '$1'";
488 #unapplied payment/refund
490 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
491 push @search, "FS::$table"->unapplied_sql . ' > 0';
495 #for the history search
496 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
497 my @history_action = split(/,/, $1);
498 push @search, 'history_action IN ('.
499 join(',', map "'$_'", @history_action ). ')';
502 if ( $cgi->param('history_date_beginning')
503 || $cgi->param('history_date_ending') ) {
504 my($h_beginning, $h_ending) =
505 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
506 push @search, "history_date >= $h_beginning ",
507 "history_date <= $h_ending";
510 #here is the agent virtualization
511 push @search, $curuser->agentnums_sql;
513 my $addl_from = FS::UI::Web::join_cust_main($table);
516 if ( $cgi->param('tax_names') ) {
517 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
521 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
523 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
524 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
525 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
526 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
527 FS::UI::Web::cust_sql_fields();
529 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
533 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
534 "omitting tax name information from report.";
539 my $search = ' WHERE '. join(' AND ', @search);
541 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) ";
542 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
544 $count_query .= "FROM $table $addl_from".
547 @count_addl = ( '$%.2f total '.$opt{name_verb} );
548 push @count_addl, '$%.2f unapplied' if $unapplied;
552 'select' => join(', ', @select),
554 'extra_sql' => "$search $group_by",
555 'order_by' => "ORDER BY $orderby",
556 'addl_from' => $addl_from,
561 #hmm... is this still used?
562 warn "undefined search magic";
564 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
567 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
570 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) FROM $table".
571 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
572 " AND ". $curuser->agentnums_sql;
573 @count_addl = ( '$%.2f total '.$opt{name_verb} );
577 'hashref' => { 'payinfo' => $payinfo,
579 'extra_sql' => $curuser->agentnums_sql.
586 $title = join('',map {ucfirst} split(/\b/,$title));