This commit was generated by cvs2svn to compensate for changes in r4888,
[freeside.git] / httemplate / search / cust_pay_batch.cgi
1 %my( $count_query, $sql_query );
2 %my $hashref = {};
3 %my @search = ();
4 %my $orderby = 'paybatchnum';
5 %
6 %my( $pay_batch, $batchnum ) = ( '', '');
7 %if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
8 %  push @search, "batchnum = $1";
9 %  $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
10 %  die "Batch $1 not found!" unless $pay_batch;
11 %  $batchnum = $pay_batch->batchnum;
12 %}
13 %
14 %if ( $cgi->param('payby') ) {
15 %  $cgi->param('payby') =~ /^(CARD|CHEK)$/
16 %    or die "illegal payby " . $cgi->param('payby');
17 %
18 %  push @search, "cust_pay_batch.payby = '$1'";
19 %}
20 %
21 %if ( not $cgi->param('dcln') ) {
22 %  push @search, "cpb.status IS DISTINCT FROM 'Approved'";
23 %}
24 %
25 %my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
26 %unless ($pay_batch){
27 %  push @search, "pay_batch.upload >= $beginning" if ($beginning);
28 %  push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
29 %  $orderby = "pay_batch.download,paybatchnum";
30 %}
31 %
32 %push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
33 %my $search = ' WHERE ' . join(' AND ', @search);
34 %
35 %$count_query = 'SELECT COUNT(*) FROM cust_pay_batch AS cpb ' .
36 %                  'LEFT JOIN cust_main USING ( custnum ) ' .
37 %                  'LEFT JOIN pay_batch USING ( batchnum )' .
38 %                 $search;
39 %
40 %#grr
41 %$sql_query = "SELECT paybatchnum,invnum,custnum,cpb.last,cpb.first," .
42 %             "cpb.payname,cpb.payinfo,cpb.exp,amount,cpb.status " .
43 %            "FROM cust_pay_batch AS cpb " .
44 %             'LEFT JOIN cust_main USING ( custnum ) ' .
45 %             'LEFT JOIN pay_batch USING ( batchnum ) ' .
46 %             "$search ORDER BY $orderby";
47 %
48 %my $html_init = '';
49 %if ( $pay_batch ) {
50 %  my $conf = new FS::Conf;
51 %  my $fixed = $conf->config('batch-fixed_format-'. $pay_batch->payby);
52 %  if (
53 %       $pay_batch->status eq 'O' 
54 %       || ( $pay_batch->status eq 'I'
55 %            && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
56 %          ) 
57 %  ) {
58 %    $html_init .= qq!<FORM ACTION="$p/misc/download-batch.cgi" METHOD="POST">!;
59 %    if ( $fixed ) {
60 %      $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
61 %    } else {
62 %      $html_init .= qq!Download batch in format <SELECT NAME="format">!.
63 %                    qq!<OPTION VALUE="">Default batch mode</OPTION>!.
64 %                    qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV file for TD Canada Trust Merchant PC Batch</OPTION>!.
65 %                    qq!<OPTION VALUE="PAP">80 byte file for TD Canada Trust PAP Batch</OPTION>!.
66 %                    qq!<OPTION VALUE="BoM">Bank of Montreal ECA batch</OPTION>!.
67 %                    qq!</SELECT>!;
68 %    }
69 %    $html_init .= qq!<INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum"><INPUT TYPE="submit" VALUE="Download"></FORM><BR>!;
70 %  }
71 %
72 %  if (
73 %       $pay_batch->status eq 'I' 
74 %       || ( $pay_batch->status eq 'R'
75 %            && $FS::CurrentUser::CurrentUser->access_right('Reprocess batches')
76 %          ) 
77 %  ) {
78 %    $html_init .= qq!<FORM ACTION="$p/misc/upload-batch.cgi" METHOD="POST" ENCTYPE="multipart/form-data">!.
79 %                  qq!Upload results<BR>!.
80 %                  qq!Filename <INPUT TYPE="file" NAME="batch_results"><BR>!;
81 %    if ( $fixed ) {
82 %      $html_init .= qq!<INPUT TYPE="hidden" NAME="format" VALUE="$fixed">!;
83 %    } else {
84 %      $html_init .= qq!Format <SELECT NAME="format">!.
85 %                    qq!<OPTION VALUE="">Default batch mode</OPTION>!.
86 %                    qq!<OPTION VALUE="csv-td_canada_trust-merchant_pc_batch">CSV results from TD Canada Trust Merchant PC Batch</OPTION>!.
87 %                    qq!<OPTION VALUE="PAP">264 byte results for TD Canada Trust PAP Batch</OPTION>!.
88 %                    qq!<OPTION VALUE="BoM">Bank of Montreal ECA results</OPTION>!.
89 %                    qq!</SELECT><BR>!;
90 %    }
91 %    $html_init .= '<INPUT TYPE="submit" VALUE="Upload"></FORM><BR>';
92 %  }
93 %
94 %}
95 %
96 %if ($pay_batch) {
97 %  my $sth = dbh->prepare($count_query) or die dbh->errstr. "doing $count_query";
98 %  $sth->execute or die "Error executing \"$count_query\": ". $sth->errstr;
99 %  my $cards = $sth->fetchrow_arrayref->[0];
100 %
101 %  my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=". $batchnum;
102 %  $sth = dbh->prepare($st) or die dbh->errstr. "doing $st";
103 %  $sth->execute or die "Error executing \"$st\": ". $sth->errstr;
104 %  my $total = $sth->fetchrow_arrayref->[0];
105 %
106 %  $html_init .= "$cards credit card payments batched<BR>\$" .
107 %                sprintf("%.2f", $total) ." total in batch<BR>";
108 %}
109 %
110 %
111 <% include('elements/search.html',
112               'title'       => 'Batch payment details',
113               'name'        => 'batch details',
114               'menubar'     => ['Main Menu'  => $p,],
115               'query'       => $sql_query,
116               'count_query' => $count_query,
117               'html_init'   => $pay_batch ? $html_init : '',
118               'header'      => [ '#',
119                                  'Inv #',
120                                  'Customer',
121                                  'Customer',
122                                  'Card Name',
123                                  'Card',
124                                  'Exp',
125                                  'Amount',
126                                  'Status',
127                                ],
128               'fields'      => [ sub {
129                                    shift->[0];
130                                  },
131                                  sub {
132                                    shift->[1];
133                                  },
134                                  sub {
135                                    shift->[2];
136                                  },
137                                  sub {
138                                    my $cpb = shift;
139                                    $cpb->[3] . ', ' . $cpb->[4];
140                                  },
141                                  sub {
142                                    shift->[5];
143                                  },
144                                  sub {
145                                    my $cardnum = shift->[6];
146                                    'x'x(length($cardnum)-4). substr($cardnum,(length($cardnum)-4));
147                                  },
148                                  sub {
149                                    shift->[7] =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
150                                    my( $mon, $year ) = ( $2, $1 );
151                                    $mon = "0$mon" if length($mon) == 1;
152                                    "$mon/$year";
153                                  },
154                                  sub {
155                                    shift->[8];
156                                  },
157                                  sub {
158                                    shift->[9];
159                                  },
160                                ],
161               'align'       => 'lllllllrl',
162               'links'       => [ ['', sub{'#';}],
163                                  ["${p}view/cust_bill.cgi?", sub{shift->[1];},],
164                                  ["${p}view/cust_main.cgi?", sub{shift->[2];},],
165                                  ["${p}view/cust_main.cgi?", sub{shift->[2];},],
166                                ],
167       )
168 %>
169