Cancel services in a particular order to get around certain inter-service dependancies
[freeside.git] / FS / FS / cust_pkg.pm
index 1bf56e1..ccd73ac 100644 (file)
@@ -1,7 +1,7 @@
 package FS::cust_pkg;
 
 use strict;
-use vars qw(@ISA $disable_agentcheck);
+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 );
@@ -25,8 +25,18 @@ use FS::Conf;
 
 @ISA = qw( FS::Record );
 
+$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 ) = @_;
@@ -148,7 +158,7 @@ sub insert {
   return $error if $error;
 
   my $cust_main = $self->cust_main;
-  return "Unknown customer ". $self->custnum unless $cust_main;
+  return "Unknown custnum: ". $self->custnum unless $cust_main;
 
   unless ( $disable_agentcheck ) {
     my $agent = qsearchs( 'agent', { 'agentnum' => $cust_main->agentnum } );
@@ -244,7 +254,9 @@ sub check {
   $self->otaker($1);
 
   if ( $self->dbdef_table->column('manual_flag') ) {
-    $self->manual_flag =~ /^([01]?)$/ or return "Illegal manual_flag";
+    $self->manual_flag('') if $self->manual_flag eq ' ';
+    $self->manual_flag =~ /^([01]?)$/
+      or return "Illegal manual_flag ". $self->manual_flag;
     $self->manual_flag($1);
   }
 
@@ -280,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') ) {
@@ -612,7 +630,7 @@ sub attribute_since_sqlradacct {
 
 Transfers as many services as possible from this package to another package.
 The destination package must already exist.  Services are moved only if 
-the destination allows services with the correct I<svcnum> (not svcdb).  
+the destination allows services with the correct I<svcpart> (not svcdb).  
 Any services that can't be moved remain in the original package.
 
 Returns an error, if there is one; otherwise, returns the number of services 
@@ -665,6 +683,9 @@ sub transfer {
 
 =item reexport
 
+This method is deprecated.  See the I<depend_jobnum> option to the insert and
+order_pkgs methods in FS::cust_main for a better way to defer provisioning.
+
 =cut
 
 sub reexport {
@@ -724,13 +745,16 @@ newly-created cust_pkg objects.
 =cut
 
 sub order {
-
-  # Rewritten to make use of the transfer() method, and in general 
-  # to not suck so badly.
-
   my ($custnum, $pkgparts, $remove_pkgnum, $return_cust_pkg) = @_;
 
   # Transactionize this whole mess
+  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;