fix mixin inheritence preventing prorate_delayed packages from billing, RT#14372
[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 and generates an invoice detail describing it.
36
37 =cut
38
39 sub calc_discount {
40   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
41
42   my $br = $self->base_recur_permonth($cust_pkg, $sdate);
43   $br += $param->{'override_charges'} if $param->{'override_charges'};
44   
45   my $tot_discount = 0;
46   #UI enforces just 1 for now, will need ordering when they can be stacked
47
48   if ( $param->{freq_override} ) {
49     # When a customer pays for more than one month at a time to receive a 
50     # term discount, freq_override is set to the number of months.
51     my $real_part_pkg = new FS::part_pkg { $self->hash };
52     $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
53     # Find a discount with that duration...
54     my @discount = grep { $_->months == $param->{freq_override} }
55                     map { $_->discount } $real_part_pkg->part_pkg_discount;
56     my $discount = shift @discount;
57     # and default to bill that many months at once.
58     $param->{months} = $param->{freq_override} unless $param->{months};
59     my $error;
60     if ($discount) {
61       # Then set the cust_pkg discount.
62       if ($discount->months == $param->{months}) {
63         $cust_pkg->discountnum($discount->discountnum);
64         $error = $cust_pkg->insert_discount;
65       } else {
66         $cust_pkg->discountnum(-1);
67         foreach ( qw( amount percent months ) ) {
68           my $method = "discountnum_$_";
69           $cust_pkg->$method($discount->$_);
70         }
71         $error = $cust_pkg->insert_discount;
72       }
73       die "error discounting using part_pkg_discount: $error" if $error;
74     }
75   }
76
77   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
78   foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
79     my $discount_left;
80     my $discount = $cust_pkg_discount->discount;
81     #UI enforces one or the other (for now?  probably for good)
82     my $amount = 0;
83     $amount += $discount->amount
84         if defined $param->{'real_pkgpart'} && $cust_pkg->pkgpart == $param->{'real_pkgpart'};
85     $amount += sprintf('%.2f', $discount->percent * $br / 100 );
86     my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
87
88     my $months = $discount->months
89     ? min( $chg_months,
90       $discount->months - $cust_pkg_discount->months_used )
91     : $chg_months;
92
93     if(defined $param->{'setup_charge'}) {
94         next unless $discount->setup;
95
96         if ( $discount->percent ) {
97             $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
98             $months = 1;
99         }
100         elsif ( $discount->amount && $discount->months == 1) {
101             $discount_left = $param->{'setup_charge'} - $discount->amount;
102             $amount = $param->{'setup_charge'} if $discount_left < 0;
103             $amount = $discount->amount if $discount_left >= 0;
104             $months = 1;
105                 
106             # transfer remainder of discount, if any, to recur
107             $param->{'discount_left_recur'}{$discount->discountnum} = 
108                 0 - $discount_left if $discount_left < 0;
109         }
110         else {
111             next; 
112         }
113     }
114     elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
115         && $param->{'discount_left_recur'}{$discount->discountnum} > 0) {
116         # use up transferred remainder of discount from setup
117         $amount = $param->{'discount_left_recur'}{$discount->discountnum};
118         $param->{'discount_left_recur'}{$discount->discountnum} = 0;
119         $months = 1;
120     }
121     elsif ( $discount->setup && $discount->months == 1 && $discount->amount ) {
122         next;
123     }
124
125     my $error = $cust_pkg_discount->increment_months_used($months)
126         if (defined $param->{'real_pkgpart'} 
127             && $cust_pkg->pkgpart == $param->{'real_pkgpart'} 
128             && ! defined $param->{'setup_charge'});
129     die "error discounting: $error" if $error;
130
131     $amount = min($amount, $br);
132     $amount *= $months;
133     $amount = sprintf('%.2f', $amount);
134
135     next unless $amount > 0;
136
137     # transfer remainder of discount, if any, to setup
138     if ( $discount->setup && $discount->amount
139         && (!$discount->months || $discount->months != 1)
140         && !defined $param->{'setup_charge'}) {
141         $discount_left = $br - $amount;
142         $amount = $br if $discount_left < 0;
143         $param->{'discount_left_setup'}{$discount->discountnum} = 
144                                     0 - $discount_left if $discount_left < 0;
145     }
146
147     #record details in cust_bill_pkg_discount
148     my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
149       'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
150       'amount'         => $amount,
151       'months'         => $months,
152     };
153     push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
154
155     #add details on discount to invoice
156     my $conf = new FS::Conf;
157     my $money_char = $conf->config('money_char') || '$';
158     $months = sprintf('%.2f', $months) if $months =~ /\./;
159
160     my $d = 'Includes ';
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;
165     push @$details, $d;
166
167     $tot_discount += $amount;
168   }
169
170   sprintf('%.2f', $tot_discount);
171 }
172
173 1;