per-package option to adjust bill date on unsuspend, RT#8434
[freeside.git] / FS / FS / part_pkg / flat.pm
1 package FS::part_pkg::flat;
2
3 use strict;
4 use vars qw( @ISA %info
5              %usage_fields %usage_recharge_fields
6              @usage_fieldorder @usage_recharge_fieldorder
7            );
8 use Tie::IxHash;
9 #use FS::Record qw(qsearch);
10 use FS::UI::bytecount;
11 use FS::part_pkg;
12
13 @ISA = qw(FS::part_pkg);
14
15 tie my %temporalities, 'Tie::IxHash',
16   'upcoming'  => "Upcoming (future)",
17   'preceding' => "Preceding (past)",
18 ;
19
20 %usage_fields = (
21
22     'seconds'       => { 'name' => 'Time limit for this package',
23                          'default' => '',
24                          'check' => sub { shift =~ /^\d*$/ },
25                        },
26     'upbytes'       => { 'name' => 'Upload limit for this package',
27                          'default' => '',
28                          'check' => sub { shift =~ /^\d*$/ },
29                          'format' => \&FS::UI::bytecount::display_bytecount,
30                          'parse' => \&FS::UI::bytecount::parse_bytecount,
31                        },
32     'downbytes'     => { 'name' => 'Download limit for this package',
33                          'default' => '',
34                          'check' => sub { shift =~ /^\d*$/ },
35                          'format' => \&FS::UI::bytecount::display_bytecount,
36                          'parse' => \&FS::UI::bytecount::parse_bytecount,
37                        },
38     'totalbytes'    => { 'name' => 'Transfer limit for this package',
39                          'default' => '',
40                          'check' => sub { shift =~ /^\d*$/ },
41                          'format' => \&FS::UI::bytecount::display_bytecount,
42                          'parse' => \&FS::UI::bytecount::parse_bytecount,
43                        },
44 );
45
46 %usage_recharge_fields = (
47
48     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
49                          'default' => '',
50                          'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
51                        },
52     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
53                          'default' => '',
54                          'check' => sub { shift =~ /^\d*$/ },
55                        },
56     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
57                          'default' => '',
58                          'check' => sub { shift =~ /^\d*$/ },
59                          'format' => \&FS::UI::bytecount::display_bytecount,
60                          'parse' => \&FS::UI::bytecount::parse_bytecount,
61                        },
62     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',
63                          'default' => '',
64                          'check' => sub { shift =~ /^\d*$/ },
65                          'format' => \&FS::UI::bytecount::display_bytecount,
66                          'parse' => \&FS::UI::bytecount::parse_bytecount,
67                        },
68     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',
69                          'default' => '',
70                          'check' => sub { shift =~ /^\d*$/ },
71                          'format' => \&FS::UI::bytecount::display_bytecount,
72                          'parse' => \&FS::UI::bytecount::parse_bytecount,
73                        },
74     'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
75                                     ' over into current period',
76                           'type' => 'checkbox',
77                         },
78     'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
79                                     'package recharge',
80                           'type' => 'checkbox',
81                         },
82 );
83
84 @usage_fieldorder = qw( seconds upbytes downbytes totalbytes );
85 @usage_recharge_fieldorder = qw(
86   recharge_amount recharge_seconds recharge_upbytes
87   recharge_downbytes recharge_totalbytes
88   usage_rollover recharge_reset
89 );
90
91 %info = (
92   'name' => 'Flat rate (anniversary billing)',
93   'shortname' => 'Anniversary',
94   'fields' => {
95     'setup_fee'     => { 'name' => 'Setup fee for this package',
96                          'default' => 0,
97                        },
98     'recur_fee'     => { 'name' => 'Recurring fee for this package',
99                          'default' => 0,
100                        },
101
102     #false laziness w/voip_cdr.pm
103     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
104                              'type' => 'select',
105                              'select_options' => \%temporalities,
106                            },
107     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
108                                    ' of service at cancellation',
109                          'type' => 'checkbox',
110                        },
111
112     #used in cust_pkg.pm so could add to any price plan
113     'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
114                        },
115     #used in cust_pkg.pm so could add to any price plan where it made sense
116     'start_1st'     => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
117                          'type' => 'checkbox',
118                        },
119     'unsuspend_adjust_bill' =>
120                        { 'name' => 'Adjust next bill date forward when '.
121                                    'unsuspending',
122                          'type' => 'checkbox',
123                        },
124
125     %usage_fields,
126     %usage_recharge_fields,
127
128     'externalid' => { 'name'   => 'Optional External ID',
129                       'default' => '',
130                     },
131   },
132   'fieldorder' => [ qw( setup_fee recur_fee
133                         recur_temporality unused_credit
134                         expire_months start_1st unsuspend_adjust_bill
135                       ),
136                     @usage_fieldorder, @usage_recharge_fieldorder,
137                     qw( externalid ),
138                   ],
139   'weight' => 10,
140 );
141
142 sub calc_setup {
143   my($self, $cust_pkg, $sdate, $details ) = @_;
144
145   my $i = 0;
146   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
147   while ($i < $count) {
148     push @$details, $self->option( 'additional_info' . $i++ );
149   }
150
151   my $quantity = $cust_pkg->quantity || 1;
152
153   sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
154 }
155
156 sub unit_setup {
157   my($self, $cust_pkg, $sdate, $details ) = @_;
158
159   $self->option('setup_fee');
160 }
161
162 sub calc_recur {
163   my $self = shift;
164   my($cust_pkg) = @_;
165
166   #my $last_bill = $cust_pkg->last_bill;
167   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
168
169   return 0
170     if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
171
172   $self->base_recur(@_);
173 }
174
175 sub base_recur {
176   my($self, $cust_pkg) = @_;
177   $self->option('recur_fee', 1) || 0;
178 }
179
180 sub base_recur_permonth {
181   my($self, $cust_pkg) = @_;
182
183   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
184
185   sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
186 }
187
188 sub calc_remain {
189   my ($self, $cust_pkg, %options) = @_;
190
191   my $time;
192   if ($options{'time'}) {
193     $time = $options{'time'};
194   } else {
195     $time = time;
196   }
197
198   my $next_bill = $cust_pkg->getfield('bill') || 0;
199
200   #my $last_bill = $cust_pkg->last_bill || 0;
201   my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
202
203   return 0 if    ! $self->base_recur($cust_pkg)
204               || ! $self->option('unused_credit', 1)
205               || ! $last_bill
206               || ! $next_bill
207               || $next_bill < $time;
208
209   my %sec = (
210     'h' =>    3600, # 60 * 60
211     'd' =>   86400, # 60 * 60 * 24
212     'w' =>  604800, # 60 * 60 * 24 * 7
213     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
214   );
215
216   $self->freq =~ /^(\d+)([hdwm]?)$/
217     or die 'unparsable frequency: '. $self->freq;
218   my $freq_sec = $1 * $sec{$2||'m'};
219   return 0 unless $freq_sec;
220
221   sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
222
223 }
224
225 sub is_free_options {
226   qw( setup_fee recur_fee );
227 }
228
229 sub is_prepaid {
230   0; #no, we're postpaid
231 }
232
233 sub usage_valuehash {
234   my $self = shift;
235   map { $_, $self->option($_) }
236     grep { $self->option($_, 'hush') } 
237     qw(seconds upbytes downbytes totalbytes);
238 }
239
240 sub reset_usage {
241   my($self, $cust_pkg, %opt) = @_;
242   warn "   resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
243   my %values = $self->usage_valuehash;
244   if ($self->option('usage_rollover', 1)) {
245     $cust_pkg->recharge(\%values);
246   }else{
247     $cust_pkg->set_usage(\%values, %opt);
248   }
249 }
250
251 1;