communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[freeside.git] / FS / FS / part_bill_event.pm
index 473e0bd..4e7aa52 100644 (file)
@@ -2,8 +2,11 @@ package FS::part_bill_event;
 
 use strict;
 use vars qw( @ISA $DEBUG @EXPORT_OK );
+use Carp qw(cluck confess);
 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 );
@@ -37,10 +40,10 @@ FS::part_bill_event - Object methods for part_bill_event records
 
 =head1 DESCRIPTION
 
-An FS::part_bill_event object represents an invoice event definition -
-a callback which is triggered when an invoice is a certain amount of time
-overdue.  FS::part_bill_event inherits from
-FS::Record.  The following fields are currently supported:
+An FS::part_bill_event object represents a deprecated, old-style invoice event
+definition - a callback which is triggered when an invoice is a certain amount
+of time overdue.  FS::part_bill_event inherits from FS::Record.  The following
+fields are currently supported:
 
 =over 4
 
@@ -60,10 +63,17 @@ 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
 
+=head1 NOTE
+
+Old-style invoice events are only useful for legacy migrations - if you are
+looking for current events see L<FS::part_event>.
+
 =head1 METHODS
 
 =over 4
@@ -131,13 +141,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 +174,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 +198,11 @@ sub check {
     }
   }
 
+  if ($self->reason){
+    my $reasonr = qsearchs('reason', {'reasonnum' => $self->reason});
+    return "Unknown reason" unless $reasonr;
+  }
+
   $self->SUPER::check;
 }
 
@@ -215,6 +234,10 @@ Requires record and payby, but event_time and extra_sql are optional.
 
 sub due_events {
   my ($record, $payby, $event_time, $extra_sql) = @_;
+
+  #cluck "DEPRECATED: FS::part_bill_event::due_events called on $record";
+  confess "DEPRECATED: FS::part_bill_event::due_events called on $record";
+
   my $interval = 0;
   if ($record->_date){ 
     $event_time = time unless $event_time;
@@ -223,6 +246,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),
@@ -250,6 +276,10 @@ Should only be performed inside a transaction.
 
 sub do_event {
   my ($self, $object, %options) = @_;
+
+  #cluck "DEPRECATED: FS::part_bill_event::do_event called on $self";
+  confess "DEPRECATED: FS::part_bill_event::do_event called on $self";
+
   warn " calling event (". $self->eventcode. ") for " . $object->table . " " ,
     $object->get($object->dbdef_table->primary_key) . "\n" if $DEBUG > 1;
   my $oldAutoCommit = $FS::UID::AutoCommit;
@@ -304,6 +334,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