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 <% include( 'search.html',
44 'name_singular' => $name_singular,
45 'query' => $sql_query,
46 'count_query' => $count_query,
47 'count_addl' => [ '$%.2f total '.$opt{name_verb}, ],
48 'redirect_empty' => $opt{'redirect_empty'},
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 $title = "\u$name_singular Search Results";
74 if ( ( $curuser->access_right('View invoices') #XXX for now
75 || $curuser->access_right('View customer payments')
77 && ! $opt{'disable_link'}
83 if ( $table eq 'cust_pay_void' ) {
86 } elsif ( $table eq /^cust_(\w+)$/ ) {
92 $link = [ "${p}view/$table.html?$q", $key ]
97 my $cust_thing = shift;
98 $cust_thing->cust_main_custnum
99 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
103 # only valid for $table == 'cust_pay' atm
105 if ( $cgi->param('tax_names') ) {
106 if ( dbh->{Driver}->{Name} eq 'Pg' ) {
113 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
114 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
115 WHERE cust_bill_pkg.pkgnum = 0
116 AND cust_bill_pay.paynum = cust_pay.paynum
121 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
123 $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
127 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
128 "omitting tax name information from report.";
137 if ( $opt{'pre_header'} ) {
138 push @header, @{ $opt{'pre_header'} };
139 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
140 push @links, map '', @{ $opt{'pre_header'} };
141 push @fields, @{ $opt{'pre_fields'} };
144 push @header, "\u$name_singular",
149 push @links, '', '', '';
150 push @fields, 'payby_payinfo_pretty',
151 sub { sprintf('$%.2f', shift->$amount_field() ) },
152 sub { time2str('%b %d %Y', shift->_date ) },
155 unless ( $opt{'disable_by'} ) {
159 push @fields, sub { my $o = shift->otaker;
160 $o = 'auto billing' if $o eq 'fs_daily';
161 $o = 'customer self-service' if $o eq 'fs_selfservice';
167 push @header, ('Tax names', 'Tax province');
169 push @links, ('','');
170 push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
171 split('\|', shift->tax_names)
174 push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
177 split('\|', shift->tax_names)
182 push @header, FS::UI::Web::cust_header();
183 $align .= FS::UI::Web::cust_aligns();
184 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
185 FS::UI::Web::cust_header();
186 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
187 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
188 push @fields, \&FS::UI::Web::cust_fields;
190 push @header, @{ $opt{'addl_header'} }
191 if $opt{'addl_header'};
192 push @fields, @{ $opt{'addl_fields'} }
193 if $opt{'addl_fields'};
195 my( $count_query, $sql_query );
196 if ( $cgi->param('magic') ) {
200 if ( $cgi->param('magic') eq '_date' ) {
202 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
203 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
204 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
205 die "unknown agentnum $1" unless $agent;
206 $title = $agent->agent. " $title";
209 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
210 push @search, "custnum = $1";
213 if ( $cgi->param('payby') ) {
214 $cgi->param('payby') =~
215 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
216 or die "illegal payby ". $cgi->param('payby');
217 push @search, "$table.payby = '$1'";
223 if ( $cardtype eq 'VisaMC' ) {
224 #avoid posix regexes for portability
226 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
227 " AND substring($table.payinfo from 1 for 4) != '4936' ".
228 " AND substring($table.payinfo from 1 for 6) ".
229 " NOT SIMILAR TO '49030[2-9]' ".
230 " AND substring($table.payinfo from 1 for 6) ".
231 " NOT SIMILAR TO '49033[5-9]' ".
232 " AND substring($table.payinfo from 1 for 6) ".
233 " NOT SIMILAR TO '49110[1-2]' ".
234 " AND substring($table.payinfo from 1 for 6) ".
235 " NOT SIMILAR TO '49117[4-9]' ".
236 " AND substring($table.payinfo from 1 for 6) ".
237 " NOT SIMILAR TO '49118[1-2]' ".
239 " OR substring($table.payinfo from 1 for 2) = '51' ".
240 " OR substring($table.payinfo from 1 for 2) = '52' ".
241 " OR substring($table.payinfo from 1 for 2) = '53' ".
242 " OR substring($table.payinfo from 1 for 2) = '54' ".
243 " OR substring($table.payinfo from 1 for 2) = '54' ".
244 " OR substring($table.payinfo from 1 for 2) = '55' ".
245 " OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
247 } elsif ( $cardtype eq 'Amex' ) {
249 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
250 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
252 } elsif ( $cardtype eq 'Discover' ) {
254 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
255 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
256 " OR substring($table.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
258 } elsif ( $cardtype eq 'Maestro' ) {
260 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
261 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
262 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
263 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
264 " OR substring($table.payinfo from 1 for 6 ) ".
265 " SIMILAR TO '49030[2-9]' ".
266 " OR substring($table.payinfo from 1 for 6 ) ".
267 " SIMILAR TO '49033[5-9]' ".
268 " OR substring($table.payinfo from 1 for 6 ) ".
269 " SIMILAR TO '49110[1-2]' ".
270 " OR substring($table.payinfo from 1 for 6 ) ".
271 " SIMILAR TO '49117[4-9]' ".
272 " OR substring($table.payinfo from 1 for 6 ) ".
273 " SIMILAR TO '49118[1-2]' ".
276 die "unknown card type $cardtype";
279 my $masksearch = $search;
280 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
283 "( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) )";
288 if ( $cgi->param('payinfo') ) {
289 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
290 or die "illegal payinfo ". $cgi->param('payinfo');
291 push @search, "$table.payinfo = '$1'";
294 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
295 push @search, "$table.usernum = $1";
298 #for cust_pay_pending... statusNOT=done
299 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
300 push @search, "status != '$1'";
303 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
304 push @search, "_date >= $beginning ",
307 if ( $table eq 'cust_pay_void' ) {
308 my($v_beginning, $v_ending) =
309 FS::UI::Web::parse_beginning_ending($cgi, 'void');
310 push @search, "void_date >= $v_beginning ",
311 "void_date <= $v_ending";
314 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
318 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
320 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
321 or die "illegal paybatch: ". $cgi->param('paybatch');
323 push @search, "paybatch = '$1'";
325 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
328 die "unknown search magic: ". $cgi->param('magic');
331 #for the history search
332 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
333 my @history_action = split(/,/, $1);
334 push @search, 'history_action IN ('.
335 join(',', map "'$_'", @history_action ). ')';
338 if ( $cgi->param('history_date_beginning')
339 || $cgi->param('history_date_ending') ) {
340 my($h_beginning, $h_ending) =
341 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
342 push @search, "history_date >= $h_beginning ",
343 "history_date <= $h_ending";
346 #here is the agent virtualization
347 push @search, $curuser->agentnums_sql;
349 my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
352 if ( $cgi->param('tax_names') ) {
353 if ( dbh->{Driver}->{Name} eq 'Pg' ) {
357 } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
359 $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
360 LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
361 LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
362 $group_by .= "GROUP BY $table.*,cust_main_custnum,".
363 FS::UI::Web::cust_sql_fields();
365 "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
369 warn "warning: unknown database type ". dbh->{Driver}->{Name}.
370 "omitting tax name information from report.";
375 my $search = ' WHERE '. join(' AND ', @search);
377 $count_query = "SELECT COUNT(*), SUM($amount_field) ".
378 "FROM $table $addl_from".
383 'select' => join(', ',
385 ( $tax_names ? $tax_names : () ),
386 'cust_main.custnum as cust_main_custnum',
387 FS::UI::Web::cust_sql_fields(),
390 'extra_sql' => "$search $group_by ORDER BY $orderby",
391 'addl_from' => $addl_from,
396 #hmm... is this still used?
398 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
401 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
404 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
405 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
406 " AND ". $curuser->agentnums_sql;
410 'hashref' => { 'payinfo' => $payinfo,
412 'extra_sql' => $curuser->agentnums_sql.