require svc_acct-usage_threshold to be set explicitly, don't default to 80%
[freeside.git] / FS / FS / part_bill_event.pm
index 473e0bd..7d00117 100644 (file)
@@ -4,6 +4,8 @@ use strict;
 use vars qw( @ISA $DEBUG @EXPORT_OK );
 use FS::Record qw( dbh qsearch qsearchs );
 use FS::Conf;
+use FS::cust_main;
+use FS::cust_bill;
 
 @ISA = qw( FS::Record );
 @EXPORT_OK = qw( due_events );
@@ -60,6 +62,8 @@ FS::Record.  The following fields are currently supported:
 
 =item plandata - additional plan data
 
+=item reason   - an associated reason for this event to fire
+
 =item disabled - Disabled flag, empty or `Y'
 
 =back
@@ -131,13 +135,16 @@ sub check {
 
     my $c = $self->eventcode;
 
+    #yay, these regexen will go away with the event refactor
+
     $c =~ /^\s*\$cust_main\->(suspend|cancel|invoicing_list_addpost|bill|collect)\(\);\s*("";)?\s*$/
 
       or $c =~ /^\s*\$cust_bill\->(comp|realtime_(card|ach|lec)|batch_card|send)\((%options)*\);\s*$/
 
       or $c =~ /^\s*\$cust_bill\->send(_if_newest)?\(\'[\w\-\s]+\'\s*(,\s*(\d+|\[\s*\d+(,\s*\d+)*\s*\])\s*,\s*'[\w\@\.\-\+]*'\s*)?\);\s*$/
 
-      or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
+#      or $c =~ /^\s*\$cust_main\->apply_payments; \$cust_main->apply_credits; "";\s*$/
+      or $c =~ /^\s*\$cust_main\->apply_payments_and_credits; "";\s*$/
 
       or $c =~ /^\s*\$cust_main\->charge\( \s*\d*\.?\d*\s*,\s*\'[\w \!\@\#\$\%\&\(\)\-\+\;\:\"\,\.\?\/]*\'\s*\);\s*$/
 
@@ -161,6 +168,7 @@ sub check {
     || $self->ut_number('weight')
     || $self->ut_textn('plan')
     || $self->ut_anything('plandata')
+    || $self->ut_numbern('reason')
   ;
     #|| $self->ut_snumber('seconds')
   return $error if $error;
@@ -184,6 +192,11 @@ sub check {
     }
   }
 
+  if ($self->reason){
+    my $reasonr = qsearchs('reason', {'reasonnum' => $self->reason});
+    return "Unknown reason" unless $reasonr;
+  }
+
   $self->SUPER::check;
 }
 
@@ -223,6 +236,9 @@ sub due_events {
   sort {    $a->seconds   <=> $b->seconds
          || $a->weight    <=> $b->weight
         || $a->eventpart <=> $b->eventpart }
+    grep { ref($record) ne 'FS::cust_bill' || $_->eventcode !~ /honor_dundate/
+           || $event_time > $record->cust_main->dundate
+         }
     grep { $_->seconds <= ( $interval )
            && ! qsearch( 'cust_bill_event', {
                           'invnum' => $record->get($record->dbdef_table->primary_key),
@@ -304,6 +320,22 @@ sub do_event {
   '';
 }
 
+=item reasontext
+
+Returns the text of any reason associated with this event.
+
+=cut
+
+sub reasontext {
+  my $self = shift;
+  my $r = qsearchs('reason', { 'reasonnum' => $self->reason });
+  if ($r){
+    $r->reason;
+  }else{
+    '';
+  }
+}
+
 =back
 
 =head1 BUGS