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' ]
107 if ( $opt{'pre_header'} ) {
108 push @header, @{ $opt{'pre_header'} };
109 $align .= 'c' x scalar(@{ $opt{'pre_header'} });
110 push @links, map '', @{ $opt{'pre_header'} };
111 push @fields, @{ $opt{'pre_fields'} };
114 push @header, "\u$name_singular",
119 push @links, '', '', '';
120 push @fields, 'payby_payinfo_pretty',
121 sub { sprintf('$%.2f', shift->$amount_field() ) },
122 sub { time2str('%b %d %Y', shift->_date ) },
125 unless ( $opt{'disable_by'} ) {
129 push @fields, sub { my $o = shift->otaker;
130 $o = 'auto billing' if $o eq 'fs_daily';
131 $o = 'customer self-service' if $o eq 'fs_selfservice';
136 push @header, FS::UI::Web::cust_header();
137 $align .= FS::UI::Web::cust_aligns();
138 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
139 FS::UI::Web::cust_header();
140 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
141 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
142 push @fields, \&FS::UI::Web::cust_fields;
144 push @header, @{ $opt{'addl_header'} }
145 if $opt{'addl_header'};
146 push @fields, @{ $opt{'addl_fields'} }
147 if $opt{'addl_fields'};
149 my( $count_query, $sql_query );
150 if ( $cgi->param('magic') ) {
154 if ( $cgi->param('magic') eq '_date' ) {
156 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
157 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
158 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
159 die "unknown agentnum $1" unless $agent;
160 $title = $agent->agent. " $title";
163 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
164 push @search, "custnum = $1";
167 if ( $cgi->param('payby') ) {
168 $cgi->param('payby') =~
169 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
170 or die "illegal payby ". $cgi->param('payby');
171 push @search, "$table.payby = '$1'";
177 if ( $cardtype eq 'VisaMC' ) {
178 #avoid posix regexes for portability
180 " ( ( substring($table.payinfo from 1 for 1) = '4' ".
181 " AND substring($table.payinfo from 1 for 4) != '4936' ".
182 " AND substring($table.payinfo from 1 for 6) ".
183 " NOT SIMILAR TO '49030[2-9]' ".
184 " AND substring($table.payinfo from 1 for 6) ".
185 " NOT SIMILAR TO '49033[5-9]' ".
186 " AND substring($table.payinfo from 1 for 6) ".
187 " NOT SIMILAR TO '49110[1-2]' ".
188 " AND substring($table.payinfo from 1 for 6) ".
189 " NOT SIMILAR TO '49117[4-9]' ".
190 " AND substring($table.payinfo from 1 for 6) ".
191 " NOT SIMILAR TO '49118[1-2]' ".
193 " OR substring($table.payinfo from 1 for 2) = '51' ".
194 " OR substring($table.payinfo from 1 for 2) = '52' ".
195 " OR substring($table.payinfo from 1 for 2) = '53' ".
196 " OR substring($table.payinfo from 1 for 2) = '54' ".
197 " OR substring($table.payinfo from 1 for 2) = '54' ".
198 " OR substring($table.payinfo from 1 for 2) = '55' ".
199 " OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
201 } elsif ( $cardtype eq 'Amex' ) {
203 " ( substring($table.payinfo from 1 for 2 ) = '34' ".
204 " OR substring($table.payinfo from 1 for 2 ) = '37' ".
206 } elsif ( $cardtype eq 'Discover' ) {
208 " ( substring($table.payinfo from 1 for 4 ) = '6011' ".
209 " OR substring($table.payinfo from 1 for 2 ) = '65' ".
210 " OR substring($table.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
212 } elsif ( $cardtype eq 'Maestro' ) {
214 " ( substring($table.payinfo from 1 for 2 ) = '63' ".
215 " OR substring($table.payinfo from 1 for 2 ) = '67' ".
216 " OR substring($table.payinfo from 1 for 6 ) = '564182' ".
217 " OR substring($table.payinfo from 1 for 4 ) = '4936' ".
218 " OR substring($table.payinfo from 1 for 6 ) ".
219 " SIMILAR TO '49030[2-9]' ".
220 " OR substring($table.payinfo from 1 for 6 ) ".
221 " SIMILAR TO '49033[5-9]' ".
222 " OR substring($table.payinfo from 1 for 6 ) ".
223 " SIMILAR TO '49110[1-2]' ".
224 " OR substring($table.payinfo from 1 for 6 ) ".
225 " SIMILAR TO '49117[4-9]' ".
226 " OR substring($table.payinfo from 1 for 6 ) ".
227 " SIMILAR TO '49118[1-2]' ".
230 die "unknown card type $cardtype";
233 my $masksearch = $search;
234 $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
237 "( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) )";
242 if ( $cgi->param('payinfo') ) {
243 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
244 or die "illegal payinfo ". $cgi->param('payinfo');
245 push @search, "$table.payinfo = '$1'";
248 if ( $cgi->param('otaker') =~ /^(\w+)$/ ) {
249 push @search, "$table.otaker = '$1'";
252 #for cust_pay_pending... statusNOT=done
253 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
254 push @search, "status != '$1'";
257 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
258 push @search, "_date >= $beginning ",
261 if ( $table eq 'cust_pay_void' ) {
262 my($v_beginning, $v_ending) =
263 FS::UI::Web::parse_beginning_ending($cgi, 'void');
264 push @search, "void_date >= $v_beginning ",
265 "void_date <= $v_ending";
268 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
272 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
274 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
275 or die "illegal paybatch: ". $cgi->param('paybatch');
277 push @search, "paybatch = '$1'";
279 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
282 die "unknown search magic: ". $cgi->param('magic');
285 #for the history search
286 if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
287 my @history_action = split(/,/, $1);
288 push @search, 'history_action IN ('.
289 join(',', map "'$_'", @history_action ). ')';
292 if ( $cgi->param('history_date_beginning')
293 || $cgi->param('history_date_ending') ) {
294 my($h_beginning, $h_ending) =
295 FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
296 push @search, "history_date >= $h_beginning ",
297 "history_date <= $h_ending";
300 #here is the agent virtualization
301 push @search, $curuser->agentnums_sql;
303 my $search = ' WHERE '. join(' AND ', @search);
305 $count_query = "SELECT COUNT(*), SUM($amount_field) ".
306 "FROM $table LEFT JOIN cust_main USING ( custnum )".
311 'select' => join(', ',
313 'cust_main.custnum as cust_main_custnum',
314 FS::UI::Web::cust_sql_fields(),
317 'extra_sql' => "$search ORDER BY $orderby",
318 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
323 #hmm... is this still used?
325 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
328 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
331 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
332 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
333 " AND ". $curuser->agentnums_sql;
337 'hashref' => { 'payinfo' => $payinfo,
339 'extra_sql' => $curuser->agentnums_sql.