1 package FS::part_event::Condition::after_event;
4 use FS::Record qw( qsearchs );
8 use base qw( FS::part_event::Condition );
10 sub description { "After running another event" }
12 # Runs the event at least X days after the most recent time another event
13 # ran on the same object.
17 'eventpart' => { label=>'Event', type=>'select-part_event',
19 hashref => { disabled => '' },
21 'run_delay' => { label=>'Delay', type=>'freq', value=>'1', },
26 # Given an event B that has this condition, where the "eventpart"
27 # option is set to event A, and the "run_delay" option is set to
29 # This condition is TRUE if:
30 # - Event A last ran X or more days in the past,
32 # - Event B has not run since the most recent occurrence of event A.
35 # similar to "once_every", but with a different eventpart
36 my($self, $object, %opt) = @_;
38 my $obj_pkey = $object->primary_key;
39 my $tablenum = $object->$obj_pkey();
41 my $before = $self->option_age_from('run_delay',$opt{'time'});
42 my $eventpart = $self->option('eventpart');
45 'eventpart' => $eventpart,
46 'tablenum' => $tablenum,
47 'status' => { op => '!=', value => 'failed' },
50 my $most_recent_other = qsearchs( {
51 'table' => 'cust_event',
53 'order_by' => " ORDER BY _date DESC LIMIT 1",
55 or return 0; # if it hasn't run at all, return false
57 return 0 if $most_recent_other->_date > $before; # we're still in the delay
59 # now see if there's been an instance of this event since the one we're
61 $hash{'eventpart'} = $self->eventpart;
62 if ( $opt{'cust_event'} and $opt{'cust_event'}->eventnum =~ /^(\d+)$/ ) {
63 $hash{'eventnum'} = { op => '!=', value => $1 };
66 my $most_recent_self = qsearchs( {
67 'table' => 'cust_event',
69 'order_by' => " ORDER BY _date DESC LIMIT 1",
72 return 0 if defined($most_recent_self)
73 and $most_recent_self->_date >= $most_recent_other->_date;
74 # the follower has already run
79 # condition_sql, maybe someday