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) },
71 # canonicalize the payby subtype string to an SQL-quoted list
73 'VisaMC' => q['VISA card', 'MasterCard'],
74 'Amex' => q['American Express card'],
75 'Discover' => q['Discover card'],
76 'Maestro' => q['Switch', 'Solo', 'Laser'],
77 'Tokenized' => q['Tokenized'],
84 my $curuser = $FS::CurrentUser::CurrentUser;
86 my $conf = FS::Conf->new;
87 my $money = ($conf->config('money_char') || '$') . '%.2f';
90 unless $curuser->access_right('Basic payment and refund reports');
92 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
94 my $has_reason = dbdef->table($table)->column('reasonnum') ? 1 : 0;
96 my $amount_field = $opt{'amount_field'};
97 my $name_singular = $opt{'name_singular'};
99 my $unapplied = $cgi->param('unapplied');
101 $title = 'Unapplied ' if $unapplied;
102 $title .= "\u$name_singular Search Results";
106 #if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
107 # || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
108 # || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
110 # && ! $opt{'disable_link'}
116 # if ( $table eq 'cust_pay_void' ) {
119 # } elsif ( $table eq /^cust_(\w+)$/ ) {
125 # $link = [ "${p}view/$table.html?$q", $key ]
129 my $cust_link = sub {
130 my $cust_thing = shift;
131 $cust_thing->cust_main_custnum
132 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
136 # only valid for $table == 'cust_pay' atm
138 if ( $cgi->param('tax_names') ) {
139 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
146 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
147 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
148 WHERE cust_bill_pkg.pkgnum = 0
149 AND cust_bill_pay.paynum = cust_pay.paynum
154 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
156 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
160 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
161 "omitting tax name information from report.";
172 if ( $opt{'pre_header'} ) {
173 push @header, @{ $opt{'pre_header'} };
174 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
175 push @links, map '', @{ $opt{'pre_header'} };
176 push @fields, @{ $opt{'pre_fields'} };
177 push @sort_fields, @{ $opt{'pre_fields'} };
180 my $sub_receipt = $opt{'disable_link'} ? '' : sub {
182 my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
183 my $table = $obj->table;
185 if ($table eq 'cust_pay_void') {
190 include('/elements/popup_link_onclick.html',
191 'action' => $p.'view/'.$table.'.html?link=popup;'.$objnum.$void,
192 'actionlabel' => emt('Payment Receipt'),
196 push @header, "\u$name_singular",
201 push @fields, 'payby_payinfo_pretty',
202 sub { sprintf($money, shift->$amount_field() ) },
204 push @link_onclicks, $sub_receipt, '';
205 push @sort_fields, 'paysort', $amount_field;
207 if ($opt{'show_card_type'}) {
208 push @header, emt('Card Type');
211 push @fields, 'paycardtype';
212 push @sort_fields, 'paycardtype';
216 push @header, emt('Unapplied');
219 push @fields, sub { sprintf($money, shift->unapplied_amount) };
220 push @sort_fields, '';
223 push @header, emt('Date');
226 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
227 push @sort_fields, '_date';
229 if ($cgi->param('show_order_number')) {
230 push @header, emt('Order Number');
233 push @fields, 'order_number';
234 push @sort_fields, 'order_number';
237 unless ( $opt{'disable_by'} ) {
238 push @header, emt('By');
241 push @fields, sub { my $o = shift->otaker;
242 $o = 'auto billing' if $o eq 'fs_daily';
243 $o = 'customer self-service' if $o eq 'fs_selfservice';
246 push @sort_fields, '';
250 push @header, (emt('Tax names'), emt('Tax province'));
252 push @links, ('','');
253 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
254 split('\|', shift->tax_names)
257 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
260 split('\|', shift->tax_names)
263 push @sort_fields, '', '';
266 push @header, FS::UI::Web::cust_header();
267 $align .= FS::UI::Web::cust_aligns();
268 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
269 FS::UI::Web::cust_header();
270 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
271 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
272 push @fields, \&FS::UI::Web::cust_fields;
273 push @sort_fields, FS::UI::Web::cust_sort_fields;
275 push @header, @{ $opt{'addl_header'} }
276 if $opt{'addl_header'};
277 push @fields, @{ $opt{'addl_fields'} }
278 if $opt{'addl_fields'};
279 push @sort_fields, @{ $opt{'addl_sort_fields'} }
280 if $opt{'addl_sort_fields'};
282 my( $count_query, $sql_query, @count_addl );
283 if ( $cgi->param('magic') ) {
288 "( $table.payby || ' ' || coalesce($table.paymask, $table.payinfo) ) AS paysort",
289 FS::UI::Web::cust_sql_fields(),
290 'cust_main.custnum AS cust_main_custnum',
292 push @select, $tax_names if $tax_names;
295 if ( $cgi->param('magic') eq '_date' ) {
297 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
298 push @search, "cust_main.agentnum = $1"; # $search{'agentnum'} = $1;
299 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
300 die "unknown agentnum $1" unless $agent;
301 $title = $agent->agent. " $title";
304 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
305 push @search, "cust_main.refnum = $1";
306 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
307 die "unknown refnum $1" unless $part_referral;
308 $title = $part_referral->referral. " $title";
311 # cust_classnum - standard matching
312 push @search, $m->comp('match-classnum',
313 param => 'cust_classnum', field => 'cust_main.classnum'
316 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
317 push @search, "$table.custnum = $1";
320 if ( $cgi->param('payby') ) {
322 my @all_payby_search = ();
323 foreach my $payby_string ( $cgi->param('payby') ) {
327 my ($payby, $subtype) = split('-', $payby_string);
328 # make sure it exists and is a transaction type
329 if ( FS::payby->payment_payby2longname($payby) ) {
330 $payby_search = "$table.payby = " . dbh->quote($payby);
332 die "illegal payby $payby_string";
337 my $in_cardtype = $cardtype_of{$subtype}
338 or die "unknown card type $subtype";
339 $payby_search .= " AND $table.paycardtype IN($in_cardtype)";
343 push @all_payby_search, $payby_search;
347 push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
351 if ( $cgi->param('paymask') ) {
352 $cgi->param('paymask') =~ /^\s*(\d+)\s*$/
353 or die "illegal paymask ". $cgi->param('paymask');
354 my $regexp = regexp_sql();
355 push @search, "$table.paymask $regexp '$1\$'";
358 if ( $cgi->param('payinfo') ) {
359 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
360 or die "illegal payinfo ". $cgi->param('payinfo');
361 my $regexp = regexp_sql();
362 push @search, "$table.payinfo $regexp '^0*$1\$'";
365 if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
366 # I think that's all the characters we need to allow.
367 # To avoid confusion, this parameter searches both auth and order_number.
368 push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
369 push @fields, 'auth', 'order_number';
370 push @header, 'Auth #', 'Transaction #';
371 push @sort_fields, '', '';
376 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
377 push @search, "$table.usernum = $1";
380 #for cust_pay_pending... statusNOT=done
381 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
382 push @search, "$table.status != '$1'";
385 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
387 push @search, "$table._date >= $beginning ",
388 "$table._date <= $ending";
390 if ( $table eq 'cust_pay_void' ) {
391 my($v_beginning, $v_ending) =
392 FS::UI::Web::parse_beginning_ending($cgi, 'void');
393 push @search, "$table.void_date >= $v_beginning ",
394 "$table.void_date <= $v_ending";
397 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field, $table);
401 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
403 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
404 or die "illegal paybatch: ". $cgi->param('paybatch');
406 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
408 } elsif ( $cgi->param('magic') eq 'batchnum' ) {
410 $cgi->param('batchnum') =~ /^(\d+)$/
411 or die "illegal batchnum: ".$cgi->param('batchnum');
413 push @search, "batchnum = $1";
415 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
418 die "unknown search magic: ". $cgi->param('magic');
421 if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
422 push @search, "paybatch = '$1'";
425 #unapplied payment/refund
427 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
428 push @search, "FS::$table"->unapplied_sql . ' > 0';
432 #for the history search
433 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
434 my @history_action = split(/,/, $1);
435 push @search, 'history_action IN ('.
436 join(',', map "'$_'", @history_action ). ')';
439 if ( $cgi->param('history_date_beginning')
440 || $cgi->param('history_date_ending') ) {
441 my($h_beginning, $h_ending) =
442 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
443 push @search, "history_date >= $h_beginning ",
444 "history_date <= $h_ending";
447 #here is the agent virtualization
448 push @search, $curuser->agentnums_sql;
450 my $addl_from = FS::UI::Web::join_cust_main($table);
453 # reasons, for refunds and voided payments
455 push @select, "reason.reason";
456 $addl_from .= " LEFT JOIN reason USING (reasonnum)\n";
457 push @fields, 'reason';
458 push @sort_fields, 'reason.reason';
459 push @header, emt('Reason');
460 if ( $cgi->param('reasonnum') =~ /^(\d+)$/ ) {
461 push @search, "COALESCE(reasonnum, 0) = $1";
465 #check for customer tags
467 foreach my $p ($cgi->param) {
468 if ($p =~ /^tagnum(\d+)/ && $1) {
469 $addl_from .= " LEFT JOIN cust_tag ON (cust_tag.custnum = cust_pay.custnum)" unless @tags;
474 my $tags = join(',',@tags);
475 push @search, "cust_tag.tagnum in ($tags)";
478 if ( $cgi->param('tax_names') ) {
479 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
483 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
485 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
486 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
487 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
488 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
489 FS::UI::Web::cust_sql_fields();
491 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
495 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
496 "omitting tax name information from report.";
501 #customer location... total false laziness w/cust_main/Search.pm
504 unless ( $cgi->param('location_history') ) {
506 AND ( cust_location.locationnum IN ( cust_main.bill_locationnum,
507 cust_main.ship_locationnum
509 OR cust_location.locationnum IN (
510 SELECT locationnum FROM cust_pkg
511 WHERE cust_pkg.custnum = cust_main.custnum
512 AND locationnum IS NOT NULL
513 AND '. FS::cust_pkg->ncancelled_recurring_sql.'
522 if ( $cgi->param('address') ) {
523 my @values = $cgi->param('address');
525 foreach (grep /\S/, @values) {
526 my $address = dbh->quote('%'. lc($_). '%');
528 "LOWER(cust_location.address1) LIKE $address",
529 "LOWER(cust_location.address2) LIKE $address";
532 push @search, "EXISTS(
533 SELECT 1 FROM cust_location
534 WHERE cust_location.custnum = cust_main.custnum
535 AND (".join(' OR ',@orwhere).")
544 if ( $cgi->param('city') =~ /\S/ ) {
545 my $city = dbh->quote($cgi->param('city'));
546 push @search, "EXISTS(
547 SELECT 1 FROM cust_location
548 WHERE cust_location.custnum = cust_main.custnum
549 AND cust_location.city = $city
557 if ( $cgi->param('county') =~ /\S/ ) {
558 my $county = dbh->quote($cgi->param('county'));
559 push @search, "EXISTS(
560 SELECT 1 FROM cust_location
561 WHERE cust_location.custnum = cust_main.custnum
562 AND cust_location.county = $county
570 if ( $cgi->param('state') =~ /\S/ ) {
571 my $state = dbh->quote($cgi->param('state'));
572 push @search, "EXISTS(
573 SELECT 1 FROM cust_location
574 WHERE cust_location.custnum = cust_main.custnum
575 AND cust_location.state = $state
583 if ( $cgi->param('zip') =~ /\S/ ) {
584 my $zip = dbh->quote($cgi->param('zip') . '%');
585 push @search, "EXISTS(
586 SELECT 1 FROM cust_location
587 WHERE cust_location.custnum = cust_main.custnum
588 AND cust_location.zip LIKE $zip
596 if ( $cgi->param('country') =~ /^(\w\w)$/ ) {
597 my $country = uc($1);
598 push @search, "EXISTS(
599 SELECT 1 FROM cust_location
600 WHERE cust_location.custnum = cust_main.custnum
601 AND cust_location.country = '$country'
606 #end of false laziness w/cust_main/Search.pm
608 my $search = ' WHERE '. join(' AND ', @search);
610 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) ";
611 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
613 $count_query .= "FROM $table $addl_from".
616 @count_addl = ( '$%.2f total '.$opt{name_verb} );
617 push @count_addl, '$%.2f unapplied' if $unapplied;
621 'select' => join(', ', @select),
623 'extra_sql' => "$search $group_by",
624 'order_by' => "ORDER BY $orderby",
625 'addl_from' => $addl_from,
630 #hmm... is this still used?
631 warn "undefined search magic";
633 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
636 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
639 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) FROM $table".
640 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
641 " AND ". $curuser->agentnums_sql;
642 @count_addl = ( '$%.2f total '.$opt{name_verb} );
646 'hashref' => { 'payinfo' => $payinfo,
648 'extra_sql' => $curuser->agentnums_sql.
655 $title = join('',map {ucfirst} split(/\b/,$title));