fix multiple services w/cust_bill_has_service condition, RT#17035
[freeside.git] / FS / FS / part_event / Condition / cust_bill_has_service.pm
1 package FS::part_event::Condition::cust_bill_has_service;
2
3 use strict;
4 use FS::cust_bill;
5
6 use base qw( FS::part_event::Condition );
7
8 sub description {
9   'Invoice is billing for certain service definitions';
10 }
11
12 sub eventtable_hashref {
13     { 'cust_main' => 0,
14       'cust_bill' => 1,
15       'cust_pkg'  => 0,
16     };
17 }
18
19 # could not find component for path '/elements/tr-select-part_svc.html'
20 # sub disabled { 1; }
21
22 sub option_fields {
23   (
24     'has_service' => { 'label'      => 'Has service',
25                        'type'       => 'select-part_svc',
26                        'multiple'   => 1, #the HTML element seems to have it
27                                           #by default, but edit/part_event.html
28                                           #(and process) need to know
29                      },
30   );
31 }
32
33 sub condition {
34   #my($self, $cust_bill, %opt) = @_;
35   my($self, $cust_bill) = @_;
36
37   my $servicenum = $self->option('has_service') || {};
38
39   grep $servicenum->{ $_->svcpart },
40     map $_->cust_svc,
41         $cust_bill->cust_pkg;
42 }
43
44 sub condition_sql {
45   my( $class, $table, %opt ) = @_;
46
47   my $servicenum =
48     $class->condition_sql_option_option('has_service');
49
50   my $sql = qq| 0 < ( SELECT COUNT(cs.svcpart)
51      FROM cust_bill_pkg cbp, cust_svc cs
52     WHERE cbp.invnum = cust_bill.invnum
53       AND cs.pkgnum = cbp.pkgnum
54       AND cs.svcpart IN $servicenum
55   )
56   |;
57   return $sql;
58 }
59
60 1;