1 package FS::part_pkg::discount_Mixin;
5 use Time::Local qw( timelocal );
6 use List::Util qw( min );
7 use FS::Record qw( qsearchs );
9 use FS::cust_bill_pkg_discount;
11 %info = ( 'disabled' => 1 );
15 FS::part_pkg::discount_Mixin - Mixin class for part_pkg:: classes that
20 package FS::part_pkg::...;
21 use base qw( FS::part_pkg::discount_Mixin );
25 my $discount = $self->calc_discount($cust_pkg, $$sdate, $details, $param);
32 =item calc_discount CUST_PKG, SDATE, DETAILS_ARRAYREF, PARAM_HASHREF
34 Takes all the arguments of calc_recur. Calculates and returns the amount
35 by which to reduce the charge; also increments months used on the discount.
37 If there is a setup fee, this will be called once with 'setup_charge' => the
38 setup fee amount (and should return the discount to be applied to the setup
39 charge, if any), and again without it (for the recurring fee discount).
40 PARAM_HASHREF carries over between the two invocations.
45 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
46 my $conf = new FS::Conf;
48 my $br = $self->base_recur($cust_pkg, $sdate);
49 $br += $param->{'override_charges'} * ($cust_pkg->part_pkg->freq || 0) if $param->{'override_charges'};
52 #UI enforces just 1 for now, will need ordering when they can be stacked
54 # discount setup/recur splitting DOES NOT TOUCH THIS YET.
55 # we need some kind of monitoring to see who if anyone still uses term
57 if ( $param->{freq_override} ) {
58 # When a customer pays for more than one month at a time to receive a
59 # term discount, freq_override is set to the number of months.
60 my $real_part_pkg = new FS::part_pkg { $self->hash };
61 $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
62 # Find a discount with that duration...
63 my @discount = grep { $_->months == $param->{freq_override} }
64 map { $_->discount } $real_part_pkg->part_pkg_discount;
65 my $discount = shift @discount;
66 # and default to bill that many months at once.
67 $param->{months} = $param->{freq_override} unless $param->{months};
70 # Then set the cust_pkg discount.
71 if ($discount->months == $param->{months}) {
72 $cust_pkg->discountnum($discount->discountnum);
73 $error = $cust_pkg->insert_discount;
75 $cust_pkg->discountnum(-1);
76 foreach ( qw( amount percent months ) ) {
77 my $method = "discountnum_$_";
78 $cust_pkg->$method($discount->$_);
80 $error = $cust_pkg->insert_discount;
82 die "error discounting using part_pkg_discount: $error" if $error;
86 my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
88 if ( defined $param->{'setup_charge'} ) {
89 @cust_pkg_discount = grep { $_->setuprecur eq 'setup' } @cust_pkg_discount;
91 @cust_pkg_discount = grep { $_->setuprecur eq 'recur' } @cust_pkg_discount;
94 foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
96 my $discount = $cust_pkg_discount->discount;
97 #UI enforces one or the other (for now? probably for good)
98 # $chg_months: the number of months we are charging recur for
99 # $months: $chg_months or the months left on the discount, whchever is less
101 my $chg_months = $cust_pkg->part_pkg->freq || 1;
102 if ( defined($param->{'months'}) ) { # then override
103 $chg_months = $param->{'months'};
106 my $months = $chg_months;
107 if ( $discount->months ) {
108 $months = min( $chg_months,
109 $discount->months - $cust_pkg_discount->months_used );
112 # $amount is now the (estimated) discount amount on the recurring charge.
113 # if it's a percent discount, that's base recur * percentage.
117 if (defined $param->{'setup_charge'}) {
119 # we are calculating the setup discount.
120 # if this discount doesn't apply to setup fees, skip it.
121 # if it's a percent discount, set $amount = percent * setup_charge.
122 # if it's a flat amount discount for one month:
123 # - if the discount amount > setup_charge, then set it to setup_charge,
124 # and set 'discount_left_recur' to the difference.
125 # - otherwise set it to just the discount amount.
126 # if it's a flat amount discount for other than one month:
127 # - skip the discount. unsure, leaving it alone for now.
129 $months = 0; # never count a setup discount as a month of discount
130 # (the recur discount in the same month should do it)
132 if ( $discount->percent > 0 ) {
133 $amount = $discount->percent * $param->{'setup_charge'} / 100;
134 } elsif ( $discount->amount > 0 ) {
135 # apply the discount amount, up to a maximum of the setup charge
136 $amount = min($discount->amount, $param->{'setup_charge'});
137 $discount_left = sprintf('%.2f', $discount->amount - $amount);
138 # transfer remainder of discount, if any, to recur
139 $param->{'discount_left_recur'}{$discount->discountnum} = $discount_left;
144 # we are calculating a recurring fee discount. estimate the recurring
145 # fee. Note we use $months here rather than $chg_months so that if the
146 # remaining discount amount is for less time than the package period,
147 # the "estimated recurring fee" is only for as long as the discount
150 my $recur_charge = $br * $months / $self->freq;
151 # round this, because the real recur charge is rounded
152 $recur_charge = sprintf('%.2f', $recur_charge);
154 # if it's a percentage discount, calculate it based on that estimate.
155 # otherwise use the flat amount.
157 if ( $discount->percent > 0 ) {
158 $amount = $recur_charge * $discount->percent / 100;
159 } elsif ( $discount->amount > 0
160 and $cust_pkg->pkgpart == $param->{'real_pkgpart'} ) {
161 $amount = $discount->amount * $months;
164 if ( exists $param->{'discount_left_recur'}{$discount->discountnum} ) {
165 # there is a discount_left_recur entry for this discountnum, so this
166 # is the second (recur) pass on the discount. use up transferred
167 # remainder of discount from setup.
169 # note that discount_left_recur can now be zero.
170 $amount = $param->{'discount_left_recur'}{$discount->discountnum};
171 $param->{'discount_left_recur'}{$discount->discountnum} = 0;
172 $months = 1; # XXX really? not $chg_months?
174 #elsif ( $discount->setup
175 # && ($discount->months || 0) == 1
176 # && $discount->amount > 0
180 # RT #11512: bugfix to prevent applying flat discount to both setup
181 # and recur. The original implementation ignored discount_left_recur
182 # if it was zero, so if the setup fee used up the entire flat
183 # discount, the recurring charge would get to use the entire flat
184 # discount also. This bugfix was a kludge. Instead, we now allow
185 # discount_left_recur to be zero in that case, and then the available
186 # recur discount is zero.
189 # Transfer remainder of discount, if any, to setup
190 # This is used when the recur phase wants to add a setup fee
191 # (prorate_defer_bill): the "discount_left_setup" amount will
192 # be subtracted in _make_lines.
193 if ( $discount->amount > 0 && ($discount->months || 0) != 1 )
195 # make sure there is a setup discount with this discountnum
196 # on the same package.
197 if ( qsearchs('cust_pkg_discount', {
198 pkgnum => $cust_pkg->pkgnum,
199 discountnum => $discount->discountnum,
200 setuprecur => 'setup'
203 # $amount is no longer permonth at this point! correct. very good.
204 $discount_left = $amount - $recur_charge; # backward, as above
205 if ( $discount_left > 0 ) {
206 $amount = $recur_charge;
207 $param->{'discount_left_setup'}{$discount->discountnum} =
213 # cap the discount amount at the recur charge
214 $amount = min($amount, $recur_charge);
216 # if this is the base pkgpart, schedule increment_months_used to run at
217 # the end of billing. (addon packages haven't been calculated yet, so
218 # don't let the discount expire during the billing process. RT#17045.)
219 if ( $cust_pkg->pkgpart == $param->{'real_pkgpart'} ) {
220 push @{ $param->{precommit_hooks} }, sub {
221 my $error = $cust_pkg_discount->increment_months_used($months);
222 die "error discounting: $error" if $error;
226 } # else not 'setup_charge'
228 $amount = sprintf('%.2f', $amount + 0.00000001 ); #so 1.005 rounds to 1.01
230 next unless $amount > 0;
232 #record details in cust_bill_pkg_discount
233 my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
234 'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
238 push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
239 $tot_discount += $amount;
243 sprintf('%.2f', $tot_discount);