permit batch redownloads
[freeside.git] / httemplate / misc / download-batch.cgi
1 %
2 %
3 %my $conf=new FS::Conf;
4 %
5 %#http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
6 %http_header('Content-Type' => 'text/plain' );
7 %
8 %my $batchnum;
9 %if ( $cgi->param('batchnum') =~ /^(\d+)$/ ) {
10 %  $batchnum = $1;
11 %} else {
12 %  die "No batch number (bad URL) \n";
13 %}
14 %
15 %my $format;
16 %if ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
17 %  $format = $1;
18 %} else {
19 %  $format = $conf->config('batch-default_format');
20 %}
21 %
22 %my $oldAutoCommit = $FS::UID::AutoCommit;
23 %local $FS::UID::AutoCommit = 0;
24 %my $dbh = dbh;
25 %
26 %my $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'O'} );
27 %unless ($pay_batch) {
28 %  $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'I'} )
29 %    if $FS::CurrentUser::CurrentUser->access_right('Reprocess batches');
30 %}
31 %die "No pending batch. \n" unless $pay_batch;
32 %
33 %my %batchhash = $pay_batch->hash;
34 %$batchhash{'status'} = 'I';
35 %$batchhash{'download'} = time unless $batchhash{'download'};
36 %my $new = new FS::pay_batch \%batchhash;
37 %my $error = $new->replace($pay_batch);
38 %die "error updating batch status: $error\n" if $error;
39 %
40 %my $batchtotal=0;
41 %my $batchcount=0;
42 %
43 %my (@date)=localtime($new->download);
44 %my $jdate = sprintf("%03d", $date[5] % 100).sprintf("%03d", $date[7] + 1);
45 %my $cdate = sprintf("%02d", $date[3]).sprintf("%02d", $date[4] + 1).
46 %            sprintf("%02d", $date[5] % 100);
47 %my $sdate = sprintf("%02d", $date[5] % 100).'/'.sprintf("%02d", $date[4] + 1).
48 %            '/'.sprintf("%02d", $date[3]);
49 %
50 %if ($format eq "BoM") {
51 %
52 %  my($origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) =
53 %    $conf->config("batchconfig-$format");
54 %  
55 <% sprintf( "A%10s%04u%06u%05u%54s\n",$origid,$pay_batch->batchnum,$jdate,$datacenter,"").
56         sprintf( "XD%03u%06u%-15s%-30s%09u%-12s   \n",$typecode,$jdate,$shortname,$longname,$mybank,$myacct )
57   %>
58 %
59 %
60 %}elsif ($format eq "PAP"){
61 %
62 %  my($origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) =
63 %    $conf->config("batchconfig-$format");
64 %  
65 <% sprintf( "H%10sD%3s%06u%-15s%09u%-12s%04u%19s\n",$origid,$typecode,$cdate,$shortname,$mybank,$myacct,$pay_batch->batchnum,"")
66
67   %>
68 %
69 %
70 %}elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
71 %#  1;
72 %}elsif ($format eq "csv-chase_canada-E-xactBatch"){
73 %
74 %  my($origid) = $conf->config("batchconfig-$format");
75 <% sprintf( '$$E-xactBatchFileV1.0$$%s:%03u$$%s',$sdate,$pay_batch->batchnum, $origid)
76   %>
77 %
78 %}else{
79 %  die "Unknown format for batch in batchconfig. \n";
80 %}
81 %
82 %
83 %for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
84 %                           qsearch('cust_pay_batch',
85 %                             {'batchnum'=>$pay_batch->batchnum} )
86 %) {
87 %
88 %  $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
89 %  my( $mon, $y ) = ( $2, $1 );
90 %  $mon = "0$mon" if $mon < 10;
91 %  my $exp = "$mon$y";
92 %  $batchcount++;
93 %  $batchtotal += $cust_pay_batch->amount;
94 %  
95 %  if ($format eq "BoM") {
96 %
97 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
98 %    
99 <% sprintf( "D%010.0f%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->paybatchnum) %>
100 %
101 %
102 %  } elsif ($format eq "PAP"){
103 %
104 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
105 %    
106 <% sprintf( "D%-23s%06u%-19s%09u%-12s%010.0f\n",$cust_pay_batch->payname,$cdate,$cust_pay_batch->paybatchnum,$aba,$account,$cust_pay_batch->amount*100) %>
107 %
108 %
109 %  } elsif ($format eq "csv-td_canada_trust-merchant_pc_batch") {
110 %
111 %    
112 ,,,,<% $cust_pay_batch->payinfo %>,<% $exp %>,<% $cust_pay_batch->amount %>,<% $cust_pay_batch->paybatchnum %>
113 %
114 %
115 %  } elsif ($format eq "csv-chase_canada-E-xactBatch"){
116 %
117 %    
118 <% $cust_pay_batch->paybatchnum %>,<% $cust_pay_batch->custnum %>,<% $cust_pay_batch->invnum %>,"<% $cust_pay_batch->payname %>",01,<% $cust_pay_batch->payinfo %>,<% $cust_pay_batch->amount %>,<% $exp %>,,
119 %
120 %
121 %  } else {
122 %    die "I'm already dead, but you did not know that.\n";
123 %  }
124 %
125 %}
126 %
127 %if ($format eq "BoM") {
128 %
129 %  
130 <% sprintf( "YD%08u%014.0f%56s\n",$batchcount,$batchtotal*100,"" ).
131         sprintf( "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","" ) %>
132 %
133 %
134 %} elsif ($format eq "PAP"){
135 %
136 %  
137 <% sprintf( "T%08u%014.0f%57s\n",$batchcount,$batchtotal*100,"" ) %>
138 %
139 %
140 %} elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
141 %  #1;
142 %} elsif ($format eq "csv-chase_canada-E-xactBatch"){
143 %  #1;
144 %} else {
145 %  die "I'm already dead (again), but you did not know that.\n";
146 %}
147 %
148 %$dbh->commit or die $dbh->errstr if $oldAutoCommit;
149 %
150 %
151
152