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('Financial 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('payinfo') ) {
352 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
353 or die "illegal payinfo ". $cgi->param('payinfo');
354 my $regexp = regexp_sql();
355 push @search, "$table.payinfo $regexp '^0*$1\$'";
358 if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
359 # I think that's all the characters we need to allow.
360 # To avoid confusion, this parameter searches both auth and order_number.
361 push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
362 push @fields, 'auth', 'order_number';
363 push @header, 'Auth #', 'Transaction #';
364 push @sort_fields, '', '';
369 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
370 push @search, "$table.usernum = $1";
373 #for cust_pay_pending... statusNOT=done
374 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
375 push @search, "$table.status != '$1'";
378 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
380 push @search, "$table._date >= $beginning ",
381 "$table._date <= $ending";
383 if ( $table eq 'cust_pay_void' ) {
384 my($v_beginning, $v_ending) =
385 FS::UI::Web::parse_beginning_ending($cgi, 'void');
386 push @search, "$table.void_date >= $v_beginning ",
387 "$table.void_date <= $v_ending";
390 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field, $table);
394 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
396 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
397 or die "illegal paybatch: ". $cgi->param('paybatch');
399 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
401 } elsif ( $cgi->param('magic') eq 'batchnum' ) {
403 $cgi->param('batchnum') =~ /^(\d+)$/
404 or die "illegal batchnum: ".$cgi->param('batchnum');
406 push @search, "batchnum = $1";
408 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
411 die "unknown search magic: ". $cgi->param('magic');
414 if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
415 push @search, "paybatch = '$1'";
418 #unapplied payment/refund
420 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
421 push @search, "FS::$table"->unapplied_sql . ' > 0';
425 #for the history search
426 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
427 my @history_action = split(/,/, $1);
428 push @search, 'history_action IN ('.
429 join(',', map "'$_'", @history_action ). ')';
432 if ( $cgi->param('history_date_beginning')
433 || $cgi->param('history_date_ending') ) {
434 my($h_beginning, $h_ending) =
435 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
436 push @search, "history_date >= $h_beginning ",
437 "history_date <= $h_ending";
440 #here is the agent virtualization
441 push @search, $curuser->agentnums_sql;
443 my $addl_from = FS::UI::Web::join_cust_main($table);
446 # reasons, for refunds and voided payments
448 push @select, "reason.reason";
449 $addl_from .= " LEFT JOIN reason USING (reasonnum)\n";
450 push @fields, 'reason';
451 push @sort_fields, 'reason.reason';
452 push @header, emt('Reason');
453 if ( $cgi->param('reasonnum') =~ /^(\d+)$/ ) {
454 push @search, "COALESCE(reasonnum, 0) = $1";
458 if ( $cgi->param('tax_names') ) {
459 if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
463 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
465 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
466 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
467 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
468 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
469 FS::UI::Web::cust_sql_fields();
471 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
475 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
476 "omitting tax name information from report.";
481 my $search = ' WHERE '. join(' AND ', @search);
483 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) ";
484 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
486 $count_query .= "FROM $table $addl_from".
489 @count_addl = ( '$%.2f total '.$opt{name_verb} );
490 push @count_addl, '$%.2f unapplied' if $unapplied;
494 'select' => join(', ', @select),
496 'extra_sql' => "$search $group_by",
497 'order_by' => "ORDER BY $orderby",
498 'addl_from' => $addl_from,
503 #hmm... is this still used?
504 warn "undefined search magic";
506 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
509 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
512 $count_query = "SELECT COUNT(*), SUM($table.$amount_field) FROM $table".
513 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
514 " AND ". $curuser->agentnums_sql;
515 @count_addl = ( '$%.2f total '.$opt{name_verb} );
519 'hashref' => { 'payinfo' => $payinfo,
521 'extra_sql' => $curuser->agentnums_sql.
528 $title = join('',map {ucfirst} split(/\b/,$title));