s/printf/sprintf/ and make the config a little less strange
[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 $format;
9 if ( $cgi->param('format') =~ /^([\w ]+)$/ ) {
10   $format = $1;
11 } else {
12   $format = $conf->config('batch_default_format');
13 }
14
15 my $oldAutoCommit = $FS::UID::AutoCommit;
16 local $FS::UID::AutoCommit = 0;
17 my $dbh = dbh;
18
19 my $pay_batch = qsearchs('pay_batch', {'status'=>''} );
20 die "No pending batch. \n" unless $pay_batch;
21
22 my %batchhash = $pay_batch->hash;
23 $batchhash{'status'} = 'I';
24 my $new = new FS::pay_batch \%batchhash;
25 my $error = $new->replace($pay_batch);
26 die "error updating batch status: $error\n" if $error;
27
28 my $batchtotal=0;
29 my $batchcount=0;
30
31 my (@date)=localtime();
32 my $jdate = sprintf("%03d", $date[5] % 100).sprintf("%03d", $date[7]);
33
34 if ($format eq "BoM") {
35
36   my($reformat,$origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) = $conf->config("batchconfig-$format");
37   %><%= sprintf( "A%10s%04u%06u%05u%54s\n",$origid,$pay_batch->batchnum,$jdate,$datacenter,"").
38         sprintf( "XD%03u%06u%-15s%-30s%09u%-12s   \n",$typecode,$jdate,$shortname,$longname,$mybank,$myacct )
39   %><%
40
41 }elsif ($format eq "CSV file for TD Canada Trust Merchant PC Batch"){
42 #  1;
43 }else{
44   die "Unknown format for batch in batchconfig. \n";
45 }
46
47
48 for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
49                            qsearch('cust_pay_batch',
50                               {'batchnum'=>$pay_batch->batchnum} )
51 ) {
52
53   $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
54   my( $mon, $y ) = ( $2, $1 );
55   $mon = "0$mon" if $mon < 10;
56   my $exp = "$mon$y";
57   $batchcount++;
58   $batchtotal += $cust_pay_batch->amount;
59   
60   if ($format eq "BoM") {
61
62     my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
63     %><%= sprintf( "D%010u%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->invnum %><%
64
65   } elsif ($format eq "CSV file for TD Canada Trust Merchant PC Batch") {
66
67     %>,,,,<%= $cust_pay_batch->payinfo %>,<%= $exp %>,<%= $cust_pay_batch->amount %>,<%= $cust_pay_batch->paybatchnum %><%
68
69   } else {
70     die "I'm already dead, but you did not know that.\n";
71   }
72
73 }
74
75 if ($format eq "BoM") {
76
77   %><%= sprintf( "YD%08u%014u%56s\n",$batchcount,$batchtotal*100,"" ).
78         sprintf( "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","" ) %><%
79
80 } elsif ($format eq "CSV file for TD Canada Trust Merchant PC Batch"){
81   #1;
82 } else {
83   die "I'm already dead (again), but you did not know that.\n";
84 }
85
86 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
87
88 %>
89