fix manual credit card payment with term discounts, #15068
[freeside.git] / httemplate / search / cust_pay_batch.cgi
1 <% include('elements/search.html',
2               'title'       => 'Batch payment details',
3               'name'        => 'batch details',
4               'query'       => $sql_query,
5               'count_query' => $count_query,
6               'html_init'   => $pay_batch ? $html_init : '',
7               'disable_download' => 1,
8               'header'      => [ '#',
9                                  'Inv #',
10                                  'Customer',
11                                  'Customer',
12                                  'Card Name',
13                                  'Card',
14                                  'Exp',
15                                  'Amount',
16                                  'Status',
17                                ],
18               'fields'      => [ sub {
19                                    shift->[0];
20                                  },
21                                  sub {
22                                    shift->[1];
23                                  },
24                                  sub {
25                                    shift->[2];
26                                  },
27                                  sub {
28                                    my $cpb = shift;
29                                    $cpb->[3] . ', ' . $cpb->[4];
30                                  },
31                                  sub {
32                                    shift->[5];
33                                  },
34                                  sub {
35                                    my $cardnum = shift->[6];
36                                    'x'x(length($cardnum)-4). substr($cardnum,(length($cardnum)-4));
37                                  },
38                                  sub {
39                                    shift->[7] =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
40                                    my( $mon, $year ) = ( $2, $1 );
41                                    $mon = "0$mon" if length($mon) == 1;
42                                    "$mon/$year";
43                                  },
44                                  sub {
45                                    shift->[8];
46                                  },
47                                  sub {
48                                    shift->[9];
49                                  },
50                                ],
51               'align'       => 'lllllllrl',
52               'links'       => [ ['', sub{'#';}],
53                                  ["${p}view/cust_bill.cgi?", sub{shift->[1];},],
54                                  ["${p}view/cust_main.cgi?", sub{shift->[2];},],
55                                  ["${p}view/cust_main.cgi?", sub{shift->[2];},],
56                                ],
57       )
58 %>
59 <%init>
60
61 my $conf = new FS::Conf;
62
63 my $curuser = $FS::CurrentUser::CurrentUser;
64
65 die "access denied"
66   unless $curuser->access_right('Financial reports')
67       || $curuser->access_right('Process batches')
68       || $curuser->access_right('Process global batches')
69       || ( $cgi->param('custnum') 
70            && (    $conf->exists('batch-enable')
71                 || $conf->config('batch-enable_payby')
72               )
73          );
74
75 my( $count_query, $sql_query );
76 my $hashref = {};
77 my @search = ();
78 my $orderby = 'paybatchnum';
79
80 my( $pay_batch, $batchnum ) = ( '', '');
81 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
82   push @search, "batchnum = $1";
83   $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
84   die "Batch $1 not found!" unless $pay_batch;
85   $batchnum = $pay_batch->batchnum;
86 }
87
88 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
89   push @search, "custnum = $1";
90 }
91
92 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
93   push @search, "pay_batch.status = '$1'";
94 }
95
96 if ( $cgi->param('payby') ) {
97   $cgi->param('payby') =~ /^(CARD|CHEK)$/
98     or die "illegal payby " . $cgi->param('payby');
99
100   push @search, "cust_pay_batch.payby = '$1'";
101 }
102
103 if ( not $cgi->param('dcln') ) {
104   push @search, "cpb.status IS DISTINCT FROM 'Approved'";
105 }
106
107 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
108 unless ($pay_batch){
109   push @search, "pay_batch.upload >= $beginning" if ($beginning);
110   push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
111   $orderby = "pay_batch.download,paybatchnum";
112 }
113
114 push @search, $curuser->agentnums_sql({ table => 'cust_main' });
115
116 push @search, $curuser->agentnums_sql({ table      => 'pay_batch',
117                                         null_right => 'Process global batches',
118                                      });
119
120 my $search = ' WHERE ' . join(' AND ', @search);
121
122 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
123                   'LEFT JOIN cust_main USING ( custnum ) ' .
124                   'LEFT JOIN pay_batch USING ( batchnum )' .
125                   $search;
126
127 #grr
128 $sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
129              "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
130              "FROM cust_pay_batch AS cpb " .
131              'LEFT JOIN cust_main USING ( custnum ) ' .
132              'LEFT JOIN pay_batch USING ( batchnum ) ' .
133              "$search ORDER BY $orderby";
134
135 my $html_init = '';
136 if ( $pay_batch ) {
137   $html_init = include('elements/cust_pay_batch_top.html', 
138                     'pay_batch' => $pay_batch);
139 }
140 </%init>