add condition_sql optimization to "Customer does not have uncancelled package of...
[freeside.git] / FS / FS / part_event / Condition / hasnt_pkgpart.pm
1 package FS::part_event::Condition::hasnt_pkgpart;
2
3 use strict;
4
5 use base qw( FS::part_event::Condition );
6
7 sub description { 'Customer does not have uncancelled package of specified definitions'; }
8
9 sub eventtable_hashref {
10     { 'cust_main' => 1,
11       'cust_bill' => 1,
12       'cust_pkg'  => 1,
13     };
14 }
15
16 sub option_fields {
17   ( 
18     'unless_pkgpart' => { 'label'    => 'Packages: ',
19                           'type'     => 'select-part_pkg',
20                           'multiple' => 1,
21                         },
22   );
23 }
24
25 #false laziness w/has_pkgpart.pm
26
27 sub condition {
28   my( $self, $object ) = @_;
29
30   my $cust_main = $self->cust_main($object);
31
32   my $unless_pkgpart = $self->option('unless_pkgpart') || {};
33   ! grep $unless_pkgpart->{ $_->pkgpart }, $cust_main->ncancelled_pkgs;
34 }
35
36 sub condition_sql {
37   my( $self, $table ) = @_;
38
39   'NOT '.
40   'ARRAY'. $self->condition_sql_option_option_integer('unless_pkgpart').
41   ' && '. #overlap (have elements in common)
42   'ARRAY( SELECT pkgpart FROM cust_pkg AS has_pkgpart_cust_pkg
43             WHERE has_pkgpart_cust_pkg.custnum = cust_main.custnum
44               AND (    has_pkgpart_cust_pkg.cancel IS NULL
45                     OR has_pkgpart_cust_pkg.cancel = 0
46                   )
47         )
48   ';
49 }
50
51 1;