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