multi-select for agent on RADIUS usage report, RT#73050
[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 <& 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'  => 'ORDER BY batchnum DESC',
36                                     },
37                  'count_query'   => $count_query,
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                  'agent_virt' => 1,
66                  'agent_null_right' => [ 'Process global invoice batches', 'Configuration' ],
67                  'agent_pos' => 1,
68                  'html_foot' => include('.foot'),
69
70 &>
71 %}
72 <%def .foot>
73 <SCRIPT type="text/javascript">
74 var timer;
75 function checkDownloadStatus(batchnum) {
76   var re = new RegExp('bill_batch_download=' + batchnum);
77   if ( re.test(document.cookie) ) {
78     window.clearInterval(timer);
79     window.location.reload();
80   }
81 }
82 function startBatch(batchnum) {
83   timer = window.setInterval(function() { 
84       checkDownloadStatus(batchnum);
85   }, 2000);
86   eval('batch'+batchnum+'process()');
87 }
88 </SCRIPT>
89 </%def>
90 <%init>
91
92 my $curuser = $FS::CurrentUser::CurrentUser;
93
94 die "access denied"
95   unless $curuser->access_right('Process invoice batches')
96       || $curuser->access_right('Process global invoice batches')
97       || $curuser->access_right('Configuration'); #remove in 2.5
98
99 my %statusmap = ('O'=>'Open', 'R'=>'Closed');
100 my $hashref = {};
101 my $count_query = "SELECT COUNT(*) FROM bill_batch WHERE". # $extra_sql AND "
102                     $curuser->agentnums_sql(
103                       'null_right' => ['Process global invoice batches', 'Configuration' ],
104                     );
105
106 #my $extra_sql = ''; # may add something here later
107 my $link = [ "${p}view/bill_batch.cgi?batchnum=", 'batchnum' ];
108
109 sub download_link {
110   my $batch = shift;
111   my $batchnum = $batch->batchnum;
112   my $close = ($batch->status eq 'O' ? ';close=1' : '');
113   my $html = qq!<FORM NAME="Download$batchnum" STYLE="display:inline">
114   <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">
115   <INPUT TYPE="hidden" NAME="close" VALUE="1">
116   !;
117   $html .= include('/elements/progress-init.html',
118     "Download$batchnum",
119     [ 'batchnum', 'close' ],
120     $p.'misc/process/bill_batch-print.html',
121     { popup_url => $p."search/bill_batch.cgi?download=$batchnum" },
122     "batch$batchnum" #key
123   );
124   $html .= '<A href="#" onclick="startBatch('.$batchnum.');">' .
125   ($batch->status eq 'O' ? '<B>Download and close</B>' : 'Download');
126   $html .= '</A></FORM>';
127   return $html;
128 }
129
130 </%init>