batch refactor continued
[freeside.git] / httemplate / misc / download-batch.cgi
1 %
2 %my $conf=new FS::Conf;
3 %
4 %#http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
5 %http_header('Content-Type' => 'text/plain' );
6 %
7 %my $format;
8 %if ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
9 %  $format = $1;
10 %} else {
11 %  $format = $conf->config('batch-default_format');
12 %}
13 %
14 %my $oldAutoCommit = $FS::UID::AutoCommit;
15 %local $FS::UID::AutoCommit = 0;
16 %my $dbh = dbh;
17 %
18 %my $pay_batch = qsearchs('pay_batch', {'status'=>''} );
19 %die "No pending batch. \n" unless $pay_batch;
20 %
21 %my %batchhash = $pay_batch->hash;
22 %$batchhash{'status'} = 'I';
23 %my $new = new FS::pay_batch \%batchhash;
24 %my $error = $new->replace($pay_batch);
25 %die "error updating batch status: $error\n" if $error;
26 %
27 %my $batchtotal=0;
28 %my $batchcount=0;
29 %
30 %my (@date)=localtime();
31 %my $jdate = sprintf("%03d", $date[5] % 100).sprintf("%03d", $date[7]);
32 %
33 %if ($format eq "BoM") {
34 %
35 %  my($origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) =
36 %    $conf->config("batchconfig-$format");
37 %  
38 <% sprintf( "A%10s%04u%06u%05u%54s\n",$origid,$pay_batch->batchnum,$jdate,$datacenter,"").
39         sprintf( "XD%03u%06u%-15s%-30s%09u%-12s   \n",$typecode,$jdate,$shortname,$longname,$mybank,$myacct )
40   %>
41 %
42 %
43 %}elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
44 %#  1;
45 %}else{
46 %  die "Unknown format for batch in batchconfig. \n";
47 %}
48 %
49 %
50 %for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
51 %                           qsearch('cust_pay_batch',
52 %                             {'batchnum'=>$pay_batch->batchnum} )
53 %) {
54 %
55 %  $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
56 %  my( $mon, $y ) = ( $2, $1 );
57 %  $mon = "0$mon" if $mon < 10;
58 %  my $exp = "$mon$y";
59 %  $batchcount++;
60 %  $batchtotal += $cust_pay_batch->amount;
61 %  
62 %  if ($format eq "BoM") {
63 %
64 %    my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
65 %    
66 <% sprintf( "D%010u%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->invnum %>
67 %
68 %
69 %  } elsif ($format eq "csv-td_canada_trust-merchant_pc_batch") {
70 %
71 %    
72 ,,,,<% $cust_pay_batch->payinfo %>,<% $exp %>,<% $cust_pay_batch->amount %>,<% $cust_pay_batch->paybatchnum %>
73 %
74 %
75 %  } else {
76 %    die "I'm already dead, but you did not know that.\n";
77 %  }
78 %
79 %}
80 %
81 %if ($format eq "BoM") {
82 %
83 %  
84 <% sprintf( "YD%08u%014u%56s\n",$batchcount,$batchtotal*100,"" ).
85         sprintf( "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","" ) %>
86 %
87 %
88 %} elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
89 %  #1;
90 %} else {
91 %  die "I'm already dead (again), but you did not know that.\n";
92 %}
93 %
94 %$dbh->commit or die $dbh->errstr if $oldAutoCommit;
95 %
96 %
97
98