import torrus 1.0.9
[freeside.git] / FS / FS / part_event / Condition / once_every.pm
1 package FS::part_event::Condition::once_every;
2
3 use strict;
4 use FS::Record qw( qsearch );
5 use FS::part_event;
6 use FS::cust_event;
7
8 use base qw( FS::part_event::Condition );
9
10 sub description { "Don't run this event more than once in the specified interval"; }
11
12 # Runs the event at most "once every X".
13
14 sub option_fields {
15   (
16     'run_delay'  => { label=>'Interval', type=>'freq', value=>'1m', },
17   );
18 }
19
20 sub condition {
21   my($self, $object, %opt) = @_;
22
23   my $obj_pkey = $object->primary_key;
24   my $tablenum = $object->$obj_pkey();
25
26   my $max_date = $self->option_age_from('run_delay',$opt{'time'});
27  
28   my @existing = qsearch( {
29     'table'     => 'cust_event',
30     'hashref'   => {
31                      'eventpart' => $self->eventpart,
32                      'tablenum'  => $tablenum,
33                      'status'    => { op=>'!=', value=>'failed'  },
34                      '_date'     => { op=>'>=', value=>$max_date },
35                    },
36     'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
37                        ? " AND eventnum != $1 "
38                        : ''
39                    ),
40   } );
41
42   ! scalar(@existing);
43
44 }
45
46 1;