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

index 198e31b..6c38900 100644 (file)
@@ -2153,6 +2153,22 @@ sub order {
       $dbh->rollback if $oldAutoCommit;
       return "Unable to transfer all services from package ".$old_pkg->pkgnum;
     }
+
+    #reset usage if changing pkgpart
+    if ($old_pkg->pkgpart != $new_pkg->pkgpart) {
+      my $part_pkg = $new_pkg->part_pkg;
+      $error = $part_pkg->reset_usage($new_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";
+      }
+    }
+
     $error = $old_pkg->cancel( quiet=>1 );
     if ($error) {
       $dbh->rollback;
@@ -2298,11 +2314,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 01ea5d4..ca398c6 100644 (file)
@@ -186,15 +186,21 @@ sub is_prepaid {
   0; #no, we're postpaid
 }
 
-sub reset_usage {
-  my($self, $cust_pkg) = @_;
-  my %values = map { $_, $self->option($_) } 
+sub usage_valuehash {
+  my $self = shift;
+  map { $_, $self->option($_) }
     grep { $self->option($_, 'hush') } 
     qw(seconds upbytes downbytes totalbytes);
+}
+
+sub reset_usage {
+  my($self, $cust_pkg, %opt) = @_;
+  warn "    resetting usage counters" if $opt{debug} > 1;
+  my %values = $self->usage_valuehash; 
   if ($self->option('usage_rollover', 1)) {
     $cust_pkg->recharge(\%values);
   }else{
-    $cust_pkg->set_usage(\%values);
+    $cust_pkg->set_usage(\%values, %opt);
   }
 }
 
index 1a9e8f2..4250db4 100644 (file)
@@ -26,6 +26,7 @@ use FS::Conf;
 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
 use FS::Msgcat qw(gettext);
 use FS::UI::bytecount;
+use FS::part_pkg;
 use FS::svc_Common;
 use FS::cust_svc;
 use FS::part_svc;
@@ -290,6 +291,7 @@ sub table_info {
                          type  => 'text',
                          disable_inventory => 1,
                          disable_select => 1,
+                         disable_part_svc_column => 1,
                        },
         'upbytes'   => { label => 'Upload',
                          type  => 'text',
@@ -297,6 +299,7 @@ sub table_info {
                          disable_select => 1,
                          'format' => \&FS::UI::bytecount::display_bytecount,
                          'parse' => \&FS::UI::bytecount::parse_bytecount,
+                         disable_part_svc_column => 1,
                        },
         'downbytes' => { label => 'Download',
                          type  => 'text',
@@ -304,6 +307,7 @@ sub table_info {
                          disable_select => 1,
                          'format' => \&FS::UI::bytecount::display_bytecount,
                          'parse' => \&FS::UI::bytecount::parse_bytecount,
+                         disable_part_svc_column => 1,
                        },
         'totalbytes'=> { label => 'Total up and download',
                          type  => 'text',
@@ -311,11 +315,13 @@ sub table_info {
                          disable_select => 1,
                          'format' => \&FS::UI::bytecount::display_bytecount,
                          'parse' => \&FS::UI::bytecount::parse_bytecount,
+                         disable_part_svc_column => 1,
                        },
         'seconds_threshold'   => { label => 'Seconds threshold',
                                    type  => 'text',
                                    disable_inventory => 1,
                                    disable_select => 1,
+                                   disable_part_svc_column => 1,
                                  },
         'upbytes_threshold'   => { label => 'Upload threshold',
                                    type  => 'text',
@@ -323,6 +329,7 @@ sub table_info {
                                    disable_select => 1,
                                    'format' => \&FS::UI::bytecount::display_bytecount,
                                    'parse' => \&FS::UI::bytecount::parse_bytecount,
+                                   disable_part_svc_column => 1,
                                  },
         'downbytes_threshold' => { label => 'Download threshold',
                                    type  => 'text',
@@ -330,6 +337,7 @@ sub table_info {
                                    disable_select => 1,
                                    'format' => \&FS::UI::bytecount::display_bytecount,
                                    'parse' => \&FS::UI::bytecount::parse_bytecount,
+                                   disable_part_svc_column => 1,
                                  },
         'totalbytes_threshold'=> { label => 'Total up and download threshold',
                                    type  => 'text',
@@ -337,6 +345,7 @@ sub table_info {
                                    disable_select => 1,
                                    'format' => \&FS::UI::bytecount::display_bytecount,
                                    'parse' => \&FS::UI::bytecount::parse_bytecount,
+                                   disable_part_svc_column => 1,
                                  },
         'last_login'=>           {
                                    label     => 'Last login',
@@ -535,6 +544,26 @@ sub insert {
     return $error;
   }
 
+  # set usage fields and thresholds if unset but set in a package def
+  if ( $self->pkgnum ) {
+    my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
+    my $part_pkg = $cust_pkg->part_pkg if $cust_pkg;
+    if ( $part_pkg && $part_pkg->can('usage_valuehash') ) {
+
+      my %values = $part_pkg->usage_valuehash;
+      my $multiplier = $conf->exists('svc_acct-usage_threshold') 
+                         ? 1 - $conf->config('svc_acct-usage_threshold')/100
+                         : 0.20;
+
+      foreach ( keys %values ) {
+        next if $self->getfield($_);
+        $self->setfield( $_, $values{$_} );
+        $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) );
+      }
+
+    }
+  }
+
   my @jobnums;
   $error = $self->SUPER::insert(
     'jobnums'       => \@jobnums,
@@ -1720,7 +1749,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. "): ".
@@ -1741,6 +1770,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});
@@ -1759,8 +1793,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;
@@ -1768,7 +1802,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 e0fb615..deb3c80 100755 (executable)
@@ -131,7 +131,14 @@ that field.
 %
 %      #yucky kludge
 %      my @fields = defined( dbdef->table($layer) )
-%                      ? grep { $_ ne 'svcnum' } fields($layer)
+%                      ? grep {
+%                               $_ ne 'svcnum' &&
+%                               ( !FS::part_svc->svc_table_fields($layer)
+%                                     ->{$_}->{disable_part_svc_column}   ||
+%                                 $part_svc->part_svc_column($_)->columnflag
+%                               )
+%                             }
+%                             fields($layer)
 %                      : ();
 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
index 147b953..4b7155f 100755 (executable)
@@ -27,7 +27,7 @@
 %                                $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);
 %    }