import torrus 1.0.9
[freeside.git] / FS / FS / part_event / Condition / once_percust.pm
1 package FS::part_event::Condition::once_percust;
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 more than once per customer"; }
11
12 sub eventtable_hashref {
13     { 'cust_main' => 0,
14       'cust_bill' => 1,
15       'cust_pkg'  => 1,
16     };
17 }
18
19 sub condition {
20   my($self, $object, %opt) = @_;
21
22   my $obj_pkey = $object->primary_key;
23   my $obj_table = $object->table;
24   my $custnum = $object->custnum;
25
26   my @where = (
27     "tablenum IN ( SELECT $obj_pkey FROM $obj_table WHERE custnum = $custnum )"
28   );
29   if ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/ ) {
30     push @where, " eventnum != $1 ";
31   }
32   my $extra_sql = ' AND '. join(' AND ', @where);
33  
34   my @existing = qsearch( {
35     'table'     => 'cust_event',
36     'hashref'   => {
37                      'eventpart' => $self->eventpart,
38                      #'tablenum'  => $tablenum,
39                      'status'    => { op=>'!=', value=>'failed' },
40                    },
41     'extra_sql' => $extra_sql,
42   } );
43
44   ! scalar(@existing);
45
46 }
47
48 #XXX test?
49 sub condition_sql {
50   my( $self, $table ) = @_;
51
52   my %pkey = %{ FS::part_event->eventtable_pkey };
53
54   my $pkey = $pkey{$table};
55
56   "0 = ( SELECT COUNT(*) FROM cust_event
57            WHERE cust_event.eventpart = part_event.eventpart
58              AND cust_event.tablenum IN (
59                SELECT $pkey FROM $table AS once_percust
60                  WHERE once_percust.custnum = cust_main.custnum )
61              AND status != 'failed'
62        )
63   ";
64
65 }
66
67 1;