027625569d1d439f8e844ce2774d1f3a1aea0057
[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
33   "0 = (select count(1) from cust_pkg 
34             where cust_pkg.no_auto = 'Y' and cust_pkg.pkgnum in
35                 (select distinct cust_bill_pkg.pkgnum 
36                     from cust_bill_pkg, cust_pkg 
37                     where cust_bill_pkg.pkgnum = cust_pkg.pkgnum
38                         and cust_bill_pkg.invnum = cust_bill.invnum
39                         and cust_bill_pkg.pkgnum > 0
40                 )
41         )
42    AND
43    0 = (select count(1) from part_pkg 
44             where part_pkg.no_auto = 'Y' and part_pkg.pkgpart in
45                 (select cust_pkg.pkgpart from cust_pkg 
46                     where pkgnum in 
47                         (select distinct cust_bill_pkg.pkgnum 
48                             from cust_bill_pkg, cust_pkg 
49                             where cust_bill_pkg.pkgnum = cust_pkg.pkgnum 
50                                 and cust_bill_pkg.invnum = cust_bill.invnum
51                                 and cust_bill_pkg.pkgnum > 0
52                         ) 
53                 )
54         )
55   ";
56 }
57
58 1;