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,
33 <% include( 'search.html',
35 'name_singular' => $name_singular,
36 'query' => $sql_query,
37 'count_query' => $count_query,
38 'count_addl' => [ '$%.2f total '.$opt{name_verb}, ],
39 'redirect_empty' => $opt{'redirect_empty'},
40 'header' => [ "\u$name_singular",
44 FS::UI::Web::cust_header(),
47 'payby_payinfo_pretty',
48 sub { sprintf('$%.2f', shift->$amount_field() ) },
49 sub { time2str('%b %d %Y', shift->_date ) },
51 \&FS::UI::Web::cust_fields,
55 join('', map 'c', @fields ).
56 FS::UI::Web::cust_aligns(),
62 ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
63 FS::UI::Web::cust_header()
71 FS::UI::Web::cust_colors(),
78 FS::UI::Web::cust_styles(),
86 my $curuser = $FS::CurrentUser::CurrentUser;
89 unless $curuser->access_right('Financial reports');
91 my $thing = $opt{'thing'};
92 my $amount_field = $opt{'amount_field'};
93 my $name_singular = $opt{'name_singular'};
95 my $title = "\u$name_singular Search Results";
99 unless ( $opt{'disable_by'} ) {
101 push @fields, sub { my $o = shift->otaker;
102 $o = 'auto billing' if $o eq 'fs_daily';
103 $o = 'customer self-service' if $o eq 'fs_selfservice';
108 push @header, @{ $opt{'addl_header'} }
109 if $opt{'addl_header'};
110 push @fields, @{ $opt{'addl_fields'} }
111 if $opt{'addl_fields'};
113 my( $count_query, $sql_query );
114 if ( $cgi->param('magic') ) {
118 if ( $cgi->param('magic') eq '_date' ) {
120 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
121 push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
122 my $agent = qsearchs('agent', { 'agentnum' => $1 } );
123 die "unknown agentnum $1" unless $agent;
124 $title = $agent->agent. " $title";
127 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
128 push @search, "custnum = $1";
131 if ( $cgi->param('payby') ) {
132 $cgi->param('payby') =~
133 /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
134 or die "illegal payby ". $cgi->param('payby');
135 push @search, "cust_$thing.payby = '$1'";
141 if ( $cardtype eq 'VisaMC' ) {
142 #avoid posix regexes for portability
144 " ( ( substring(cust_$thing.payinfo from 1 for 1) = '4' ".
145 " AND substring(cust_$thing.payinfo from 1 for 4) != '4936' ".
146 " AND substring(cust_$thing.payinfo from 1 for 6) ".
147 " NOT SIMILAR TO '49030[2-9]' ".
148 " AND substring(cust_$thing.payinfo from 1 for 6) ".
149 " NOT SIMILAR TO '49033[5-9]' ".
150 " AND substring(cust_$thing.payinfo from 1 for 6) ".
151 " NOT SIMILAR TO '49110[1-2]' ".
152 " AND substring(cust_$thing.payinfo from 1 for 6) ".
153 " NOT SIMILAR TO '49117[4-9]' ".
154 " AND substring(cust_$thing.payinfo from 1 for 6) ".
155 " NOT SIMILAR TO '49118[1-2]' ".
157 " OR substring(cust_$thing.payinfo from 1 for 2) = '51' ".
158 " OR substring(cust_$thing.payinfo from 1 for 2) = '52' ".
159 " OR substring(cust_$thing.payinfo from 1 for 2) = '53' ".
160 " OR substring(cust_$thing.payinfo from 1 for 2) = '54' ".
161 " OR substring(cust_$thing.payinfo from 1 for 2) = '54' ".
162 " OR substring(cust_$thing.payinfo from 1 for 2) = '55' ".
163 " OR substring(cust_$thing.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
165 } elsif ( $cardtype eq 'Amex' ) {
167 " ( substring(cust_$thing.payinfo from 1 for 2 ) = '34' ".
168 " OR substring(cust_$thing.payinfo from 1 for 2 ) = '37' ".
170 } elsif ( $cardtype eq 'Discover' ) {
172 " ( substring(cust_$thing.payinfo from 1 for 4 ) = '6011' ".
173 " OR substring(cust_$thing.payinfo from 1 for 2 ) = '65' ".
174 " OR substring(cust_$thing.payinfo from 1 for 3 ) = '622' ". #China Union Pay processed as Discover outside CN
176 } elsif ( $cardtype eq 'Maestro' ) {
178 " ( substring(cust_$thing.payinfo from 1 for 2 ) = '63' ".
179 " OR substring(cust_$thing.payinfo from 1 for 2 ) = '67' ".
180 " OR substring(cust_$thing.payinfo from 1 for 6 ) = '564182' ".
181 " OR substring(cust_$thing.payinfo from 1 for 4 ) = '4936' ".
182 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
183 " SIMILAR TO '49030[2-9]' ".
184 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
185 " SIMILAR TO '49033[5-9]' ".
186 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
187 " SIMILAR TO '49110[1-2]' ".
188 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
189 " SIMILAR TO '49117[4-9]' ".
190 " OR substring(cust_$thing.payinfo from 1 for 6 ) ".
191 " SIMILAR TO '49118[1-2]' ".
194 die "unknown card type $cardtype";
197 my $masksearch = $search;
198 $masksearch =~ s/cust_$thing\.payinfo/cust_$thing.paymask/gi;
201 "( $search OR ( cust_$thing.paymask IS NOT NULL AND $masksearch ) )";
206 if ( $cgi->param('payinfo') ) {
207 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
208 or die "illegal payinfo ". $cgi->param('payinfo');
209 push @search, "cust_$thing.payinfo = '$1'";
212 #for cust_pay_pending... statusNOT=done
213 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
214 push @search, "status != '$1'";
217 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
218 push @search, "_date >= $beginning ",
221 if ( $thing eq 'pay_void' ) {
222 my($v_beginning, $v_ending) =
223 FS::UI::Web::parse_beginning_ending($cgi, 'void');
224 push @search, "void_date >= $v_beginning ",
225 "void_date <= $v_ending";
228 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
232 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
234 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
235 or die "illegal paybatch: ". $cgi->param('paybatch');
237 push @search, "paybatch = '$1'";
239 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
242 die "unknown search magic: ". $cgi->param('magic');
245 #here is the agent virtualization
246 push @search, $curuser->agentnums_sql;
248 my $search = ' WHERE '. join(' AND ', @search);
250 $count_query = "SELECT COUNT(*), SUM($amount_field) ".
251 "FROM cust_$thing LEFT JOIN cust_main USING ( custnum )".
255 'table' => "cust_$thing",
256 'select' => join(', ',
258 'cust_main.custnum as cust_main_custnum',
259 FS::UI::Web::cust_sql_fields(),
262 'extra_sql' => "$search ORDER BY $orderby",
263 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
268 #hmm... is this still used?
270 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
273 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
276 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM cust_$thing".
277 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
278 " AND ". $curuser->agentnums_sql;
281 'table' => "cust_$thing",
282 'hashref' => { 'payinfo' => $payinfo,
284 'extra_sql' => $curuser->agentnums_sql.
291 if ( ( $curuser->access_right('View invoices') #XXX for now
292 || $curuser->access_right('View customer payments')
294 && ! $opt{'disable_link'}
297 my $key = $thing eq 'pay_void' ? 'paynum' : $thing.'num';
298 my $q = ( $thing eq 'pay_void' ? 'void=1;' : '' ). "$key=";
299 $link = [ "${p}view/cust_$thing.html?$q", $key ]
302 my $cust_link = sub {
303 my $cust_thing = shift;
304 $cust_thing->cust_main_custnum
305 ? [ "${p}view/cust_main.cgi?", 'custnum' ]