1 package FS::part_event::Action::referral_pkg_discount;
4 use base qw( FS::part_event::Action );
6 sub description { "Discount the referring customer's package"; }
8 #sub eventtable_hashref {
13 'if_pkgpart' => { 'label' => 'Only packages',
14 'type' => 'select-part_pkg',
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' },
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
33 my( $self, $cust_object, $cust_event ) = @_;
35 my $cust_main = $self->cust_main($cust_object);
37 return 'No referring customer' unless $cust_main->referral_custnum;
39 my $referring_cust_main = $cust_main->referring_cust_main;
40 #return 'Referring customer is cancelled'
41 # if $referring_cust_main->status eq 'cancelled';
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;
48 my $cust_pkg = $cust_pkg[0]; #only one
50 #end of false laziness
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;
56 if ( @my_cust_pkg_discount ) { #increment the existing one instead
58 die "guru meditation #and: multiple discounts"
59 if scalar(@my_cust_pkg_discount) > 1;
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;
66 my $error = $cust_pkg_discount->decrement_months_used( $discount->months );
67 die "Error extending discount: $error\n" if $error;
69 } elsif ( @cust_pkg_discount ) {
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";
76 } else { #normal case, create a new one
78 my $cust_pkg_discount = new FS::cust_pkg_discount {
79 'pkgnum' => $cust_pkg->pkgnum,
80 'discountnum' => $self->option('discountnum'),
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,
92 my $error = $cust_pkg_discount->insert;
93 die "Error discounting package: $error\n" if $error;