internationalization/localization, RT12515
[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 <& search.html,
43                 'title'          => $title, # XXX: translate
44                 'name_singular'  => $name_singular,
45                 'query'          => $sql_query,
46                 'count_query'    => $count_query,
47                 'count_addl'     => \@count_addl,
48                 'redirect_empty' => $opt{'redirect_empty'},
49                 'header'         => \@header,
50                 'fields'         => \@fields,
51                 'sort_fields'    => \@sort_fields,
52                 'align'          => $align,
53                 'links'          => \@links,
54                 'color'          => \@color,
55                 'style'          => \@style,
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 $unapplied = $cgi->param('unapplied');
72 my $title = '';
73 $title = 'Unapplied ' if $unapplied;
74 $title .= "\u$name_singular Search Results";
75
76 my $link = '';
77 if (    ( $curuser->access_right('View invoices') #XXX for now
78           || $curuser->access_right('View customer payments')
79         )
80      && ! $opt{'disable_link'}
81    )
82 {
83
84   my $key;
85   my $q = '';
86   if ( $table eq 'cust_pay_void' ) {
87     $key = 'paynum';
88     $q .= 'void=1;';
89   } elsif ( $table eq /^cust_(\w+)$/ ) {
90     $key = $1.'num';
91   }
92   
93   if ( $key ) {
94     $q .= "$key=";
95     $link = [ "${p}view/$table.html?$q", $key ]
96   }
97 }
98
99 my $cust_link = sub {
100   my $cust_thing = shift;
101   $cust_thing->cust_main_custnum
102     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
103     : '';
104 };
105
106 # only valid for $table == 'cust_pay' atm
107 my  $tax_names = '';
108 if ( $cgi->param('tax_names') ) {
109   if ( dbh->{Driver}->{Name} eq 'Pg' ) {
110
111     $tax_names = "
112       array_to_string(
113         array(
114           SELECT itemdesc
115             FROM cust_bill_pay
116             LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
117             LEFT JOIN cust_bill_pkg USING ( billpkgnum )
118               WHERE cust_bill_pkg.pkgnum = 0
119                 AND cust_bill_pay.paynum = cust_pay.paynum
120         ), '|'
121       ) AS tax_names"
122     ;
123
124   } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
125
126     $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
127
128   } else {
129
130     warn "warning: unknown database type ". dbh->{Driver}->{Name}.
131          "omitting tax name information from report.";
132
133   }
134 }
135
136 my @header = ();
137 my @fields = ();
138 my @sort_fields = ();
139 my $align = '';
140 my @links = ();
141 if ( $opt{'pre_header'} ) {
142   push @header, @{ $opt{'pre_header'} };
143   $align .= 'c' x scalar(@{ $opt{'pre_header'} });
144   push @links, map '', @{ $opt{'pre_header'} };
145   push @fields, @{ $opt{'pre_fields'} };
146   push @sort_fields, @{ $opt{'pre_fields'} };
147 }
148
149 push @header, "\u$name_singular",
150               'Amount',
151 ;
152 $align .= 'rr';
153 push @links, '', '';
154 push @fields, 'payby_payinfo_pretty',
155               sub { sprintf('$%.2f', shift->$amount_field() ) },
156 ;
157 push @sort_fields, '', $amount_field;
158
159 if ( $unapplied ) {
160   push @header, emt('Unapplied');
161   $align .= 'r';
162   push @links, '';
163   push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) };
164   push @sort_fields, '';
165 }
166
167 push @header, emt('Date');
168 $align .= 'r';
169 push @links, '';
170 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
171 push @sort_fields, '_date';
172
173 unless ( $opt{'disable_by'} ) {
174   push @header, emt('By');
175   $align .= 'c';
176   push @links, '';
177   push @fields, sub { my $o = shift->otaker;
178                       $o = 'auto billing'          if $o eq 'fs_daily';
179                       $o = 'customer self-service' if $o eq 'fs_selfservice';
180                       $o;
181                     };
182 }
183
184 if ( $tax_names ) {
185   push @header, (emt('Tax names'), emt('Tax province'));
186   $align .= 'cc';
187   push @links, ('','');
188   push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
189                                    split('\|', shift->tax_names)
190                            );
191                     };
192   push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
193                                          else { () }
194                                        }
195                                    split('\|', shift->tax_names)
196                            );
197                     };
198 }
199
200 push @header, FS::UI::Web::cust_header();
201 $align .=  FS::UI::Web::cust_aligns();
202 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
203                  FS::UI::Web::cust_header();
204 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
205 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
206 push @fields, \&FS::UI::Web::cust_fields;
207
208 push @header, @{ $opt{'addl_header'} }
209   if $opt{'addl_header'};
210 push @fields, @{ $opt{'addl_fields'} }
211   if $opt{'addl_fields'};
212
213 my( $count_query, $sql_query, @count_addl );
214 if ( $cgi->param('magic') ) {
215
216   my @search = ();
217   my @select = (
218     "$table.*",
219     FS::UI::Web::cust_sql_fields(),
220     'cust_main.custnum AS cust_main_custnum',
221   );
222   push @select, $tax_names if $tax_names;
223
224   my $orderby;
225   if ( $cgi->param('magic') eq '_date' ) {
226
227     if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
228       push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
229       my $agent = qsearchs('agent', { 'agentnum' => $1 } );
230       die "unknown agentnum $1" unless $agent;
231       $title = $agent->agent. " $title";
232     }
233
234     if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
235       push @search, "custnum = $1";
236     }
237
238     if ( $cgi->param('payby') ) {
239       $cgi->param('payby') =~
240         /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
241           or die "illegal payby ". $cgi->param('payby');
242       push @search, "$table.payby = '$1'";
243       if ( $3 ) {
244
245         my $cardtype = $3;
246
247         my $search;
248         if ( $cardtype eq 'VisaMC' ) {
249           #avoid posix regexes for portability
250           $search =
251             " ( (     substring($table.payinfo from 1 for 1) = '4'     ".
252             "     AND substring($table.payinfo from 1 for 4) != '4936' ".
253             "     AND substring($table.payinfo from 1 for 6)           ".
254             "         NOT SIMILAR TO '49030[2-9]'                        ".
255             "     AND substring($table.payinfo from 1 for 6)           ".
256             "         NOT SIMILAR TO '49033[5-9]'                        ".
257             "     AND substring($table.payinfo from 1 for 6)           ".
258             "         NOT SIMILAR TO '49110[1-2]'                        ".
259             "     AND substring($table.payinfo from 1 for 6)           ".
260             "         NOT SIMILAR TO '49117[4-9]'                        ".
261             "     AND substring($table.payinfo from 1 for 6)           ".
262             "         NOT SIMILAR TO '49118[1-2]'                        ".
263             "   )".
264             "   OR substring($table.payinfo from 1 for 2) = '51' ".
265             "   OR substring($table.payinfo from 1 for 2) = '52' ".
266             "   OR substring($table.payinfo from 1 for 2) = '53' ".
267             "   OR substring($table.payinfo from 1 for 2) = '54' ".
268             "   OR substring($table.payinfo from 1 for 2) = '54' ".
269             "   OR substring($table.payinfo from 1 for 2) = '55' ".
270             "   OR substring($table.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
271             " ) ";
272         } elsif ( $cardtype eq 'Amex' ) {
273           $search =
274             " (    substring($table.payinfo from 1 for 2 ) = '34' ".
275             "   OR substring($table.payinfo from 1 for 2 ) = '37' ".
276             " ) ";
277         } elsif ( $cardtype eq 'Discover' ) {
278           $search =
279             " (    substring($table.payinfo from 1 for 4 ) = '6011'  ".
280             "   OR substring($table.payinfo from 1 for 2 ) = '65'    ".
281             "   OR substring($table.payinfo from 1 for 3 ) = '622'   ". #China Union Pay processed as Discover outside CN
282             " ) ";
283         } elsif ( $cardtype eq 'Maestro' ) { 
284           $search =
285             " (    substring($table.payinfo from 1 for 2 ) = '63'     ".
286             "   OR substring($table.payinfo from 1 for 2 ) = '67'     ".
287             "   OR substring($table.payinfo from 1 for 6 ) = '564182' ".
288             "   OR substring($table.payinfo from 1 for 4 ) = '4936'   ".
289             "   OR substring($table.payinfo from 1 for 6 )            ".
290             "      SIMILAR TO '49030[2-9]'                             ".
291             "   OR substring($table.payinfo from 1 for 6 )            ".
292             "      SIMILAR TO '49033[5-9]'                             ".
293             "   OR substring($table.payinfo from 1 for 6 )            ".
294             "      SIMILAR TO '49110[1-2]'                             ".
295             "   OR substring($table.payinfo from 1 for 6 )            ".
296             "      SIMILAR TO '49117[4-9]'                             ".
297             "   OR substring($table.payinfo from 1 for 6 )            ".
298             "      SIMILAR TO '49118[1-2]'                             ".
299             " ) ";
300         } else {
301           die "unknown card type $cardtype";
302         }
303
304         my $masksearch = $search;
305         $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
306
307         push @search,
308           "( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) )";
309
310       }
311     }
312
313     if ( $cgi->param('payinfo') ) {
314       $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
315         or die "illegal payinfo ". $cgi->param('payinfo');
316       push @search, "$table.payinfo = '$1'";
317     }
318
319     if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
320       push @search, "$table.usernum = $1";
321     }
322
323     #for cust_pay_pending...  statusNOT=done
324     if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
325       push @search, "status != '$1'";
326     }
327
328     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
329     push @search, "_date >= $beginning ",
330                   "_date <= $ending";
331
332     if ( $table eq 'cust_pay_void' ) {
333       my($v_beginning, $v_ending) =
334         FS::UI::Web::parse_beginning_ending($cgi, 'void');
335       push @search, "void_date >= $v_beginning ",
336                     "void_date <= $v_ending";
337     }
338
339     push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
340
341     $orderby = '_date';
342
343   } elsif ( $cgi->param('magic') eq 'paybatch' ) {
344
345     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
346       or die "illegal paybatch: ". $cgi->param('paybatch');
347
348     push @search, "paybatch = '$1'";
349
350     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
351
352   } else {
353     die "unknown search magic: ". $cgi->param('magic');
354   }
355
356   #unapplied payment/refund
357   if ( $unapplied ) {
358     push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
359     push @search, "FS::$table"->unapplied_sql . ' > 0';
360
361   }
362
363   #for the history search
364   if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
365     my @history_action = split(/,/, $1);
366     push @search, 'history_action IN ('.
367                     join(',', map "'$_'", @history_action ). ')';
368   }
369
370   if (    $cgi->param('history_date_beginning')
371        || $cgi->param('history_date_ending')    ) {
372       my($h_beginning, $h_ending) =
373         FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
374       push @search, "history_date >= $h_beginning ",
375                     "history_date <= $h_ending";
376   }
377
378   #here is the agent virtualization
379   push @search, $curuser->agentnums_sql;
380
381   my $addl_from = ' LEFT JOIN cust_main USING ( custnum ) ';
382   my $group_by = '';
383
384   if ( $cgi->param('tax_names') ) {
385     if ( dbh->{Driver}->{Name} eq 'Pg' ) {
386
387       0;#twiddle thumbs
388
389     } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
390
391       $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
392                      LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
393                      LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
394       $group_by  .= "GROUP BY $table.*,cust_main_custnum,".
395                     FS::UI::Web::cust_sql_fields();
396       push @search,
397        "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
398
399     } else {
400
401       warn "warning: unknown database type ". dbh->{Driver}->{Name}.
402            "omitting tax name information from report.";
403
404     }
405   }
406
407   my $search = ' WHERE '. join(' AND ', @search);
408
409   $count_query = "SELECT COUNT(*), SUM($amount_field) ";
410   $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') ' 
411     if $unapplied;
412   $count_query .= "FROM $table $addl_from".
413                   "$search $group_by";
414
415   @count_addl = ( '$%.2f total '.$opt{name_verb} );
416   push @count_addl, '$%.2f unapplied' if $unapplied;
417
418   $sql_query = {
419     'table'     => $table,
420     'select'    => join(', ', @select),
421     'hashref'   => {},
422     'extra_sql' => "$search $group_by",
423     'order_by'  => "ORDER BY $orderby",
424     'addl_from' => $addl_from,
425   };
426
427 } else {
428
429   #hmm... is this still used?
430
431   $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
432   my $payinfo = $1;
433
434   $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
435   my $payby = $1;
436
437   $count_query = "SELECT COUNT(*), SUM($amount_field) FROM $table".
438                  "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
439                  "  AND ". $curuser->agentnums_sql;
440   @count_addl = ( '$%.2f total '.$opt{name_verb} );
441
442   $sql_query = {
443     'table'     => $table,
444     'hashref'   => { 'payinfo' => $payinfo,
445                      'payby'   => $payby    },
446     'extra_sql' => $curuser->agentnums_sql.
447                    " ORDER BY _date",
448   };
449
450 }
451
452 # for consistency
453 $title = join('',map {ucfirst} split(/\b/,$title));
454
455 </%init>