"Bill now" link uses job queue/progressbar, RT#8995
authormark <mark>
Tue, 6 Jul 2010 12:18:11 +0000 (12:18 +0000)
committermark <mark>
Tue, 6 Jul 2010 12:18:11 +0000 (12:18 +0000)
FS/FS/cust_main.pm
httemplate/elements/bill.html [new file with mode: 0644]
httemplate/elements/progress-init.html
httemplate/misc/bill.cgi
httemplate/view/cust_main/billing.html

index d585b0d..1f69bd1 100644 (file)
@@ -16,6 +16,8 @@ use Exporter;
 use Scalar::Util qw( blessed );
 use List::Util qw( min );
 use Time::Local qw(timelocal);
+use Storable qw(thaw);
+use MIME::Base64;
 use Data::Dumper;
 use Tie::IxHash;
 use Digest::MD5 qw(md5_base64);
@@ -2552,6 +2554,10 @@ Any other true value causes errors to die.
 
 Debugging level.  Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
 
+=item job
+
+Optional FS::queue entry to receive status updates.
+
 =back
 
 Options are passed to the B<bill> and B<collect> methods verbatim, so all
@@ -2568,7 +2574,9 @@ sub bill_and_collect {
   #pre-printing invoices
 
   $options{'actual_time'} ||= time;
+  my $job = $options{'job'};
 
+  $job->update_statustext('0,cleaning expired packages') if $job;
   $error = $self->cancel_expired_pkgs( $options{actual_time} );
   if ( $error ) {
     $error = "Error expiring custnum ". $self->custnum. ": $error";
@@ -2585,6 +2593,7 @@ sub bill_and_collect {
     else                                                     { warn   $error; }
   }
 
+  $job->update_statustext('20,billing packages') if $job;
   $error = $self->bill( %options );
   if ( $error ) {
     $error = "Error billing custnum ". $self->custnum. ": $error";
@@ -2593,6 +2602,7 @@ sub bill_and_collect {
     else                                                     { warn   $error; }
   }
 
+  $job->update_statustext('50,applying payments and credits') if $job;
   $error = $self->apply_payments_and_credits;
   if ( $error ) {
     $error = "Error applying custnum ". $self->custnum. ": $error";
@@ -2601,6 +2611,7 @@ sub bill_and_collect {
     else                                                     { warn   $error; }
   }
 
+  $job->update_statustext('70,running collection events') if $job;
   unless ( $conf->exists('cancelled_cust-noevents')
            && ! $self->num_ncancelled_pkgs
   ) {
@@ -2612,6 +2623,7 @@ sub bill_and_collect {
       else                                                   { warn   $error; }
     }
   }
+  $job->update_statustext('100,finished') if $job;
 
   '';
 
@@ -8017,9 +8029,6 @@ sub email_search_result {
   return '';
 }
 
-use Storable qw(thaw);
-use Data::Dumper;
-use MIME::Base64;
 sub process_email_search_result {
   my $job = shift;
   #warn "$me process_re_X $method for job $job\n" if $DEBUG;
@@ -8955,6 +8964,18 @@ sub queued_bill {
   $cust_main->bill_and_collect( %args );
 }
 
+sub process_bill_and_collect {
+  my $job = shift;
+  my $param = thaw(decode_base64(shift));
+  my $cust_main = qsearchs( 'cust_main', { custnum => $param->{'custnum'} } )
+      or die "custnum '$param->{custnum}' not found!\n";
+  $param->{'job'}   = $job;
+  $param->{'fatal'} = 1; # runs from job queue, will be caught
+  $param->{'retry'} = 1;
+
+  $cust_main->bill_and_collect( %$param );
+}
+
 sub _upgrade_data { #class method
   my ($class, %opts) = @_;
 
diff --git a/httemplate/elements/bill.html b/httemplate/elements/bill.html
new file mode 100644 (file)
index 0000000..335779a
--- /dev/null
@@ -0,0 +1,49 @@
+<%doc>
+Clickable link to bill a customer.
+
+Example:
+<FORM name="MyForm">
+<% include( '/elements/bill.html',
+            ###
+            # required
+            ###
+            custnum   => $custnum,
+            label     => 'Bill Now!',
+            formname  => 'MyForm',
+
+            ###
+            # recommended
+            ###
+            url       => $p.'view/cust_main.cgi?'.$custnum,
+
+            ###
+            # optional, can contain any FS::cust_main::bill_and_collect options
+            ###
+            bill_opts => { 'batch_card' => 'yes' },
+) %>
+</FORM>
+</%doc>
+<% include('/elements/progress-init.html',
+          $formname,
+          [ 'custnum', @opt_keys ],
+          $p.'misc/bill.cgi',
+          $url ? { url => $url } : { message => $message },
+          $key,
+) %>
+<A HREF="javascript:void(0);" onclick="javascript:<%$key%>process();"><%$label%></A>
+<INPUT TYPE="hidden" NAME="custnum" VALUE="<%$custnum%>">
+% foreach(@opt_keys) {
+<INPUT TYPE="hidden" NAME="<%$_%>" VALUE="<%$bill_opts->{$_}%>">
+% }
+<%init>
+my %opt = @_;
+my $custnum   = $opt{'custnum'};
+my $label     = $opt{'label'};
+my $formname  = $opt{'formname'};
+my $key       = $formname.'bill'.$custnum;
+my $url       = $opt{'url'} || '';
+my $message   = $opt{'message'} || 'Finished!';
+my $bill_opts = $opt{'bill_opts'} || {};
+my @opt_keys  = keys(%$bill_opts);
+my @opt_vals  = values(%$bill_opts);
+</%init>
index 20eb9bf..8b8da66 100644 (file)
@@ -11,7 +11,7 @@ In misc/something.html:
              $p.'misc/process_something.html',
              { url => $p.'where_to_go_next.html' },
          #or { message => 'Finished!' },
-         );
+         ) %>
   </FORM>
   <SCRIPT TYPE="text/javascript>process();</SCRIPT>
 
@@ -36,11 +36,9 @@ sub process_whatever { #class method
   do_phase3;
   $job->update_statustext(60);
   # etc.
-  return 'BLAH BLAH NOBODY WILL EVER SEE THIS RETURN VALUE';
+  return 'this value will be ignored';
 }
 
-I am not responsible for errors in the above documentation.
-
 </%doc>
 <% include('/elements/xmlhttp.html',
               'method' => 'POST',
index 6151dce..2bc43d7 100755 (executable)
@@ -1,38 +1,8 @@
-%if ( $error ) {
-%  errorpage($error);
-%} else {
-<% $cgi->redirect(popurl(2). "view/cust_main.cgi?$custnum") %>
-%}
+<% $server->process %>
 <%init>
 
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Bill customer now');
-
-#untaint custnum
-my($query) = $cgi->keywords;
-$query =~ /^(\d*)$/;
-my $custnum = $1;
-my $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
-die "Can't find customer!\n" unless $cust_main;
-
-my $conf = new FS::Conf;
-
-my $error = $cust_main->bill_and_collect( 'fatal' => 'return',
-                                          'retry' => 'yes',
-                                        );
-
-                                  #'invoice-time'=>$time,
-                                  #'batch_card'=> 'yes',
-                                  #'batch_card'=> 'no',
-                                  #'report_badcard'=> 'yes',
-                                  #'retry_card' => 'yes',
-
-                                  #this is used only by cust_main::batch_card
-                                  #need to pick & create an actual config
-                                  #value if we're going to turn this on
-                                  #("realtime-backend" doesn't exist,
-                                  # "backend-realtime" is for something
-                                  #  entirely different)
-                                  #'realtime' => $conf->exists('realtime-backend'),
-
+my $server = FS::UI::Web::JSRPC->new('FS::cust_main::process_bill_and_collect', $cgi);
 </%init>
+
index c8d0c47..6fe6c49 100644 (file)
@@ -1,3 +1,4 @@
+<FORM name="billnow">
 Billing information
 %# If we can't see the unencrypted card, then bill now is an exercise in
 %# frustration (without some sort of job queue magic to send it to a secure
@@ -5,7 +6,13 @@ Billing information
 %if (  $FS::CurrentUser::CurrentUser->access_right('Bill customer now')
 %      && ! $cust_main->is_encrypted($cust_main->payinfo)
 %   ) { 
-  (<A HREF="<% $p %>misc/bill.cgi?<% $cust_main->custnum %>">Bill now</A>)
+%#  (<A HREF="<% $p %>misc/bill.cgi?<% $cust_main->custnum %>">Bill now</A>)
+  (<% include('/elements/bill.html',
+                custnum   => $cust_main->custnum,
+                label     => 'Bill now',
+                formname  => 'billnow',
+                url       => $p.'view/cust_main.cgi?'.$cust_main->custnum,
+  ) %>)
 % } 
 
 <% ntable("#cccccc") %><TR><TD><% ntable("#cccccc",2) %>