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' ],
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,
61 my $curuser = $FS::CurrentUser::CurrentUser;
64 unless $curuser->access_right('Financial reports');
66 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
68 my $amount_field = $opt{'amount_field'};
69 my $name_singular = $opt{'name_singular'};
71 my $unapplied = $cgi->param('unapplied');
73 $title = 'Unapplied ' if $unapplied;
74 $title .= "\u$name_singular Search Results";
77 if ( ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
78 || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
79 || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
81 && ! $opt{'disable_link'}
87 if ( $table eq 'cust_pay_void' ) {
90 } elsif ( $table eq /^cust_(\w+)$/ ) {
96 $link = [ "${p}view/$table.html?$q", $key ]
100 my $cust_link = sub {
101 my $cust_thing = shift;
102 $cust_thing->cust_main_custnum
103 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
107 # only valid for $table == 'cust_pay' atm
109 if ( $cgi->param('tax_names') ) {
110 if ( dbh->{Driver}->{Name} eq 'Pg' ) {
117 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
118 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
119 WHERE cust_bill_pkg.pkgnum = 0
120 AND cust_bill_pay.paynum = cust_pay.paynum
125 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
127 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
131 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
132 "omitting tax name information from report.";
139 my @sort_fields = ();
142 if ( $opt{'pre_header'} ) {
143 push @header, @{ $opt{'pre_header'} };
144 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
145 push @links, map '', @{ $opt{'pre_header'} };
146 push @fields, @{ $opt{'pre_fields'} };
147 push @sort_fields, @{ $opt{'pre_fields'} };
150 push @header, "\u$name_singular",
155 push @fields, 'payby_payinfo_pretty',
156 sub { sprintf('$%.2f', shift->$amount_field() ) },
158 push @sort_fields, '', $amount_field;
161 push @header, emt('Unapplied');
164 push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) };
165 push @sort_fields, '';
168 push @header, emt('Date');
171 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
172 push @sort_fields, '_date';
174 unless ( $opt{'disable_by'} ) {
175 push @header, emt('By');
178 push @fields, sub { my $o = shift->otaker;
179 $o = 'auto billing' if $o eq 'fs_daily';
180 $o = 'customer self-service' if $o eq 'fs_selfservice';
186 push @header, (emt('Tax names'), emt('Tax province'));
188 push @links, ('','');
189 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
190 split('\|', shift->tax_names)
193 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
196 split('\|', shift->tax_names)
201 push @header, FS::UI::Web::cust_header();
202 $align .= FS::UI::Web::cust_aligns();
203 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
204 FS::UI::Web::cust_header();
205 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
206 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
207 push @fields, \&FS::UI::Web::cust_fields;
209 push @header, @{ $opt{'addl_header'} }
210 if $opt{'addl_header'};
211 push @fields, @{ $opt{'addl_fields'} }
212 if $opt{'addl_fields'};
214 my( $count_query, $sql_query, @count_addl );
215 if ( $cgi->param('magic') ) {
220 FS::UI::Web::cust_sql_fields(),
221 'cust_main.custnum AS cust_main_custnum',
223 push @select, $tax_names if $tax_names;
226 if ( $cgi->param('magic') eq '_date' ) {
228 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
229 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
230 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
231 die "unknown agentnum $1" unless $agent;
232 $title = $agent->agent. " $title";
235 if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
236 push @search, "refnum = $1";
237 my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
238 die "unknown refnum $1" unless $part_referral;
239 $title = $part_referral->referral. " $title";
242 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
243 push @search, "custnum = $1";
246 if ( $cgi->param('payby') ) {
247 $cgi->param('payby') =~
248 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
249 or die "illegal payby ". $cgi->param('payby');
250 push @search, "$table.payby = '$1'";
256 if ( $cardtype eq 'VisaMC' ) {
257 #avoid posix regexes for portability
259 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
260 " AND substring($table.payinfo from 1 for 4) != '4936' ".
261 " AND substring($table.payinfo from 1 for 6) ".
262 " NOT SIMILAR TO '49030[2-9]' ".
263 " AND substring($table.payinfo from 1 for 6) ".
264 " NOT SIMILAR TO '49033[5-9]' ".
265 " AND substring($table.payinfo from 1 for 6) ".
266 " NOT SIMILAR TO '49110[1-2]' ".
267 " AND substring($table.payinfo from 1 for 6) ".
268 " NOT SIMILAR TO '49117[4-9]' ".
269 " AND substring($table.payinfo from 1 for 6) ".
270 " NOT SIMILAR TO '49118[1-2]' ".
272 " OR substring($table.payinfo from 1 for 2) = '51' ".
273 " OR substring($table.payinfo from 1 for 2) = '52' ".
274 " OR substring($table.payinfo from 1 for 2) = '53' ".
275 " OR substring($table.payinfo from 1 for 2) = '54' ".
276 " OR substring($table.payinfo from 1 for 2) = '54' ".
277 " OR substring($table.payinfo from 1 for 2) = '55' ".
278 " OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
280 } elsif ( $cardtype eq 'Amex' ) {
282 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
283 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
285 } elsif ( $cardtype eq 'Discover' ) {
287 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
288 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
289 " OR substring($table.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
291 } elsif ( $cardtype eq 'Maestro' ) {
293 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
294 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
295 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
296 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
297 " OR substring($table.payinfo from 1 for 6 ) ".
298 " SIMILAR TO '49030[2-9]' ".
299 " OR substring($table.payinfo from 1 for 6 ) ".
300 " SIMILAR TO '49033[5-9]' ".
301 " OR substring($table.payinfo from 1 for 6 ) ".
302 " SIMILAR TO '49110[1-2]' ".
303 " OR substring($table.payinfo from 1 for 6 ) ".
304 " SIMILAR TO '49117[4-9]' ".
305 " OR substring($table.payinfo from 1 for 6 ) ".
306 " SIMILAR TO '49118[1-2]' ".
309 die "unknown card type $cardtype";
312 my $masksearch = $search;
313 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
316 "( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) )";
321 if ( $cgi->param('payinfo') ) {
322 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
323 or die "illegal payinfo ". $cgi->param('payinfo');
324 push @search, "$table.payinfo = '$1'";
327 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
328 push @search, "$table.usernum = $1";
331 #for cust_pay_pending... statusNOT=done
332 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
333 push @search, "status != '$1'";
336 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
337 push @search, "_date >= $beginning ",
340 if ( $table eq 'cust_pay_void' ) {
341 my($v_beginning, $v_ending) =
342 FS::UI::Web::parse_beginning_ending($cgi, 'void');
343 push @search, "void_date >= $v_beginning ",
344 "void_date <= $v_ending";
347 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
351 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
353 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
354 or die "illegal paybatch: ". $cgi->param('paybatch');
356 push @search, "paybatch = '$1'";
358 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
361 die "unknown search magic: ". $cgi->param('magic');
364 #unapplied payment/refund
366 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
367 push @search, "FS::$table"->unapplied_sql . ' > 0';
371 #for the history search
372 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
373 my @history_action = split(/,/, $1);
374 push @search, 'history_action IN ('.
375 join(',', map "'$_'", @history_action ). ')';
378 if ( $cgi->param('history_date_beginning')
379 || $cgi->param('history_date_ending') ) {
380 my($h_beginning, $h_ending) =
381 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
382 push @search, "history_date >= $h_beginning ",
383 "history_date <= $h_ending";
386 #here is the agent virtualization
387 push @search, $curuser->agentnums_sql;
389 my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
392 if ( $cgi->param('tax_names') ) {
393 if ( dbh->{Driver}->{Name} eq 'Pg' ) {
397 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
399 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
400 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
401 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
402 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
403 FS::UI::Web::cust_sql_fields();
405 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
409 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
410 "omitting tax name information from report.";
415 my $search = ' WHERE '. join(' AND ', @search);
417 $count_query = "SELECT COUNT(*), SUM($amount_field) ";
418 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
420 $count_query .= "FROM $table $addl_from".
423 @count_addl = ( '$%.2f total '.$opt{name_verb} );
424 push @count_addl, '$%.2f unapplied' if $unapplied;
428 'select' => join(', ', @select),
430 'extra_sql' => "$search $group_by",
431 'order_by' => "ORDER BY $orderby",
432 'addl_from' => $addl_from,
437 #hmm... is this still used?
439 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
442 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
445 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
446 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
447 " AND ". $curuser->agentnums_sql;
448 @count_addl = ( '$%.2f total '.$opt{name_verb} );
452 'hashref' => { 'payinfo' => $payinfo,
454 'extra_sql' => $curuser->agentnums_sql.
461 $title = join('',map {ucfirst} split(/\b/,$title));