implement condition_sql for FS::part_event::Condition::cust_bill_hasnt_noauto, RT12714
[freeside.git] / FS / FS / part_event / Condition / cust_bill_hasnt_noauto.pm
1 package FS::part_event::Condition::cust_bill_hasnt_noauto;
2
3 use strict;
4 use FS::cust_bill;
5
6 use base qw( FS::part_event::Condition );
7
8 sub description {
9   'Invoice eligible for automatic collection';
10 }
11
12 sub eventtable_hashref {
13     { 'cust_main' => 0,
14       'cust_bill' => 1,
15       'cust_pkg'  => 0,
16     };
17 }
18
19 sub condition {
20   #my($self, $cust_bill, %opt) = @_;
21   my($self, $cust_bill) = @_;
22
23   ! $cust_bill->no_auto;
24 }
25
26 sub condition_sql {
27   my( $class, $table, %opt ) = @_;
28   
29   # XXX: can be made faster with optimizations?
30   # -remove some/all sub-selects?
31   # -remove the two main separate selects?
32   # -add indices on cust_pkg.no_auto and part_pkg.no_auto and others?
33
34   "0 = (select count(1) from cust_pkg 
35             where cust_pkg.no_auto = 'Y' and cust_pkg.pkgnum in
36                 (select distinct cust_bill_pkg.pkgnum 
37                     from cust_bill_pkg, cust_pkg 
38                     where cust_bill_pkg.pkgnum = cust_pkg.pkgnum
39                         and cust_bill_pkg.invnum = cust_bill.invnum
40                         and cust_bill_pkg.pkgnum > 0
41                 )
42         )
43    AND
44    0 = (select count(1) from part_pkg 
45             where part_pkg.no_auto = 'Y' and part_pkg.pkgpart in
46                 (select cust_pkg.pkgpart from cust_pkg 
47                     where pkgnum in 
48                         (select distinct cust_bill_pkg.pkgnum 
49                             from cust_bill_pkg, cust_pkg 
50                             where cust_bill_pkg.pkgnum = cust_pkg.pkgnum 
51                                 and cust_bill_pkg.invnum = cust_bill.invnum
52                                 and cust_bill_pkg.pkgnum > 0
53                         ) 
54                 )
55         )
56   ";
57 }
58
59 1;