updated quick payment entry
[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') =~ /^(CARD|CHEK|BILL)(-(VisaMC|Amex|Discover))?$/
20            or die "illegal payby ". $cgi->param('payby');
21          push @search, "cust_pay.payby = '$1'";
22          if ( $3 ) {
23            if ( $3 eq 'VisaMC' ) {
24              #avoid posix regexes for portability
25              push @search,
26                " (    substring(cust_pay.payinfo from 1 for 1) = '4'  ".
27                "   OR substring(cust_pay.payinfo from 1 for 2) = '51' ".
28                "   OR substring(cust_pay.payinfo from 1 for 2) = '52' ".
29                "   OR substring(cust_pay.payinfo from 1 for 2) = '53' ".
30                "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
31                "   OR substring(cust_pay.payinfo from 1 for 2) = '54' ".
32                "   OR substring(cust_pay.payinfo from 1 for 2) = '55' ".
33                " ) ";
34            } elsif ( $3 eq 'Amex' ) {
35              push @search,
36                " (    substring(cust_pay.payinfo from 1 for 2 ) = '34' ".
37                "   OR substring(cust_pay.payinfo from 1 for 2 ) = '37' ".
38                " ) ";
39            } elsif ( $3 eq 'Discover' ) {
40              push @search,
41                " substring(cust_pay.payinfo from 1 for 4 ) = '6011' ";
42            } else {
43              die "unknown card type $3";
44            }
45          }
46        }
47   
48        my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
49        push @search, "_date >= $beginning ",
50                      "_date <= $ending";
51   
52        $orderby = '_date';
53    
54      } elsif ( $cgi->param('magic') eq 'paybatch' ) {
55
56        $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
57          or die "illegal paybatch: ". $cgi->param('paybatch');
58
59        push @search, "paybatch = '$1'";
60
61        $orderby = "LOWER(company || ' ' || last || ' ' || first )";
62
63      } else {
64        die "unknown search magic: ". $cgi->param('magic');
65      }
66
67      my $search = '';
68      if ( @search ) {
69        $search = ' WHERE '. join(' AND ', @search);
70      }
71   
72      $count_query = "SELECT COUNT(*), SUM(paid) ".
73                     "FROM cust_pay LEFT JOIN cust_main USING ( custnum )".
74                     $search;
75
76      $sql_query = {
77        'table'     => 'cust_pay',
78        'select'    => join(', ',
79                         'cust_pay.*',
80                         'cust_main.custnum as cust_main_custnum',
81                         FS::UI::Web::cust_sql_fields(),
82                       ),
83        'hashref'   => {},
84        'extra_sql' => "$search ORDER BY $orderby",
85        'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
86      };
87
88    } else {
89    
90      $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
91      my $payinfo = $1;
92    
93      $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
94      my $payby = $1;
95    
96      $count_query = "SELECT COUNT(*), SUM(paid) FROM cust_pay ".
97                     "WHERE payinfo = '$payinfo' AND payby = '$payby'";
98    
99      $sql_query = {
100        'table'     => 'cust_pay',
101        'hashref'   => { 'payinfo' => $payinfo,
102                         'payby'   => $payby    },
103        'extra_sql' => "ORDER BY _date",
104      };
105    
106    }
107
108    my $link = sub {
109      my $cust_pay = shift;
110      $cust_pay->cust_main_custnum
111        ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
112        : '';
113    };
114
115 %><%= include( 'elements/search.html',
116                  'title'       => $title,
117                  'name'        => 'payments',
118                  'query'       => $sql_query,
119                  'count_query' => $count_query,
120                  'count_addl'  => [ '$%.2f total paid', ],
121                  'header'      => [ 'Payment',
122                                     'Amount',
123                                     'Date',
124                                     FS::UI::Web::cust_header(),
125                                   ],
126                  'fields'      => [
127                    sub {
128                      my $cust_pay = shift;
129                      if ( $cust_pay->payby eq 'CARD' ) {
130                        'Card #'. $cust_pay->payinfo_masked;
131                      } elsif ( $cust_pay->payby eq 'CHEK' ) {
132                        'E-check acct#'. $cust_pay->payinfo;
133                      } elsif ( $cust_pay->payby eq 'BILL' ) {
134                        'Check #'. $cust_pay->payinfo;
135                      } elsif ( $cust_pay->payby eq 'PREP' ) {
136                        'Prepaid card #'. $cust_pay->payinfo;
137                      } else {
138                        $cust_pay->payby. ' '. $cust_pay->payinfo;
139                      }
140                    },
141                    sub { sprintf('$%.2f', shift->paid ) },
142                    sub { time2str('%b %d %Y', shift->_date ) },
143                    \&FS::UI::Web::cust_fields,
144                  ],
145                  #'align' => 'lrrrll',
146                  'align' => 'rrr',
147                  'links' => [
148                    '',
149                    '',
150                    '',
151                    ( map { $link } FS::UI::Web::cust_header() ),
152                  ],
153       )
154 %>