fix payment and refund searches by otaker (now usernum), RT#9555
[freeside.git] / httemplate / search / elements / cust_pay_or_refund.html
1 <%doc>
2
3 Examples:
4
5   include( 'elements/cust_pay_or_refund.html',
6                'thing'          => 'pay',
7                'amount_field'   => 'paid',
8                'name_singular'  => 'payment',
9                'name_verb'      => 'paid',
10          )
11
12   include( 'elements/cust_pay_or_refund.html',
13                'thing'          => 'refund',
14                'amount_field'   => 'refund',
15                'name_singular'  => 'refund',
16                'name_verb'      => 'refunded',
17          )
18
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',
24                'disable_link'   => 1,
25                'disable_by'     => 1,
26                'html_init'      => '',
27                'addl_header'    => [],
28                'addl_fields'    => [],
29                'redirect_empty' => $redirect_empty,
30           )
31
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' ],
39          )
40
41 </%doc>
42 <% include( 'search.html',
43                 'title'          => $title,
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'},
49                 'header'         => \@header,
50                 'fields'         => \@fields,
51                 'align'          => $align,
52                 'links'          => \@links,
53                 'color'          => \@color,
54                 'style'          => \@style,
55           )
56 %>
57 <%init>
58
59 my %opt = @_;
60
61 my $curuser = $FS::CurrentUser::CurrentUser;
62
63 die "access denied"
64   unless $curuser->access_right('Financial reports');
65
66 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
67
68 my $amount_field = $opt{'amount_field'};
69 my $name_singular = $opt{'name_singular'};
70
71 my $title = "\u$name_singular Search Results";
72
73 my $link = '';
74 if (    ( $curuser->access_right('View invoices') #XXX for now
75           || $curuser->access_right('View customer payments')
76         )
77      && ! $opt{'disable_link'}
78    )
79 {
80
81   my $key;
82   my $q = '';
83   if ( $table eq 'cust_pay_void' ) {
84     $key = 'paynum';
85     $q .= 'void=1;';
86   } elsif ( $table eq /^cust_(\w+)$/ ) {
87     $key = $1.'num';
88   }
89   
90   if ( $key ) {
91     $q .= "$key=";
92     $link = [ "${p}view/$table.html?$q", $key ]
93   }
94 }
95
96 my $cust_link = sub {
97   my $cust_thing = shift;
98   $cust_thing->cust_main_custnum
99     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
100     : '';
101 };
102
103 my @header = ();
104 my @fields = ();
105 my $align = '';
106 my @links = ();
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'} };
112 }
113
114 push @header, "\u$name_singular",
115               'Amount',
116               'Date',
117 ;
118 $align .= 'rrr';
119 push @links, '', '', '';
120 push @fields, 'payby_payinfo_pretty',
121               sub { sprintf('$%.2f', shift->$amount_field() ) },
122               sub { time2str('%b %d %Y', shift->_date ) },
123 ;
124
125 unless ( $opt{'disable_by'} ) {
126   push @header, 'By';
127   $align .= 'c';
128   push @links, '';
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';
132                       $o;
133                     };
134 }
135
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;
143
144 push @header, @{ $opt{'addl_header'} }
145   if $opt{'addl_header'};
146 push @fields, @{ $opt{'addl_fields'} }
147   if $opt{'addl_fields'};
148
149 my( $count_query, $sql_query );
150 if ( $cgi->param('magic') ) {
151
152   my @search = ();
153   my $orderby;
154   if ( $cgi->param('magic') eq '_date' ) {
155
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";
161     }
162
163     if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
164       push @search, "custnum = $1";
165     }
166
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'";
172       if ( $3 ) {
173
174         my $cardtype = $3;
175
176         my $search;
177         if ( $cardtype eq 'VisaMC' ) {
178           #avoid posix regexes for portability
179           $search =
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]'                        ".
192             "   )".
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
200             " ) ";
201         } elsif ( $cardtype eq 'Amex' ) {
202           $search =
203             " (    substring($table.payinfo from 1 for 2 ) = '34' ".
204             "   OR substring($table.payinfo from 1 for 2 ) = '37' ".
205             " ) ";
206         } elsif ( $cardtype eq 'Discover' ) {
207           $search =
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
211             " ) ";
212         } elsif ( $cardtype eq 'Maestro' ) { 
213           $search =
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]'                             ".
228             " ) ";
229         } else {
230           die "unknown card type $cardtype";
231         }
232
233         my $masksearch = $search;
234         $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
235
236         push @search,
237           "( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) )";
238
239       }
240     }
241
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'";
246     }
247
248     if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
249       push @search, "$table.usernum = $1";
250     }
251
252     #for cust_pay_pending...  statusNOT=done
253     if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
254       push @search, "status != '$1'";
255     }
256
257     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
258     push @search, "_date >= $beginning ",
259                   "_date <= $ending";
260
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";
266     }
267
268     push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
269
270     $orderby = '_date';
271
272   } elsif ( $cgi->param('magic') eq 'paybatch' ) {
273
274     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
275       or die "illegal paybatch: ". $cgi->param('paybatch');
276
277     push @search, "paybatch = '$1'";
278
279     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
280
281   } else {
282     die "unknown search magic: ". $cgi->param('magic');
283   }
284
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 ). ')';
290   }
291
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";
298   }
299
300   #here is the agent virtualization
301   push @search, $curuser->agentnums_sql;
302
303   my $search = ' WHERE '. join(' AND ', @search);
304
305   $count_query = "SELECT COUNT(*), SUM($amount_field) ".
306                  "FROM $table LEFT JOIN cust_main USING ( custnum )".
307                  $search;
308
309   $sql_query = {
310     'table'     => $table,
311     'select'    => join(', ',
312                      "$table.*",
313                      'cust_main.custnum as cust_main_custnum',
314                      FS::UI::Web::cust_sql_fields(),
315                    ),
316     'hashref'   => {},
317     'extra_sql' => "$search ORDER BY $orderby",
318     'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
319   };
320
321 } else {
322
323   #hmm... is this still used?
324
325   $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
326   my $payinfo = $1;
327
328   $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
329   my $payby = $1;
330
331   $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
332                  "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
333                  "  AND ". $curuser->agentnums_sql;
334
335   $sql_query = {
336     'table'     => $table,
337     'hashref'   => { 'payinfo' => $payinfo,
338                      'payby'   => $payby    },
339     'extra_sql' => $curuser->agentnums_sql.
340                    " ORDER BY _date",
341   };
342
343 }
344
345 </%init>