default to a session cookie instead of setting an explicit timeout, weird timezone...
[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 sub condition_sql {
49   my( $self, $table ) = @_;
50
51   my %pkey = %{ FS::part_event->eventtable_pkey };
52
53   my $pkey = $pkey{$table};
54
55   "NOT EXISTS ( SELECT 1 FROM cust_event
56                   WHERE cust_event.eventpart = part_event.eventpart
57                     AND cust_event.tablenum IN (
58                       SELECT $pkey FROM $table AS once_percust
59                         WHERE once_percust.custnum = cust_main.custnum )
60                     AND status != 'failed'
61               )
62   ";
63
64 }
65
66 1;