optimize history search (invoice rending time for svc_ records with tons of changes...
[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   my $conf = new FS::Conf;
42
43   my $br = $self->base_recur_permonth($cust_pkg, $sdate);
44   $br += $param->{'override_charges'} if $param->{'override_charges'};
45  
46   my $tot_discount = 0;
47   #UI enforces just 1 for now, will need ordering when they can be stacked
48
49   if ( $param->{freq_override} ) {
50     # When a customer pays for more than one month at a time to receive a 
51     # term discount, freq_override is set to the number of months.
52     my $real_part_pkg = new FS::part_pkg { $self->hash };
53     $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
54     # Find a discount with that duration...
55     my @discount = grep { $_->months == $param->{freq_override} }
56                     map { $_->discount } $real_part_pkg->part_pkg_discount;
57     my $discount = shift @discount;
58     # and default to bill that many months at once.
59     $param->{months} = $param->{freq_override} unless $param->{months};
60     my $error;
61     if ($discount) {
62       # Then set the cust_pkg discount.
63       if ($discount->months == $param->{months}) {
64         $cust_pkg->discountnum($discount->discountnum);
65         $error = $cust_pkg->insert_discount;
66       } else {
67         $cust_pkg->discountnum(-1);
68         foreach ( qw( amount percent months ) ) {
69           my $method = "discountnum_$_";
70           $cust_pkg->$method($discount->$_);
71         }
72         $error = $cust_pkg->insert_discount;
73       }
74       die "error discounting using part_pkg_discount: $error" if $error;
75     }
76   }
77
78   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
79   foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
80     my $discount_left;
81     my $discount = $cust_pkg_discount->discount;
82     #UI enforces one or the other (for now?  probably for good)
83     my $amount = 0;
84     $amount += $discount->amount
85         if $cust_pkg->pkgpart == $param->{'real_pkgpart'};
86     $amount += sprintf('%.2f', $discount->percent * $br / 100 );
87     my $chg_months = defined($param->{'months'}) ?
88                       $param->{'months'} :
89                       $cust_pkg->part_pkg->freq;
90
91     my $months = $discount->months
92     ? min( $chg_months,
93       $discount->months - $cust_pkg_discount->months_used )
94     : $chg_months;
95
96     if (defined $param->{'setup_charge'}) {
97         next unless $discount->setup;
98
99         if ( $discount->percent > 0 ) {
100             $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
101             $months = 1;
102         } elsif ( $discount->amount > 0 && $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         } else {
112             next; 
113         }
114     } elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
115               && $param->{'discount_left_recur'}{$discount->discountnum} > 0
116             ) {
117         # use up transferred remainder of discount from setup
118         $amount = $param->{'discount_left_recur'}{$discount->discountnum};
119         $param->{'discount_left_recur'}{$discount->discountnum} = 0;
120         $months = 1;
121     } elsif (    $discount->setup
122               && $discount->months == 1
123               && $discount->amount > 0
124             ) {
125         next;
126     }
127
128     if ( ! defined $param->{'setup_charge'} ) {
129       if ( $cust_pkg->pkgpart == $param->{'real_pkgpart'} ) {
130         push @{ $param->{precommit_hooks} }, sub {
131           my $error = $cust_pkg_discount->increment_months_used($months);
132           die "error discounting: $error" if $error;
133         };
134       }
135
136       $amount = min($amount, $br);
137       $amount *= $months;
138     }
139
140     $amount = sprintf('%.2f', $amount + 0.00000001 ); #so 1.005 rounds to 1.01
141
142     next unless $amount > 0;
143
144     # transfer remainder of discount, if any, to setup
145     if ( $discount->setup && $discount->amount > 0
146         && (!$discount->months || $discount->months != 1)
147         && !defined $param->{'setup_charge'}
148        )
149     {
150       $discount_left = $br - $amount;
151       if ( $discount_left < 0 ) {
152         $amount = $br;
153         $param->{'discount_left_setup'}{$discount->discountnum} = 
154           0 - $discount_left;
155       }
156     }
157
158     #record details in cust_bill_pkg_discount
159     my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
160       'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
161       'amount'         => $amount,
162       'months'         => $months,
163     };
164     push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
165
166     #add details on discount to invoice
167     my $money_char = $conf->config('money_char') || '$';
168     $months = sprintf('%.2f', $months) if $months =~ /\./;
169
170     my $d = 'Includes ';
171     my $format;
172
173     if ( $months eq '1' ) {
174       $d .= "discount of $money_char$amount";
175       $d .= " each" if $cust_pkg->quantity > 1;
176       $format = 'Undiscounted amount: %s%.2f';
177     } else {
178       $d .= 'setup ' if defined $param->{'setup_charge'};
179       $d .= 'discount of '. $discount->description_short;
180       $d .= " for $months months"
181         unless defined $param->{'setup_charge'};
182       $d .= ": $money_char$amount" if $discount->percent;
183       $format = 'Undiscounted monthly amount: %s%.2f';
184     }
185
186     push @$details, $d;
187     push @$details, sprintf( $format, $money_char, $br );
188
189     $tot_discount += $amount;
190   }
191
192   sprintf('%.2f', $tot_discount);
193 }
194
195 1;