upon first download, have batches auto-set amount to customer balance if it is smaller
[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 %}else{
27 %  die "Unknown format for batch in batchconfig. \n";
28 %}
29 %
30 %
31 %for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
32 %                           qsearch('cust_pay_batch',
33 %                             {'batchnum'=>$pay_batch->batchnum} )
34 %) {
35 %
36 %  $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
37 %  my( $mon, $y ) = ( $2, $1 );
38 %  if ( $conf->exists('batch-increment_expiration') ) {
39 %    my( $curmon, $curyear ) = (localtime(time))[4,5];
40 %    $curmon++; $curyear-=100;
41 %    $y++ while $y < $curyear || ( $y == $curyear && $mon < $curmon );
42 %  }
43 %  $mon = "0$mon" if $mon =~ /^\d$/;
44 %  $y = "0$y" if $y =~ /^\d$/;
45 %  my $exp = "$mon$y";
46 %
47 %  if ( $first_download ) {
48 %    my $balance = $cust_pay_batch->cust_main->balance;
49 %    if ( $balance <= 0 ) {
50 %      my $error = $cust_pay_batch->delete;
51 %      if ( $error ) {
52 %        $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
53 %        die $error;
54 %      }
55 %      next;
56 %    } elsif ( $balance < $cust_pay_batch->amount ) {
57 %      $cust_pay_batch->amount($balance);
58 %      my $error = $cust_pay_batch->replace;
59 %      if ( $error ) {
60 %        $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
61 %        die $error;
62 %      }
63 %    #} elsif ( $balance > $cust_pay_batch->amount ) {
64 %    } 
65 %  }
66 %
67 %  $batchcount++;
68 %  $batchtotal += $cust_pay_batch->amount;
69 %  
70 %  if ($format eq "BoM") {
71 %
72 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
73 %    
74 <% sprintf( "D%010.0f%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->paybatchnum) %>
75 %
76 %
77 %  } elsif ($format eq "PAP"){
78 %
79 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
80 %    
81 <% 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) %>
82 %
83 %
84 %  } elsif ($format eq "csv-td_canada_trust-merchant_pc_batch") {
85 %
86 %    
87 ,,,,<% $cust_pay_batch->payinfo %>,<% $exp %>,<% $cust_pay_batch->amount %>,<% $cust_pay_batch->paybatchnum %>
88 %
89 %
90 %  } elsif ($format eq "csv-chase_canada-E-xactBatch"){
91 %
92 %  my $payname=$cust_pay_batch->payname; $payname =~ tr/",/  /; #payinfo too? :P
93 <% $cust_pay_batch->paybatchnum %>,<% $cust_pay_batch->custnum %>,<% $cust_pay_batch->invnum %>,"<% $payname %>",00,<% $cust_pay_batch->payinfo %>,<% $cust_pay_batch->amount %>,<% $exp %>,,
94 %
95 %
96 %  } else {
97 %    die "I'm already dead, but you did not know that.\n";
98 %  }
99 %
100 %}
101 %
102 %if ($format eq "BoM") {
103 %
104 %  
105 <% sprintf( "YD%08u%014.0f%56s\n",$batchcount,$batchtotal*100,"" ).
106         sprintf( "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","" ) %>
107 %
108 %
109 %} elsif ($format eq "PAP"){
110 %
111 %  
112 <% sprintf( "T%08u%014.0f%57s\n",$batchcount,$batchtotal*100,"" ) %>
113 %
114 %
115 %} elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
116 %  #1;
117 %} elsif ($format eq "csv-chase_canada-E-xactBatch"){
118 %  #1;
119 %} else {
120 %  die "I'm already dead (again), but you did not know that.\n";
121 %}
122 %
123 %$dbh->commit or die $dbh->errstr if $oldAutoCommit;
124 <%init>
125
126 my $conf=new FS::Conf;
127
128 #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
129 http_header('Content-Type' => 'text/plain' );
130
131 my $batchnum;
132 if ( $cgi->param('batchnum') =~ /^(\d+)$/ ) {
133   $batchnum = $1;
134 } else {
135   die "No batch number (bad URL) \n";
136 }
137
138 my $format;
139 if ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
140   $format = $1;
141 } else {
142   $format = $conf->config('batch-default_format');
143 }
144
145 my $oldAutoCommit = $FS::UID::AutoCommit;
146 local $FS::UID::AutoCommit = 0;
147 my $dbh = dbh;
148
149 my $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'O'} );
150 my $first_download = 1;
151 unless ($pay_batch) {
152   $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'I'} )
153     if $FS::CurrentUser::CurrentUser->access_right('Reprocess batches');
154   $first_download = 0;
155 }
156 die "No pending batch. \n" unless $pay_batch;
157
158 my $error = $pay_batch->set_status('I');
159 die "error updating batch status: $error\n" if $error;
160
161 my $batchtotal=0;
162 my $batchcount=0;
163
164 my (@date)=localtime($pay_batch->download);
165 my $jdate = sprintf("%03d", $date[5] % 100).sprintf("%03d", $date[7] + 1);
166 my $cdate = sprintf("%02d", $date[3]).sprintf("%02d", $date[4] + 1).
167             sprintf("%02d", $date[5] % 100);
168 my $sdate = sprintf("%02d", $date[5] % 100).'/'.sprintf("%02d", $date[4] + 1).
169             '/'.sprintf("%02d", $date[3]);
170
171 </%init>