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