RT# 76303 - fixed bug with tags not saving when selected
[freeside.git] / FS / FS / part_event / Condition / has_cust_tag.pm
1 package FS::part_event::Condition::has_cust_tag;
2 use base qw( FS::part_event::Condition );
3
4 use strict;
5 #use FS::Record qw( qsearch );
6
7 sub description {
8   'Customer has tag',
9 }
10
11 sub eventtable_hashref {
12     { 'cust_main'      => 1,
13       'cust_bill'      => 1,
14       'cust_pkg'       => 1,
15       'cust_pay'       => 1,
16       'cust_pay_batch' => 1,
17       'cust_statement' => 1,
18     };
19 }
20
21 sub option_fields {
22   (
23     'tagnum'  => { 'label'    => 'Customer tag',
24                    'type'     => 'select-cust_tag',
25                    'multiple' => 1,
26                  },
27   );
28 }
29
30 sub condition {
31   my( $self, $object ) = @_;
32
33   my $cust_main = $self->cust_main($object);
34
35   my $hashref = $self->option('tagnum') || {};
36   grep $hashref->{ $_->tagnum }, $cust_main->cust_tag;
37 }
38
39 sub condition_sql {
40   my( $self, $table ) = @_;
41
42   my $matching_tags = 
43     "SELECT tagnum FROM cust_tag WHERE cust_tag.custnum = $table.custnum".
44     " AND cust_tag.tagnum IN ".
45     $self->condition_sql_option_option_integer('tagnum');
46
47   "EXISTS($matching_tags)";
48 }
49
50 1;