capture B:OP and B:BP failure_status values from payment attempts, #21117
[freeside.git] / httemplate / search / cust_pay_batch.cgi
1 <& 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                                   sub { $_[0]->display_status },
34                                   'error_message',
35                                 ],
36               'align'       => 'rrrlllcrlll',
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 <%init>
46
47 my $conf = new FS::Conf;
48
49 my $curuser = $FS::CurrentUser::CurrentUser;
50
51 die "access denied"
52   unless $curuser->access_right('Financial reports')
53       || $curuser->access_right('Process batches')
54       || $curuser->access_right('Process global batches')
55       || ( $cgi->param('custnum') 
56            && (    $conf->exists('batch-enable')
57                 || $conf->config('batch-enable_payby')
58               )
59          );
60
61 my( $count_query, $sql_query );
62 my $hashref = {};
63 my @search = ();
64 my $orderby = 'paybatchnum';
65
66 my( $pay_batch, $batchnum ) = ( '', '');
67 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
68   push @search, "batchnum = $1";
69   $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
70   die "Batch $1 not found!" unless $pay_batch;
71   $batchnum = $pay_batch->batchnum;
72 }
73
74 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
75   push @search, "custnum = $1";
76 }
77
78 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
79   push @search, "pay_batch.status = '$1'";
80 }
81
82 if ( $cgi->param('payby') ) {
83   $cgi->param('payby') =~ /^(CARD|CHEK)$/
84     or die "illegal payby " . $cgi->param('payby');
85
86   push @search, "cust_pay_batch.payby = '$1'";
87 }
88
89 if ( not $cgi->param('dcln') ) {
90   push @search, "cust_pay_batch.status IS DISTINCT FROM 'Approved'";
91 }
92
93 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
94 unless ($pay_batch){
95   push @search, "pay_batch.upload >= $beginning" if ($beginning);
96   push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
97   $orderby = "pay_batch.download,paybatchnum";
98 }
99
100 push @search, $curuser->agentnums_sql({ table => 'cust_main' });
101
102 push @search, $curuser->agentnums_sql({ table      => 'pay_batch',
103                                         null_right => 'Process global batches',
104                                      });
105
106 my $search = ' WHERE ' . join(' AND ', @search);
107
108 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch ' .
109                   'LEFT JOIN cust_main USING ( custnum ) ' .
110                   'LEFT JOIN pay_batch USING ( batchnum )' .
111                   $search;
112
113 $sql_query = {
114   'table'     => 'cust_pay_batch',
115   'select'    => 'cust_pay_batch.*, cust_main.*, cust_pay.paynum',
116   'hashref'   => {},
117   'addl_from' => 'LEFT JOIN pay_batch USING ( batchnum ) '.
118                  'LEFT JOIN cust_main USING ( custnum ) '.
119                  
120                  'LEFT JOIN cust_pay  USING ( batchnum, custnum ) ',
121   'extra_sql' => $search,
122   'order_by'  => "ORDER BY $orderby",
123 };
124
125 my $sub_receipt = sub {
126   my $paynum = shift->paynum or return '';
127   include('/elements/popup_link_onclick.html',
128     'action'  => $p.'view/cust_pay.html?link=popup;paynum='.$paynum,
129     'actionlabel' => emt('Payment Receipt'),
130   );
131 };
132
133 my $html_init = '';
134 if ( $pay_batch ) {
135   $html_init = include('elements/cust_pay_batch_top.html', 
136                     'pay_batch' => $pay_batch);
137 }
138 </%init>