discounts on setup fees, part 2 of 2, RT11512
[freeside.git] / FS / FS / part_pkg / discount_Mixin.pm
1 package FS::part_pkg::discount_Mixin;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use FS::part_pkg;
6 use FS::cust_pkg;
7 use FS::cust_bill_pkg_discount;
8 use Time::Local qw(timelocal);
9 use List::Util 'min';
10
11 @ISA = qw(FS::part_pkg);
12 %info = ( 'disabled' => 1 );
13
14 =head1 NAME
15
16 FS::part_pkg::discount_Mixin - Mixin class for part_pkg:: classes that 
17 can be discounted.
18
19 =head1 SYNOPSIS
20
21 package FS::part_pkg::...;
22 use base qw( FS::part_pkg::discount_Mixin );
23
24 sub calc_recur {
25   ...
26   my $discount = $self->calc_discount($cust_pkg, $$sdate, $details, $param);
27   $charge -= $discount;
28   ...
29 }
30
31 =head METHODS
32
33 =item calc_discount
34
35 Takes all the arguments of calc_recur.  Calculates and returns  the amount 
36 by which to reduce the recurring fee; also increments months used on the 
37 discount and generates an invoice detail describing it.
38
39 =cut
40
41 sub calc_discount {
42   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
43
44   my $br = $self->base_recur($cust_pkg, $sdate);
45   $br += $param->{'override_charges'} if $param->{'override_charges'};
46   
47   my $tot_discount = 0;
48   #UI enforces just 1 for now, will need ordering when they can be stacked
49
50   if ( $param->{freq_override} ) {
51     # When a customer pays for more than one month at a time to receive a 
52     # term discount, freq_override is set to the number of months.
53     my $real_part_pkg = new FS::part_pkg { $self->hash };
54     $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
55     # Find a discount with that duration...
56     my @discount = grep { $_->months == $param->{freq_override} }
57                     map { $_->discount } $real_part_pkg->part_pkg_discount;
58     my $discount = shift @discount;
59     # and default to bill that many months at once.
60     $param->{months} = $param->{freq_override} unless $param->{months};
61     my $error;
62     if ($discount) {
63       # Then set the cust_pkg discount.
64       if ($discount->months == $param->{months}) {
65         $cust_pkg->discountnum($discount->discountnum);
66         $error = $cust_pkg->insert_discount;
67       } else {
68         $cust_pkg->discountnum(-1);
69         foreach ( qw( amount percent months ) ) {
70           my $method = "discountnum_$_";
71           $cust_pkg->$method($discount->$_);
72         }
73         $error = $cust_pkg->insert_discount;
74       }
75       die "error discounting using part_pkg_discount: $error" if $error;
76     }
77   }
78
79   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
80   foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
81     my $discount_left;
82     my $discount = $cust_pkg_discount->discount;
83     #UI enforces one or the other (for now?  probably for good)
84     my $amount = 0;
85     $amount += $discount->amount
86         if defined $param->{'real_pkgpart'} && $cust_pkg->pkgpart == $param->{'real_pkgpart'};
87     $amount += sprintf('%.2f', $discount->percent * $br / 100 );
88     my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
89
90     my $months = $discount->months
91     ? min( $chg_months,
92       $discount->months - $cust_pkg_discount->months_used )
93     : $chg_months;
94
95     if(defined $param->{'setup_charge'}) {
96         next unless $discount->setup;
97
98         if ( $discount->percent ) {
99             $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
100             $months = 1;
101         }
102         elsif ( $discount->amount && $discount->months == 1) {
103             $discount_left = $param->{'setup_charge'} - $discount->amount;
104             $amount = $param->{'setup_charge'} if $discount_left < 0;
105             $amount = $discount->amount if $discount_left >= 0;
106             $months = 1;
107                 
108             # transfer remainder of discount, if any, to recur
109             $param->{'discount_left_recur'}{$discount->discountnum} = 
110                 0 - $discount_left if $discount_left < 0;
111         }
112         else {
113             next; 
114         }
115     }
116     elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
117         && $param->{'discount_left_recur'}{$discount->discountnum} > 0) {
118         # use up transferred remainder of discount from setup
119         $amount = $param->{'discount_left_recur'}{$discount->discountnum};
120         $param->{'discount_left_recur'}{$discount->discountnum} = 0;
121         $months = 1;
122     }
123
124     my $error = $cust_pkg_discount->increment_months_used($months)
125         if (defined $param->{'real_pkgpart'} 
126             && $cust_pkg->pkgpart == $param->{'real_pkgpart'} 
127             && ! defined $param->{'setup_charge'});
128     die "error discounting: $error" if $error;
129
130     $amount *= $months;
131     $amount = sprintf('%.2f', $amount);
132
133     next unless $amount > 0;
134             
135     # transfer remainder of discount, if any, to setup
136     if ( $discount->setup && $discount->amount
137         && (!$discount->months || $discount->months != 1)
138         && !defined $param->{'setup_charge'}) {
139         $discount_left = $br - $amount;
140         $amount = $br if $discount_left < 0;
141         $param->{'discount_left_setup'}{$discount->discountnum} = 
142                                     0 - $discount_left if $discount_left < 0;
143     }
144
145     #record details in cust_bill_pkg_discount
146     my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
147       'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
148       'amount'         => $amount,
149       'months'         => $months,
150     };
151     push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
152
153     #add details on discount to invoice
154     my $conf = new FS::Conf;
155     my $money_char = $conf->config('money_char') || '$';
156     $months = sprintf('%.2f', $months) if $months =~ /\./;
157
158     my $d = 'Includes ';
159     $d .= 'setup ' if defined $param->{'setup_charge'};
160     $d .= 'discount of '. $discount->description_short;
161     $d .= " for $months month". ( $months!=1 ? 's' : '' ) unless defined $param->{'setup_charge'};
162     $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
163     push @$details, $d;
164
165     $tot_discount += $amount;
166   }
167
168   sprintf('%.2f', $tot_discount);
169 }
170
171 1;