1 package FS::part_pkg::discount_Mixin;
5 use Time::Local qw( timelocal );
6 use List::Util qw( min );
8 use FS::cust_bill_pkg_discount;
10 %info = ( 'disabled' => 1 );
14 FS::part_pkg::discount_Mixin - Mixin class for part_pkg:: classes that
19 package FS::part_pkg::...;
20 use base qw( FS::part_pkg::discount_Mixin );
24 my $discount = $self->calc_discount($cust_pkg, $$sdate, $details, $param);
33 Takes all the arguments of calc_recur. Calculates and returns the amount
34 by which to reduce the recurring fee; also increments months used on the
35 discount and generates an invoice detail describing it.
40 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
41 my $conf = new FS::Conf;
43 my $br = $self->base_recur_permonth($cust_pkg, $sdate);
44 $br += $param->{'override_charges'} if $param->{'override_charges'};
47 #UI enforces just 1 for now, will need ordering when they can be stacked
49 if ( $param->{freq_override} ) {
50 # When a customer pays for more than one month at a time to receive a
51 # term discount, freq_override is set to the number of months.
52 my $real_part_pkg = new FS::part_pkg { $self->hash };
53 $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
54 # Find a discount with that duration...
55 my @discount = grep { $_->months == $param->{freq_override} }
56 map { $_->discount } $real_part_pkg->part_pkg_discount;
57 my $discount = shift @discount;
58 # and default to bill that many months at once.
59 $param->{months} = $param->{freq_override} unless $param->{months};
62 # Then set the cust_pkg discount.
63 if ($discount->months == $param->{months}) {
64 $cust_pkg->discountnum($discount->discountnum);
65 $error = $cust_pkg->insert_discount;
67 $cust_pkg->discountnum(-1);
68 foreach ( qw( amount percent months ) ) {
69 my $method = "discountnum_$_";
70 $cust_pkg->$method($discount->$_);
72 $error = $cust_pkg->insert_discount;
74 die "error discounting using part_pkg_discount: $error" if $error;
78 my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
79 foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
81 my $discount = $cust_pkg_discount->discount;
82 #UI enforces one or the other (for now? probably for good)
84 $amount += $discount->amount
85 if defined $param->{'real_pkgpart'} && $cust_pkg->pkgpart == $param->{'real_pkgpart'};
86 $amount += sprintf('%.2f', $discount->percent * $br / 100 );
87 my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
89 my $months = $discount->months
91 $discount->months - $cust_pkg_discount->months_used )
94 if(defined $param->{'setup_charge'}) {
95 next unless $discount->setup;
97 if ( $discount->percent ) {
98 $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
101 elsif ( $discount->amount && $discount->months == 1) {
102 $discount_left = $param->{'setup_charge'} - $discount->amount;
103 $amount = $param->{'setup_charge'} if $discount_left < 0;
104 $amount = $discount->amount if $discount_left >= 0;
107 # transfer remainder of discount, if any, to recur
108 $param->{'discount_left_recur'}{$discount->discountnum} =
109 0 - $discount_left if $discount_left < 0;
115 elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
116 && $param->{'discount_left_recur'}{$discount->discountnum} > 0) {
117 # use up transferred remainder of discount from setup
118 $amount = $param->{'discount_left_recur'}{$discount->discountnum};
119 $param->{'discount_left_recur'}{$discount->discountnum} = 0;
122 elsif ( $discount->setup && $discount->months == 1 && $discount->amount ) {
126 my $error = $cust_pkg_discount->increment_months_used($months)
127 if (defined $param->{'real_pkgpart'}
128 && $cust_pkg->pkgpart == $param->{'real_pkgpart'}
129 && ! defined $param->{'setup_charge'});
130 die "error discounting: $error" if $error;
132 $amount = min($amount, $br);
134 $amount = sprintf('%.2f', $amount);
136 next unless $amount > 0;
138 # transfer remainder of discount, if any, to setup
139 if ( $discount->setup && $discount->amount
140 && (!$discount->months || $discount->months != 1)
141 && !defined $param->{'setup_charge'}) {
142 $discount_left = $br - $amount;
143 $amount = $br if $discount_left < 0;
144 $param->{'discount_left_setup'}{$discount->discountnum} =
145 0 - $discount_left if $discount_left < 0;
148 #record details in cust_bill_pkg_discount
149 my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
150 'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
154 push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
156 #add details on discount to invoice
157 my $money_char = $conf->config('money_char') || '$';
158 $months = sprintf('%.2f', $months) if $months =~ /\./;
161 $d .= 'setup ' if defined $param->{'setup_charge'};
162 $d .= 'discount of '. $discount->description_short;
163 $d .= " for $months month". ( $months!=1 ? 's' : '' ) unless defined $param->{'setup_charge'};
164 $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
167 $tot_discount += $amount;
170 sprintf('%.2f', $tot_discount);