add otaker to payment/refund search, RT#6407
[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     if ( $cgi->param('otaker') =~ /^(\w+)$/ ) {
213       push @search, "cust_$thing.otaker = '$1'";
214     }
215
216     #for cust_pay_pending...  statusNOT=done
217     if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
218       push @search, "status != '$1'";
219     }
220
221     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
222     push @search, "_date >= $beginning ",
223                   "_date <= $ending";
224
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";
230     }
231
232     push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field );
233
234     $orderby = '_date';
235
236   } elsif ( $cgi->param('magic') eq 'paybatch' ) {
237
238     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
239       or die "illegal paybatch: ". $cgi->param('paybatch');
240
241     push @search, "paybatch = '$1'";
242
243     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
244
245   } else {
246     die "unknown search magic: ". $cgi->param('magic');
247   }
248
249   #here is the agent virtualization
250   push @search, $curuser->agentnums_sql;
251
252   my $search = ' WHERE '. join(' AND ', @search);
253
254   $count_query = "SELECT COUNT(*), SUM($amount_field) ".
255                  "FROM cust_$thing LEFT JOIN cust_main USING ( custnum )".
256                  $search;
257
258   $sql_query = {
259     'table'     => "cust_$thing",
260     'select'    => join(', ',
261                      "cust_$thing.*",
262                      'cust_main.custnum as cust_main_custnum',
263                      FS::UI::Web::cust_sql_fields(),
264                    ),
265     'hashref'   => {},
266     'extra_sql' => "$search ORDER BY $orderby",
267     'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
268   };
269
270 } else {
271
272   #hmm... is this still used?
273
274   $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
275   my $payinfo = $1;
276
277   $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
278   my $payby = $1;
279
280   $count_query = "SELECT COUNT(*), SUM($amount_field) FROM cust_$thing".
281                  "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
282                  "  AND ". $curuser->agentnums_sql;
283
284   $sql_query = {
285     'table'     => "cust_$thing",
286     'hashref'   => { 'payinfo' => $payinfo,
287                      'payby'   => $payby    },
288     'extra_sql' => $curuser->agentnums_sql.
289                    " ORDER BY _date",
290   };
291
292 }
293
294 my $link = '';
295 if (    ( $curuser->access_right('View invoices') #XXX for now
296           || $curuser->access_right('View customer payments')
297         )
298      && ! $opt{'disable_link'}
299    )
300 {
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 ]
304 }
305
306 my $cust_link = sub {
307   my $cust_thing = shift;
308   $cust_thing->cust_main_custnum
309     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
310     : '';
311 };
312
313 </%init>