947f0c37daf8531b9c5e8a38a771f2fac40c518e
[freeside.git] / FS / FS / part_event / Action / addtag.pm
1 package FS::part_event::Action::addtag;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5 use FS::Record qw( qsearch );
6
7 sub description { 'Add customer tag'; }
8
9 sub eventtable_hashref {
10     { 'cust_main'      => 1,
11       'cust_bill'      => 1,
12       'cust_pkg'       => 1,
13       'cust_pay'       => 1,
14       'cust_pay_batch' => 1,
15       'cust_statement' => 1,
16     };
17 }
18
19 sub option_fields {
20   (
21     'tagnum'  => { 'label'    => 'Customer tag',
22                    'type'     => 'select-cust_tag',
23                    'multiple' => 1,
24                  },
25   );
26 }
27
28 sub default_weight { 20; }
29
30 sub do_action {
31   my( $self, $object, $tagnum ) = @_;
32
33   my %exists = map { $_->tagnum => $_->tagnum } 
34         qsearch({
35           table     => 'cust_tag',
36           hashref   => { custnum  => $object->custnum, },
37         });
38
39   my @tags = split(/\,/, $self->option('tagnum'));
40   foreach my $tagnum ( split(/\,/, $self->option('tagnum') ) ) {
41     if ( !$exists{$tagnum} ) {
42       my $cust_tag = new FS::cust_tag { 'tagnum'  => $tagnum,
43                                         'custnum' => $object->custnum, };
44       my $error = $cust_tag->insert;
45       if ( $error ) {
46         return $error;
47       }
48     }
49   }
50   '';
51 }
52
53 1;