This commit was generated by cvs2svn to compensate for changes in r6255,
[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            #&& $FS::CurrentUser::CurrentUser->access_right('View customer batched payments')
68          );
69
70 my( $count_query, $sql_query );
71 my $hashref = {};
72 my @search = ();
73 my $orderby = 'paybatchnum';
74
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;
81 }
82
83 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
84   push @search, "custnum = $1";
85 }
86
87 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
88   push @search, "pay_batch.status = '$1'";
89 }
90
91 if ( $cgi->param('payby') ) {
92   $cgi->param('payby') =~ /^(CARD|CHEK)$/
93     or die "illegal payby " . $cgi->param('payby');
94
95   push @search, "cust_pay_batch.payby = '$1'";
96 }
97
98 if ( not $cgi->param('dcln') ) {
99   push @search, "cpb.status IS DISTINCT FROM 'Approved'";
100 }
101
102 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
103 unless ($pay_batch){
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";
107 }
108
109 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
110 my $search = ' WHERE ' . join(' AND ', @search);
111
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 )' .
115                   $search;
116
117 #grr
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";
124
125 my $html_init = '';
126 if ( $pay_batch ) {
127   my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
128   if (
129        $pay_batch->status eq 'O' 
130        || ( $pay_batch->status eq 'I'
131             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
132           ) 
133   ) {
134     $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
135     if ( $fixed ) {
136       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
137     } else {
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>!.
145                     qq!</SELECT>!;
146     }
147     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
148   }
149
150   if (
151        $pay_batch->status eq 'I' 
152        || ( $pay_batch->status eq 'R'
153             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
154           ) 
155   ) {
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>!;
159     if ( $fixed ) {
160       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
161     } else {
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>!.
169                     qq!</SELECT><BR>!;
170     }
171     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
172     $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
173   }
174
175 }
176
177 if ($pay_batch) {
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];
181
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];
186
187   $html_init .= "$cards credit card payments batched<BR>\$" .
188                 sprintf("%.2f", $total) ." total in batch<BR>";
189 }
190
191 </%init>