suspend event option to skip packages with a start_date, RT#83847
[freeside.git] / FS / FS / part_event / Action / referral_pkg_discount.pm
1 package FS::part_event::Action::referral_pkg_discount;
2
3 use strict;
4 use base qw( FS::part_event::Action );
5
6 sub description { "Discount the referring customer's package"; }
7
8 #sub eventtable_hashref {
9 #}
10
11 sub option_fields {
12   (
13     'if_pkgpart'  => { 'label'    => 'Only packages',
14                        'type'     => 'select-part_pkg',
15                        'multiple' => 1,
16                      },
17     'discountnum' => { 'label'    => 'Discount',
18                        'type'     => 'select-table', #we don't handle the select-discount create a discount case
19                        'table'    => 'discount',
20                        'name_col' => 'description', #well, method
21                        'order_by' => 'ORDER BY discountnum', #requied because name_col is a method
22                        'hashref'  => { 'disabled' => '',
23                                        'months'   => { op=>'!=', value=>'0' },
24                                      },
25                        'disable_empty' => 1,
26                      },
27   );
28 }
29
30 #false laziness w/referral_pkg_billdate, probably should make
31 # Mixin/referral_pkg.pm if we need changes or anything else in this vein
32 sub do_action {
33   my( $self, $cust_object, $cust_event ) = @_;
34
35   my $cust_main = $self->cust_main($cust_object);
36
37   return 'No referring customer' unless $cust_main->referral_custnum;
38
39   my $referring_cust_main = $cust_main->referring_cust_main;
40   #return 'Referring customer is cancelled'
41   #  if $referring_cust_main->status eq 'cancelled';
42
43   my %if_pkgpart = map { $_=>1 } split(/\s*,\s*/, $self->option('if_pkgpart') );
44   my @cust_pkg = grep $if_pkgpart{ $_->pkgpart },
45                       $referring_cust_main->billing_pkgs;
46   return 'No qualifying billing package definition' unless @cust_pkg;
47
48   my $cust_pkg = $cust_pkg[0]; #only one
49
50   #end of false laziness
51
52   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
53   my @my_cust_pkg_discount =
54     grep { $_->discountnum == $self->option('discountnum') } @cust_pkg_discount;
55
56   if ( @my_cust_pkg_discount ) { #increment the existing one instead
57
58     die "guru meditation #and: multiple discounts"
59       if scalar(@my_cust_pkg_discount) > 1;
60  
61     my $cust_pkg_discount = $my_cust_pkg_discount[0];
62     my $discount = $cust_pkg_discount->discount;
63     die "guru meditation #goob: can't extended non-expiring discount"
64       if $discount->months == 0;
65
66     my $error = $cust_pkg_discount->decrement_months_used( $discount->months );
67     die "Error extending discount: $error\n" if $error;
68
69   } elsif ( @cust_pkg_discount ) {
70
71     #"stacked" discount case not possible from UI, not handled, so prevent
72     # against creating one here.  i guess we could try to find a different
73     # @cust_pkg above if this case needed to be handled better?
74     die "Can't discount an already discounted package";
75
76   } else { #normal case, create a new one
77
78     my $cust_pkg_discount = new FS::cust_pkg_discount {
79       'pkgnum'      => $cust_pkg->pkgnum,
80       'discountnum' => $self->option('discountnum'),
81       'months_used' => 0,
82       #'end_date'    => '',
83       #we dont handle the create a new discount case
84       #'_type'       => scalar($cgi->param('discountnum__type')),
85       #'amount'      => scalar($cgi->param('discountnum_amount')),
86       #'percent'     => scalar($cgi->param('discountnum_percent')),
87       #'months'      => scalar($cgi->param('discountnum_months')),
88       #'setup'       => scalar($cgi->param('discountnum_setup')),
89       ##'linked'       => scalar($cgi->param('discountnum_linked')),
90       ##'disabled'    => $self->discountnum_disabled,
91     };
92     my $error = $cust_pkg_discount->insert;
93     die "Error discounting package: $error\n" if $error;
94
95   }
96
97   '';
98
99 }
100
101 1;