This commit was generated by cvs2svn to compensate for changes in r5562,
[freeside.git] / httemplate / search / cust_pay_batch.cgi
1 <% include('elements/search.html',
2               'title'       => 'Batch payment details',
3               'name'        => 'batch details',
4               'menubar'     => ['Main Menu'  => $p,],
5               'query'       => $sql_query,
6               'count_query' => $count_query,
7               'html_init'   => $pay_batch ? $html_init : '',
8               'header'      => [ '#',
9                                  'Inv #',
10                                  'Customer',
11                                  'Customer',
12                                  'Card Name',
13                                  'Card',
14                                  'Exp',
15                                  'Amount',
16                                  'Status',
17                                ],
18               'fields'      => [ sub {
19                                    shift->[0];
20                                  },
21                                  sub {
22                                    shift->[1];
23                                  },
24                                  sub {
25                                    shift->[2];
26                                  },
27                                  sub {
28                                    my $cpb = shift;
29                                    $cpb->[3] . ', ' . $cpb->[4];
30                                  },
31                                  sub {
32                                    shift->[5];
33                                  },
34                                  sub {
35                                    my $cardnum = shift->[6];
36                                    'x'x(length($cardnum)-4). substr($cardnum,(length($cardnum)-4));
37                                  },
38                                  sub {
39                                    shift->[7] =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
40                                    my( $mon, $year ) = ( $2, $1 );
41                                    $mon = "0$mon" if length($mon) == 1;
42                                    "$mon/$year";
43                                  },
44                                  sub {
45                                    shift->[8];
46                                  },
47                                  sub {
48                                    shift->[9];
49                                  },
50                                ],
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];},],
56                                ],
57       )
58 %>
59 <%init>
60
61 my $conf = new FS::Conf;
62
63 die "access denied"
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')
69          );
70
71 my( $count_query, $sql_query );
72 my $hashref = {};
73 my @search = ();
74 my $orderby = 'paybatchnum';
75
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;
82 }
83
84 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
85   push @search, "custnum = $1";
86 }
87
88 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
89   push @search, "pay_batch.status = '$1'";
90 }
91
92 if ( $cgi->param('payby') ) {
93   $cgi->param('payby') =~ /^(CARD|CHEK)$/
94     or die "illegal payby " . $cgi->param('payby');
95
96   push @search, "cust_pay_batch.payby = '$1'";
97 }
98
99 if ( not $cgi->param('dcln') ) {
100   push @search, "cpb.status IS DISTINCT FROM 'Approved'";
101 }
102
103 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
104 unless ($pay_batch){
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";
108 }
109
110 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
111 my $search = ' WHERE ' . join(' AND ', @search);
112
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 )' .
116                   $search;
117
118 #grr
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";
125
126 my $html_init = '';
127 if ( $pay_batch ) {
128   my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
129   if (
130        $pay_batch->status eq 'O' 
131        || ( $pay_batch->status eq 'I'
132             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
133           ) 
134   ) {
135     $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
136     if ( $fixed ) {
137       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
138     } else {
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>!.
146                     qq!</SELECT>!;
147     }
148     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
149   }
150
151   if (
152        $pay_batch->status eq 'I' 
153        || ( $pay_batch->status eq 'R'
154             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
155           ) 
156   ) {
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>!;
160     if ( $fixed ) {
161       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
162     } else {
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>!.
170                     qq!</SELECT><BR>!;
171     }
172     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
173     $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
174   }
175
176 }
177
178 if ($pay_batch) {
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];
182
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];
187
188   $html_init .= "$cards credit card payments batched<BR>\$" .
189                 sprintf("%.2f", $total) ." total in batch<BR>";
190 }
191
192 </%init>