hopefully fix once.pm properly...
[freeside.git] / FS / FS / part_event / Condition / once.pm
1 package FS::part_event::Condition::once;
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 this event again after it has completed sucessfully"; }
11
12 sub implicit_flag { 10; }
13
14 sub remove_warning {
15   'Are you sure you want to remove this condition?  Doing so will allow this event to run every time the other conditions are satisfied, even if it has already run sucessfully.'; #better error msg?
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=>'NOT IN', value=>"('failed','new')" },
30                     'status'    => { op=>'!=', value=>'failed' },
31                   },
32     'addl_sql' => ( $opt{'cust_event'}->eventnum =~ /^(\d+)$/
33                       ? " AND eventnum != $1 "
34                       : ''
35                   ),
36   } );
37
38   ! scalar(@existing);
39
40 }
41
42 sub condition_sql {
43   my( $self, $table ) = @_;
44
45   my %tablenum = %{ FS::part_event->eventtable_pkey_sql };
46
47   "0 = ( SELECT COUNT(*) FROM cust_event
48            WHERE cust_event.eventpart = part_event.eventpart
49              AND cust_event.tablenum = $tablenum{$table}
50              AND status != 'failed'
51        )
52   ";
53
54 }
55
56 1;