1 package FS::part_event::Condition::times_percust;
4 use FS::Record qw( qsearch );
8 use base qw( FS::part_event::Condition );
10 sub description { "Run this event the specified number of times per customer"; }
14 'run_times' => { label=>'Number of times', type=>'text', value=>'1', },
18 sub eventtable_hashref {
26 my($self, $object, %opt) = @_;
28 my $obj_pkey = $object->primary_key;
29 my $obj_table = $object->table;
30 my $custnum = $object->custnum;
33 "tablenum IN ( SELECT $obj_pkey FROM $obj_table WHERE custnum = $custnum )"
35 if ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/ ) {
36 push @where, " eventnum != $1 ";
38 my $extra_sql = ' AND '. join(' AND ', @where);
40 my @existing = qsearch( {
41 'table' => 'cust_event',
43 'eventpart' => $self->eventpart,
44 #'tablenum' => $tablenum,
45 'status' => { op=>'!=', value=>'failed' },
47 'extra_sql' => $extra_sql,
50 scalar(@existing) < $self->option('run_times');
55 my( $class, $table, %opt ) = @_;
57 my %pkey = %{ FS::part_event->eventtable_pkey };
60 $class->condition_sql_option_integer('run_times', $opt{'driver_name'});
62 my $pkey = $pkey{$table};
64 my $existing = "( SELECT COUNT(*) FROM cust_event
65 WHERE cust_event.eventpart = part_event.eventpart
66 AND cust_event.tablenum IN (
67 SELECT $pkey FROM $table AS times_percust
68 WHERE times_percust.custnum = cust_main.custnum )
69 AND status != 'failed'
72 "$existing < $run_times";