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