531a6a80ec5800bd4ebda20370c5f1b39b120afe
[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' => [ 'prorate_Mixin', '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     'prorate_defer_bill' => { 
54                           'name' => 'When synchronizing, defer the bill until '.
55                                     'the customer\'s next bill date',
56                           'type' => 'checkbox',
57                         },
58     'prorate_round_day' => {
59                           'name' => 'When synchronizing, round the prorated '.
60                                     'period to the nearest full day',
61                           'type' => 'checkbox',
62                         },
63     'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
64
65     'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
66                         'type' => 'checkbox',
67                       },
68     'unsuspend_adjust_bill' => 
69                         { 'name' => 'Adjust next bill date forward when '.
70                                     'unsuspending',
71                           'type' => 'checkbox',
72                         },
73     'bill_recur_on_cancel' => {
74                         'name' => 'Bill the last period on cancellation',
75                         'type' => 'checkbox',
76                         },
77     'bill_suspend_as_cancel' => {
78                         'name' => 'Bill immediately upon suspension', #desc?
79                         'type' => 'checkbox',
80                         },
81     'externalid' => { 'name'   => 'Optional External ID',
82                       'default' => '',
83                     },
84   },
85   'fieldorder' => [ qw( recur_temporality 
86                         expire_months adjourn_months
87                         contract_end_months
88                         start_1st
89                         sync_bill_date prorate_defer_bill prorate_round_day
90                         suspend_bill unsuspend_adjust_bill
91                         bill_recur_on_cancel
92                         bill_suspend_as_cancel
93                         externalid ),
94                   ],
95   'weight' => 10,
96 );
97
98 sub price_info {
99     my $self = shift;
100     my $conf = new FS::Conf;
101     my $money_char = $conf->config('money_char') || '$';
102     my $setup = $self->option('setup_fee') || 0;
103     my $recur = $self->option('recur_fee', 1) || 0;
104     my $str = '';
105     $str = $money_char . $setup . ($recur ? ' setup ' : ' one-time') if $setup;
106     $str .= ', ' if ($setup && $recur);
107     $str .= $money_char. $recur. '/'. $self->freq_pretty if $recur;
108     $str;
109 }
110
111 sub calc_setup {
112   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
113
114   return 0 if $self->prorate_setup($cust_pkg, $sdate);
115
116   my $i = 0;
117   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
118   while ($i < $count) {
119     push @$details, $self->option( 'additional_info' . $i++ );
120   }
121
122   my $quantity = $cust_pkg->quantity || 1;
123
124   my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
125
126   my $discount = 0;
127   if ( $charge > 0 ) {
128       $param->{'setup_charge'} = $charge;
129       $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
130       delete $param->{'setup_charge'};
131   }
132
133   sprintf('%.2f', $charge - $discount);
134 }
135
136 sub unit_setup {
137   my($self, $cust_pkg, $sdate, $details ) = @_;
138
139   $self->option('setup_fee') || 0;
140 }
141
142 sub calc_recur {
143   my $self = shift;
144   my($cust_pkg, $sdate, $details, $param ) = @_;
145
146   #my $last_bill = $cust_pkg->last_bill;
147   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
148
149   return 0
150     if $self->recur_temporality eq 'preceding' && !$last_bill;
151
152   my $charge = $self->base_recur($cust_pkg, $sdate);
153   if ( my $cutoff_day = $self->cutoff_day($cust_pkg) ) {
154     $charge = $self->calc_prorate(@_, $cutoff_day);
155   }
156   elsif ( $param->{freq_override} ) {
157     # XXX not sure if this should be mutually exclusive with sync_bill_date.
158     # Given the very specific problem that freq_override is meant to 'solve',
159     # it probably should.
160     $charge *= $param->{freq_override} if $param->{freq_override};
161   }
162
163   my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
164   return sprintf('%.2f', $charge - $discount);
165 }
166
167 sub cutoff_day {
168   my $self = shift;
169   my $cust_pkg = shift;
170   if ( $self->option('sync_bill_date',1) ) {
171     my $next_bill = $cust_pkg->cust_main->next_bill_date;
172     if ( defined($next_bill) ) {
173       return (localtime($next_bill))[3];
174     }
175   }
176   return 0;
177 }
178
179 sub base_recur {
180   my($self, $cust_pkg, $sdate) = @_;
181   $self->option('recur_fee', 1) || 0;
182 }
183
184 sub base_recur_permonth {
185   my($self, $cust_pkg) = @_;
186
187   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
188
189   sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
190 }
191
192 sub calc_remain {
193   my ($self, $cust_pkg, %options) = @_;
194
195   my $time;
196   if ($options{'time'}) {
197     $time = $options{'time'};
198   } else {
199     $time = time;
200   }
201
202   my $next_bill = $cust_pkg->getfield('bill') || 0;
203
204   return 0 if    ! $self->base_recur($cust_pkg, \$time)
205               || ! $next_bill
206               || $next_bill < $time;
207
208   my %sec = (
209     'h' =>    3600, # 60 * 60
210     'd' =>   86400, # 60 * 60 * 24
211     'w' =>  604800, # 60 * 60 * 24 * 7
212     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
213   );
214
215   $self->freq =~ /^(\d+)([hdwm]?)$/
216     or die 'unparsable frequency: '. $self->freq;
217   my $freq_sec = $1 * $sec{$2||'m'};
218   return 0 unless $freq_sec;
219
220   sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
221
222 }
223
224 sub is_free_options {
225   qw( setup_fee recur_fee );
226 }
227
228 sub is_prepaid { 0; } #no, we're postpaid
229
230 sub can_start_date { ! shift->option('start_1st', 1) }
231
232 sub can_discount { 1; }
233
234 sub recur_temporality {
235   my $self = shift;
236   $self->option('recur_temporality', 1);
237 }
238
239 sub usage_valuehash {
240   my $self = shift;
241   map { $_, $self->option($_) }
242     grep { $self->option($_, 'hush') } 
243     qw(seconds upbytes downbytes totalbytes);
244 }
245
246 sub reset_usage {
247   my($self, $cust_pkg, %opt) = @_;
248   warn "   resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
249   my %values = $self->usage_valuehash;
250   if ($self->option('usage_rollover', 1)) {
251     $cust_pkg->recharge(\%values);
252   }else{
253     $cust_pkg->set_usage(\%values, %opt);
254   }
255 }
256
257 1;