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', },
18 'run_delay' => { label=>'Delay', type=>'freq', value=>'1', },
23 # Given an event B that has this condition, where the "eventpart"
24 # option is set to event A, and the "run_delay" option is set to
26 # This condition is TRUE if:
27 # - Event A last ran X or more days in the past,
29 # - Event B has not run since the most recent occurrence of event A.
32 # similar to "once_every", but with a different eventpart
33 my($self, $object, %opt) = @_;
35 my $obj_pkey = $object->primary_key;
36 my $tablenum = $object->$obj_pkey();
38 my $before = $self->option_age_from('run_delay',$opt{'time'});
39 my $eventpart = $self->option('eventpart');
42 'eventpart' => $eventpart,
43 'tablenum' => $tablenum,
44 'status' => { op => '!=', value => 'failed' },
47 my $most_recent_other = qsearchs( {
48 'table' => 'cust_event',
50 'order_by' => " ORDER BY _date DESC LIMIT 1",
52 or return 0; # if it hasn't run at all, return false
54 return 0 if $most_recent_other->_date > $before; # we're still in the delay
56 # now see if there's been an instance of this event since the one we're
58 $hash{'eventpart'} = $self->eventpart;
59 if ( $opt{'cust_event'} and $opt{'cust_event'}->eventnum =~ /^(\d+)$/ ) {
60 $hash{'eventnum'} = { op => '!=', value => $1 };
63 my $most_recent_self = qsearchs( {
64 'table' => 'cust_event',
66 'order_by' => " ORDER BY _date DESC LIMIT 1",
69 return 0 if defined($most_recent_self)
70 and $most_recent_self->_date >= $most_recent_other->_date;
71 # the follower has already run
76 # condition_sql, maybe someday