1 package FS::part_pkg::discount_Mixin;
4 use vars qw(@ISA %info);
7 use FS::cust_bill_pkg_discount;
8 use Time::Local qw(timelocal);
11 @ISA = qw(FS::part_pkg);
12 %info = ( 'disabled' => 1 );
16 FS::part_pkg::discount_Mixin - Mixin class for part_pkg:: classes that
21 package FS::part_pkg::...;
22 use base qw( FS::part_pkg::discount_Mixin );
26 my $discount = $self->calc_discount($cust_pkg, $$sdate, $details, $param);
35 Takes all the arguments of calc_recur. Calculates and returns the amount
36 by which to reduce the recurring fee; also increments months used on the
37 discount and generates an invoice detail describing it.
42 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
44 my $br = $self->base_recur($cust_pkg, $sdate);
45 $br += $param->{'override_charges'} if $param->{'override_charges'};
47 return 0 if defined $param->{'setup_charge'} && $param->{'setup_charge'} == 0;
50 #UI enforces just 1 for now, will need ordering when they can be stacked
52 if ( $param->{freq_override} ) {
53 # When a customer pays for more than one month at a time to receive a
54 # term discount, freq_override is set to the number of months.
55 my $real_part_pkg = new FS::part_pkg { $self->hash };
56 $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
57 # Find a discount with that duration...
58 my @discount = grep { $_->months == $param->{freq_override} }
59 map { $_->discount } $real_part_pkg->part_pkg_discount;
60 my $discount = shift @discount;
61 # and default to bill that many months at once.
62 $param->{months} = $param->{freq_override} unless $param->{months};
65 # Then set the cust_pkg discount.
66 if ($discount->months == $param->{months}) {
67 $cust_pkg->discountnum($discount->discountnum);
68 $error = $cust_pkg->insert_discount;
70 $cust_pkg->discountnum(-1);
71 foreach ( qw( amount percent months ) ) {
72 my $method = "discountnum_$_";
73 $cust_pkg->$method($discount->$_);
75 $error = $cust_pkg->insert_discount;
77 die "error discounting using part_pkg_discount: $error" if $error;
81 my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
82 foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
83 my $discount = $cust_pkg_discount->discount;
84 #UI enforces one or the other (for now? probably for good)
86 $amount += $discount->amount
87 if $cust_pkg->pkgpart == $param->{real_pkgpart};
88 $amount += sprintf('%.2f', $discount->percent * $br / 100 );
89 my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
91 my $months = $discount->months
93 $discount->months - $cust_pkg_discount->months_used )
96 if(defined $param->{'setup_charge'}) {
97 next unless $discount->setup;
99 if ( $discount->percent ) {
100 $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
105 my $error = $cust_pkg_discount->increment_months_used($months)
106 if ($cust_pkg->pkgpart == $param->{real_pkgpart}
107 && ! defined $param->{'setup_charge'});
108 die "error discounting: $error" if $error;
111 $amount = sprintf('%.2f', $amount);
113 next unless $amount > 0;
115 #record details in cust_bill_pkg_discount
116 my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
117 'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
121 push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
123 #add details on discount to invoice
124 my $conf = new FS::Conf;
125 my $money_char = $conf->config('money_char') || '$';
126 $months = sprintf('%.2f', $months) if $months =~ /\./;
129 $d .= 'setup ' if defined $param->{'setup_charge'};
130 $d .= 'discount of '. $discount->description_short;
131 $d .= " for $months month". ( $months!=1 ? 's' : '' ) unless defined $param->{'setup_charge'};
132 $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
135 $tot_discount += $amount;
138 sprintf('%.2f', $tot_discount);