da3d125233ba0d1d6910582c9e7e6d9b0c2cdb0e
[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      my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
46      push @search, "_date >= $beginning ",
47                    "_date <= $ending";
48
49      my $search = '';
50      if ( @search ) {
51        $search = ' WHERE '. join(' AND ', @search);
52      }
53
54      $count_query = "SELECT COUNT(*), SUM(paid) ".
55                     "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
56                     $search;
57    
58      $sql_query = {
59        'table'     => 'cust_pay',
60        'select'    => join(', ',
61                         'cust_pay.*',
62                         'cust_main.custnum as cust_main_custnum',
63                         FS::UI::Web::cust_sql_fields(),
64                       ),
65        'hashref'   => {},
66        'extra_sql' => "$search ORDER BY _date",
67        'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
68      };
69    
70    } else {
71    
72      $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
73      my $payinfo = $1;
74    
75      $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
76      my $payby = $1;
77    
78      $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay ".
79                     "WHERE payinfo = '$payinfo' AND payby = '$payby'";
80    
81      $sql_query = {
82        'table'     => 'cust_pay',
83        'hashref'   => { 'payinfo' => $payinfo,
84                         'payby'   => $payby    },
85        'extra_sql' => "ORDER BY _date",
86      };
87    
88    }
89
90    my $link = sub {
91      my $cust_pay = shift;
92      $cust_pay->cust_main_custnum
93        ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
94        : '';
95    };
96
97 %><%= include( 'elements/search.html',
98                  'title'       => $title,
99                  'name'        => 'payments',
100                  'query'       => $sql_query,
101                  'count_query' => $count_query,
102                  'count_addl'  => [ '$%.2f total paid', ],
103                  'header'      => [ 'Payment',
104                                     'Amount',
105                                     'Date',
106                                     FS::UI::Web::cust_header(),
107                                   ],
108                  'fields'      => [
109                    sub {
110                      my $cust_pay = shift;
111                      if ( $cust_pay->payby eq 'CARD' ) {
112                        'Card #'. $cust_pay->payinfo_masked;
113                      } elsif ( $cust_pay->payby eq 'CHEK' ) {
114                        'E-check acct#'. $cust_pay->payinfo;
115                      } elsif ( $cust_pay->payby eq 'BILL' ) {
116                        'Check #'. $cust_pay->payinfo;
117                      } elsif ( $cust_pay->payby eq 'PREP' ) {
118                        'Prepaid card #'. $cust_pay->payinfo;
119                      } else {
120                        $cust_pay->payby. ' '. $cust_pay->payinfo;
121                      }
122                    },
123                    sub { sprintf('$%.2f', shift->paid ) },
124                    sub { time2str('%b %d %Y', shift->_date ) },
125                    \&FS::UI::Web::cust_fields,
126                  ],
127                  #'align' => 'lrrrll',
128                  'align' => 'rrr',
129                  'links' => [
130                    '',
131                    '',
132                    '',
133                    ( map { $link } FS::UI::Web::cust_header() ),
134                  ],
135       )
136 %>