add "Don't run this event more than once per customer in the specified interval"...
[freeside.git] / FS / FS / part_event / Condition / once_percust_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 per customer in the specified interval"; }
11
12 sub eventtable_hashref {
13     { 'cust_main' => 0,
14       'cust_bill' => 1,
15       'cust_pkg'  => 1,
16     };
17 }
18
19 # Runs the event at most "once every X", per customer.
20
21 sub option_fields {
22   (
23     'run_delay'  => { label=>'Interval', type=>'freq', value=>'1m', },
24   );
25 }
26
27 sub condition {
28   my($self, $object, %opt) = @_;
29
30   my $obj_pkey = $object->primary_key;
31   my $obj_table = $object->table;
32   my $custnum = $object->custnum;
33
34   my @where = (
35     "tablenum IN ( SELECT $obj_pkey FROM $obj_table WHERE custnum = $custnum )"
36   );
37   if ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/ ) {
38     push @where, " eventnum != $1 ";
39   }
40   my $extra_sql = ' AND '. join(' AND ', @where);
41  
42   my $max_date = $self->option_age_from('run_delay', $opt{'time'});
43  
44   my @existing = qsearch( {
45     'table'     => 'cust_event',
46     'hashref'   => {
47                      'eventpart' => $self->eventpart,
48                      'status'    => { op=>'!=', value=>'failed'  },
49                      '_date'     => { op=>'>',  value=>$max_date },
50                    },
51     'extra_sql' => $extra_sql,
52   } );
53
54   ! scalar(@existing);
55
56 }
57
58 1;