From b3cc20aeca25e4351c6ec3b795a951e6124f6376 Mon Sep 17 00:00:00 2001 From: Jonathan Prykop Date: Mon, 14 Mar 2016 23:39:18 -0500 Subject: [PATCH] RT#28648: Unsuspend when past due balance is paid --- FS/FS/Conf.pm | 9 ++++++--- FS/FS/cust_credit.pm | 15 +++------------ FS/FS/cust_main_Mixin.pm | 39 +++++++++++++++++++++++++++++++++++++++ FS/FS/cust_pay.pm | 24 ++++++++++++------------ 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 72b449c03..47eccf837 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -1967,10 +1967,13 @@ and customer address. Include units.', }, { - 'key' => 'unsuspendauto', + 'key' => 'unsuspend_balance', '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' + ], }, { diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index 4be4b177a..094437e22 100644 --- a/FS/FS/cust_credit.pm +++ b/FS/FS/cust_credit.pm @@ -3,7 +3,7 @@ use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::reason_Mixin 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 ); @@ -34,7 +34,6 @@ $ignore_empty_reasonnum = 0; $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; - #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; diff --git a/FS/FS/cust_main_Mixin.pm b/FS/FS/cust_main_Mixin.pm index de13847a4..9b4ae3a09 100644 --- a/FS/FS/cust_main_Mixin.pm +++ b/FS/FS/cust_main_Mixin.pm @@ -654,6 +654,45 @@ sub time2str_local { $string; } +=item unsuspend_balance + +If conf I 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 diff --git a/FS/FS/cust_pay.pm b/FS/FS/cust_pay.pm index 620f6c629..86e7968a2 100644 --- a/FS/FS/cust_pay.pm +++ b/FS/FS/cust_pay.pm @@ -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 - $unsuspendauto $ignore_noapply + $ignore_noapply ); 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; - $unsuspendauto = $conf->exists('unsuspendauto'); } ); @encrypted_fields = ('payinfo'); @@ -355,16 +354,8 @@ sub insert { $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 @@ -1214,6 +1205,15 @@ sub _upgrade_data { #class method 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 { -- 2.11.0