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