X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_main_Mixin.pm;h=bbba8c5f74dce21b6def0c7754dfc4e7dc2f4c35;hb=31807e3e9acddff34011e919728c113e69ad9a26;hp=3d05f8473a49ac1cf2882b60f7d6cc3f019b7907;hpb=76e8fffdfe3b6f6f8ab422038b62e40cc10f95e8;p=freeside.git diff --git a/FS/FS/cust_main_Mixin.pm b/FS/FS/cust_main_Mixin.pm index 3d05f8473..bbba8c5f7 100644 --- a/FS/FS/cust_main_Mixin.pm +++ b/FS/FS/cust_main_Mixin.pm @@ -313,8 +313,6 @@ in HASHREF. Valid parameters are: =item status -=item payby - =back =cut @@ -339,15 +337,6 @@ sub cust_search_sql { push @search, $class->$method(); } - #payby - my @payby = ref($param->{'payby'}) - ? @{ $param->{'payby'} } - : split(',', $param->{'payby'}); - @payby = grep /^([A-Z]{4})$/, @payby; - if ( @payby ) { - push @search, 'cust_main.payby IN ('. join(',', map "'$_'", @payby). ')'; - } - #here is the agent virtualization push @search, $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' ); @@ -426,6 +415,18 @@ sub email_search_result { if ( $msgnum ) { $msg_template = qsearchs('msg_template', { msgnum => $msgnum } ) or die "msgnum $msgnum not found\n"; + } else { + $msg_template = FS::msg_template->new({ + from_addr => $from, + msgname => $subject, # maybe a timestamp also? + disabled => 'D', # 'D'raft + # msgclass, maybe + }); + $error = $msg_template->insert( + subject => $subject, + body => $html_body, + ); + return "$error (when creating draft template)" if $error; } my $sql_query = $class->search($param->{'search'}); @@ -446,7 +447,7 @@ sub email_search_result { my %sent_to = (); if ( !$msg_template ) { - # XXX create on the fly + die "email_search_result now requires a msg_template"; } #eventually order+limit magic to reduce memory use? @@ -516,6 +517,14 @@ sub email_search_result { } } # foreach $obj + # if the message template was created as "draft", change its status to + # "completed" + if ($msg_template->disabled eq 'D') { + $msg_template->set('disabled' => 'C'); + my $error = $msg_template->replace; + warn "$error (setting draft message template status)" if $error; + } + if(@retry_jobs) { # fail the job, but with a status message that makes it clear # something was sent. @@ -537,9 +546,6 @@ sub process_email_search_result { $param->{'search'} = thaw(decode_base64($param->{'search'})) or die "process_email_search_result requires search params.\n"; -# $param->{'payby'} = [ split(/\0/, $param->{'payby'}) ] -# unless ref($param->{'payby'}); - my $table = $param->{'table'} or die "process_email_search_result requires table.\n"; @@ -648,6 +654,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