manual batch approval and TD EFT fixes, RT#10545
[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               'header'      => [ '#',
8                                  'Inv #',
9                                  'Customer',
10                                  'Customer',
11                                  'Card Name',
12                                  'Card',
13                                  'Exp',
14                                  'Amount',
15                                  'Status',
16                                ],
17               'fields'      => [ sub {
18                                    shift->[0];
19                                  },
20                                  sub {
21                                    shift->[1];
22                                  },
23                                  sub {
24                                    shift->[2];
25                                  },
26                                  sub {
27                                    my $cpb = shift;
28                                    $cpb->[3] . ', ' . $cpb->[4];
29                                  },
30                                  sub {
31                                    shift->[5];
32                                  },
33                                  sub {
34                                    my $cardnum = shift->[6];
35                                    'x'x(length($cardnum)-4). substr($cardnum,(length($cardnum)-4));
36                                  },
37                                  sub {
38                                    shift->[7] =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
39                                    my( $mon, $year ) = ( $2, $1 );
40                                    $mon = "0$mon" if length($mon) == 1;
41                                    "$mon/$year";
42                                  },
43                                  sub {
44                                    shift->[8];
45                                  },
46                                  sub {
47                                    shift->[9];
48                                  },
49                                ],
50               'align'       => 'lllllllrl',
51               'links'       => [ ['', sub{'#';}],
52                                  ["${p}view/cust_bill.cgi?", sub{shift->[1];},],
53                                  ["${p}view/cust_main.cgi?", sub{shift->[2];},],
54                                  ["${p}view/cust_main.cgi?", sub{shift->[2];},],
55                                ],
56       )
57 %>
58 <%init>
59
60 my $conf = new FS::Conf;
61
62 die "access denied"
63   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
64       || $FS::CurrentUser::CurrentUser->access_right('Process batches')
65       || ( $cgi->param('custnum') 
66            && (    $conf->exists('batch-enable')
67                 || $conf->config('batch-enable_payby')
68               )
69            #&& $FS::CurrentUser::CurrentUser->access_right('View customer batched payments')
70          );
71
72 my( $count_query, $sql_query );
73 my $hashref = {};
74 my @search = ();
75 my $orderby = 'paybatchnum';
76
77 my( $pay_batch, $batchnum ) = ( '', '');
78 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
79   push @search, "batchnum = $1";
80   $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
81   die "Batch $1 not found!" unless $pay_batch;
82   $batchnum = $pay_batch->batchnum;
83 }
84
85 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
86   push @search, "custnum = $1";
87 }
88
89 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
90   push @search, "pay_batch.status = '$1'";
91 }
92
93 if ( $cgi->param('payby') ) {
94   $cgi->param('payby') =~ /^(CARD|CHEK)$/
95     or die "illegal payby " . $cgi->param('payby');
96
97   push @search, "cust_pay_batch.payby = '$1'";
98 }
99
100 if ( not $cgi->param('dcln') ) {
101   push @search, "cpb.status IS DISTINCT FROM 'Approved'";
102 }
103
104 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
105 unless ($pay_batch){
106   push @search, "pay_batch.upload >= $beginning" if ($beginning);
107   push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
108   $orderby = "pay_batch.download,paybatchnum";
109 }
110
111 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
112 my $search = ' WHERE ' . join(' AND ', @search);
113
114 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
115                   'LEFT JOIN cust_main USING ( custnum ) ' .
116                   'LEFT JOIN pay_batch USING ( batchnum )' .
117                   $search;
118
119 #grr
120 $sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
121              "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
122              "FROM cust_pay_batch AS cpb " .
123              'LEFT JOIN cust_main USING ( custnum ) ' .
124              'LEFT JOIN pay_batch USING ( batchnum ) ' .
125              "$search ORDER BY $orderby";
126
127 my $html_init = '<TABLE>';
128 if ( $pay_batch ) {
129   my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
130   if (
131        $pay_batch->status eq 'O' 
132        || ( $pay_batch->status eq 'I'
133             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
134           ) 
135        || ( $pay_batch->status eq 'R'
136             && $FS::CurrentUser::CurrentUser->access_right('Redownload resolved batches')
137           ) 
138   ) {
139     $html_init .= qq!<TR><FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
140     if ( $fixed ) {
141       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
142     } else {
143       $html_init .= qq!Download batch in format !.
144                     qq!<SELECT NAME="format">!.
145                     qq!<OPTION VALUE="">Default batch mode</OPTION>!.
146                     qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
147                     qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
148                     qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
149                     qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
150                     qq!<OPTION VALUE="ach-spiritone">Spiritone ACH batch</OPTION>!.
151                     qq!<OPTION VALUE="paymentech">Chase Paymentech XML</OPTION>!.
152                     qq!<OPTION VALUE="RBC">Royal Bank of Canada PDS</OPTION>!.
153                     qq!<OPTION VALUE="td_eft1464">TD Commercial Banking EFT 1464 byte</OPTION>!.
154
155                     qq!</SELECT>!;
156     }
157     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR><BR></TR>!;
158   }
159
160   if (
161        $pay_batch->status eq 'I' 
162        || ( $pay_batch->status eq 'R'
163             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
164           ) 
165   ) {
166     $html_init .= '<TR>'.
167                   include('/elements/form-file_upload.html',
168                             'name'      => 'FileUpload',
169                             'action'    => "$p/misc/upload-batch.cgi",
170                             'num_files' => 1,
171                             'fields'    => [ 'batchnum', 'format' ],
172                             'message'   => 'Batch results uploaded.',
173                             ) .
174                   'Upload results<BR></TR><TR>'.
175                   include('/elements/file-upload.html',
176                             'field'   => 'file',
177                             'label'   => 'Filename',
178                             'no_table'=> 1
179                          ).
180                   '<BR></TR>'
181                   ;
182     if ( $fixed ) {
183       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
184     } else {
185       # should pull this from %import_info
186       $html_init .= qq!<TR>Format !.
187                     qq!<SELECT NAME="format">!.
188                     qq!<OPTION VALUE="">Default batch mode</OPTION>!.
189                     qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
190                     qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
191                     qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
192                     qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
193                     qq!<OPTION VALUE="ach-spiritone">Spiritone ACH batch</OPTION>!.
194                     qq!<OPTION VALUE="paymentech">Chase Paymentech XML</OPTION>!.
195                     qq!<OPTION VALUE="RBC">Royal Bank of Canada PDS</OPTION>!.
196                     qq!<OPTION VALUE="td_eftack264">TD EFT Acknowledgement</OPTION>!.
197                     qq!<OPTION VALUE="td_eftret80">TD EFT Returned Items</OPTION>!.
198                     qq!</SELECT><BR></TR>!;
199     }
200     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
201     $html_init .= '<TR> <INPUT TYPE="submit" VALUE="Upload"></FORM><BR> </TR>';
202     if ( $conf->exists('batch-manual_approval') and $pay_batch->status eq 'I') {
203       $html_init .= qq!<TR><INPUT TYPE="button" VALUE="Manually approve" onclick="
204 if ( confirm('Approve all remaining payments in this batch?') )
205   window.location.href='${p}misc/process/pay_batch-approve.cgi?batchnum=$batchnum';"></TR>!
206     }
207   }
208   $html_init .= '</TABLE>';
209 }
210
211 if ($pay_batch) {
212   my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
213   $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
214   my $cards = $sth->fetchrow_arrayref->[0];
215
216   my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
217   $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
218   $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
219   my $total = $sth->fetchrow_arrayref->[0];
220
221   $html_init .= "$cards credit card payments batched<BR>\$" .
222                 sprintf("%.2f", $total) ." total in batch<BR>";
223 }
224
225 </%init>