record and show batch payment status info, #21117
[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                                  'Cust #',
11                                  'Customer',
12                                  'Card Name',
13                                  'Card',
14                                  'Exp',
15                                  'Amount',
16                                  'Status',
17                                  '', # error_message
18                                ],
19               'fields'      => [  'paybatchnum',
20                                   'invnum',
21                                   'custnum',
22                                   sub { $_[0]->cust_main->name_short },
23                                   'payname',
24                                   'mask_payinfo',
25                                   sub {
26                                     return('') if $_[0]->payby ne 'CARD';
27                                     $_[0]->get('exp') =~ /^\d\d(\d\d)-(\d\d)/;
28                                     sprintf('%02d/%02d',$1,$2);
29                                   },
30                                   sub {
31                                     sprintf('%.02f', $_[0]->amount)
32                                   },
33                                   'status',
34                                   'error_message',
35                                 ],
36               'align'       => 'rrrlllcrll',
37               'links'       => [ '',
38                                  ["${p}view/cust_bill.cgi?", 'invnum'],
39                                  (["${p}view/cust_main.cgi?", 'custnum']) x 2,
40                                ],
41               'link_onclicks' => [ ('') x 8,
42                                    $sub_receipt
43                                  ],
44       )
45 %>
46 <%init>
47
48 my $conf = new FS::Conf;
49
50 my $curuser = $FS::CurrentUser::CurrentUser;
51
52 die "access denied"
53   unless $curuser->access_right('Financial reports')
54       || $curuser->access_right('Process batches')
55       || $curuser->access_right('Process global batches')
56       || ( $cgi->param('custnum') 
57            && (    $conf->exists('batch-enable')
58                 || $conf->config('batch-enable_payby')
59               )
60          );
61
62 my( $count_query, $sql_query );
63 my $hashref = {};
64 my @search = ();
65 my $orderby = 'paybatchnum';
66
67 my( $pay_batch, $batchnum ) = ( '', '');
68 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
69   push @search, "batchnum = $1";
70   $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
71   die "Batch $1 not found!" unless $pay_batch;
72   $batchnum = $pay_batch->batchnum;
73 }
74
75 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
76   push @search, "custnum = $1";
77 }
78
79 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
80   push @search, "pay_batch.status = '$1'";
81 }
82
83 if ( $cgi->param('payby') ) {
84   $cgi->param('payby') =~ /^(CARD|CHEK)$/
85     or die "illegal payby " . $cgi->param('payby');
86
87   push @search, "cust_pay_batch.payby = '$1'";
88 }
89
90 if ( not $cgi->param('dcln') ) {
91   push @search, "cpb.status IS DISTINCT FROM 'Approved'";
92 }
93
94 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
95 unless ($pay_batch){
96   push @search, "pay_batch.upload >= $beginning" if ($beginning);
97   push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
98   $orderby = "pay_batch.download,paybatchnum";
99 }
100
101 push @search, $curuser->agentnums_sql({ table => 'cust_main' });
102
103 push @search, $curuser->agentnums_sql({ table      => 'pay_batch',
104                                         null_right => 'Process global batches',
105                                      });
106
107 my $search = ' WHERE ' . join(' AND ', @search);
108
109 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
110                   'LEFT JOIN cust_main USING ( custnum ) ' .
111                   'LEFT JOIN pay_batch USING ( batchnum )' .
112                   $search;
113
114 $sql_query = {
115   'table'     => 'cust_pay_batch',
116   'select'    => 'cust_pay_batch.*, cust_main.*, cust_pay.paynum',
117   'hashref'   => {},
118   'addl_from' => 'LEFT JOIN pay_batch USING ( batchnum ) '.
119                  'LEFT JOIN cust_main USING ( custnum ) '.
120                  
121                  'LEFT JOIN cust_pay  USING ( batchnum, custnum ) ',
122   'extra_sql' => $search,
123   'order_by'  => "ORDER BY $orderby",
124 };
125
126 my $sub_receipt = sub {
127   my $paynum = shift->paynum or return '';
128   include('/elements/popup_link_onclick.html',
129     'action'  => $p.'view/cust_pay.html?link=popup;paynum='.$paynum,
130     'actionlabel' => emt('Payment Receipt'),
131   );
132 };
133
134 my $html_init = '';
135 if ( $pay_batch ) {
136   $html_init = include('elements/cust_pay_batch_top.html', 
137                     'pay_batch' => $pay_batch);
138 }
139 </%init>