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