1 <% include('elements/search.html',
2 'title' => 'Batch payment details',
3 'name' => 'batch details',
4 'menubar' => ['Main Menu' => $p,],
6 'count_query' => $count_query,
7 'html_init' => $pay_batch ? $html_init : '',
29 $cpb->[3] . ', ' . $cpb->[4];
35 my $cardnum = shift->[6];
36 'x'x(length($cardnum)-4). substr($cardnum,(length($cardnum)-4));
39 shift->[7] =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
40 my( $mon, $year ) = ( $2, $1 );
41 $mon = "0$mon" if length($mon) == 1;
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];},],
61 my $conf = new FS::Conf;
64 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
65 || $FS::CurrentUser::CurrentUser->access_right('Process batches')
66 || ( $cgi->param('custnum')
67 && $conf->exists('batch-enable')
68 #&& $FS::CurrentUser::CurrentUser->access_right('View customer batched payments')
71 my( $count_query, $sql_query );
74 my $orderby = 'paybatchnum';
76 my( $pay_batch, $batchnum ) = ( '', '');
77 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
78 push @search, "batchnum = $1";
79 $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
80 die "Batch $1 not found!" unless $pay_batch;
81 $batchnum = $pay_batch->batchnum;
84 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
85 push @search, "custnum = $1";
88 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
89 push @search, "pay_batch.status = '$1'";
92 if ( $cgi->param('payby') ) {
93 $cgi->param('payby') =~ /^(CARD|CHEK)$/
94 or die "illegal payby " . $cgi->param('payby');
96 push @search, "cust_pay_batch.payby = '$1'";
99 if ( not $cgi->param('dcln') ) {
100 push @search, "cpb.status IS DISTINCT FROM 'Approved'";
103 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
105 push @search, "pay_batch.upload >= $beginning" if ($beginning);
106 push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
107 $orderby = "pay_batch.download,paybatchnum";
110 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
111 my $search = ' WHERE ' . join(' AND ', @search);
113 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
114 'LEFT JOIN cust_main USING ( custnum ) ' .
115 'LEFT JOIN pay_batch USING ( batchnum )' .
119 $sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
120 "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
121 "FROM cust_pay_batch AS cpb " .
122 'LEFT JOIN cust_main USING ( custnum ) ' .
123 'LEFT JOIN pay_batch USING ( batchnum ) ' .
124 "$search ORDER BY $orderby";
128 my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
130 $pay_batch->status eq 'O'
131 || ( $pay_batch->status eq 'I'
132 && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
135 $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
137 $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
139 $html_init .= qq!Download batch in format <SELECT NAME="format">!.
140 qq!<OPTION VALUE="">Default batch mode</OPTION>!.
141 qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
142 qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
143 qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
144 qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
145 qq!<OPTION VALUE="ach-spiritone">Spiritone ACH batch</OPTION>!.
148 $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
152 $pay_batch->status eq 'I'
153 || ( $pay_batch->status eq 'R'
154 && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
157 $html_init .= qq!<FORM ACTION="$p/misc/upload-batch.cgi" METHOD="POST" ENCTYPE="multipart/form-data">!.
158 qq!Upload results<BR>!.
159 qq!Filename <INPUT TYPE="file" NAME="batch_results"><BR>!;
161 $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
163 $html_init .= qq!Format <SELECT NAME="format">!.
164 qq!<OPTION VALUE="">Default batch mode</OPTION>!.
165 qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
166 qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
167 qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
168 qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
169 qq!<OPTION VALUE="ach-spiritone">Spiritone ACH batch</OPTION>!.
172 $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
173 $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
179 my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
180 $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
181 my $cards = $sth->fetchrow_arrayref->[0];
183 my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
184 $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
185 $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
186 my $total = $sth->fetchrow_arrayref->[0];
188 $html_init .= "$cards credit card payments batched<BR>\$" .
189 sprintf("%.2f", $total) ." total in batch<BR>";