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
40 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
41 my $conf = new FS::Conf;
43 my $br_permonth = $self->base_recur_permonth($cust_pkg, $sdate);
44 $br_permonth += $param->{'override_charges'} if $param->{'override_charges'};
46 my $br = $self->base_recur($cust_pkg, $sdate);
47 $br += $param->{'override_charges'} * ($cust_pkg->part_pkg->freq || 0) if $param->{'override_charges'};
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 ) {
84 my $discount = $cust_pkg_discount->discount;
85 #UI enforces one or the other (for now? probably for good)
87 $amount += $discount->amount
88 if $cust_pkg->pkgpart == $param->{'real_pkgpart'};
89 $amount += sprintf('%.2f', $discount->percent * $br_permonth / 100 ); # FIXME: should this use $br / $freq to avoid rounding errors?
90 my $chg_months = defined($param->{'months'}) ?
92 $cust_pkg->part_pkg->freq;
94 my $months = $discount->months
96 $discount->months - $cust_pkg_discount->months_used )
99 if (defined $param->{'setup_charge'}) {
100 next unless $discount->setup;
102 if ( $discount->percent > 0 ) {
103 $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
105 } elsif ( $discount->amount > 0 && $discount->months == 1) {
106 $discount_left = $param->{'setup_charge'} - $discount->amount;
107 $amount = $param->{'setup_charge'} if $discount_left < 0;
108 $amount = $discount->amount if $discount_left >= 0;
111 # transfer remainder of discount, if any, to recur
112 $param->{'discount_left_recur'}{$discount->discountnum} =
113 0 - $discount_left if $discount_left < 0;
117 } elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
118 && $param->{'discount_left_recur'}{$discount->discountnum} > 0
120 # use up transferred remainder of discount from setup
121 $amount = $param->{'discount_left_recur'}{$discount->discountnum};
122 $param->{'discount_left_recur'}{$discount->discountnum} = 0;
124 } elsif ( $discount->setup
125 && $discount->months == 1
126 && $discount->amount > 0
131 if ( ! defined $param->{'setup_charge'} ) {
132 if ( $cust_pkg->pkgpart == $param->{'real_pkgpart'} ) {
133 push @{ $param->{precommit_hooks} }, sub {
134 my $error = $cust_pkg_discount->increment_months_used($months);
135 die "error discounting: $error" if $error;
139 $amount = min($amount * $months, $br);
142 $amount = sprintf('%.2f', $amount + 0.00000001 ); #so 1.005 rounds to 1.01
144 next unless $amount > 0;
146 # transfer remainder of discount, if any, to setup
147 if ( $discount->setup && $discount->amount > 0
148 && (!$discount->months || $discount->months != 1)
149 && !defined $param->{'setup_charge'}
152 $discount_left = $br_permonth - $amount; # FIXME: $amount is no longer permonth at this point!
153 if ( $discount_left < 0 ) {
154 $amount = $br_permonth; # FIXME: seems like this should *= $months
155 $param->{'discount_left_setup'}{$discount->discountnum} =
160 #record details in cust_bill_pkg_discount
161 my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
162 'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
166 push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
167 $tot_discount += $amount;
169 #add details on discount to invoice
170 # no longer! this is now done during rendering based on the existence
171 # of the cust_bill_pkg_discount record
173 #my $money_char = $conf->config('money_char') || '$';
174 #$months = sprintf('%.2f', $months) if $months =~ /\./;
176 #my $d = 'Includes ';
179 #if ( $months eq '1' ) {
180 # $d .= "discount of $money_char$amount";
181 # $d .= " each" if $cust_pkg->quantity > 1;
182 # $format = 'Undiscounted amount: %s%.2f';
184 # $d .= 'setup ' if defined $param->{'setup_charge'};
185 # $d .= 'discount of '. $discount->description_short;
186 # $d .= " for $months months"
187 # unless defined $param->{'setup_charge'};
188 # $d .= ": $money_char$amount" if $discount->percent;
189 # $format = 'Undiscounted monthly amount: %s%.2f';
193 #push @$details, sprintf( $format, $money_char, $br_permonth );
197 sprintf('%.2f', $tot_discount);