summaryrefslogtreecommitdiff
path: root/FS/FS/part_event/Condition/once_every.pm
blob: a0d9d6802085d44feb765e61d4ff882393fb4f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package FS::part_event::Condition::once_every;

use strict;
use FS::Record qw( qsearch );
use FS::part_event;
use FS::cust_event;

use base qw( FS::part_event::Condition );

sub description { "Don't run this event more than once in interval"; }

# Runs the event at most "once every X".

sub option_fields {
  (
    'run_delay'  => { label=>'Interval', type=>'freq', value=>'1m', },
  );
}

sub condition {
  my($self, $object, %opt) = @_;

  my $obj_pkey = $object->primary_key;
  my $tablenum = $object->$obj_pkey();

  my $max_date = $self->option_age_from('run_delay',$opt{'time'});
 
  my @existing = qsearch( {
    'table'     => 'cust_event',
    'hashref'   => {
                     'eventpart' => $self->eventpart,
                     'tablenum'  => $tablenum,
                     'status'    => { op=>'!=', value=>'failed'  },
                     '_date'     => { op=>'>=', value=>$max_date },
                   },
    'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
                       ? " AND eventnum != $1 "
                       : ''
                   ),
  } );

  ! scalar(@existing);

}

1;