e4882d1e894dd8fbd6e581e873a89f3998875b5b
[freeside.git] / httemplate / misc / download-batch.cgi
1 <% $exporttext %><%init>
2
3 #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
4 http_header('Content-Type' => 'text/plain' ); # not necessarily correct...
5
6 my $batchnum;
7
8 if ( $cgi->param('batchnum') =~ /^(\d+)$/ ) {
9   $batchnum = $1;
10 } else {
11   die "No batch number (bad URL) \n";
12 }
13
14 my %opt;
15 if ( $cgi->param('gatewaynum') =~ /^(\d+)$/ ) {
16   my $gateway = FS::payment_gateway->by_key($1);
17   die "gatewaynum $1 not found" unless $gateway;
18   $opt{'gateway'} = $gateway;
19 }
20 elsif ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
21   $opt{'format'} = $1;
22 }
23
24 my $credit_transactions = "EXISTS (SELECT 1 FROM cust_pay_batch WHERE batchnum = $batchnum AND paycode = 'C') AS arecredits";
25 my $pay_batch = qsearchs({ 'select'    => "*, $credit_transactions",
26                            'table'     => 'pay_batch',
27                            'hashref'   => { batchnum => $batchnum },
28                          });
29 die "Batch not found: '$batchnum'" if !$pay_batch;
30
31 if ($pay_batch->{Hash}->{arecredits}) {
32   my $export_format = "FS::pay_batch::".$opt{'format'};
33   die "You are trying to download a credit (batch refund) batch and The format ".$opt{'format'}." can not handle refunds.\n" unless $export_format->can('can_handle_credits');
34 }
35
36 my $exporttext = $pay_batch->export_batch(%opt);
37 unless ($exporttext) {
38   http_header('Content-Type' => 'text/html' );
39   $exporttext = <<EOF;
40 <SCRIPT>
41 alert('Batch was empty, and has been resolved');
42 window.top.location.href = '${p}search/pay_batch.cgi?magic=_date;open=1;intransit=1;resolved=1';
43 </SCRIPT>
44 EOF
45 }
46
47 </%init>