searching for voided payments by void date as well, RT#5786
[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 </%doc>
33 <% include( 'search.html',
34                 'title'          => $title,
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",
41                                       'Amount',
42                                       'Date',
43                                       @header,
44                                       FS::UI::Web::cust_header(),
45                                     ],
46                 'fields'       => [
47                   'payby_payinfo_pretty',
48                   sub { sprintf('$%.2f', shift->$amount_field() ) },
49                   sub { time2str('%b %d %Y', shift->_date ) },
50                   @fields,
51                   \&FS::UI::Web::cust_fields,
52                 ],
53                 #'align' => 'lrrrll',
54                 'align' => 'rrr'.
55                            join('', map 'c', @fields ).
56                            FS::UI::Web::cust_aligns(),
57                 'links' => [
58                   $link,
59                   $link,
60                   $link,
61                   ( map '', @fields ),
62                   ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
63                         FS::UI::Web::cust_header()
64                   ),
65                 ],
66                 'color' => [ 
67                              '',
68                              '',
69                              '',
70                              ( map '', @fields ),
71                              FS::UI::Web::cust_colors(),
72                            ],
73                 'style' => [ 
74                              '',
75                              '',
76                              '',
77                              ( map '', @fields ),
78                              FS::UI::Web::cust_styles(),
79                            ],
80           )
81 %>
82 <%init>
83
84 my %opt = @_;
85
86 my $curuser = $FS::CurrentUser::CurrentUser;
87
88 die "access denied"
89   unless $curuser->access_right('Financial reports');
90
91 my $thing = $opt{'thing'};
92 my $amount_field = $opt{'amount_field'};
93 my $name_singular = $opt{'name_singular'};
94
95 my $title = "\u$name_singular Search Results";
96
97 my @header = ();
98 my @fields = ();
99 unless ( $opt{'disable_by'} ) {
100   push @header, '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';
104                       $o;
105                     };
106 }
107
108 push @header, @{ $opt{'addl_header'} }
109   if $opt{'addl_header'};
110 push @fields, @{ $opt{'addl_fields'} }
111   if $opt{'addl_fields'};
112
113 my( $count_query, $sql_query );
114 if ( $cgi->param('magic') ) {
115
116   my @search = ();
117   my $orderby;
118   if ( $cgi->param('magic') eq '_date' ) {
119
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";
125     }
126
127     if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
128       push @search, "custnum = $1";
129     }
130
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'";
136       if ( $3 ) {
137
138         my $cardtype = $3;
139
140         my $search;
141         if ( $cardtype eq 'VisaMC' ) {
142           #avoid posix regexes for portability
143           $search =
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]'                        ".
156             "   )".
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
164             " ) ";
165         } elsif ( $cardtype eq 'Amex' ) {
166           $search =
167             " (    substring(cust_$thing.payinfo from 1 for 2 ) = '34' ".
168             "   OR substring(cust_$thing.payinfo from 1 for 2 ) = '37' ".
169             " ) ";
170         } elsif ( $cardtype eq 'Discover' ) {
171           $search =
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
175             " ) ";
176         } elsif ( $cardtype eq 'Maestro' ) { 
177           $search =
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]'                             ".
192             " ) ";
193         } else {
194           die "unknown card type $cardtype";
195         }
196
197         my $masksearch = $search;
198         $masksearch =~ s/cust_$thing\.payinfo/cust_$thing.paymask/gi;
199
200         push @search,
201           "( $search OR ( cust_$thing.paymask IS NOT NULL AND $masksearch ) )";
202
203       }
204     }
205
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'";
210     }
211
212     #for cust_pay_pending...  statusNOT=done
213     if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
214       push @search, "status != '$1'";
215     }
216
217     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
218     push @search, "_date >= $beginning ",
219                   "_date <= $ending";
220
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";
226     }
227
228     push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
229
230     $orderby = '_date';
231
232   } elsif ( $cgi->param('magic') eq 'paybatch' ) {
233
234     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
235       or die "illegal paybatch: ". $cgi->param('paybatch');
236
237     push @search, "paybatch = '$1'";
238
239     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
240
241   } else {
242     die "unknown search magic: ". $cgi->param('magic');
243   }
244
245   #here is the agent virtualization
246   push @search, $curuser->agentnums_sql;
247
248   my $search = ' WHERE '. join(' AND ', @search);
249
250   $count_query = "SELECT COUNT(*), SUM($amount_field) ".
251                  "FROM cust_$thing LEFT JOIN cust_main USING ( custnum )".
252                  $search;
253
254   $sql_query = {
255     'table'     => "cust_$thing",
256     'select'    => join(', ',
257                      "cust_$thing.*",
258                      'cust_main.custnum as cust_main_custnum',
259                      FS::UI::Web::cust_sql_fields(),
260                    ),
261     'hashref'   => {},
262     'extra_sql' => "$search ORDER BY $orderby",
263     'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
264   };
265
266 } else {
267
268   #hmm... is this still used?
269
270   $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
271   my $payinfo = $1;
272
273   $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
274   my $payby = $1;
275
276   $count_query = "SELECT COUNT(*), SUM($amount_field) FROM cust_$thing".
277                  "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
278                  "  AND ". $curuser->agentnums_sql;
279
280   $sql_query = {
281     'table'     => "cust_$thing",
282     'hashref'   => { 'payinfo' => $payinfo,
283                      'payby'   => $payby    },
284     'extra_sql' => $curuser->agentnums_sql.
285                    " ORDER BY _date",
286   };
287
288 }
289
290 my $link = '';
291 if (    ( $curuser->access_right('View invoices') #XXX for now
292           || $curuser->access_right('View customer payments')
293         )
294      && ! $opt{'disable_link'}
295    )
296 {
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 ]
300 }
301
302 my $cust_link = sub {
303   my $cust_thing = shift;
304   $cust_thing->cust_main_custnum
305     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
306     : '';
307 };
308
309 </%init>