batch refactor continued
[freeside.git] / FS / FS / part_bill_event.pm
index 2aef5bc..473e0bd 100644 (file)
@@ -1,11 +1,13 @@
 package FS::part_bill_event;
 
 use strict;
-use vars qw( @ISA );
-use FS::Record qw( qsearch qsearchs );
+use vars qw( @ISA $DEBUG @EXPORT_OK );
+use FS::Record qw( dbh qsearch qsearchs );
 use FS::Conf;
 
-@ISA = qw(FS::Record);
+@ISA = qw( FS::Record );
+@EXPORT_OK = qw( due_events );
+$DEBUG = 0;
 
 =head1 NAME
 
@@ -26,6 +28,13 @@ FS::part_bill_event - Object methods for part_bill_event records
 
   $error = $record->check;
 
+  $error = $record->do_event( $direct_object );
+  
+  @events = due_events ( { 'record' => $event_triggering_record,
+                           'payby'  => $payby,
+                          'event_time => $_date,
+                          'extra_sql  => $extra } );
+
 =head1 DESCRIPTION
 
 An FS::part_bill_event object represents an invoice event definition -
@@ -124,7 +133,7 @@ sub check {
 
     $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)\(\);\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*$/
 
@@ -197,6 +206,103 @@ sub templatename {
   }
 }
 
+=item due_events
+
+Returns the list of events due, if any, or false if there is none.
+Requires record and payby, but event_time and extra_sql are optional.
+
+=cut
+
+sub due_events {
+  my ($record, $payby, $event_time, $extra_sql) = @_;
+  my $interval = 0;
+  if ($record->_date){ 
+    $event_time = time unless $event_time;
+    $interval = $event_time - $record->_date;
+  }
+  sort {    $a->seconds   <=> $b->seconds
+         || $a->weight    <=> $b->weight
+        || $a->eventpart <=> $b->eventpart }
+    grep { $_->seconds <= ( $interval )
+           && ! qsearch( 'cust_bill_event', {
+                          'invnum' => $record->get($record->dbdef_table->primary_key),
+                          'eventpart' => $_->eventpart,
+                          'status' => 'done',
+                                                                        } )
+        }
+      qsearch( {
+        'table'     => 'part_bill_event',
+       'hashref'   => { 'payby'    => $payby,
+                        'disabled' => '',             },
+       'extra_sql' => $extra_sql,
+      } );
+
+
+}
+
+=item do_event
+
+Performs the event and returns any errors that occur.
+Requires a record on which to perform the event.
+Should only be performed inside a transaction.
+
+=cut
+
+sub do_event {
+  my ($self, $object, %options) = @_;
+  warn " calling event (". $self->eventcode. ") for " . $object->table . " " ,
+    $object->get($object->dbdef_table->primary_key) . "\n" if $DEBUG > 1;
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+
+  #  for "callback" -- heh
+  my $cust_main = $object->cust_main;
+  my $cust_bill;
+  if ($object->table eq 'cust_bill'){
+    $cust_bill = $object;
+  }
+  my $cust_pay_batch;
+  if ($object->table eq 'cust_pay_batch'){
+    $cust_pay_batch = $object;
+  }
+
+  my $error;
+  {
+    local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
+    $error = eval $self->eventcode;
+  }
+
+  my $status = '';
+  my $statustext = '';
+  if ( $@ ) {
+    $status = 'failed';
+    $statustext = $@;
+  } elsif ( $error ) {
+    $status = 'done';
+    $statustext = $error;
+  } else {
+    $status = 'done';
+  }
+
+  #add cust_bill_event
+  my $cust_bill_event = new FS::cust_bill_event {
+#    'invnum'     => $object->get($object->dbdef_table->primary_key),
+    'invnum'     => $object->invnum,
+    'eventpart'  => $self->eventpart,
+    '_date'      => time,
+    'status'     => $status,
+    'statustext' => $statustext,
+  };
+  $error = $cust_bill_event->insert;
+  if ( $error ) {
+    my $e = 'WARNING: Event run but database not updated - '.
+            'error inserting cust_bill_event, invnum #'.  $object->invnum .
+           ', eventpart '. $self->eventpart.": $error";
+    warn $e;
+    return $e;
+  }
+  '';
+}
 
 =back