rearrange flow of batch download, #947
authormark <mark>
Sat, 30 Jul 2011 23:20:37 +0000 (23:20 +0000)
committermark <mark>
Sat, 30 Jul 2011 23:20:37 +0000 (23:20 +0000)
FS/FS/bill_batch.pm
httemplate/elements/progress-popup.html
httemplate/misc/download-bill_batch.html [new file with mode: 0644]
httemplate/search/bill_batch.cgi
httemplate/view/bill_batch.cgi

index 3afe209..7300f45 100644 (file)
@@ -80,6 +80,7 @@ sub print_pdf {
       die $error if $error;
     }
   }
+  $job->update_statustext(100, 'Combining invoices') if $job;
 
   return $pdf_out->toPDF;
 }
@@ -128,6 +129,11 @@ sub process_print_pdf {
   my $batch = FS::bill_batch->by_key($param->{batchnum});
   die "batch '$param->{batchnum}' not found!\n" if !$batch;
 
+  if ( $param->{'close'} ) {
+    my $error = $batch->close;
+    die $error if $error;
+  }
+
   my $pdf = $batch->print_pdf($job);
   $batch->pdf($pdf);
   my $error = $batch->replace;
index 73440bb..2d27388 100644 (file)
@@ -63,12 +63,13 @@ function updateStatus( status_statustext ) {
 %   }
 
 % } elsif ( $url ) { 
-
+    parent.nd(1);
     window.top.location.href = '<% $url %>';
 % } elsif ( $popup_url ) {
     document.location.replace('<% $popup_url %>');
 % } else { 
 
+
     alert('job done but no url or message specified');
 % } 
   } else if ( status.indexOf('done') > -1 ) {
diff --git a/httemplate/misc/download-bill_batch.html b/httemplate/misc/download-bill_batch.html
new file mode 100644 (file)
index 0000000..00d9236
--- /dev/null
@@ -0,0 +1,20 @@
+<%init>
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('View invoices');
+my ($batchnum) = $cgi->keywords;
+$batchnum =~ /^\d+$/ or die "invalid batchnum '$batchnum'";
+my $batch = FS::bill_batch->by_key($batchnum)
+  or die "Batch $batchnum not found";
+
+# send the batch
+my $content = $batch->pdf;
+$m->clear_buffer;
+$r->content_type('application/pdf');
+$r->headers_out->add( 'Content-Disposition' => 
+  'attachment;filename="invoice_batch_'.$batchnum.'.pdf"');
+$m->print($content);
+
+$batch->pdf('');
+my $error = $batch->replace;
+warn "error deleting cached PDF: '$error'\n" if $error;
+</%init>
index 72e5d3a..a181aab 100755 (executable)
@@ -1,3 +1,18 @@
+% my $batchnum = $cgi->param('download');
+% if ( $batchnum =~ /^\d+$/ ) {
+%   $cgi->delete('download');
+<HTML>
+<HEAD><TITLE>Starting download...</TITLE>
+<SCRIPT TYPE="text/javascript">
+function refreshParent() {
+  window.top.setTimeout("window.top.location.href = '<% $cgi->self_url %>'", 2000);
+  window.top.location.replace('<%$p%>misc/download-bill_batch.html?<%$batchnum%>');
+}
+</SCRIPT>
+</HEAD><BODY onload="refreshParent();">
+<& /elements/footer.html &>
+% }
+% else {
 <% include( 'elements/search.html',
                  'title'         => 'Invoice Batches',
                 'name_singular' => 'batch',
                                      sub {
                                        $statusmap{shift->status};
                                      },
-                                      sub { shift->status eq 'O' ? 
-                                            'Download and close' : 'Download' 
-                                      },
+                                      \&download_link,
                                    ],
                 'links'         => [
                                       $link,
                                       $link,
                                       $link,
-                                      $dlink,
-                                    ],
-                'style'         => [
-                                     '',
-                                      '',
                                       '',
-                                     sub { shift->status eq 'O' ? "b" : '' },
-                                   ],
+                                    ],
                  'really_disable_download' => 1,
       )
 
 %>
+%}
 <%init>
 
 die "access denied"
@@ -56,10 +64,28 @@ my $count_query = 'SELECT COUNT(*) FROM bill_batch';
 
 my $extra_sql = ''; # may add something here later
 my $link = [ "${p}view/bill_batch.cgi?batchnum=", 'batchnum' ];
-my $dlink = sub {
-  [ "${p}view/bill_batch.cgi?start_download=1;".
-      (shift->status eq 'O' ? 'close=1;' : '').
-      'batchnum=',
-    'batchnum'] 
-};
+
+my $download_id = int(rand(1000000));
+
+sub download_link {
+  my $batch = shift;
+  my $batchnum = $batch->batchnum;
+  my $close = ($batch->status eq 'O' ? ';close=1' : '');
+  my $html = qq!<FORM NAME="Download$batchnum" STYLE="display:inline">
+  <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">
+  <INPUT TYPE="hidden" NAME="close" VALUE="1">
+  !;
+  $html .= include('/elements/progress-init.html',
+    "Download$batchnum",
+    [ 'batchnum', 'close' ],
+    $p.'misc/process/bill_batch-print.html',
+    { url => $p."search/bill_batch.cgi?download=$batchnum" },
+    "batch$batchnum" #key
+  );
+  $html .= '<A href="#" onclick="batch'.$batchnum.'process();">' .
+  ($batch->status eq 'O' ? '<B>Download and close</B>' : 'Download');
+  $html .= '</A></FORM>';
+  return $html;
+}
+
 </%init>
index c848664..7d64039 100644 (file)
@@ -1,39 +1,8 @@
-% if($magic eq 'download') {
-%   my $content = $batch->pdf;
-%   $batch->pdf('');
-%   my $error = $batch->replace;
-%   warn "error deleting cached PDF: '$error'\n" if $error;
-%
-%   $m->clear_buffer;
-%   $r->content_type('application/pdf');
-%   $r->headers_out->add('Content-Disposition' => 'attachment;filename="invoice_batch_'.$batchnum.'.pdf"');
-<% $content %>
-% }
-%
-% elsif ($magic eq 'download_popup') {
-%
-<& /elements/header-popup.html,
-  { 'etc' => 'BGCOLOR="#ccccff"' } &>
-<SCRIPT type="text/javascript">
-function start() {
-window.open('<% $cgi->self_url . ';magic=download' %>');
-parent.nd(1);
-}
-</SCRIPT>
-<TABLE WIDTH="100%"><TR><TD STYLE="text-align:center;vertical-align:middle">
-<FONT SIZE="+1">
-<A HREF="javascript:start()">Download batch #<% $batchnum %></A>
-</FONT>
-</TD></TR></TABLE>
-<& /elements/footer.html &>
-%
-% }
-%
-% else {
 <% include('/search/elements/search.html', 
               'title'     => $close ?
                               "Batch $batchnum closed." :
                               "Invoice Batch $batchnum",
+              'menubar'   => ['All batches' => $p.'search/bill_batch.cgi'],
               'name'      => 'invoices',
               'query'     => { 'table'   => 'cust_bill_batch',
                                'select'  => join(', ',
@@ -49,7 +18,6 @@ parent.nd(1);
                              },
               'count_query' => "SELECT COUNT(*) FROM cust_bill_batch WHERE batchnum = $batchnum",
               'html_init' => $html_init,
-              'html_foot' => $html_foot,
               'header'    => [ 'Invoice #',
                                'Amount',
                                'Date',
@@ -67,7 +35,6 @@ parent.nd(1);
                              ],
               'really_disable_download' => 1,
 ) %>
-% }
 <%init>
 
 die "access denied"
@@ -80,38 +47,23 @@ my $batchnum = $cgi->param('batchnum');
 $batch = FS::bill_batch->by_key($batchnum);
 die "Batch '$batchnum' not found!\n" if !$batch;
 
-my $magic = $cgi->param('magic');
-$cgi->delete('magic');
-
 my $close = $cgi->param('close');
 $batch->close if $close;
 
-my $html_init = '';
-my $html_foot = '';
-if ( !$magic ) {
-  $html_init .= qq!<FORM NAME="OneTrueForm">
-    <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">!;
-  $html_init .= include('/elements/progress-init.html',
-                  'OneTrueForm',
-                  [ 'batchnum' ],
-                  $p.'misc/process/bill_batch-print.html',
-                  {
-                    'popup_url' => $cgi->self_url . ';magic=download_popup',
-                  },
-                  '',
-  );
-  $html_init .= '</FORM>
-<A HREF="javascript:process()">Download this batch</A></BR>';
-  if ( $batch->status eq 'O' ) {
-    $cgi->param('close' => 1);
-    $html_init .= '<A HREF="'.$cgi->self_url.'">Close this batch</A><BR>';
-  }
-  $html_init .= '<BR>';
-  if ( $cgi->param('start_download') ) {
-    $cgi->delete('start_download');
-    $html_foot = '<SCRIPT TYPE="text/javascript">process();</SCRIPT>';
-  }
+my $html_init = qq!<FORM NAME="OneTrueForm">
+  <INPUT TYPE="hidden" NAME="batchnum" VALUE="$batchnum">! .
+  include('/elements/progress-init.html',
+    'OneTrueForm',
+    [ 'batchnum' ],
+    $p.'misc/process/bill_batch-print.html',
+    { url => $p.'misc/download-bill_batch.html?'.$batchnum }
+  ) .
+  '<A HREF="#" onclick="process();">Download this batch</A></FORM><BR>';
+if ( $batch->status eq 'O' ) {
+  $cgi->param('close' => 1);
+  $html_init .= '<A HREF="'.$cgi->self_url.'">Close this batch</A><BR>';
 }
+$html_init .= '<BR>';
 
 my $link = [ "$p/view/cust_bill.cgi?", 'invnum' ];
 my $clink = [ "$p/view/cust_main.cgi?", 'custnum' ];