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