1609 correct spiritone ACH
[freeside.git] / httemplate / misc / download-batch.cgi
1 %if ($format eq "BoM") {
2 %
3 %  my($origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) =
4 %    $conf->config("batchconfig-$format");
5 %  
6 <% sprintf( "A%10s%04u%06u%05u%54s\n",$origid,$pay_batch->batchnum,$jdate,$datacenter,"").
7         sprintf( "XD%03u%06u%-15s%-30s%09u%-12s   \n",$typecode,$jdate,$shortname,$longname,$mybank,$myacct )
8   %>
9 %
10 %}elsif ($format eq "PAP"){
11 %
12 %  my($origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) =
13 %    $conf->config("batchconfig-$format");
14 %  
15 <% sprintf( "H%10sD%3s%06u%-15s%09u%-12s%04u%19s\n",$origid,$typecode,$cdate,$shortname,$mybank,$myacct,$pay_batch->batchnum,"") %>
16 %
17 %
18 %}elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
19 %#  1;
20 %}elsif ($format eq "csv-chase_canada-E-xactBatch"){
21 %
22 %  my($origid) = $conf->config("batchconfig-$format");
23 <% sprintf( '$$E-xactBatchFileV1.0$$%s:%03u$$%s',$sdate,$pay_batch->batchnum, $origid)
24   %>
25 %
26 %}elsif ($format eq "ach-spiritone"){
27 %#  1;
28 %}else{
29 %  die "Unknown format for batch in batchconfig. \n";
30 %}
31 %
32 %
33 %for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
34 %                           qsearch('cust_pay_batch',
35 %                             {'batchnum'=>$pay_batch->batchnum} )
36 %) {
37 %
38 %  $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
39 %  my( $mon, $y ) = ( $2, $1 );
40 %  if ( $conf->exists('batch-increment_expiration') ) {
41 %    my( $curmon, $curyear ) = (localtime(time))[4,5];
42 %    $curmon++; $curyear-=100;
43 %    $y++ while $y < $curyear || ( $y == $curyear && $mon < $curmon );
44 %  }
45 %  $mon = "0$mon" if $mon =~ /^\d$/;
46 %  $y = "0$y" if $y =~ /^\d$/;
47 %  my $exp = "$mon$y";
48 %
49 %  if ( $first_download ) {
50 %    my $balance = $cust_pay_batch->cust_main->balance;
51 %    if ( $balance <= 0 ) {
52 %      my $error = $cust_pay_batch->delete;
53 %      if ( $error ) {
54 %        $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
55 %        die $error;
56 %      }
57 %      next;
58 %    } elsif ( $balance < $cust_pay_batch->amount ) {
59 %      $cust_pay_batch->amount($balance);
60 %      my $error = $cust_pay_batch->replace;
61 %      if ( $error ) {
62 %        $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
63 %        die $error;
64 %      }
65 %    #} elsif ( $balance > $cust_pay_batch->amount ) {
66 %    } 
67 %  }
68 %
69 %  $batchcount++;
70 %  $batchtotal += $cust_pay_batch->amount;
71 %  
72 %  if ($format eq "BoM") {
73 %
74 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
75 %    
76 <% sprintf( "D%010.0f%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->paybatchnum) %>
77 %
78 %
79 %  } elsif ($format eq "PAP"){
80 %
81 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
82 %    
83 <% 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) %>
84 %
85 %
86 %  } elsif ($format eq "csv-td_canada_trust-merchant_pc_batch") {
87 %
88 %    
89 ,,,,<% $cust_pay_batch->payinfo %>,<% $exp %>,<% $cust_pay_batch->amount %>,<% $cust_pay_batch->paybatchnum %>
90 %
91 %
92 %  } elsif ($format eq "csv-chase_canada-E-xactBatch"){
93 %
94 %  my $payname=$cust_pay_batch->payname; $payname =~ tr/",/  /; #payinfo too? :P
95 <% $cust_pay_batch->paybatchnum %>,<% $cust_pay_batch->custnum %>,<% $cust_pay_batch->invnum %>,"<% $payname %>",00,<% $cust_pay_batch->payinfo %>,<% $cust_pay_batch->amount %>,<% $exp %>,,
96 %
97 %
98 %  }elsif ($format eq "ach-spiritone"){
99 %
100 %  my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
101 %  my $payname=$cust_pay_batch->first. " ". $cust_pay_batch->last;
102 %  $payname =~ tr/",/  /;                                        #payinfo too?
103 %  my $batchline = qq!"$payname","!.$cust_pay_batch->paybatchnum.
104 %                  qq!","$aba","$account","27","!.$cust_pay_batch->amount.
105 %                  qq!","27","0.00"!;
106 %  push @batchlines, $batchline;
107 <% $batchline %>
108 %
109 %  } else {
110 %    die "I'm already dead, but you did not know that.\n";
111 %  }
112 %
113 %}
114 %
115 %if ($format eq "BoM") {
116 %
117 %  
118 <% sprintf( "YD%08u%014.0f%56s\n",$batchcount,$batchtotal*100,"" ).
119         sprintf( "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","" ) %>
120 %
121 %
122 %} elsif ($format eq "PAP"){
123 %
124 %  
125 <% sprintf( "T%08u%014.0f%57s\n",$batchcount,$batchtotal*100,"" ) %>
126 %
127 %
128 %} elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
129 %  #1;
130 %} elsif ($format eq "csv-chase_canada-E-xactBatch"){
131 %  #1;
132 %} elsif ($format eq "ach-spiritone"){
133 %  #1;
134 %} else {
135 %  die "I'm already dead (again), but you did not know that.\n";
136 %}
137 %
138 <%init>
139
140 my $conf=new FS::Conf;
141
142 #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
143 http_header('Content-Type' => 'text/plain' );
144
145 my $batchnum;
146 if ( $cgi->param('batchnum') =~ /^(\d+)$/ ) {
147   $batchnum = $1;
148 } else {
149   die "No batch number (bad URL) \n";
150 }
151
152 my $format;
153 if ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
154   $format = $1;
155 } else {
156   $format = $conf->config('batch-default_format');
157 }
158
159 my $autopost;
160 if ( $format eq 'ach-spiritone' ) {
161   $autopost = 1;
162 }else{
163   $autopost = 0;
164 }
165
166 my $oldAutoCommit = $FS::UID::AutoCommit;
167 local $FS::UID::AutoCommit = 0;
168 my $dbh = dbh;
169
170 my $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'O'} );
171 my $first_download = 1;
172 unless ($pay_batch) {
173   $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'I'} )
174     if $FS::CurrentUser::CurrentUser->access_right('Reprocess batches');
175   $first_download = 0;
176 }
177 die "No pending batch. \n" unless $pay_batch;
178
179 my $error = $pay_batch->set_status('I');
180 die "error updating batch status: $error\n" if $error;
181
182 my $batchtotal=0;
183 my $batchcount=0;
184
185 my (@date)=localtime($pay_batch->download);
186 my $jdate = sprintf("%03d", $date[5] % 100).sprintf("%03d", $date[7] + 1);
187 my $cdate = sprintf("%02d", $date[3]).sprintf("%02d", $date[4] + 1).
188             sprintf("%02d", $date[5] % 100);
189 my $sdate = sprintf("%02d", $date[5] % 100).'/'.sprintf("%02d", $date[4] + 1).
190             '/'.sprintf("%02d", $date[3]);
191
192 my @batchlines = ();
193 </%init>
194 <%cleanup>
195 if ($autopost) {
196   my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
197   my $fh = new File::Temp(
198     TEMPLATE => 'paybatch.'. $batchnum .'.XXXXXXXX',
199     DIR      => $dir,
200   ) or die "can't open temp file: $!\n";
201
202   print $fh map{ "$_\n" } @batchlines;
203   seek $fh, 0, 0;
204
205   $error = $pay_batch->import_results( 'filehandle' => $fh,
206                                        'format'     => $format,
207                                      );
208   die $error if $error;
209 }
210
211 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
212
213 </%cleanup>