rearrange flow of batch download, #947
[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'  => 'ORDER BY batchnum DESC',
23                                     },
24                  'count_query'   => $count_query,
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                  'agent_virt' => 1,
53                  'agent_null_right' => [ 'Process global invoice batches', 'Configuration' ],
54                  'agent_pos' => 1,
55
56       )
57
58 %>
59 %}
60 <%init>
61
62 my $curuser = $FS::CurrentUser::CurrentUser;
63
64 die "access denied"
65   unless $curuser->access_right('Process invoice batches')
66       || $curuser->access_right('Process global invoice batches')
67       || $curuser->access_right('Configuration'); #remove in 2.5
68
69 my %statusmap = ('O'=>'Open', 'R'=>'Closed');
70 my $hashref = {};
71 my $count_query = "SELECT COUNT(*) FROM bill_batch WHERE". # $extra_sql AND "
72                     $curuser->agentnums_sql(
73                       'null_right' => ['Process global invoice batches', 'Configuration' ],
74                     );
75
76 #my $extra_sql = ''; # may add something here later
77 my $link = [ "${p}view/bill_batch.cgi?batchnum=", 'batchnum' ];
78
79 my $download_id = int(rand(1000000));
80
81 sub download_link {
82   my $batch = shift;
83   my $batchnum = $batch->batchnum;
84   my $close = ($batch->status eq 'O' ? ';close=1' : '');
85   my $html = qq!<FORM NAME="Download$batchnum" STYLE="display:inline">
86   <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">
87   <INPUT TYPE="hidden" NAME="close" VALUE="1">
88   !;
89   $html .= include('/elements/progress-init.html',
90     "Download$batchnum",
91     [ 'batchnum', 'close' ],
92     $p.'misc/process/bill_batch-print.html',
93     { url => $p."search/bill_batch.cgi?download=$batchnum" },
94     "batch$batchnum" #key
95   );
96   $html .= '<A href="#" onclick="batch'.$batchnum.'process();">' .
97   ($batch->status eq 'O' ? '<B>Download and close</B>' : 'Download');
98   $html .= '</A></FORM>';
99   return $html;
100 }
101
102 </%init>