1 <% include('elements/search.html',
2 'title' => 'Batch payment details',
3 'name' => 'batch details',
5 'count_query' => $count_query,
6 'html_init' => $pay_batch ? $html_init : '',
28 $cpb->[3] . ', ' . $cpb->[4];
34 my $cardnum = shift->[6];
35 'x'x(length($cardnum)-4). substr($cardnum,(length($cardnum)-4));
38 shift->[7] =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
39 my( $mon, $year ) = ( $2, $1 );
40 $mon = "0$mon" if length($mon) == 1;
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];},],
60 my $conf = new FS::Conf;
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 #&& $FS::CurrentUser::CurrentUser->access_right('View customer batched payments')
70 my( $count_query, $sql_query );
73 my $orderby = 'paybatchnum';
75 my( $pay_batch, $batchnum ) = ( '', '');
76 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
77 push @search, "batchnum = $1";
78 $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
79 die "Batch $1 not found!" unless $pay_batch;
80 $batchnum = $pay_batch->batchnum;
83 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
84 push @search, "custnum = $1";
87 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
88 push @search, "pay_batch.status = '$1'";
91 if ( $cgi->param('payby') ) {
92 $cgi->param('payby') =~ /^(CARD|CHEK)$/
93 or die "illegal payby " . $cgi->param('payby');
95 push @search, "cust_pay_batch.payby = '$1'";
98 if ( not $cgi->param('dcln') ) {
99 push @search, "cpb.status IS DISTINCT FROM 'Approved'";
102 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
104 push @search, "pay_batch.upload >= $beginning" if ($beginning);
105 push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
106 $orderby = "pay_batch.download,paybatchnum";
109 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
110 my $search = ' WHERE ' . join(' AND ', @search);
112 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
113 'LEFT JOIN cust_main USING ( custnum ) ' .
114 'LEFT JOIN pay_batch USING ( batchnum )' .
118 $sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
119 "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
120 "FROM cust_pay_batch AS cpb " .
121 'LEFT JOIN cust_main USING ( custnum ) ' .
122 'LEFT JOIN pay_batch USING ( batchnum ) ' .
123 "$search ORDER BY $orderby";
127 my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
129 $pay_batch->status eq 'O'
130 || ( $pay_batch->status eq 'I'
131 && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
134 $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
136 $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
138 $html_init .= qq!Download batch in format <SELECT NAME="format">!.
139 qq!<OPTION VALUE="">Default batch mode</OPTION>!.
140 qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
141 qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
142 qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
143 qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
144 qq!<OPTION VALUE="ach-spiritone">Spiritone ACH batch</OPTION>!.
147 $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
151 $pay_batch->status eq 'I'
152 || ( $pay_batch->status eq 'R'
153 && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
156 $html_init .= qq!<FORM ACTION="$p/misc/upload-batch.cgi" METHOD="POST" ENCTYPE="multipart/form-data">!.
157 qq!Upload results<BR>!.
158 qq!Filename <INPUT TYPE="file" NAME="batch_results"><BR>!;
160 $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
162 $html_init .= qq!Format <SELECT NAME="format">!.
163 qq!<OPTION VALUE="">Default batch mode</OPTION>!.
164 qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
165 qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
166 qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
167 qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
168 qq!<OPTION VALUE="ach-spiritone">Spiritone ACH batch</OPTION>!.
171 $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
172 $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
178 my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
179 $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
180 my $cards = $sth->fetchrow_arrayref->[0];
182 my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
183 $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
184 $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
185 my $total = $sth->fetchrow_arrayref->[0];
187 $html_init .= "$cards credit card payments batched<BR>\$" .
188 sprintf("%.2f", $total) ." total in batch<BR>";