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') #XXX for now
78 || $curuser->access_right('View customer payments')
80 && ! $opt{'disable_link'}
86 if ( $table eq 'cust_pay_void' ) {
89 } elsif ( $table eq /^cust_(\w+)$/ ) {
95 $link = [ "${p}view/$table.html?$q", $key ]
100 my $cust_thing = shift;
101 $cust_thing->cust_main_custnum
102 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
106 # only valid for $table == 'cust_pay' atm
108 if ( $cgi->param('tax_names') ) {
109 if ( dbh->{Driver}->{Name} eq 'Pg' ) {
116 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
117 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
118 WHERE cust_bill_pkg.pkgnum = 0
119 AND cust_bill_pay.paynum = cust_pay.paynum
124 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
126 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
130 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
131 "omitting tax name information from report.";
138 my @sort_fields = ();
141 if ( $opt{'pre_header'} ) {
142 push @header, @{ $opt{'pre_header'} };
143 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
144 push @links, map '', @{ $opt{'pre_header'} };
145 push @fields, @{ $opt{'pre_fields'} };
146 push @sort_fields, @{ $opt{'pre_fields'} };
149 push @header, "\u$name_singular",
154 push @fields, 'payby_payinfo_pretty',
155 sub { sprintf('$%.2f', shift->$amount_field() ) },
157 push @sort_fields, '', $amount_field;
160 push @header, emt('Unapplied');
163 push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) };
164 push @sort_fields, '';
167 push @header, emt('Date');
170 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
171 push @sort_fields, '_date';
173 unless ( $opt{'disable_by'} ) {
174 push @header, emt('By');
177 push @fields, sub { my $o = shift->otaker;
178 $o = 'auto billing' if $o eq 'fs_daily';
179 $o = 'customer self-service' if $o eq 'fs_selfservice';
185 push @header, (emt('Tax names'), emt('Tax province'));
187 push @links, ('','');
188 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
189 split('\|', shift->tax_names)
192 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
195 split('\|', shift->tax_names)
200 push @header, FS::UI::Web::cust_header();
201 $align .= FS::UI::Web::cust_aligns();
202 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
203 FS::UI::Web::cust_header();
204 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
205 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
206 push @fields, \&FS::UI::Web::cust_fields;
208 push @header, @{ $opt{'addl_header'} }
209 if $opt{'addl_header'};
210 push @fields, @{ $opt{'addl_fields'} }
211 if $opt{'addl_fields'};
213 my( $count_query, $sql_query, @count_addl );
214 if ( $cgi->param('magic') ) {
219 FS::UI::Web::cust_sql_fields(),
220 'cust_main.custnum AS cust_main_custnum',
222 push @select, $tax_names if $tax_names;
225 if ( $cgi->param('magic') eq '_date' ) {
227 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
228 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
229 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
230 die "unknown agentnum $1" unless $agent;
231 $title = $agent->agent. " $title";
234 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
235 push @search, "custnum = $1";
238 if ( $cgi->param('payby') ) {
239 $cgi->param('payby') =~
240 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
241 or die "illegal payby ". $cgi->param('payby');
242 push @search, "$table.payby = '$1'";
248 if ( $cardtype eq 'VisaMC' ) {
249 #avoid posix regexes for portability
251 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
252 " AND substring($table.payinfo from 1 for 4) != '4936' ".
253 " AND substring($table.payinfo from 1 for 6) ".
254 " NOT SIMILAR TO '49030[2-9]' ".
255 " AND substring($table.payinfo from 1 for 6) ".
256 " NOT SIMILAR TO '49033[5-9]' ".
257 " AND substring($table.payinfo from 1 for 6) ".
258 " NOT SIMILAR TO '49110[1-2]' ".
259 " AND substring($table.payinfo from 1 for 6) ".
260 " NOT SIMILAR TO '49117[4-9]' ".
261 " AND substring($table.payinfo from 1 for 6) ".
262 " NOT SIMILAR TO '49118[1-2]' ".
264 " OR substring($table.payinfo from 1 for 2) = '51' ".
265 " OR substring($table.payinfo from 1 for 2) = '52' ".
266 " OR substring($table.payinfo from 1 for 2) = '53' ".
267 " OR substring($table.payinfo from 1 for 2) = '54' ".
268 " OR substring($table.payinfo from 1 for 2) = '54' ".
269 " OR substring($table.payinfo from 1 for 2) = '55' ".
270 " OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
272 } elsif ( $cardtype eq 'Amex' ) {
274 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
275 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
277 } elsif ( $cardtype eq 'Discover' ) {
279 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
280 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
281 " OR substring($table.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
283 } elsif ( $cardtype eq 'Maestro' ) {
285 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
286 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
287 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
288 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
289 " OR substring($table.payinfo from 1 for 6 ) ".
290 " SIMILAR TO '49030[2-9]' ".
291 " OR substring($table.payinfo from 1 for 6 ) ".
292 " SIMILAR TO '49033[5-9]' ".
293 " OR substring($table.payinfo from 1 for 6 ) ".
294 " SIMILAR TO '49110[1-2]' ".
295 " OR substring($table.payinfo from 1 for 6 ) ".
296 " SIMILAR TO '49117[4-9]' ".
297 " OR substring($table.payinfo from 1 for 6 ) ".
298 " SIMILAR TO '49118[1-2]' ".
301 die "unknown card type $cardtype";
304 my $masksearch = $search;
305 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
308 "( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) )";
313 if ( $cgi->param('payinfo') ) {
314 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
315 or die "illegal payinfo ". $cgi->param('payinfo');
316 push @search, "$table.payinfo = '$1'";
319 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
320 push @search, "$table.usernum = $1";
323 #for cust_pay_pending... statusNOT=done
324 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
325 push @search, "status != '$1'";
328 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
329 push @search, "_date >= $beginning ",
332 if ( $table eq 'cust_pay_void' ) {
333 my($v_beginning, $v_ending) =
334 FS::UI::Web::parse_beginning_ending($cgi, 'void');
335 push @search, "void_date >= $v_beginning ",
336 "void_date <= $v_ending";
339 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
343 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
345 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
346 or die "illegal paybatch: ". $cgi->param('paybatch');
348 push @search, "paybatch = '$1'";
350 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
353 die "unknown search magic: ". $cgi->param('magic');
356 #unapplied payment/refund
358 push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
359 push @search, "FS::$table"->unapplied_sql . ' > 0';
363 #for the history search
364 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
365 my @history_action = split(/,/, $1);
366 push @search, 'history_action IN ('.
367 join(',', map "'$_'", @history_action ). ')';
370 if ( $cgi->param('history_date_beginning')
371 || $cgi->param('history_date_ending') ) {
372 my($h_beginning, $h_ending) =
373 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
374 push @search, "history_date >= $h_beginning ",
375 "history_date <= $h_ending";
378 #here is the agent virtualization
379 push @search, $curuser->agentnums_sql;
381 my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
384 if ( $cgi->param('tax_names') ) {
385 if ( dbh->{Driver}->{Name} eq 'Pg' ) {
389 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
391 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
392 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
393 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
394 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
395 FS::UI::Web::cust_sql_fields();
397 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
401 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
402 "omitting tax name information from report.";
407 my $search = ' WHERE '. join(' AND ', @search);
409 $count_query = "SELECT COUNT(*), SUM($amount_field) ";
410 $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') '
412 $count_query .= "FROM $table $addl_from".
415 @count_addl = ( '$%.2f total '.$opt{name_verb} );
416 push @count_addl, '$%.2f unapplied' if $unapplied;
420 'select' => join(', ', @select),
422 'extra_sql' => "$search $group_by",
423 'order_by' => "ORDER BY $orderby",
424 'addl_from' => $addl_from,
429 #hmm... is this still used?
431 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
434 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
437 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
438 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
439 " AND ". $curuser->agentnums_sql;
440 @count_addl = ( '$%.2f total '.$opt{name_verb} );
444 'hashref' => { 'payinfo' => $payinfo,
446 'extra_sql' => $curuser->agentnums_sql.
453 $title = join('',map {ucfirst} split(/\b/,$title));