so Search.tsf and Search.rdf work
[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') && $cgi->param('magic') eq '_date' ) {
5    
6      my @search = ();
7
8      if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
9        push @search, "agentnum = $1"; # $search{'agentnum'} = $1;
10        my $agent = qsearchs('agent', { 'agentnum' => $1 } );
11        die "unknown agentnum $1" unless $agent;
12        $title = $agent->agent. " $title";
13      }
14    
15      if ( $cgi->param('payby') ) {
16        $cgi->param('payby') =~ /^(CARD|CHEK|BILL)(-(VisaMC|Amex|Discover))?$/
17          or die "illegal payby ". $cgi->param('payby');
18        push @search, "cust_pay.payby = '$1'";
19        if ( $3 ) {
20          if ( $3 eq 'VisaMC' ) {
21            #avoid posix regexes for portability
22            push @search,
23              " (    substring(cust_pay.payinfo from 1 for 1) = '4'  ".
24              "   OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
25              "   OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
26              "   OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
27              "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
28              "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
29              "   OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
30              " ) ";
31          } elsif ( $3 eq 'Amex' ) {
32            push @search,
33              " (    substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
34              "   OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
35              " ) ";
36          } elsif ( $3 eq 'Discover' ) {
37            push @search,
38              " substring(cust_pay.payinfo from 1 for 4 ) = '6011' ";
39          } else {
40            die "unknown card type $3";
41          }
42        }
43      }
44    
45      #false laziness with cust_pkg.cgi
46      if ( $cgi->param('beginning')
47           && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
48        my $beginning = str2time($1);
49        push @search, "_date >= $beginning ";
50      }
51      if ( $cgi->param('ending')
52                && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
53        my $ending = str2time($1) + 86399;
54        push @search, " _date <= $ending ";
55      }
56      if ( $cgi->param('begin')
57           && $cgi->param('begin') =~ /^(\d+)$/ ) {
58        push @search, "_date >= $1 ";
59      }
60      if ( $cgi->param('end')
61                && $cgi->param('end') =~ /^(\d+)$/ ) {
62        push @search, " _date < $1 ";
63      }
64    
65      my $search = '';
66      if ( @search ) {
67        $search = ' WHERE '. join(' AND ', @search);
68      }
69
70      $count_query = "SELECT COUNT(*), SUM(paid) ".
71                     "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
72                     $search;
73    
74      $sql_query = {
75        'table'     => 'cust_pay',
76        'select'    => 'cust_pay.*, cust_main.last, cust_main.first, cust_main.company',
77        'hashref'   => {},
78        'extra_sql' => "$search ORDER BY _date",
79        'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
80      };
81    
82    } else {
83    
84      $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
85      my $payinfo = $1;
86    
87      $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
88      my $payby = $1;
89    
90      $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay ".
91                     "WHERE payinfo = '$payinfo' AND payby = '$payby'";
92    
93      $sql_query = {
94        'table'     => 'cust_pay',
95        'hashref'   => { 'payinfo' => $payinfo,
96                         'payby'   => $payby    },
97        'extra_sql' => "ORDER BY _date",
98      };
99    
100    }
101
102    my $link = [ "${p}view/cust_main.cgi?", 'custnum' ];
103
104 %><%= include( 'elements/search.html',
105                  'title'       => $title,
106                  'name'        => 'payments',
107                  'query'       => $sql_query,
108                  'count_query' => $count_query,
109                  'count_addl'  => [ '$%.2f total paid', ],
110                  'header'      =>
111                    [ qw(Payment Amount Date), 'Cust #', 'Contact name',
112                      'Company', ],
113                  'fields'      => [
114                    sub {
115                      my $cust_pay = shift;
116                      if ( $cust_pay->payby eq 'CARD' ) {
117                        'Card #'. $cust_pay->payinfo_masked;
118                      } elsif ( $cust_pay->payby eq 'CHEK' ) {
119                        'E-check acct#'. $cust_pay->payinfo;
120                      } elsif ( $cust_pay->payby eq 'BILL' ) {
121                        'Check #'. $cust_pay->payinfo;
122                      } else {
123                        $cust_pay->payby. ' '. $cust_pay->payinfo;
124                      }
125                    },
126                    sub { sprintf('$%.2f', shift->paid ) },
127                    sub { time2str('%b %d %Y', shift->_date ) },
128                    'custnum',
129                    sub { $_[0]->get('last'). ', '. $_[0]->first; },
130                    'company',
131                  ],
132                  'align' => 'lrrrll',
133                  'links' => [
134                    '',
135                    '',
136                    '',
137                    $link,
138                    $link,
139                    $link,
140                  ],
141       )
142 %>