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 if ( $cgi->param('otaker') =~ /^(\w+)$/ ) {
213 push @search, "cust_$thing.otaker = '$1'";
216 #for cust_pay_pending... statusNOT=done
217 if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
218 push @search, "status != '$1'";
221 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
222 push @search, "_date >= $beginning ",
225 if ( $thing eq 'pay_void' ) {
226 my($v_beginning, $v_ending) =
227 FS::UI::Web::parse_beginning_ending($cgi, 'void');
228 push @search, "void_date >= $v_beginning ",
229 "void_date <= $v_ending";
232 push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
236 } elsif ( $cgi->param('magic') eq 'paybatch' ) {
238 $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
239 or die "illegal paybatch: ". $cgi->param('paybatch');
241 push @search, "paybatch = '$1'";
243 $orderby = "LOWER(company || ' ' || last || ' ' || first )";
246 die "unknown search magic: ". $cgi->param('magic');
249 #here is the agent virtualization
250 push @search, $curuser->agentnums_sql;
252 my $search = ' WHERE '. join(' AND ', @search);
254 $count_query = "SELECT COUNT(*), SUM($amount_field) ".
255 "FROM cust_$thing LEFT JOIN cust_main USING ( custnum )".
259 'table' => "cust_$thing",
260 'select' => join(', ',
262 'cust_main.custnum as cust_main_custnum',
263 FS::UI::Web::cust_sql_fields(),
266 'extra_sql' => "$search ORDER BY $orderby",
267 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
272 #hmm... is this still used?
274 $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
277 $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
280 $count_query = "SELECT COUNT(*), SUM($amount_field) FROM cust_$thing".
281 " WHERE payinfo = '$payinfo' AND payby = '$payby'".
282 " AND ". $curuser->agentnums_sql;
285 'table' => "cust_$thing",
286 'hashref' => { 'payinfo' => $payinfo,
288 'extra_sql' => $curuser->agentnums_sql.
295 if ( ( $curuser->access_right('View invoices') #XXX for now
296 || $curuser->access_right('View customer payments')
298 && ! $opt{'disable_link'}
301 my $key = $thing eq 'pay_void' ? 'paynum' : $thing.'num';
302 my $q = ( $thing eq 'pay_void' ? 'void=1;' : '' ). "$key=";
303 $link = [ "${p}view/cust_$thing.html?$q", $key ]
306 my $cust_link = sub {
307 my $cust_thing = shift;
308 $cust_thing->cust_main_custnum
309 ? [ "${p}view/cust_main.cgi?", 'custnum' ]