reliably start batch download, #947
[freeside.git] / httemplate / search / bill_batch.cgi
1 % my $batchnum = $cgi->param('download');
2 % if ( $batchnum =~ /^\d+$/ ) {
3 %   my $download = $p."misc/download-bill_batch.html?$batchnum";
4 <HTML>
5 <HEAD><TITLE>Starting download...</TITLE>
6 <SCRIPT TYPE="text/javascript">
7 function start() {
8   window.location.replace('<% $download %>');
9 }
10 </SCRIPT>
11 <!--[if lte IE 7]>
12 <SCRIPT TYPE="text/javascript">function start() {}</SCRIPT>
13 <![endif]-->
14 </HEAD>
15 <BODY onload="start()" STYLE="background-color:#ccccff">
16 <TABLE STYLE="height:125px; width:100%; text-align:center"><TR><TD STYLE="vertical-align:middle;text-align:center">
17 <A HREF="<% $download %>">Click here if your download does not start</A>
18 </TD></TR></TABLE>
19 <& /elements/footer.html &>
20 % }
21 % else {
22 %# delete existing download cookie
23 %   my $cookie = CGI::Cookie->new(
24 %     -name => 'bill_batch_download',
25 %     -value => 0,
26 %     -expires => '-1d',
27 %   );
28 %   $r->headers_out->add( 'Set-Cookie' => $cookie->as_string );
29 <% include( 'elements/search.html',
30                  'title'         => 'Invoice Batches',
31                  'name_singular' => 'batch',
32                  'query'         => { 'table'     => 'bill_batch',
33                                       'hashref'   => $hashref,
34                                       'extra_sql' => $extra_sql.
35                                         'ORDER BY batchnum DESC',
36                                     },
37                  'count_query'   => "$count_query $extra_sql",
38                  'header'        => [ 'Batch',
39                                       'Item Count',
40                                       'Status',
41                                       '',
42                                     ],
43                  'align'         => 'rrcc',
44                  'fields'        => [ 'batchnum',
45                                       sub {
46                                         my $st = "SELECT COUNT(*) from cust_bill_batch WHERE batchnum=" . shift->batchnum;
47                                         my $sth = dbh->prepare($st)
48                                           or die dbh->errstr. "doing $st";
49                                         $sth->execute
50                                           or die "Error executing \"$st\": ". $sth->errstr;
51                                         $sth->fetchrow_arrayref->[0];
52                                       },
53                                       sub {
54                                         $statusmap{shift->status};
55                                       },
56                                       \&download_link,
57                                     ],
58                  'links'         => [
59                                       $link,
60                                       $link,
61                                       $link,
62                                       '',
63                                     ],
64                  'really_disable_download' => 1,
65                  'html_foot'     => include('.foot'),
66       )
67
68 %>
69 %}
70 <%def .foot>
71 <SCRIPT type="text/javascript">
72 var timer;
73 function checkDownloadStatus(batchnum) {
74   var re = new RegExp('bill_batch_download=' + batchnum);
75   if ( re.test(document.cookie) ) {
76     window.clearInterval(timer);
77     window.location.reload();
78   }
79 }
80 function startBatch(batchnum) {
81   timer = window.setInterval(function() {
82       checkDownloadStatus(batchnum);
83     }, 2000);
84   eval('batch'+batchnum+'process()');
85 }
86 </SCRIPT>
87 </%def>
88 <%init>
89
90 die "access denied"
91   unless $FS::CurrentUser::CurrentUser->access_right('View invoices');
92
93 my %statusmap = ('O'=>'Open', 'R'=>'Closed');
94 my $hashref = {};
95 my $count_query = 'SELECT COUNT(*) FROM bill_batch';
96
97 my $extra_sql = ''; # may add something here later
98 my $link = [ "${p}view/bill_batch.cgi?batchnum=", 'batchnum' ];
99
100
101 sub download_link {
102   my $batch = shift;
103   my $batchnum = $batch->batchnum;
104   my $close = ($batch->status eq 'O' ? ';close=1' : '');
105   my $html = qq!<FORM NAME="Download$batchnum" STYLE="display:inline">
106   <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">
107   <INPUT TYPE="hidden" NAME="close" VALUE="1">
108   !;
109   $html .= include('/elements/progress-init.html',
110     "Download$batchnum",
111     [ 'batchnum', 'close' ],
112     $p.'misc/process/bill_batch-print.html',
113     { popup_url => $p."search/bill_batch.cgi?download=$batchnum" },
114     "batch$batchnum" #key
115   );
116   $html .= '<A href="#" onclick="startBatch('.$batchnum.');">' .
117   ($batch->status eq 'O' ? '<B>Download and close</B>' : 'Download');
118   $html .= '</A></FORM>';
119   return $html;
120 }
121
122 </%init>