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