From 49deddfdc5f60c5cde01a5152e6bae858ed8e72a Mon Sep 17 00:00:00 2001
From: Jonathan Prykop <jonathan@freeside.biz>
Date: Fri, 20 Feb 2015 20:07:18 -0600
Subject: [PATCH] RT#14671: Usage for current day when billing outstanding
 usage (for cancelling customers) [now uses suspend for everything]

---
 FS/FS/cust_pkg.pm                             | 32 ++++++++++++-------
 .../view/cust_main/packages/status.html       | 12 +++++--
 2 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm
index fb3b0ff80..b64d4dc38 100644
--- a/FS/FS/cust_pkg.pm
+++ b/FS/FS/cust_pkg.pm
@@ -784,6 +784,10 @@ to a different pkgpart or location, and probably shouldn't be in any other
 case.  If it's not set, the 'unused_credit_cancel' part_pkg option will 
 be used.
 
+=item delay_cancel - for internal use, to allow proper handling of
+supplemental packages when the main package is flagged to suspend 
+before cancelling
+
 =back
 
 If there is an error, returns the error, otherwise returns false.
@@ -823,7 +827,7 @@ sub cancel {
   my $date = $options{'date'} if $options{'date'}; # expire/cancel later
   $date = '' if ($date && $date <= $cancel_time);      # complain instead?
 
-  my $delay_cancel = undef;
+  my $delay_cancel = $options{'delay_cancel'};
   if ( !$date && $self->part_pkg->option('delay_cancel',1)
        && (($self->status eq 'active') || ($self->status eq 'suspended'))
   ) {
@@ -907,7 +911,7 @@ sub cancel {
   if ( $date ) {
     $hash{'expire'} = $date;
     if ($delay_cancel) {
-      $hash{'susp'} = $cancel_time unless $self->susp;
+      # just to be sure these are clear
       $hash{'adjourn'} = undef;
       $hash{'resume'} = undef;
     }
@@ -934,21 +938,24 @@ sub cancel {
   }
 
   foreach my $supp_pkg ( $self->supplemental_pkgs ) {
-    if ($delay_cancel) {
-      $error = $supp_pkg->suspend(
-        'from_main'   => 1, 
-        'from_cancel' => 1,
-        'time'        => $cancel_time
-      );
-    } else {
-      $error = $supp_pkg->cancel(%options, 'from_main' => 1);
-    }
+    $error = $supp_pkg->cancel(%options, 
+      'from_main' => 1, 
+      'date' => $date, #in case it got changed by delay_cancel
+      'delay_cancel' => $delay_cancel,
+    );
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "canceling supplemental pkg#".$supp_pkg->pkgnum.": $error";
     }
   }
 
+  if ($delay_cancel && !$options{'from_main'}) {
+    $error = $new->suspend(
+      'from_cancel' => 1,
+      'time'        => $cancel_time
+    );
+  }
+
   unless ($date) {
     foreach my $usage ( $self->cust_pkg_usage ) {
       $error = $usage->delete;
@@ -3391,6 +3398,9 @@ really the whole point of the delay_cancel option.
 
 sub is_status_delay_cancel {
   my ($self) = @_;
+  if ( $self->main_pkgnum and $self->pkglinknum ) {
+    return $self->main_pkg->is_status_delay_cancel;
+  }
   return 0 unless $self->part_pkg->option('delay_cancel',1);
   return 0 unless $self->status eq 'suspended';
   return 0 unless $self->expire;
diff --git a/httemplate/view/cust_main/packages/status.html b/httemplate/view/cust_main/packages/status.html
index f760d6fcc..36419646c 100644
--- a/httemplate/view/cust_main/packages/status.html
+++ b/httemplate/view/cust_main/packages/status.html
@@ -54,9 +54,15 @@
         <% pkg_status_row( $cust_pkg, emt('On Hold'), '', 'color'=>'7E0079', %opt ) %>
 
 %     } else { #status: suspended
-
-        <% pkg_status_row( $cust_pkg, emt('Suspended'), 'susp', 'color'=>'FF9900', %opt ) %>
-%       my $cpr = $cust_pkg->last_cust_pkg_reason('susp');
+%       my ($cpr,$susplabel);
+%       if ($cust_pkg->is_status_delay_cancel) {
+%         $cpr = $cust_pkg->last_cust_pkg_reason('expire');
+%         $susplabel = 'Suspended (Cancelled)';
+%       } else {
+%         $cpr = $cust_pkg->last_cust_pkg_reason('susp');
+%         $susplabel = 'Suspended';
+%       }
+        <% pkg_status_row( $cust_pkg, emt($susplabel), 'susp', 'color'=>'FF9900', %opt ) %>
         <% pkg_reason_row( $cust_pkg, $cpr, 'color' => 'FF9900', %opt ) %>
 
 %     }
-- 
2.20.1