cursorize part_event::initialize so that it works on large data sets
[freeside.git] / FS / FS / part_event.pm
index b7371c9..1c23899 100644 (file)
@@ -1,15 +1,15 @@
 package FS::part_event;
+use base qw( FS::m2name_Common FS::option_Common );
 
 use strict;
-use base qw( FS::m2name_Common FS::option_Common );
 use vars qw( $DEBUG );
 use Carp qw(confess);
 use FS::Record qw( dbh qsearch qsearchs );
 use FS::Conf;
+use FS::Cursor;
 use FS::part_event_option;
 use FS::part_event_condition;
 use FS::cust_event;
-use FS::agent;
 
 $DEBUG = 0;
 
@@ -169,13 +169,6 @@ sub _rebless {
 Returns the conditions associated with this event, as FS::part_event_condition
 objects (see L<FS::part_event_condition>)
 
-=cut
-
-sub part_event_condition {
-  my $self = shift;
-  qsearch( 'part_event_condition', { 'eventpart' => $self->eventpart } );
-}
-
 =item new_cust_event OBJECT, [ OPTION => VALUE ]
 
 Creates a new customer event (see L<FS::cust_event>) for the provided object.
@@ -223,13 +216,6 @@ sub reasontext { confess "part_event->reasontext deprecated"; }
 
 Returns the associated agent for this event, if any, as an FS::agent object.
 
-=cut
-
-sub agent {
-  my $self = shift;
-  qsearchs('agent', { 'agentnum' => $self->agentnum } );
-}
-
 =item templatename
 
 Returns the alternate invoice template name, if any, or false if there is
@@ -266,10 +252,28 @@ but can be useful when configuring events.
 
 =cut
 
-sub targets {
+sub targets { # may want to cursor this also
   my $self = shift;
   my %opt = @_;
-  my $time = $opt{'time'} || time;
+  my $time = $opt{'time'} ||= time;
+  
+  my $query = $self->_target_query(%opt);
+  my @objects = qsearch($query);
+  my @tested_objects;
+  foreach my $object ( @objects ) {
+    my $cust_event = $self->new_cust_event($object, 'time' => $time);
+    next unless $cust_event->test_conditions;
+
+    $object->set('cust_event', $cust_event);
+    push @tested_objects, $object;
+  }
+  @tested_objects;
+}
+
+sub _target_query {
+  my $self = shift;
+  my %opt = @_;
+  my $time = $opt{'time'};
 
   my $eventpart = $self->eventpart;
   $eventpart =~ /^\d+$/ or die "bad eventpart $eventpart";
@@ -285,7 +289,9 @@ sub targets {
   }
 
   # this is the 'event' side
-  my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
+  my $join = FS::part_event_condition->join_conditions_sql( $eventtable,
+    'time' => $time
+  );
   my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
     'time' => $time
   );
@@ -298,23 +304,15 @@ sub targets {
   # and don't enforce disabled because we want to be able to see targets 
   # for a disabled event
 
-  my @objects = qsearch({
+  {
       table     => $eventtable,
       hashref   => {},
       addl_from => $join,
       extra_sql => "WHERE $where",
-  });
-  my @tested_objects;
-  foreach my $object ( @objects ) {
-    my $cust_event = $self->new_cust_event($object, 'time' => $time);
-    next unless $cust_event->test_conditions;
-
-    $object->set('cust_event', $cust_event);
-    push @tested_objects, $object;
-  }
-  @tested_objects;
+  };
 }
 
+
 =item initialize PARAMS
 
 Identify all objects eligible for this event and create L<FS::cust_event>
@@ -336,26 +334,26 @@ sub initialize {
   my $self = shift;
   my $error;
 
-  my $oldAutoCommit = $FS::UID::AutoCommit;
-  local $FS::UID::AutoCommit = 0;
-  my $dbh = dbh;
+  my $time = time;
+
+  local $FS::UID::AutoCommit = 1;
+  my $cursor = FS::Cursor->new( $self->_target_query('time' => $time) );
+  while (my $object = $cursor->fetch) {
+
+    my $cust_event = $self->new_cust_event($object, 'time' => $time);
+    next unless $cust_event->test_conditions;
 
-  my @objects = $self->targets;
-  foreach my $object ( @objects ) {
-    my $cust_event = $object->get('cust_event');
     $cust_event->status('initial');
     $error = $cust_event->insert;
-    last if $error;
+    die $error if $error;
   }
-  if ( !$error and $self->disabled ) {
+
+  # on successful completion only, re-enable the event
+  if ( $self->disabled ) {
     $self->disabled('');
     $error = $self->replace;
+    die $error if $error;
   }
-  if ( $error ) {
-    $dbh->rollback;
-    return $error;
-  }
-  $dbh->commit if $oldAutoCommit;
   return;
 }
 
@@ -382,9 +380,10 @@ sub eventtable_labels {
     'cust_pkg'       => 'Package',
     'cust_bill'      => 'Invoice',
     'cust_main'      => 'Customer',
+    'cust_pay'       => 'Payment',
     'cust_pay_batch' => 'Batch payment',
     'cust_statement' => 'Statement',  #too general a name here? "Invoice group"?
-    'svc_acct'       => 'Login service',
+    'svc_acct'       => 'Account service (svc_acct)',
   ;
 
   \%hash
@@ -421,6 +420,7 @@ sub eventtable_pkey {
     'cust_main'      => 'custnum',
     'cust_bill'      => 'invnum',
     'cust_pkg'       => 'pkgnum',
+    'cust_pay'       => 'paynum',
     'cust_pay_batch' => 'paybatchnum',
     'cust_statement' => 'statementnum',
     'svc_acct'       => 'svcnum',
@@ -583,6 +583,7 @@ sub actions {
   (
     map  { $_ => $actions{$_} }
     sort { $actions{$a}->{'default_weight'}<=>$actions{$b}->{'default_weight'} }
+       # || $actions{$a}->{'description'} cmp $actions{$b}->{'description'} }
     $class->all_actions( $eventtable )
   );