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 If the configuration option 'discount-show_available' is enabled, and this 
38 package is eligible for a prepayment discount but doesn't have one, an 
39 invoice detail will be generated to describe the available discounts.
40
41 =cut
42
43 sub calc_discount {
44   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
45   my $conf = new FS::Conf;
46
47   my $br = $self->base_recur_permonth($cust_pkg, $sdate);
48   $br += $param->{'override_charges'} if $param->{'override_charges'};
49   
50   my $tot_discount = 0;
51   #UI enforces just 1 for now, will need ordering when they can be stacked
52
53   if ( $param->{freq_override} ) {
54     # When a customer pays for more than one month at a time to receive a 
55     # term discount, freq_override is set to the number of months.
56     my $real_part_pkg = new FS::part_pkg { $self->hash };
57     $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
58     # Find a discount with that duration...
59     my @discount = grep { $_->months == $param->{freq_override} }
60                     map { $_->discount } $real_part_pkg->part_pkg_discount;
61     my $discount = shift @discount;
62     # and default to bill that many months at once.
63     $param->{months} = $param->{freq_override} unless $param->{months};
64     my $error;
65     if ($discount) {
66       # Then set the cust_pkg discount.
67       if ($discount->months == $param->{months}) {
68         $cust_pkg->discountnum($discount->discountnum);
69         $error = $cust_pkg->insert_discount;
70       } else {
71         $cust_pkg->discountnum(-1);
72         foreach ( qw( amount percent months ) ) {
73           my $method = "discountnum_$_";
74           $cust_pkg->$method($discount->$_);
75         }
76         $error = $cust_pkg->insert_discount;
77       }
78       die "error discounting using part_pkg_discount: $error" if $error;
79     }
80   }
81
82   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
83   foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
84     my $discount_left;
85     my $discount = $cust_pkg_discount->discount;
86     #UI enforces one or the other (for now?  probably for good)
87     my $amount = 0;
88     $amount += $discount->amount
89         if defined $param->{'real_pkgpart'} && $cust_pkg->pkgpart == $param->{'real_pkgpart'};
90     $amount += sprintf('%.2f', $discount->percent * $br / 100 );
91     my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
92
93     my $months = $discount->months
94     ? min( $chg_months,
95       $discount->months - $cust_pkg_discount->months_used )
96     : $chg_months;
97
98     if(defined $param->{'setup_charge'}) {
99         next unless $discount->setup;
100
101         if ( $discount->percent ) {
102             $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
103             $months = 1;
104         }
105         elsif ( $discount->amount && $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         }
115         else {
116             next; 
117         }
118     }
119     elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
120         && $param->{'discount_left_recur'}{$discount->discountnum} > 0) {
121         # use up transferred remainder of discount from setup
122         $amount = $param->{'discount_left_recur'}{$discount->discountnum};
123         $param->{'discount_left_recur'}{$discount->discountnum} = 0;
124         $months = 1;
125     }
126     elsif ( $discount->setup && $discount->months == 1 && $discount->amount ) {
127         next;
128     }
129
130     my $error = $cust_pkg_discount->increment_months_used($months)
131         if (defined $param->{'real_pkgpart'} 
132             && $cust_pkg->pkgpart == $param->{'real_pkgpart'} 
133             && ! defined $param->{'setup_charge'});
134     die "error discounting: $error" if $error;
135
136     $amount = min($amount, $br);
137     $amount *= $months;
138     $amount = sprintf('%.2f', $amount);
139
140     next unless $amount > 0;
141
142     # transfer remainder of discount, if any, to setup
143     if ( $discount->setup && $discount->amount
144         && (!$discount->months || $discount->months != 1)
145         && !defined $param->{'setup_charge'}) {
146         $discount_left = $br - $amount;
147         $amount = $br if $discount_left < 0;
148         $param->{'discount_left_setup'}{$discount->discountnum} = 
149                                     0 - $discount_left if $discount_left < 0;
150     }
151
152     #record details in cust_bill_pkg_discount
153     my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
154       'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
155       'amount'         => $amount,
156       'months'         => $months,
157     };
158     push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
159
160     #add details on discount to invoice
161     my $money_char = $conf->config('money_char') || '$';
162     $months = sprintf('%.2f', $months) if $months =~ /\./;
163
164     my $d = 'Includes ';
165     $d .= 'setup ' if defined $param->{'setup_charge'};
166     $d .= 'discount of '. $discount->description_short;
167     $d .= " for $months month". ( $months!=1 ? 's' : '' ) unless defined $param->{'setup_charge'};
168     $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
169     push @$details, $d;
170
171     $tot_discount += $amount;
172   }
173
174   if (!@cust_pkg_discount and $conf->exists('discount-show_available') ) {
175     push @$details, $self->available_discounts;
176   }
177
178   sprintf('%.2f', $tot_discount);
179 }
180
181 =item available_discounts
182
183 Returns a list of details decribing the available prepayment discounts 
184 for this package.
185
186 =cut
187
188 sub available_discounts {
189   my $self = shift;
190   return if $self->freq ne '1';
191   my @details;
192   my $money_char = FS::Conf->new->config('money_char') || '$';
193   my @discounts = map { $_->discount } $self->part_pkg_discount;
194   # probably the most logical way to arrange these
195   foreach my $discount (sort { $a->months <=> $b->months } @discounts) {
196     my $months = $discount->months;
197     my $amount;
198     if ( $discount->amount > 0 ) {
199       $amount = $money_char . sprintf('%.2f', $discount->amount);
200     }
201     elsif ( $discount->percent ) {
202       $amount = $discount->percent .'%';
203     }
204     else { #?
205       next;
206     }
207     push @details, "Prepay $months months for $amount discount."
208       # better way to display this?
209       # if it's a problem, we'll add discount.invoice_text or something
210       # for the customer-visible text line.
211     }
212   return @details;
213 }
214
215
216 1;