usage suspend vs admin suspend -- avoid actual cust_pkg::suspend except legacy cases
authorjeff <jeff>
Wed, 11 Apr 2007 04:28:22 +0000 (04:28 +0000)
committerjeff <jeff>
Wed, 11 Apr 2007 04:28:22 +0000 (04:28 +0000)
FS/FS/Schema.pm
FS/FS/cust_pkg.pm
FS/FS/cust_svc.pm
FS/FS/svc_Common.pm
FS/FS/svc_acct.pm
httemplate/view/cust_main/packages.html

index 591793b..c71bb01 100644 (file)
@@ -725,6 +725,7 @@ sub tables_hashref {
         'svcnum',    'serial',    '',   '', '', '', 
         'pkgnum',    'int',    'NULL',   '', '', '', 
         'svcpart',   'int',    '',   '', '', '', 
+        'overlimit', @date_type, '', '', 
       ],
       'primary_key' => 'svcnum',
       'unique' => [],
index 3e36ae2..616a480 100644 (file)
@@ -799,6 +799,19 @@ sub cust_svc {
 
 }
 
+=item overlimit [ SVCPART ]
+
+Returns the services for this package which have exceeded their
+usage limit as FS::cust_svc objects (see L<FS::cust_svc>).  If a svcpart
+is specified, return only the matching services.
+
+=cut
+
+sub overlimit {
+  my $self = shift;
+  grep { $_->overlimit } $self->cust_svc;
+}
+
 =item h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] 
 
 Returns historical services for this package created before END TIMESTAMP and
index cdb34cd..81d7b17 100644 (file)
@@ -68,6 +68,8 @@ The following fields are currently supported:
 
 =item svcpart - Service definition (see L<FS::part_svc>)
 
+=item overlimit - date the service exceeded its usage limit
+
 =back
 
 =head1 METHODS
@@ -154,6 +156,54 @@ sub cancel {
 
 }
 
+=item overlimit [ ACTION ]
+
+Retrieves or sets the overlimit date.  If ACTION is absent, return
+the present value of overlimit.  If ACTION is present, it can
+have the value 'suspend' or 'unsuspend'.  In the case of 'suspend' overlimit
+is set to the current time if it is not already set.  The 'unsuspend' value
+causes the time to be cleared.  
+
+If there is an error on setting, returns the error, otherwise returns false.
+
+=cut
+
+sub overlimit {
+  my $self = shift;
+  my $action = shift or return $self->getfield('overlimit');
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE'; 
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  if ( $action eq 'suspend' ) {
+    $self->setfield('overlimit', time) unless $self->getfield('overlimit');
+  }elsif ( $action eq 'unsuspend' ) {
+    $self->setfield('overlimit', '');
+  }else{
+    die "unexpected action value: $action";
+  }
+
+  local $ignore_quantity = 1;
+  my $error = $self->replace;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return "Error setting overlimit: $error";
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  ''; #no errors
+
+}
+
 =item replace OLD_RECORD
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
@@ -175,6 +225,8 @@ sub replace {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
+  $old = $new->replace_old unless defined($old);
+  
   if ( $new->svcpart != $old->svcpart ) {
     my $svc_x = $new->svc_x;
     my $new_svc_x = ref($svc_x)->new({$svc_x->hash, svcpart=>$new->svcpart });
@@ -212,6 +264,7 @@ sub check {
     $self->ut_numbern('svcnum')
     || $self->ut_numbern('pkgnum')
     || $self->ut_number('svcpart')
+    || $self->ut_numbern('overlimit')
   ;
   return $error if $error;
 
index f60a7d9..52226d2 100644 (file)
@@ -735,6 +735,17 @@ sub export {
 
 }
 
+=item overlimit
+
+Sets or retrieves overlimit date.
+
+=cut
+
+sub overlimit {
+  my $self = shift;
+  $self->cust_svc->overlimit(@_);
+}
+
 =item cancel
 
 Stub - returns false (no error) so derived classes don't need to define this
index cb3f38b..1ef2119 100644 (file)
@@ -1590,7 +1590,7 @@ sub _op_usage {
 
   if ( &{$op2condition{$op}}($self, $column, $amount) ) {
     foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
-      if ($part_export->option('overlimit_groups')) {
+      if ($part_export->option('overlimit_groups'), 1) {
         my ($new,$old);
         my $other = new FS::svc_acct $self->hashref;
         my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
@@ -1602,6 +1602,7 @@ sub _op_usage {
           $new = $self; $old = $other;
         }
         my $error = $part_export->export_replace($new, $old);
+        $error ||= $self->overlimit($action);
         if ( $error ) {
           $dbh->rollback if $oldAutoCommit;
           return "Error replacing radius groups in export, ${op}: $error";
@@ -1614,6 +1615,7 @@ sub _op_usage {
        && &{$op2condition{$op}}($self, $column, $amount)    ) {
     #my $error = $self->$action();
     my $error = $self->cust_svc->cust_pkg->$action();
+    $error ||= $self->overlimit($action);
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "Error ${action}ing: $error";
@@ -1707,8 +1709,22 @@ sub set_usage {
       if $rv == 0;
   }
 
-  if ( $conf->exists("svc_acct-usage_unsuspend") && $reset ) {
-    my $error = $self->cust_svc->cust_pkg->unsuspend;
+  if ( $reset ) {
+    my $error = $self->overlimit('unsuspend');
+
+    foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
+      if ($part_export->option('overlimit_groups'), 1) {
+        my $old = new FS::svc_acct $self->hashref;
+        my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
+                       ($self, $part_export->option('overlimit_groups'));
+        $old->usergroup( $groups );
+        $error ||= $part_export->export_replace($self, $old);
+      }
+    }
+
+    if ( $conf->exists("svc_acct-usage_unsuspend")) {
+      $error ||= $self->cust_svc->cust_pkg->unsuspend;
+    }
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "Error unsuspending: $error";
index b54208b..c569046 100755 (executable)
@@ -270,12 +270,15 @@ Current packages
           <% pkg_datestr($cust_pkg, 'setup', $conf) %>
         </TR>
 % } else { 
-
-
+% if (scalar($cust_pkg->overlimit)) {
+        <TR>
+          <TD COLSPAN=<%$colspan%>><FONT COLOR="#FFD000"><B>Overlimit</B></FONT>,&nbsp;<% $billed_or_prepaid %>&nbsp;<% myfreq($part_pkg) %></TD>
+        </TR>
+%}else{
         <TR>
           <TD COLSPAN=<%$colspan%>><FONT COLOR="#00CC00"><B>Active</B></FONT>,&nbsp;<% $billed_or_prepaid %>&nbsp;<% myfreq($part_pkg) %></TD>
         </TR>
-
+% } 
         <TR>
           <TD WIDTH="<%$width%>" ALIGN="right">Setup&nbsp;</TD>
           <% pkg_datestr($cust_pkg, 'setup', $conf) %>
@@ -347,6 +350,13 @@ Current packages
       </TR>
 
       <TR>
+        <TD ALIGN="right" COLSPAN="2" VALIGN="top" STYLE="padding-bottom:1px;padding-top:0px"><FONT SIZE="-2" COLOR="#FFD000">
+
+            <% $cust_svc->overlimit ? "Overlimit: ". time2str('%b %o %Y' . ($conf->exists('cust_pkg-display_times') ? ' %l:%M %P' : ''), $cust_svc->overlimit) : '' %>
+          </FONT></TD>
+      </TR>
+
+      <TR>
         <TD ALIGN="right" VALIGN="top" STYLE="padding-bottom:5px;padding-top:0px"><FONT SIZE="-2">
 
 %         if ( $curuser->access_right('Recharge customer service')