Optimize "Customer has a referring customer" condition, RT#74452
[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 if ( $cgi->param('batchnum') =~ /^(\d+)$/ ) {
8   $batchnum = $1;
9 } else {
10   die "No batch number (bad URL) \n";
11 }
12
13 my %opt;
14 if ( $cgi->param('gatewaynum') =~ /^(\d+)$/ ) {
15   my $gateway = FS::payment_gateway->by_key($1);
16   die "gatewaynum $1 not found" unless $gateway;
17   $opt{'gateway'} = $gateway;
18 }
19 elsif ( $cgi->param('format') =~ /^([\w\- ]+)$/ ) {
20   $opt{'format'} = $1;
21 }
22
23 my $credit_transactions = "EXISTS (SELECT 1 FROM cust_pay_batch WHERE batchnum = $batchnum AND paycode = 'C') AS arecredits";
24 my $pay_batch = qsearchs({ 'select'    => "*, $credit_transactions",
25                            'table'     => 'pay_batch',
26                            'hashref'   => { batchnum => $batchnum },
27                          });
28 die "Batch not found: '$batchnum'" if !$pay_batch;
29
30 if ($pay_batch->{Hash}->{arecredits}) {
31   my $export_format = "FS::pay_batch::".$opt{'format'};
32     die "This format can not handle refunds." unless $export_format->can('can_handle_credits');
33 }
34
35 my $exporttext = $pay_batch->export_batch(%opt);
36 unless ($exporttext) {
37   http_header('Content-Type' => 'text/html' );
38   $exporttext = <<EOF;
39 <SCRIPT>
40 alert('Batch was empty, and has been resolved');
41 window.top.location.href = '${p}search/pay_batch.cgi?magic=_date;open=1;intransit=1;resolved=1';
42 </SCRIPT>
43 EOF
44 }
45
46 </%init>