discount action UI improvements: don't show 'Custom discount' which we can't add...
[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                        #(well, since 2013 it winds up calling select-discount
30                        # anyway (but not tr-select-discount)
31                        #'name_col' => 'description', #well, method
32                        #'order_by' => 'ORDER BY discountnum', #requied because name_col is a method
33                        'disable_empty' => 1,
34                        'hashref'  => { 'disabled' => '',
35                                        'months'   => { op=>'!=', value=>'0' },
36                                      },
37                        'disable_custom_discount' => 1,
38                      },
39   );
40 }
41
42 #lots of false laziness with referral_pkg_discount
43 #but also lots of doing it differently...and better???
44 sub do_action {
45   my( $self, $object, $cust_event ) = @_;
46
47   my $cust_main = $self->cust_main($object);
48   my %if_pkgpart = map { $_=>1 } split(/\s*,\s*/, $self->option('if_pkgpart') );
49   my $allpkgs = (keys %if_pkgpart) ? 0 : 1;
50
51   my @cust_pkg = ();
52   if ( $object->table eq 'cust_pkg' ) {
53
54     return 'Package is suspended' if $object->susp;
55     return 'Package not selected'
56       if ! $allpkgs && ! $if_pkgpart{ $object->pkgpart };
57     return 'Package frequency not monthly or a multiple'
58       if $object->part_pkg->freq !~ /^\d+$/;
59
60     @cust_pkg = ( $object );
61
62   } else {
63
64     @cust_pkg = grep { ( $allpkgs || $if_pkgpart{ $_->pkgpart } ) 
65                          && $_->part_pkg->freq
66                          #remove after fixing discount bug with non-monthly pkgs
67                          && ( $_->part_pkg->freq =~ /^\d+$/) } 
68                      $cust_main->unsuspended_pkgs;
69     return 'No qualifying packages' unless @cust_pkg;
70
71   }
72
73   my $gotit = 0;
74   foreach my $cust_pkg (@cust_pkg) {
75
76     my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
77
78     #our logic here only makes sense insomuch as you can't have multiple discounts
79     die "Unexpected multiple discounts, contact developers"
80       if scalar(@cust_pkg_discount) > 1;
81
82     my @my_cust_pkg_discount =
83       grep { $_->discountnum == $self->option('discountnum') } @cust_pkg_discount;
84
85     if ( @my_cust_pkg_discount ) { #reset the existing one instead
86
87       $gotit = 1;
88
89       #it's already got this discount and discount never expires--great, move on
90       next unless $cust_pkg_discount[0]->discount->months;
91         
92       #reset the discount
93       my $error = $cust_pkg_discount[0]->decrement_months_used( $cust_pkg_discount[0]->months_used );
94       die "Error extending discount: $error\n" if $error;
95
96     } elsif ( @cust_pkg_discount ) {
97
98       #can't currently discount an already discounted package,
99       #but maybe we can discount a different package
100       next;
101
102     } else { #normal case, create a new one
103
104       $gotit = 1;
105       my $cust_pkg_discount = new FS::cust_pkg_discount {
106         'pkgnum'      => $cust_pkg->pkgnum,
107         'discountnum' => $self->option('discountnum'),
108         'months_used' => 0
109       };
110       my $error = $cust_pkg_discount->insert;
111       die "Error discounting package: $error\n" if $error;
112
113     }
114   }
115
116   return $gotit ? '' : 'Discount not applied due to existing discounts';
117
118 }
119
120 1;