a181aab570510779ed0fdf20e28bf4360562d5fb
[freeside.git] / httemplate / search / bill_batch.cgi
1 % my $batchnum = $cgi->param('download');
2 % if ( $batchnum =~ /^\d+$/ ) {
3 %   $cgi->delete('download');
4 <HTML>
5 <HEAD><TITLE>Starting download...</TITLE>
6 <SCRIPT TYPE="text/javascript">
7 function refreshParent() {
8   window.top.setTimeout("window.top.location.href = '<% $cgi->self_url %>'", 2000);
9   window.top.location.replace('<%$p%>misc/download-bill_batch.html?<%$batchnum%>');
10 }
11 </SCRIPT>
12 </HEAD><BODY onload="refreshParent();">
13 <& /elements/footer.html &>
14 % }
15 % else {
16 <% include( 'elements/search.html',
17                  'title'         => 'Invoice Batches',
18                  'name_singular' => 'batch',
19                  'query'         => { 'table'     => 'bill_batch',
20                                       'hashref'   => $hashref,
21                                       'extra_sql' => $extra_sql.
22                                         'ORDER BY batchnum DESC',
23                                     },
24                  'count_query'   => "$count_query $extra_sql",
25                  'header'        => [ 'Batch',
26                                       'Item Count',
27                                       'Status',
28                                       '',
29                                     ],
30                  'align'         => 'rrcc',
31                  'fields'        => [ 'batchnum',
32                                       sub {
33                                         my $st = "SELECT COUNT(*) from cust_bill_batch WHERE batchnum=" . shift->batchnum;
34                                         my $sth = dbh->prepare($st)
35                                           or die dbh->errstr. "doing $st";
36                                         $sth->execute
37                                           or die "Error executing \"$st\": ". $sth->errstr;
38                                         $sth->fetchrow_arrayref->[0];
39                                       },
40                                       sub {
41                                         $statusmap{shift->status};
42                                       },
43                                       \&download_link,
44                                     ],
45                  'links'         => [
46                                       $link,
47                                       $link,
48                                       $link,
49                                       '',
50                                     ],
51                  'really_disable_download' => 1,
52       )
53
54 %>
55 %}
56 <%init>
57
58 die "access denied"
59   unless $FS::CurrentUser::CurrentUser->access_right('View invoices');
60
61 my %statusmap = ('O'=>'Open', 'R'=>'Closed');
62 my $hashref = {};
63 my $count_query = 'SELECT COUNT(*) FROM bill_batch';
64
65 my $extra_sql = ''; # may add something here later
66 my $link = [ "${p}view/bill_batch.cgi?batchnum=", 'batchnum' ];
67
68 my $download_id = int(rand(1000000));
69
70 sub download_link {
71   my $batch = shift;
72   my $batchnum = $batch->batchnum;
73   my $close = ($batch->status eq 'O' ? ';close=1' : '');
74   my $html = qq!<FORM NAME="Download$batchnum" STYLE="display:inline">
75   <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">
76   <INPUT TYPE="hidden" NAME="close" VALUE="1">
77   !;
78   $html .= include('/elements/progress-init.html',
79     "Download$batchnum",
80     [ 'batchnum', 'close' ],
81     $p.'misc/process/bill_batch-print.html',
82     { url => $p."search/bill_batch.cgi?download=$batchnum" },
83     "batch$batchnum" #key
84   );
85   $html .= '<A href="#" onclick="batch'.$batchnum.'process();">' .
86   ($batch->status eq 'O' ? '<B>Download and close</B>' : 'Download');
87   $html .= '</A></FORM>';
88   return $html;
89 }
90
91 </%init>