fix data type issue, #11834
[freeside.git] / FS / FS / part_event / Condition / times.pm
1 package FS::part_event::Condition::times;
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 { "Run this event the specified number of times"; }
11
12 sub option_fields {
13   (
14     'run_times'  => { label=>'Number of times', type=>'text', value=>'1', },
15   );
16 }
17
18 sub condition {
19   my($self, $object, %opt) = @_;
20
21   my $obj_pkey = $object->primary_key;
22   my $tablenum = $object->$obj_pkey();
23  
24   my @existing = qsearch( {
25     'table'     => 'cust_event',
26     'hashref'   => {
27                      'eventpart' => $self->eventpart,
28                      'tablenum'  => $tablenum,
29                      'status'    => { op=>'!=', value=>'failed' },
30                    },
31     'extra_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
32                        ? " AND eventnum != $1 "
33                        : ''
34                    ),
35   } );
36
37   scalar(@existing) <= $self->option('run_times');
38
39 }
40
41 sub condition_sql {
42   my( $class, $table ) = @_;
43
44   my %tablenum = %{ FS::part_event->eventtable_pkey_sql };
45
46   my $existing = "( SELECT COUNT(*) FROM cust_event
47                       WHERE cust_event.eventpart = part_event.eventpart
48                         AND cust_event.tablenum = $tablenum{$table}
49                         AND status != 'failed'
50                   )";
51
52   "$existing <= ". $class->condition_sql_option_integer('run_times');
53
54 }
55
56 1;