value issues and many bits remain
[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'=>'O'} );
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($origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) =
37     $conf->config("batchconfig-$format");
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 }elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
43 #  1;
44 }else{
45   die "Unknown format for batch in batchconfig. \n";
46 }
47
48
49 for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
50                            qsearch('cust_pay_batch',
51                               {'batchnum'=>$pay_batch->batchnum} )
52 ) {
53
54   $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
55   my( $mon, $y ) = ( $2, $1 );
56   $mon = "0$mon" if $mon < 10;
57   my $exp = "$mon$y";
58   $batchcount++;
59   $batchtotal += $cust_pay_batch->amount;
60   
61   if ($format eq "BoM") {
62
63     my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
64     %><%= sprintf( "D%010u%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->invnum %><%
65
66   } elsif ($format eq "csv-td_canada_trust-merchant_pc_batch") {
67
68     %>,,,,<%= $cust_pay_batch->payinfo %>,<%= $exp %>,<%= $cust_pay_batch->amount %>,<%= $cust_pay_batch->paybatchnum %><%
69
70   } else {
71     die "I'm already dead, but you did not know that.\n";
72   }
73
74 }
75
76 if ($format eq "BoM") {
77
78   %><%= sprintf( "YD%08u%014u%56s\n",$batchcount,$batchtotal*100,"" ).
79         sprintf( "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","" ) %><%
80
81 } elsif ($format eq "csv-td_canada_trust-merchant_pc_batch"){
82   #1;
83 } else {
84   die "I'm already dead (again), but you did not know that.\n";
85 }
86
87 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
88
89 %>
90