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

my $conf=new FS::Conf;

#http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
http_header('Content-Type' => 'text/plain' );

my $batchnum;
if ( $cgi->param('batchnum') =~ /^(\d+)$/ ) {
  $batchnum = $1;
} else {
  die "No batch number (bad URL) \n";
}

my $format;
if ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
  $format = $1;
} else {
  $format = $conf->config('batch-default_format');
}

my $autopost;
if ( $format eq 'ach-spiritone' ) {
  $autopost = 1;
}else{
  $autopost = 0;
}

my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;

my $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'O'} );
my $first_download = 1;
unless ($pay_batch) {
  $pay_batch = qsearchs('pay_batch', {'batchnum'=>$batchnum, 'status'=>'I'} )
    if $FS::CurrentUser::CurrentUser->access_right('Reprocess batches');
  $first_download = 0;
}
die "No pending batch. \n" unless $pay_batch;

my $error = $pay_batch->set_status('I');
die "error updating batch status: $error\n" if $error;

my $batchtotal=0;
my $batchcount=0;

my (@date)=localtime($pay_batch->download);
my $jdate = sprintf("%03d", $date[5] % 100).sprintf("%03d", $date[7] + 1);
my $cdate = sprintf("%02d", $date[3]).sprintf("%02d", $date[4] + 1).
            sprintf("%02d", $date[5] % 100);
my $sdate = sprintf("%02d", $date[5] % 100).'/'.sprintf("%02d", $date[4] + 1).
            '/'.sprintf("%02d", $date[3]);

my @batchlines = ();
</%init>
<%cleanup>
if ($autopost) {
  my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
  my $fh = new File::Temp(
    TEMPLATE => 'paybatch.'. $batchnum .'.XXXXXXXX',
    DIR      => $dir,
  ) or die "can't open temp file: $!\n";

  print $fh map{ "$_\n" } @batchlines;
  seek $fh, 0, 0;

  $error = $pay_batch->import_results( 'filehandle' => $fh,
                                       'format'     => $format,
                                     );
  die $error if $error;
}

$dbh->commit or die $dbh->errstr if $oldAutoCommit;

</%cleanup>