RT# 81961 Repair broken links in POD documentation
[freeside.git] / FS / FS / part_event.pm
index 6751776..be89459 100644 (file)
@@ -6,6 +6,7 @@ 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;
@@ -251,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,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>
@@ -323,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;
 }
 
@@ -372,7 +383,7 @@ sub eventtable_labels {
     '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
@@ -559,7 +570,7 @@ foreach my $INC ( @INC ) {
     }
     $actions{$mod} = {
       ( map { $_ => "FS::part_event::Action::$mod"->$_() }
-            qw( description eventtable_hashref default_weight deprecated )
+            qw( description eventtable_hashref default_weight deprecated will_send_invoice )
             #option_fields_hashref
       ),
       'option_fields' => [ "FS::part_event::Action::$mod"->option_fields() ],
@@ -571,9 +582,11 @@ sub actions {
   my( $class, $eventtable ) = @_;
   (
     map  { $_ => $actions{$_} }
-    sort { $actions{$a}->{'default_weight'}<=>$actions{$b}->{'default_weight'} }
-       # || $actions{$a}->{'description'} cmp $actions{$b}->{'description'} }
-    $class->all_actions( $eventtable )
+    sort {
+         $actions{$a}->{'default_weight'} <=> $actions{$b}->{'default_weight'}
+      || $actions{$a}->{'description'}    cmp $actions{$b}->{'description'}
+    }
+      $class->all_actions( $eventtable )
   );
 
 }
@@ -591,6 +604,20 @@ sub all_actions {
        keys %actions
 }
 
+=item invoice_send_actions
+
+Returns a list of action names that send invoices
+
+=cut
+
+sub invoice_send_actions {
+  my ( $class ) = @_;
+
+  grep { $actions{$_}->{'will_send_invoice'} }
+       keys %actions
+
+}
+
 =item process_initialize 'eventpart' => EVENTPART
 
 Job queue wrapper for "initialize".  EVENTPART identifies the 
@@ -611,7 +638,7 @@ sub process_initialize {
 =head1 SEE ALSO
 
 L<FS::part_event_option>, L<FS::part_event_condition>, L<FS::cust_main>,
-L<FS::cust_pkg>, L<FS::svc_acct>, L<FS::cust_bill>, L<FS::cust_bill_event>, 
+L<FS::cust_pkg>, L<FS::svc_acct>, L<FS::cust_bill>, L<FS::cust_event>,
 L<FS::Record>,
 schema.html from the base documentation.