RT#28648: Unsuspend when past due balance is paid
authorJonathan Prykop <jonathan@freeside.biz>
Tue, 15 Mar 2016 04:39:18 +0000 (23:39 -0500)
committerJonathan Prykop <jonathan@freeside.biz>
Tue, 15 Mar 2016 04:39:18 +0000 (23:39 -0500)
FS/FS/Conf.pm
FS/FS/cust_credit.pm
FS/FS/cust_main_Mixin.pm
FS/FS/cust_pay.pm

index 72b449c..47eccf8 100644 (file)
@@ -1967,10 +1967,13 @@ and customer address. Include units.',
   },
 
   {
   },
 
   {
-    'key'         => 'unsuspendauto',
+    'key'         => 'unsuspend_balance',
     'section'     => 'billing',
     'section'     => 'billing',
-    'description' => 'Enables the automatic unsuspension of suspended packages when a customer\'s balance due changes from positive to zero or negative as the result of a payment or credit',
-    'type'        => 'checkbox',
+    'description' => 'Enables the automatic unsuspension of suspended packages when a customer\'s balance due is at or below the specified amount after a payment or credit',
+    'type'        => 'select',
+    'select_enum' => [ 
+      '', 'Zero', 'Latest invoice charges'
+    ],
   },
 
   {
   },
 
   {
index 4be4b17..094437e 100644 (file)
@@ -3,7 +3,7 @@ use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::reason_Mixin
              FS::Record );
 
 use strict;
              FS::Record );
 
 use strict;
-use vars qw( $conf $unsuspendauto $me $DEBUG
+use vars qw( $conf $me $DEBUG
              $otaker_upgrade_kludge $ignore_empty_reasonnum
            );
 use List::Util qw( min );
              $otaker_upgrade_kludge $ignore_empty_reasonnum
            );
 use List::Util qw( min );
@@ -34,7 +34,6 @@ $ignore_empty_reasonnum = 0;
 $FS::UID::callback{'FS::cust_credit'} = sub { 
 
   $conf = new FS::Conf;
 $FS::UID::callback{'FS::cust_credit'} = sub { 
 
   $conf = new FS::Conf;
-  $unsuspendauto = $conf->exists('unsuspendauto');
 
 };
 
 
 };
 
@@ -210,16 +209,8 @@ sub insert {
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
-  #false laziness w/ cust_pay::insert
-  if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
-    my @errors = $cust_main->unsuspend;
-    #return 
-    # side-fx with nested transactions?  upstack rolls back?
-    warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
-         join(' / ', @errors)
-      if @errors;
-  }
-  #eslaf
+  # possibly trigger package unsuspend, doesn't abort transaction on failure
+  $self->unsuspend_balance if $old_balance;
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
index de13847..9b4ae3a 100644 (file)
@@ -654,6 +654,45 @@ sub time2str_local {
   $string;
 }
 
   $string;
 }
 
+=item unsuspend_balance
+
+If conf I<unsuspend_balance> is set and customer's current balance is
+beneath the set threshold, unsuspends customer packages.
+
+=cut
+
+sub unsuspend_balance {
+  my $self = shift;
+  my $cust_main = $self->cust_main;
+  my $conf = $self->conf;
+  my $setting = $conf->config('unsuspend_balance');
+  my $maxbalance;
+  if ($setting eq 'Zero') {
+    $maxbalance = 0;
+  } elsif ($setting eq 'Latest invoice charges') {
+    my @cust_bill = $cust_main->cust_bill();
+    my $cust_bill = $cust_bill[-1]; #always want the most recent one
+    return unless $cust_bill;
+    $maxbalance = $cust_bill->charged || 0;
+  } elsif (length($setting)) {
+    warn "Unrecognized unsuspend_balance setting $setting";
+    return;
+  } else {
+    return;
+  }
+  my $balance = $cust_main->balance || 0;
+  if ($balance <= $maxbalance) {
+    # or should this be 
+    # my @errors = grep { ($_->get('setup')) && $_->unsuspend } $cust_main->unflagged_suspended_pkgs;
+    my @errors = $cust_main->unsuspend;
+    # side-fx with nested transactions?  upstack rolls back?
+    warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
+         join(' / ', @errors)
+      if @errors;
+  }
+  return;
+}
+
 =back
 
 =head1 BUGS
 =back
 
 =head1 BUGS
index 620f6c6..86e7968 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
              FS::reason_Mixin FS::Record);
 use vars qw( $DEBUG $me $conf @encrypted_fields
 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
              FS::reason_Mixin FS::Record);
 use vars qw( $DEBUG $me $conf @encrypted_fields
-             $unsuspendauto $ignore_noapply
+             $ignore_noapply
            );
 use Date::Format;
 use Business::CreditCard;
            );
 use Date::Format;
 use Business::CreditCard;
@@ -36,7 +36,6 @@ $ignore_noapply = 0;
 #ask FS::UID to run this stuff for us later
 FS::UID->install_callback( sub { 
   $conf = new FS::Conf;
 #ask FS::UID to run this stuff for us later
 FS::UID->install_callback( sub { 
   $conf = new FS::Conf;
-  $unsuspendauto = $conf->exists('unsuspendauto');
 } );
 
 @encrypted_fields = ('payinfo');
 } );
 
 @encrypted_fields = ('payinfo');
@@ -355,16 +354,8 @@ sub insert {
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
-  #false laziness w/ cust_credit::insert
-  if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
-    my @errors = $cust_main->unsuspend;
-    #return 
-    # side-fx with nested transactions?  upstack rolls back?
-    warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
-         join(' / ', @errors)
-      if @errors;
-  }
-  #eslaf
+  # possibly trigger package unsuspend, doesn't abort transaction on failure
+  $self->unsuspend_balance if $old_balance;
 
   #bill setup fees for voip_cdr bill_every_call packages
   #some false laziness w/search in freeside-cdrd
 
   #bill setup fees for voip_cdr bill_every_call packages
   #some false laziness w/search in freeside-cdrd
@@ -1214,6 +1205,15 @@ sub _upgrade_data {  #class method
       process_upgrade_paybatch();
     }
   }
       process_upgrade_paybatch();
     }
   }
+
+  # unsuspendauto upgrade
+  # could just as easily go in cust_credit, or even cust_bill or cust_main
+  # but here works
+  if ($conf->exists('unsuspendauto') && !$conf->config('unsuspend_balance')) {
+    $conf->set('unsuspend_balance','Zero');
+    $conf->delete('unsuspendauto');
+  }
+
 }
 
 sub process_upgrade_paybatch {
 }
 
 sub process_upgrade_paybatch {