first stab at BoM download
[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 #need default
9 my $formatconfig = "batchconfig".$cgi->param('format');
10
11 die "No batch configuration exists.\n$formatconfig\n" unless $conf->exists($formatconfig);
12 my $format = $conf->config($formatconfig);
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   my($reformat,$origid,$datacenter,$typecode,$shortname,$longname,$mybank,$myacct) = $conf->config('batchconfig');
35   printf "A%10s%04u%06u%05u%54s\n",$origid,$pay_batch->batchnum,$jdate,$datacenter,"";
36   printf "XD%03u%06u%-15s%-30s%09u%-12s   \n",$typecode,$jdate,$shortname,$longname,$mybank,$myacct;
37 }elsif ($format eq "CSV file for TD Canada Trust Merchant PC Batch"){
38   1;
39 }else{
40   die "Unknown format for batch in batchconfig. \n";
41 }
42
43
44 for my $cust_pay_batch ( sort { $a->paybatchnum <=> $b->paybatchnum }
45                            qsearch('cust_pay_batch',
46                               {'batchnum'=>$pay_batch->batchnum} )
47 ) {
48
49 $cust_pay_batch->exp =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
50 my( $mon, $y ) = ( $2, $1 );
51 $mon = "0$mon" if $mon < 10;
52 my $exp = "$mon$y";
53 $batchcount++;
54 $batchtotal += $cust_pay_batch->amount;
55
56 if ($format eq "BoM") {
57   my( $account, $aba ) = split( '@', $cust_pay_batch->payinfo );
58   printf "D%010u%09u%-12s%-29s%-19s\n",$cust_pay_batch->amount*100,$aba,$account,$cust_pay_batch->payname,$cust_pay_batch->invnum;
59 }elsif ($format eq "CSV file for TD Canada Trust Merchant PC Batch"){
60 %>,,,,<%= $cust_pay_batch->payinfo %>,<%= $exp %>,<%= $cust_pay_batch->amount %>,<%= $cust_pay_batch->paybatchnum %>
61 <% }else{
62    die "I'm already dead, but you did not know that.\n";
63 }
64
65 }
66
67 if ($format eq "BoM") {
68   printf "YD%08u%014u%56s\n",$batchcount,$batchtotal*100,"";
69   printf "Z%014u%05u%014u%05u%41s\n",$batchtotal*100,$batchcount,"0","0","";
70 }elsif ($format eq "CSV file for TD Canada Trust Merchant PC Batch"){
71   1;
72 } else{
73   die "I'm already dead (again), but you did not know that.\n";
74 }
75
76 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
77
78 %>
79