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