Changed min() call to reduce possibility of rounding error returning a
[freeside.git] / FS / FS / part_pkg / discount_Mixin.pm
1 package FS::part_pkg::discount_Mixin;
2
3 use strict;
4 use vars qw( %info );
5 use Time::Local qw( timelocal );
6 use List::Util  qw( min );
7 use FS::cust_pkg;
8 use FS::cust_bill_pkg_discount;
9
10 %info = ( 'disabled' => 1 );
11
12 =head1 NAME
13
14 FS::part_pkg::discount_Mixin - Mixin class for part_pkg:: classes that 
15 can be discounted.
16
17 =head1 SYNOPSIS
18
19 package FS::part_pkg::...;
20 use base qw( FS::part_pkg::discount_Mixin );
21
22 sub calc_recur {
23   ...
24   my $discount = $self->calc_discount($cust_pkg, $$sdate, $details, $param);
25   $charge -= $discount;
26   ...
27 }
28
29 =head METHODS
30
31 =item calc_discount
32
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.
36
37 =cut
38
39 sub calc_discount {
40   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
41   my $conf = new FS::Conf;
42
43   my $br_permonth = $self->base_recur_permonth($cust_pkg, $sdate);
44   $br_permonth += $param->{'override_charges'} if $param->{'override_charges'};
45
46   my $br = $self->base_recur($cust_pkg, $sdate);
47   $br += $param->{'override_charges'} * ($cust_pkg->part_pkg->freq || 0) if $param->{'override_charges'};
48
49   my $tot_discount = 0;
50   #UI enforces just 1 for now, will need ordering when they can be stacked
51
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};
63     my $error;
64     if ($discount) {
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;
69       } else {
70         $cust_pkg->discountnum(-1);
71         foreach ( qw( amount percent months ) ) {
72           my $method = "discountnum_$_";
73           $cust_pkg->$method($discount->$_);
74         }
75         $error = $cust_pkg->insert_discount;
76       }
77       die "error discounting using part_pkg_discount: $error" if $error;
78     }
79   }
80
81   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
82   foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
83     my $discount_left;
84     my $discount = $cust_pkg_discount->discount;
85     #UI enforces one or the other (for now?  probably for good)
86     my $amount = 0;
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'}) ?
91                       $param->{'months'} :
92                       $cust_pkg->part_pkg->freq;
93
94     my $months = $discount->months
95     ? min( $chg_months,
96       $discount->months - $cust_pkg_discount->months_used )
97     : $chg_months;
98
99     if (defined $param->{'setup_charge'}) {
100         next unless $discount->setup;
101
102         if ( $discount->percent > 0 ) {
103             $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
104             $months = 1;
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;
109             $months = 1;
110                 
111             # transfer remainder of discount, if any, to recur
112             $param->{'discount_left_recur'}{$discount->discountnum} = 
113                 0 - $discount_left if $discount_left < 0;
114         } else {
115             next; 
116         }
117     } elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
118               && $param->{'discount_left_recur'}{$discount->discountnum} > 0
119             ) {
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;
123         $months = 1;
124     } elsif (    $discount->setup
125               && $discount->months == 1
126               && $discount->amount > 0
127             ) {
128         next;
129     }
130
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;
136         };
137       }
138
139       $amount = min($amount * $months, $br);
140     }
141
142     $amount = sprintf('%.2f', $amount + 0.00000001 ); #so 1.005 rounds to 1.01
143
144     next unless $amount > 0;
145
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'}
150        )
151     {
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} = 
156           0 - $discount_left;
157       }
158     }
159
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,
163       'amount'         => $amount,
164       'months'         => $months,
165     };
166     push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
167     $tot_discount += $amount;
168
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
172     #
173     #my $money_char = $conf->config('money_char') || '$';
174     #$months = sprintf('%.2f', $months) if $months =~ /\./;
175
176     #my $d = 'Includes ';
177     #my $format;
178
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';
183     #} else {
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';
190     #}
191
192     #push @$details, $d;
193     #push @$details, sprintf( $format, $money_char, $br_permonth );
194
195   }
196
197   sprintf('%.2f', $tot_discount);
198 }
199
200 1;