Cancel services in a particular order to get around certain inter-service dependancies
authorkhoff <khoff>
Sat, 27 Mar 2004 01:05:33 +0000 (01:05 +0000)
committerkhoff <khoff>
Sat, 27 Mar 2004 01:05:33 +0000 (01:05 +0000)
FS/FS/Conf.pm
FS/FS/cust_pkg.pm
FS/FS/svc_acct.pm
FS/FS/svc_domain.pm

index ecd2a97..e6a970c 100644 (file)
@@ -1171,6 +1171,12 @@ httemplate/docs/config.html
     'description' => 'Allow negative charges.  Normally not used unless importing data from a legacy system that requires this.',
     'type'        => 'checkbox',
   },
+  {
+      'key'         => 'auto_unset_catchall',
+      'section'     => '',
+      'description' => 'When canceling a svc_acct that is the email catchall for one or more svc_domains, automatically set their catchall fields to null.  If this option is not set, the attempt will simply fail.',
+      'type'        => 'checkbox',
+  },
 
   {
     'key'         => 'system_usernames',
index d60e95b..ccd73ac 100644 (file)
@@ -1,7 +1,7 @@
 package FS::cust_pkg;
 
 use strict;
-use vars qw(@ISA $disable_agentcheck $DEBUG);
+use vars qw(@ISA $disable_agentcheck @SVCDB_CANCEL_SEQ $DEBUG);
 use FS::UID qw( getotaker dbh );
 use FS::Record qw( qsearch qsearchs );
 use FS::Misc qw( send_email );
@@ -29,6 +29,14 @@ $DEBUG = 0;
 
 $disable_agentcheck = 0;
 
+# The order in which to unprovision services.
+@SVCDB_CANCEL_SEQ = qw( svc_external
+                       svc_www
+                       svc_forward 
+                       svc_acct 
+                       svc_domain 
+                       svc_broadband );
+
 sub _cache {
   my $self = shift;
   my ( $hashref, $cache ) = @_;
@@ -284,16 +292,22 @@ sub cancel {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
+  my %svc;
   foreach my $cust_svc (
-    qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
+      qsearch( 'cust_svc', { 'pkgnum' => $self->pkgnum } )
   ) {
-    my $error = $cust_svc->cancel;
+    push @{ $svc{$cust_svc->part_svc->svcdb} }, $cust_svc;
+  }
 
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "Error cancelling cust_svc: $error";
-    }
+  foreach my $svcdb (@SVCDB_CANCEL_SEQ) {
+    foreach my $cust_svc (@{ $svc{$svcdb} }) {
+      my $error = $cust_svc->cancel;
 
+      if ( $error ) {
+       $dbh->rollback if $oldAutoCommit;
+       return "Error cancelling cust_svc: $error";
+      }
+    }
   }
 
   unless ( $self->getfield('cancel') ) {
index 4ea5252..100af6c 100644 (file)
@@ -674,10 +674,36 @@ sub unsuspend {
 
 =item cancel
 
-Just returns false (no error) for now.
-
 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
 
+If the B<auto_unset_catchall> configuration option is set, this method will
+automatically remove any references to the canceled service in the catchall
+field of svc_domain.  This allows packages that contain both a svc_domain and
+its catchall svc_acct to be canceled in one step.
+
+=cut
+
+sub cancel {
+  # Only one thing to do at this level
+  my $self = shift;
+  foreach my $svc_domain (
+      qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
+    if($conf->exists('auto_unset_catchall')) {
+      my %hash = $svc_domain->hash;
+      $hash{catchall} = '';
+      my $new = new FS::svc_domain ( \%hash );
+      my $error = $new->replace($svc_domain);
+      return $error if $error;
+    } else {
+      return "cannot unprovision svc_acct #".$self->svcnum.
+         " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
+    }
+  }
+
+  $self->SUPER::cancel;
+}
+
+
 =item check
 
 Checks all fields to make sure this is a valid service.  If there is an error,
index c88b3e6..4dd6342 100644 (file)
@@ -256,6 +256,8 @@ sub replace {
   return "Can't change domain - reorder."
     if $old->getfield('domain') ne $new->getfield('domain'); 
 
+  # Better to do it here than to force the caller to remember that svc_domain is weird.
+  $new->setfield(action => 'M');
   my $error = $new->SUPER::replace($old);
   return $error if $error;
 }