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