more DTRT with usage on service transfer between packages and recharges RT #2884...
authorjeff <jeff>
Mon, 23 Mar 2009 23:33:04 +0000 (23:33 +0000)
committerjeff <jeff>
Mon, 23 Mar 2009 23:33:04 +0000 (23:33 +0000)
FS/FS/cust_pkg.pm
FS/FS/part_pkg/flat.pm
FS/FS/svc_acct.pm
httemplate/misc/process/recharge_svc.html

index 7c8656c..f2c3ccc 100644 (file)
@@ -1135,6 +1135,21 @@ sub change {
     return "Unable to transfer all services from package ". $self->pkgnum;
   }
 
+  #reset usage if changing pkgpart
+  if ($self->pkgpart != $cust_pkg->pkgpart) {
+    my $part_pkg = $cust_pkg->part_pkg;
+    $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
+                                                 ? ()
+                                                 : ( 'null' => 1 )
+                                   )
+      if $part_pkg->can('reset_usage');
+
+    if ($error) {
+      $dbh->rollback if $oldAutoCommit;
+      return "Error setting usage values: $error";
+    }
+  }
+
   #Good to go, cancel old package.
   $error = $self->cancel( quiet=>1 );
   if ($error) {
@@ -2712,11 +2727,11 @@ All svc_accts which are part of this package have their values reset.
 =cut
 
 sub set_usage {
-  my ($self, $valueref) = @_;
+  my ($self, $valueref, %opt) = @_;
 
   foreach my $cust_svc ($self->cust_svc){
     my $svc_x = $cust_svc->svc_x;
-    $svc_x->set_usage($valueref)
+    $svc_x->set_usage($valueref, %opt)
       if $svc_x->can("set_usage");
   }
 }
index 8fd0286..009e54c 100644 (file)
@@ -209,7 +209,7 @@ sub reset_usage {
   if ($self->option('usage_rollover', 1)) {
     $cust_pkg->recharge(\%values);
   }else{
-    $cust_pkg->set_usage(\%values);
+    $cust_pkg->set_usage(\%values, %opt);
   }
 }
 
index fcd73ac..c6857e2 100644 (file)
@@ -1821,7 +1821,7 @@ sub _op_usage {
 }
 
 sub set_usage {
-  my( $self, $valueref ) = @_;
+  my( $self, $valueref, %options ) = @_;
 
   warn "$me set_usage called for svcnum ". $self->svcnum.
        ' ('. $self->email. "): ".
@@ -1842,6 +1842,11 @@ sub set_usage {
 
   my $reset = 0;
   my %handyhash = ();
+  if ( $options{null} ) { 
+    %handyhash = ( map { ( $_ => 'NULL', $_."_threshold" => 'NULL' ) }
+                   qw( seconds upbytes downbytes totalbytes )
+                 );
+  }
   foreach my $field (keys %$valueref){
     $reset = 1 if $valueref->{$field};
     $self->setfield($field, $valueref->{$field});
@@ -1860,8 +1865,8 @@ sub set_usage {
   #die $error if $error;         #services not explicity changed via the UI
 
   my $sql = "UPDATE svc_acct SET " .
-    join (',', map { "$_ =  ?" } (keys %handyhash) ).
-    " WHERE svcnum = ?";
+    join (',', map { "$_ =  $handyhash{$_}" } (keys %handyhash) ).
+    " WHERE svcnum = ". $self->svcnum;
 
   warn "$me $sql\n"
     if $DEBUG;
@@ -1869,7 +1874,7 @@ sub set_usage {
   if (scalar(keys %handyhash)) {
     my $sth = $dbh->prepare( $sql )
       or die "Error preparing $sql: ". $dbh->errstr;
-    my $rv = $sth->execute((values %handyhash), $self->svcnum);
+    my $rv = $sth->execute();
     die "Error executing $sql: ". $sth->errstr
       unless defined($rv);
     die "Can't update usage for svcnum ". $self->svcnum
index 8c4d13e..5f68bf1 100755 (executable)
@@ -1,6 +1,6 @@
 %if ($error) {
 %  $cgi->param('error', $error);
-<% cgi->redirect(popurl(2). "recharge_svc.html?". $cgi->query_string ) %>
+<% $cgi->redirect(popurl(2). "recharge_svc.html?". $cgi->query_string ) %>
 %} else {
 <% header("Package recharged") %>
   <SCRIPT TYPE="text/javascript">
@@ -47,7 +47,7 @@ unless ($error) {
     $error = $cust_main->recharge_prepay( $prepaid );
   } elsif ( $payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP)$/ ) {
     my $part_pkg = $svc_acct->cust_svc->cust_pkg->part_pkg;
-    $amount = $part_pkg->option('recharge_amount', 1);
+    my $amount = $part_pkg->option('recharge_amount', 1);
     my %rhash = map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_) }
       grep { $part_pkg->option($_, 1) }
       qw ( recharge_seconds recharge_upbytes recharge_downbytes
@@ -63,7 +63,7 @@ unless ($error) {
                                 $description, $part_pkg->taxclass);
 
     if ($part_pkg->option('recharge_reset', 1)) {
-      $error ||= $svc_acct->set_usage(\%rhash);
+      $error ||= $svc_acct->set_usage(\%rhash, 'null' => 1);
     }else{
       $error ||= $svc_acct->recharge(\%rhash);
     }