add customer status column to customer & most other reports. also put the C in ACL...
[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 die "access denied"
62   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
63       || $FS::CurrentUser::CurrentUser->access_right('Process batches');
64
65 my( $count_query, $sql_query );
66 my $hashref = {};
67 my @search = ();
68 my $orderby = 'paybatchnum';
69
70 my( $pay_batch, $batchnum ) = ( '', '');
71 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
72   push @search, "batchnum = $1";
73   $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
74   die "Batch $1 not found!" unless $pay_batch;
75   $batchnum = $pay_batch->batchnum;
76 }
77
78 if ( $cgi->param('payby') ) {
79   $cgi->param('payby') =~ /^(CARD|CHEK)$/
80     or die "illegal payby " . $cgi->param('payby');
81
82   push @search, "cust_pay_batch.payby = '$1'";
83 }
84
85 if ( not $cgi->param('dcln') ) {
86   push @search, "cpb.status IS DISTINCT FROM 'Approved'";
87 }
88
89 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
90 unless ($pay_batch){
91   push @search, "pay_batch.upload >= $beginning" if ($beginning);
92   push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
93   $orderby = "pay_batch.download,paybatchnum";
94 }
95
96 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
97 my $search = ' WHERE ' . join(' AND ', @search);
98
99 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
100                   'LEFT JOIN cust_main USING ( custnum ) ' .
101                   'LEFT JOIN pay_batch USING ( batchnum )' .
102                   $search;
103
104 #grr
105 $sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
106              "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
107              "FROM cust_pay_batch AS cpb " .
108              'LEFT JOIN cust_main USING ( custnum ) ' .
109              'LEFT JOIN pay_batch USING ( batchnum ) ' .
110              "$search ORDER BY $orderby";
111
112 my $html_init = '';
113 if ( $pay_batch ) {
114   my $conf = new FS::Conf;
115   my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
116   if (
117        $pay_batch->status eq 'O' 
118        || ( $pay_batch->status eq 'I'
119             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
120           ) 
121   ) {
122     $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
123     if ( $fixed ) {
124       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
125     } else {
126       $html_init .= qq!Download batch in format <SELECT NAME="format">!.
127                     qq!<OPTION VALUE="">Default batch mode</OPTION>!.
128                     qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
129                     qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
130                     qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
131                     qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
132                     qq!</SELECT>!;
133     }
134     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
135   }
136
137   if (
138        $pay_batch->status eq 'I' 
139        || ( $pay_batch->status eq 'R'
140             && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
141           ) 
142   ) {
143     $html_init .= qq!<FORM ACTION="$p/misc/upload-batch.cgi" METHOD="POST" ENCTYPE="multipart/form-data">!.
144                   qq!Upload results<BR>!.
145                   qq!Filename <INPUT TYPE="file" NAME="batch_results"><BR>!;
146     if ( $fixed ) {
147       $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
148     } else {
149       $html_init .= qq!Format <SELECT NAME="format">!.
150                     qq!<OPTION VALUE="">Default batch mode</OPTION>!.
151                     qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
152                     qq!<OPTION VALUE="csv-chase_canada-E-xactBatch">CSV file for Chase Canada E-xactBatch</OPTION>!.
153                     qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
154                     qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
155                     qq!</SELECT><BR>!;
156     }
157     $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
158     $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
159   }
160
161 }
162
163 if ($pay_batch) {
164   my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
165   $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
166   my $cards = $sth->fetchrow_arrayref->[0];
167
168   my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
169   $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
170   $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
171   my $total = $sth->fetchrow_arrayref->[0];
172
173   $html_init .= "$cards credit card payments batched<BR>\$" .
174                 sprintf("%.2f", $total) ." total in batch<BR>";
175 }
176
177 </%init>