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