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