X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main_Mixin.pm;h=96e520d52261815585dd3cf9d2d7dac9c26a9f73;hb=be4379d3f8238376f913337b6402dd8b6167913a;hp=769b049db5d0356087cf0aed15eddee5a3faed41;hpb=a792cb684abd3f132c683821416e4c97a7ee471c;p=freeside.git diff --git a/FS/FS/cust_main_Mixin.pm b/FS/FS/cust_main_Mixin.pm index 769b049db..96e520d52 100644 --- a/FS/FS/cust_main_Mixin.pm +++ b/FS/FS/cust_main_Mixin.pm @@ -2,7 +2,7 @@ package FS::cust_main_Mixin; use strict; use vars qw( $DEBUG $me ); -use Carp qw( confess ); +use Carp qw( confess carp cluck ); use FS::UID qw(dbh); use FS::cust_main; use FS::Record qw( qsearch qsearchs ); @@ -38,6 +38,7 @@ sub cust_linked { $_[0]->custnum; } sub cust_main { my $self = shift; + cluck ref($self). '->cust_main called' if $DEBUG; $self->cust_linked ? qsearchs('cust_main', {custnum => $self->custnum}) : ''; } @@ -644,6 +645,57 @@ 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; + + # kind of a pain to load/check all cust_bill instead of just open ones, + # but if for some reason payment gets applied to later bills before + # earlier ones, we still want to consider the later ones as allowable balance + } 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 + if ($cust_bill) { + $maxbalance = $cust_bill->charged || 0; + } else { + $maxbalance = 0; + } + } elsif ($setting eq 'Charges not past due') { + my $now = time; + $maxbalance = 0; + foreach my $cust_bill ($cust_main->cust_bill()) { + next unless $now <= ($cust_bill->due_date || $cust_bill->_date); + $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) { + 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