fix discounts not appearing for one-time charge packages, RT#13654
[freeside.git] / FS / FS / part_pkg / flat.pm
1 package FS::part_pkg::flat;
2 use base qw( FS::part_pkg::prorate_Mixin
3              FS::part_pkg::discount_Mixin
4              FS::part_pkg
5            );
6
7 use strict;
8 use vars qw( %info %usage_recharge_fields @usage_recharge_fieldorder );
9 use Tie::IxHash;
10 use List::Util qw( min );
11 use FS::UI::bytecount;
12 use FS::Conf;
13
14 tie my %temporalities, 'Tie::IxHash',
15   'upcoming'  => "Upcoming (future)",
16   'preceding' => "Preceding (past)",
17 ;
18
19 tie my %contract_years, 'Tie::IxHash', (
20   '' => '(none)',
21   map { $_*12 => $_ } (1..5),
22 );
23
24 %info = (
25   'name' => 'Flat rate (anniversary billing)',
26   'shortname' => 'Anniversary',
27   'inherit_fields' => [ 'usage_Mixin', 'global_Mixin' ],
28   'fields' => {
29     #false laziness w/voip_cdr.pm
30     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
31                              'type' => 'select',
32                              'select_options' => \%temporalities,
33                            },
34
35     #used in cust_pkg.pm so could add to any price plan
36     'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
37                        },
38     'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
39                        },
40     'contract_end_months'=> { 
41                         'name' => 'Auto-add a contract end date this number of years out',
42                         'type' => 'select',
43                         'select_options' => \%contract_years,
44                       },
45     #used in cust_pkg.pm so could add to any price plan where it made sense
46     'start_1st'     => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
47                          'type' => 'checkbox',
48                        },
49     'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
50                                     'with the customer\'s other packages',
51                           'type' => 'checkbox',
52                         },
53     'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
54                         'type' => 'checkbox',
55                       },
56     'unsuspend_adjust_bill' => 
57                         { 'name' => 'Adjust next bill date forward when '.
58                                     'unsuspending',
59                           'type' => 'checkbox',
60                         },
61
62     'externalid' => { 'name'   => 'Optional External ID',
63                       'default' => '',
64                     },
65   },
66   'fieldorder' => [ qw( recur_temporality 
67                         expire_months adjourn_months
68                         contract_end_months
69                         start_1st sync_bill_date
70                         suspend_bill unsuspend_adjust_bill
71                         externalid ),
72                   ],
73   'weight' => 10,
74 );
75
76 sub price_info {
77     my $self = shift;
78     my $conf = new FS::Conf;
79     my $money_char = $conf->config('money_char') || '$';
80     my $setup = $self->option('setup_fee') || 0;
81     my $recur = $self->option('recur_fee', 1) || 0;
82     my $str = '';
83     $str = $money_char . $setup . ' one-time' if $setup;
84     $str .= ', ' if ($setup && $recur);
85     $str .= $money_char . $recur . ' recurring ' if $recur;
86     $str;
87 }
88
89 sub calc_setup {
90   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
91
92   my $i = 0;
93   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
94   while ($i < $count) {
95     push @$details, $self->option( 'additional_info' . $i++ );
96   }
97
98   my $quantity = $cust_pkg->quantity || 1;
99
100   my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
101
102   my $discount = 0;
103   if ( $charge > 0 ) {
104       $param->{'setup_charge'} = $charge;
105       $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
106       delete $param->{'setup_charge'};
107   }
108
109   sprintf('%.2f', $charge - $discount);
110 }
111
112 sub unit_setup {
113   my($self, $cust_pkg, $sdate, $details ) = @_;
114
115   $self->option('setup_fee') || 0;
116 }
117
118 sub calc_recur {
119   my $self = shift;
120   my($cust_pkg, $sdate, $details, $param ) = @_;
121
122   #my $last_bill = $cust_pkg->last_bill;
123   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
124
125   return 0
126     if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
127
128   my $charge = $self->base_recur($cust_pkg, $sdate);
129   if ( $self->option('sync_bill_date',1) ) {
130     my $next_bill = $cust_pkg->cust_main->next_bill_date;
131     if ( defined($next_bill) ) {
132       my $cutoff_day = (localtime($next_bill))[3];
133       $charge = $self->calc_prorate(@_, $cutoff_day);
134     }
135   }
136   elsif ( $param->{freq_override} ) {
137     # XXX not sure if this should be mutually exclusive with sync_bill_date.
138     # Given the very specific problem that freq_override is meant to 'solve',
139     # it probably should.
140     $charge *= $param->{freq_override} if $param->{freq_override};
141   }
142
143   my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
144   return sprintf('%.2f', $charge - $discount);
145 }
146
147 sub base_recur {
148   my($self, $cust_pkg, $sdate) = @_;
149   $self->option('recur_fee', 1) || 0;
150 }
151
152 sub base_recur_permonth {
153   my($self, $cust_pkg) = @_;
154
155   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
156
157   sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
158 }
159
160 sub calc_remain {
161   my ($self, $cust_pkg, %options) = @_;
162
163   my $time;
164   if ($options{'time'}) {
165     $time = $options{'time'};
166   } else {
167     $time = time;
168   }
169
170   my $next_bill = $cust_pkg->getfield('bill') || 0;
171
172   return 0 if    ! $self->base_recur($cust_pkg, \$time)
173               || ! $next_bill
174               || $next_bill < $time;
175
176   my %sec = (
177     'h' =>    3600, # 60 * 60
178     'd' =>   86400, # 60 * 60 * 24
179     'w' =>  604800, # 60 * 60 * 24 * 7
180     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
181   );
182
183   $self->freq =~ /^(\d+)([hdwm]?)$/
184     or die 'unparsable frequency: '. $self->freq;
185   my $freq_sec = $1 * $sec{$2||'m'};
186   return 0 unless $freq_sec;
187
188   sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
189
190 }
191
192 sub is_free_options {
193   qw( setup_fee recur_fee );
194 }
195
196 sub is_prepaid { 0; } #no, we're postpaid
197
198 sub can_start_date { ! shift->option('start_1st', 1) }
199
200 sub can_discount { 1; }
201
202 sub usage_valuehash {
203   my $self = shift;
204   map { $_, $self->option($_) }
205     grep { $self->option($_, 'hush') } 
206     qw(seconds upbytes downbytes totalbytes);
207 }
208
209 sub reset_usage {
210   my($self, $cust_pkg, %opt) = @_;
211   warn "   resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
212   my %values = $self->usage_valuehash;
213   if ($self->option('usage_rollover', 1)) {
214     $cust_pkg->recharge(\%values);
215   }else{
216     $cust_pkg->set_usage(\%values, %opt);
217   }
218 }
219
220 1;