add condition_sql optimization to "Customer has a referring customer with uncancelled...
[freeside.git] / FS / FS / part_event / Condition / has_referral_pkgpart.pm
1 package FS::part_event::Condition::has_referral_pkgpart;
2 use base qw( FS::part_event::Condition );
3
4 use FS::part_event::Condition::has_referral_custnum;
5 #maybe i should be incorporated in has_referral_custnum
6
7 use strict;
8
9 sub description { 'Customer has a referring customer with uncancelled specific package(s)'; }
10
11 sub option_fields {
12   ( 
13     'if_pkgpart' => { 'label'    => 'Only packages: ',
14                       'type'     => 'select-part_pkg',
15                       'multiple' => 1,
16                       'toggle_disabled' => 1,
17                     },
18   );
19 }
20
21 #lots of falze laziness w/has_pkgpart..
22
23 sub condition {
24   my($self, $object, %opt) = @_;
25
26   return 0 unless FS::part_event::Condition::has_referral_custnum::condition($self, $object, %opt);
27
28   my $cust_main = $self->cust_main($object);
29
30   my $if_pkgpart = $self->option('if_pkgpart') || {};
31   grep $if_pkgpart->{ $_->pkgpart },
32     $cust_main->referral_custnum_cust_main->ncancelled_pkgs( 'skip_label_sort'=> 1);
33                                             #maybe billing_pkgs
34 }
35
36 sub condition_sql {
37   my( $self, $table ) = @_;
38
39   'ARRAY'. $self->condition_sql_option_option_integer('if_pkgpart').
40   ' && '. #overlap (have elements in common)
41   'ARRAY( SELECT pkgpart FROM cust_pkg AS has_referral_pkgpart_cust_pkg
42             WHERE has_referral_pkgpart_cust_pkg.custnum = cust_main.referral_custnum
43               AND (    has_referral_pkgpart_cust_pkg.cancel IS NULL
44                     OR has_referral_pkgpart_cust_pkg.cancel = 0
45                   )
46         )
47   ';
48 }
49
50 1;
51