+=item targets OPTIONS
+
+Returns all objects (of type C<FS::eventtable>, for this object's
+C<eventtable>) eligible for processing under this event, as of right now.
+The L<FS::cust_event> object used to test event conditions will be
+included in each object as the 'cust_event' pseudo-field.
+
+This is not used in normal event processing (which is done on a
+per-customer basis to control timing of pre- and post-billing events)
+but can be useful when configuring events.
+
+=cut
+
+sub targets { # may want to cursor this also
+ my $self = shift;
+ my %opt = @_;
+ 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";
+ my $eventtable = $self->eventtable;
+
+ # find all objects that meet the conditions for this part_event
+ my $linkage = '';
+ # this is the 'object' side of the FROM clause
+ if ( $eventtable ne 'cust_main' ) {
+ $linkage =
+ ($self->eventtables_cust_join->{$eventtable} || '') .
+ ' LEFT JOIN cust_main USING (custnum) ';
+ }
+
+ # this is the 'event' side
+ my $join = FS::part_event_condition->join_conditions_sql( $eventtable,
+ 'time' => $time
+ );
+ my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
+ 'time' => $time
+ );
+ $join = $linkage .
+ " INNER JOIN part_event ON ( part_event.eventpart = $eventpart ) $join";
+
+ $where .= ' AND cust_main.agentnum = '.$self->agentnum
+ if $self->agentnum;
+ # don't enforce check_freq since this is a special, out-of-order check
+ # and don't enforce disabled because we want to be able to see targets
+ # for a disabled event
+
+ {
+ table => $eventtable,
+ hashref => {},
+ addl_from => $join,
+ extra_sql => "WHERE $where",
+ };
+}
+
+
+=item initialize PARAMS
+
+Identify all objects eligible for this event and create L<FS::cust_event>
+records for each of them, as of the present time, with status "initial". When
+combined with conditions that prevent an event from running more than once
+(at all or within some period), this will exclude any objects that met the
+conditions before the event was created.
+
+If an L<FS::part_event> object needs to be initialized, it should be created
+in a disabled state to avoid running the event prematurely for any existing
+objects. C<initialize> will enable it once all the cust_event records
+have been created.
+
+This may take some time, so it should be run from the job queue.
+
+=cut
+
+sub initialize {
+ my $self = shift;
+ my $error;
+
+ 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;
+
+ $cust_event->status('initial');
+ $error = $cust_event->insert;
+ die $error if $error;
+ }
+
+ # on successful completion only, re-enable the event
+ if ( $self->disabled ) {
+ $self->disabled('');
+ $error = $self->replace;
+ die $error if $error;
+ }
+ return;
+}
+
+=cut
+
+