add cust_pay.otaker field; populate it based on history on upgrades, and show the...
[freeside.git] / httemplate / search / cust_pay.cgi
1 <% include( 'elements/search.html',
2                  'title'       => $title,
3                  'name'        => 'payments',
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'count_addl'  => [ '$%.2f total paid', ],
7                  'header'      => [ 'Payment',
8                                     'Amount',
9                                     'Date',
10                                     'By',
11                                     FS::UI::Web::cust_header(),
12                                   ],
13                  'fields'      => [
14                    sub {
15                      my $cust_pay = shift;
16                      if ( $cust_pay->payby eq 'CARD' ) {
17                        'Card #'. $cust_pay->paymask;
18                      } elsif ( $cust_pay->payby eq 'CHEK' ) {
19                        'E-check acct#'. $cust_pay->payinfo;
20                      } elsif ( $cust_pay->payby eq 'BILL' ) {
21                        'Check #'. $cust_pay->payinfo;
22                      } elsif ( $cust_pay->payby eq 'PREP' ) {
23                        'Prepaid card #'. $cust_pay->payinfo;
24                      } elsif ( $cust_pay->payby eq 'CASH' ) {
25                        'Cash '. $cust_pay->payinfo;
26                      } elsif ( $cust_pay->payby eq 'WEST' ) {
27                        'Western Union'; #. $cust_pay->payinfo;
28                      } elsif ( $cust_pay->payby eq 'MCRD' ) {
29                        'Manual credit card'; #. $cust_pay->payinfo;
30                      } else {
31                        $cust_pay->payby. ' '. $cust_pay->payinfo;
32                      }
33                    },
34                    sub { sprintf('$%.2f', shift->paid ) },
35                    sub { time2str('%b %d %Y', shift->_date ) },
36                    sub { my $o = shift->otaker;
37                          $o = 'auto billing'          if $o eq 'fs_daily';
38                          $o = 'customer self-service' if $o eq 'fs_selfservice';
39                          $o;
40                        },
41                    \&FS::UI::Web::cust_fields,
42                  ],
43                  #'align' => 'lrrrll',
44                  'align' => 'rrrc'.FS::UI::Web::cust_aligns(),
45                  'links' => [
46                    $link,
47                    $link,
48                    $link,
49                    '',
50                    ( map { $_ ne 'Cust. Status' ? $cust_link : '' }
51                          FS::UI::Web::cust_header()
52                    ),
53                  ],
54                  'color' => [ 
55                               '',
56                               '',
57                               '',
58                               '',
59                               FS::UI::Web::cust_colors(),
60                             ],
61                  'style' => [ 
62                               '',
63                               '',
64                               '',
65                               '',
66                               FS::UI::Web::cust_styles(),
67                             ],
68       )
69 %>
70 <%init>
71
72 die "access denied"
73   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
74
75 my $title = 'Payment Search Results';
76 my( $count_query, $sql_query );
77 if ( $cgi->param('magic') ) {
78
79   my @search = ();
80   my $orderby;
81   if ( $cgi->param('magic') eq '_date' ) {
82
83
84     if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
85       push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
86       my $agent = qsearchs('agent', { 'agentnum' => $1 } );
87       die "unknown agentnum $1" unless $agent;
88       $title = $agent->agent. " $title";
89     }
90   
91     if ( $cgi->param('payby') ) {
92       $cgi->param('payby') =~
93         /^(CARD|CHEK|BILL|PREP|CASH|WEST|MCRD)(-(VisaMC|Amex|Discover|Maestro))?$/
94           or die "illegal payby ". $cgi->param('payby');
95       push @search, "cust_pay.payby = '$1'";
96       if ( $3 ) {
97
98         my $cardtype = $3;
99
100         my $search;
101         if ( $cardtype eq 'VisaMC' ) {
102           #avoid posix regexes for portability
103           $search =
104             " ( (     substring(cust_pay.payinfo from 1 for 1) = '4'     ".
105             "     AND substring(cust_pay.payinfo from 1 for 4) != '4936' ".
106             "     AND substring(cust_pay.payinfo from 1 for 6)           ".
107             "         NOT SIMILAR TO '49030[2-9]'                        ".
108             "     AND substring(cust_pay.payinfo from 1 for 6)           ".
109             "         NOT SIMILAR TO '49033[5-9]'                        ".
110             "     AND substring(cust_pay.payinfo from 1 for 6)           ".
111             "         NOT SIMILAR TO '49110[1-2]'                        ".
112             "     AND substring(cust_pay.payinfo from 1 for 6)           ".
113             "         NOT SIMILAR TO '49117[4-9]'                        ".
114             "     AND substring(cust_pay.payinfo from 1 for 6)           ".
115             "         NOT SIMILAR TO '49118[1-2]'                        ".
116             "   )".
117             "   OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
118             "   OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
119             "   OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
120             "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
121             "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
122             "   OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
123             "   OR substring(cust_pay.payinfo from 1 for 2) = '36' ". #Diner's int'l processed as Visa/MC inside US
124             " ) ";
125         } elsif ( $cardtype eq 'Amex' ) {
126           $search =
127             " (    substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
128             "   OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
129             " ) ";
130         } elsif ( $cardtype eq 'Discover' ) {
131           $search =
132             " (    substring(cust_pay.payinfo from 1 for 4 ) = '6011'  ".
133             "   OR substring(cust_pay.payinfo from 1 for 2 ) = '65'    ".
134             "   OR substring(cust_pay.payinfo from 1 for 3 ) = '622'   ". #China Union Pay processed as Discover outside CN
135             " ) ";
136         } elsif ( $cardtype eq 'Maestro' ) { 
137           $search =
138             " (    substring(cust_pay.payinfo from 1 for 2 ) = '63'     ".
139             "   OR substring(cust_pay.payinfo from 1 for 2 ) = '67'     ".
140             "   OR substring(cust_pay.payinfo from 1 for 6 ) = '564182' ".
141             "   OR substring(cust_pay.payinfo from 1 for 4 ) = '4936'   ".
142             "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
143             "      SIMILAR TO '49030[2-9]'                             ".
144             "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
145             "      SIMILAR TO '49033[5-9]'                             ".
146             "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
147             "      SIMILAR TO '49110[1-2]'                             ".
148             "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
149             "      SIMILAR TO '49117[4-9]'                             ".
150             "   OR substring(cust_pay.payinfo from 1 for 6 )            ".
151             "      SIMILAR TO '49118[1-2]'                             ".
152             " ) ";
153         } else {
154           die "unknown card type $cardtype";
155         }
156
157         my $masksearch = $search;
158         $masksearch =~ s/cust_pay\.payinfo/cust_pay.paymask/gi;
159
160         push @search,
161           "( $search OR ( cust_pay.paymask IS NOT NULL AND $masksearch ) )";
162
163       }
164     }
165
166     if ( $cgi->param('payinfo') ) {
167       $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
168         or die "illegal payinfo ". $cgi->param('payinfo');
169       push @search, "cust_pay.payinfo = '$1'";
170     }
171
172     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
173     push @search, "_date >= $beginning ",
174                   "_date <= $ending";
175
176     push @search, FS::UI::Web::parse_lt_gt($cgi, 'paid' );
177
178     $orderby = '_date';
179
180   } elsif ( $cgi->param('magic') eq 'paybatch' ) {
181
182     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
183       or die "illegal paybatch: ". $cgi->param('paybatch');
184
185     push @search, "paybatch = '$1'";
186
187     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
188
189   } else {
190     die "unknown search magic: ". $cgi->param('magic');
191   }
192
193   #here is the agent virtualization
194   push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
195
196   my $search = ' WHERE '. join(' AND ', @search);
197
198   $count_query = "SELECT COUNT(*), SUM(paid) ".
199                  "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
200                  $search;
201
202   $sql_query = {
203     'table'     => 'cust_pay',
204     'select'    => join(', ',
205                      'cust_pay.*',
206                      'cust_main.custnum as cust_main_custnum',
207                      FS::UI::Web::cust_sql_fields(),
208                    ),
209     'hashref'   => {},
210     'extra_sql' => "$search ORDER BY $orderby",
211     'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
212   };
213
214 } else {
215
216   #hmm... is this still used?
217
218   $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
219   my $payinfo = $1;
220
221   $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
222   my $payby = $1;
223
224   $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay".
225                  "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
226                  "  AND ". $FS::CurrentUser::CurrentUser->agentnums_sql;
227
228   $sql_query = {
229     'table'     => 'cust_pay',
230     'hashref'   => { 'payinfo' => $payinfo,
231                      'payby'   => $payby    },
232     'extra_sql' => $FS::CurrentUser::CurrentUser->agentnums_sql.
233                    " ORDER BY _date",
234   };
235
236 }
237
238 my $link = [ "${p}view/cust_pay.html?paynum=", 'paynum' ];
239
240 my $cust_link = sub {
241   my $cust_pay = shift;
242   $cust_pay->cust_main_custnum
243     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
244     : '';
245 };
246
247 </%init>