on 3.x, don't create a default cancel reason when an expire reason is missing, #71623
[freeside.git] / FS / FS / cust_pkg.pm
index 9bcf455..e0e710e 100644 (file)
@@ -815,9 +815,13 @@ the date.  You are PROBABLY looking to expire the account instead of using
 this.
 
 =item reason - can be set to a cancellation reason (see L<FS:reason>), 
-either a reasonnum of an existing reason, or passing a hashref will create 
-a new reason.  The hashref should have the following keys: typenum - Reason 
-type (see L<FS::reason_type>, reason - Text of the new reason.
+either a reasonnum of an existing reason, or a hashref to create 
+a new reason.  The hashref should have the following keys:
+typenum - Reason type (see L<FS::reason_type>
+reason - Text of the new reason.
+
+If this argument isn't given or is a false value, then the package will be
+canceled with no reason.
 
 =item date - can be set to a unix style timestamp to specify when to 
 cancel (expire)
@@ -1078,6 +1082,10 @@ Cancels this package if its expire date has been reached.
 
 =cut
 
+# XXX should look for an expire reason
+# but seems to be unused; this is now handled more holistically in
+# cust_main::Billing
+
 sub cancel_if_expired {
   my $self = shift;
   my $time = shift || time;
@@ -1226,7 +1234,13 @@ sub uncancel_svc_summary {
           'uncancel_svcnum' => $svc_x->get('_h_svc_x')->svcnum,
         };
         $svc_x->pkgnum($self->pkgnum); # provisioning services on a canceled package, will be rolled back
-        if ($opt{'no_test_reprovision'} or $svc_x->insert) {
+        my $insert_error;
+        unless ($opt{'no_test_reprovision'}) {
+          # avoid possibly fatal errors from missing linked records
+          eval { $insert_error = $svc_x->insert };
+          $insert_error ||= $@;
+        }
+        if ($opt{'no_test_reprovision'} or $insert_error) {
           # avoid possibly fatal errors from missing linked records
           eval { $out->{'label'} = $svc_x->label };
           eval { $out->{'label'} = $svc_x->get('_h_svc_x')->label } unless defined($out->{'label'});
@@ -2484,6 +2498,21 @@ sub change {
       return "transferring package notes: $error";
     }
   }
+
+  # transfer scheduled expire/adjourn reasons
+  foreach my $action ('expire', 'adjourn') {
+    if ( $cust_pkg->get($action) ) {
+      my $reason = $self->last_cust_pkg_reason($action);
+      if ( $reason ) {
+        $reason->set('pkgnum', $cust_pkg->pkgnum);
+        $error = $reason->replace;
+        if ( $error ) {
+          $dbh->rollback if $oldAutoCommit;
+          return "transferring $action reason: $error";
+        }
+      }
+    }
+  }
   
   my @new_supp_pkgs;