discount as a package-specific event, RT#39870
[freeside.git] / FS / FS / part_event / Action / pkg_discount.pm
1 package FS::part_event::Action::pkg_discount;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5
6 sub description { "Discount unsuspended package(s) (monthly recurring only)"; }
7
8 sub eventtable_hashref {
9   { 'cust_main' => 1,
10     'cust_pkg'  => 1,
11   };
12 }
13
14 sub event_stage { 'pre-bill'; }
15
16 sub option_fields {
17   (
18     'if_pkgpart'  => { 'label'    => 'Only packages',
19                        'type'     => 'select-table',
20                        'table'    => 'part_pkg',
21                        'name_col' => 'pkg',
22                        #can tweak after fixing discount bug with non-monthly recurring pkgs 
23                        'extra_sql' => q(AND freq NOT LIKE '0%' AND freq NOT LIKE '%d' AND freq NOT LIKE '%h' AND freq NOT LIKE '%w'), 
24                        'multiple' => 1,
25                      },
26     'discountnum' => { 'label'    => 'Discount',
27                        'type'     => 'select-table', #we don't handle the select-discount create a discount case
28                        'table'    => 'discount',
29                        'name_col' => 'description', #well, method
30                        'order_by' => 'ORDER BY discountnum', #requied because name_col is a method
31                        'hashref'  => { 'disabled' => '',
32                                        'months'   => { op=>'!=', value=>'0' },
33                                      },
34                        'disable_empty' => 1,
35                      },
36   );
37 }
38
39 #lots of false laziness with referral_pkg_discount
40 #but also lots of doing it differently...and better???
41 sub do_action {
42   my( $self, $object, $cust_event ) = @_;
43
44   my $cust_main = $self->cust_main($object);
45   my %if_pkgpart = map { $_=>1 } split(/\s*,\s*/, $self->option('if_pkgpart') );
46   my $allpkgs = (keys %if_pkgpart) ? 0 : 1;
47
48   my @cust_pkg = ();
49   if ( $object->table eq 'cust_pkg' ) {
50
51     return 'Package is suspended' if $object->susp;
52     return 'Package not selected'
53       if ! $allpkgs && ! $if_pkgpart{ $object->pkgpart };
54     return 'Package frequency not monthly or a multiple'
55       if $object->part_pkg->freq !~ /^\d+$/;
56
57     @cust_pkg = ( $object );
58
59   } else {
60
61     @cust_pkg = grep { ( $allpkgs || $if_pkgpart{ $_->pkgpart } ) 
62                          && $_->part_pkg->freq
63                          #remove after fixing discount bug with non-monthly pkgs
64                          && ( $_->part_pkg->freq =~ /^\d+$/) } 
65                      $cust_main->unsuspended_pkgs;
66     return 'No qualifying packages' unless @cust_pkg;
67
68   }
69
70   my $gotit = 0;
71   foreach my $cust_pkg (@cust_pkg) {
72
73     my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
74
75     #our logic here only makes sense insomuch as you can't have multiple discounts
76     die "Unexpected multiple discounts, contact developers"
77       if scalar(@cust_pkg_discount) > 1;
78
79     my @my_cust_pkg_discount =
80       grep { $_->discountnum == $self->option('discountnum') } @cust_pkg_discount;
81
82     if ( @my_cust_pkg_discount ) { #reset the existing one instead
83
84       $gotit = 1;
85
86       #it's already got this discount and discount never expires--great, move on
87       next unless $cust_pkg_discount[0]->discount->months;
88         
89       #reset the discount
90       my $error = $cust_pkg_discount[0]->decrement_months_used( $cust_pkg_discount[0]->months_used );
91       die "Error extending discount: $error\n" if $error;
92
93     } elsif ( @cust_pkg_discount ) {
94
95       #can't currently discount an already discounted package,
96       #but maybe we can discount a different package
97       next;
98
99     } else { #normal case, create a new one
100
101       $gotit = 1;
102       my $cust_pkg_discount = new FS::cust_pkg_discount {
103         'pkgnum'      => $cust_pkg->pkgnum,
104         'discountnum' => $self->option('discountnum'),
105         'months_used' => 0
106       };
107       my $error = $cust_pkg_discount->insert;
108       die "Error discounting package: $error\n" if $error;
109
110     }
111   }
112
113   return $gotit ? '' : 'Discount not applied due to existing discounts';
114
115 }
116
117 1;