RT#947: batch download of invoice PDFs
[freeside.git] / httemplate / view / bill_batch.cgi
1 % if($magic eq 'print') {
2 <% include('/elements/header.html', "Download Batch") %>
3 <FORM NAME="OneTrueForm">
4 <INPUT TYPE="hidden" NAME="batchnum" VALUE="<% $batchnum %>">
5 % $cgi->delete('magic');
6 <% include('/elements/progress-init.html',
7               'OneTrueForm',
8               [ 'batchnum' ],
9               $p.'misc/process/bill_batch-print.html',
10               {'url' => $cgi->self_url . ';magic=download'},
11               '',
12 ) %></FORM>
13 <SCRIPT TYPE="text/javascript">process();</SCRIPT>
14 <% include('/elements/footer.html') %>
15 % }
16
17 % elsif($magic eq 'download') {
18 %   $m->clear_buffer;
19 %   $r->content_type('application/pdf');
20 %   $r->headers_out->add('Content-Disposition' => 'attachment;filename="invoice_batch_'.$batchnum.'.pdf"');
21 <% $batch->pdf %>
22 %   $batch->pdf('');
23 %   my $error = $batch->replace;
24 %   warn "error deleting cached PDF: '$error'\n" if $error;
25 % }
26 % else {
27 <% include('/search/elements/search.html', 
28               'title'     => $close ?
29                               "Batch $batchnum closed." :
30                               "Invoice Batch $batchnum",
31               'name'      => 'invoices',
32               'query'     => { 'table'   => 'cust_bill_batch',
33                                'select'  => join(', ',
34                                           'cust_bill.*',
35                                           FS::UI::Web::cust_sql_fields(),
36                                           'cust_main.custnum AS cust_main_custnum',
37                                 ),
38                                'hashref' => { },
39                                'addl_from' => 
40                                  'LEFT JOIN cust_bill USING ( invnum ) '.
41                                  'LEFT JOIN cust_main USING ( custnum )',
42                                'extra_sql' => '',
43                                  " WHERE batchnum = $batchnum",
44                              },
45               'count_query' => "SELECT COUNT(*) FROM cust_bill_batch WHERE batchnum = $batchnum",
46               'html_init' => $html_init,
47               'header'    => [ 'Invoice #',
48                                'Amount',
49                                'Date',
50                                'Customer',
51                              ],
52               'fields'    => [ sub { shift->cust_bill->display_invnum },
53                                sub { sprintf($money_char.'%.2f', 
54                                       shift->cust_bill->charged ) },
55                                sub { time2str('%b %d %Y', 
56                                       shift->cust_bill->_date ) },
57                                sub { shift->cust_bill->cust_main->name },
58                              ],
59               'align'     => 'rrll',
60               'links'     => [ ($link) x 3, $clink,
61                              ],
62               'really_disable_download' => 1,
63 ) %>
64 % }
65 <%init>
66
67 die "access denied"
68   unless $FS::CurrentUser::CurrentUser->access_right('View invoices');
69
70 use Data::Dumper;
71 warn Dumper($cgi->Vars);
72
73 my $conf = new FS::Conf;
74 my $batch;
75 my $batchnum = $cgi->param('batchnum');
76
77 $batch = FS::bill_batch->by_key($batchnum);
78 die "Batch '$batchnum' not found!\n" if !$batch;
79
80 my $magic = $cgi->param('magic');
81 my $html_init = '';
82
83 my $close = $cgi->param('close');
84 $batch->close if $close;
85
86 if(!$magic) {
87   $cgi->param('magic' => 'print');
88   $cgi->delete('close');
89   $html_init  = '<A HREF="'.$cgi->self_url.'">Download this batch</A><BR>';
90   if($batch->status eq 'O') {
91     $cgi->param('close' => 1);
92     $cgi->delete('magic');
93     $html_init .= '<A HREF="'.$cgi->self_url.'">Close this batch</A><BR>';
94   }
95   $html_init .= '<BR>';
96 }
97
98 my $link = [ "$p/view/cust_bill.cgi?", 'invnum' ];
99 my $clink = [ "$p/view/cust_main.cgi?", 'custnum' ];
100 my $money_char = $conf->config('money_char') || '$';
101
102 </%init>