1 package FS::part_pkg::flat;
4 use vars qw( @ISA %info
5 %usage_fields %usage_recharge_fields
6 @usage_fieldorder @usage_recharge_fieldorder
9 use List::Util qw(min); # max);
10 #use FS::Record qw(qsearch);
11 use FS::UI::bytecount;
15 @ISA = qw(FS::part_pkg
16 FS::part_pkg::prorate_Mixin
17 FS::part_pkg::discount_Mixin);
19 tie my %temporalities, 'Tie::IxHash',
20 'upcoming' => "Upcoming (future)",
21 'preceding' => "Preceding (past)",
24 tie my %contract_years, 'Tie::IxHash', (
26 map { $_*12 => $_ } (1..5),
31 'seconds' => { 'name' => 'Time limit for this package',
33 'check' => sub { shift =~ /^\d*$/ },
35 'upbytes' => { 'name' => 'Upload limit for this package',
37 'check' => sub { shift =~ /^\d*$/ },
38 'format' => \&FS::UI::bytecount::display_bytecount,
39 'parse' => \&FS::UI::bytecount::parse_bytecount,
41 'downbytes' => { 'name' => 'Download limit for this package',
43 'check' => sub { shift =~ /^\d*$/ },
44 'format' => \&FS::UI::bytecount::display_bytecount,
45 'parse' => \&FS::UI::bytecount::parse_bytecount,
47 'totalbytes' => { 'name' => 'Transfer limit for this package',
49 'check' => sub { shift =~ /^\d*$/ },
50 'format' => \&FS::UI::bytecount::display_bytecount,
51 'parse' => \&FS::UI::bytecount::parse_bytecount,
55 %usage_recharge_fields = (
57 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
59 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
61 'recharge_seconds' => { 'name' => 'Recharge time for this package',
63 'check' => sub { shift =~ /^\d*$/ },
65 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
67 'check' => sub { shift =~ /^\d*$/ },
68 'format' => \&FS::UI::bytecount::display_bytecount,
69 'parse' => \&FS::UI::bytecount::parse_bytecount,
71 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
73 'check' => sub { shift =~ /^\d*$/ },
74 'format' => \&FS::UI::bytecount::display_bytecount,
75 'parse' => \&FS::UI::bytecount::parse_bytecount,
77 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
79 'check' => sub { shift =~ /^\d*$/ },
80 'format' => \&FS::UI::bytecount::display_bytecount,
81 'parse' => \&FS::UI::bytecount::parse_bytecount,
83 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
84 ' over into current period',
87 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
93 @usage_fieldorder = qw( seconds upbytes downbytes totalbytes );
94 @usage_recharge_fieldorder = qw(
95 recharge_amount recharge_seconds recharge_upbytes
96 recharge_downbytes recharge_totalbytes
97 usage_rollover recharge_reset
101 'name' => 'Flat rate (anniversary billing)',
102 'shortname' => 'Anniversary',
104 'setup_fee' => { 'name' => 'Setup fee for this package',
107 'recur_fee' => { 'name' => 'Recurring fee for this package',
111 #false laziness w/voip_cdr.pm
112 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
114 'select_options' => \%temporalities,
116 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
117 ' of service at cancellation',
118 'type' => 'checkbox',
121 #used in cust_pkg.pm so could add to any price plan
122 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
124 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
126 'contract_end_months'=> {
127 'name' => 'Auto-add a contract end date this number of years out',
129 'select_options' => \%contract_years,
131 #used in cust_pkg.pm so could add to any price plan where it made sense
132 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
133 'type' => 'checkbox',
135 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
136 'with the customer\'s other packages',
137 'type' => 'checkbox',
139 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
140 'type' => 'checkbox',
142 'unsuspend_adjust_bill' =>
143 { 'name' => 'Adjust next bill date forward when '.
145 'type' => 'checkbox',
149 %usage_recharge_fields,
151 'externalid' => { 'name' => 'Optional External ID',
155 'fieldorder' => [ qw( setup_fee recur_fee
156 recur_temporality unused_credit
157 expire_months adjourn_months
159 start_1st sync_bill_date
160 suspend_bill unsuspend_adjust_bill
162 @usage_fieldorder, @usage_recharge_fieldorder,
169 my($self, $cust_pkg, $sdate, $details ) = @_;
172 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
173 while ($i < $count) {
174 push @$details, $self->option( 'additional_info' . $i++ );
177 my $quantity = $cust_pkg->quantity || 1;
179 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
183 my($self, $cust_pkg, $sdate, $details ) = @_;
185 $self->option('setup_fee') || 0;
190 my($cust_pkg, $sdate, $details, $param ) = @_;
192 #my $last_bill = $cust_pkg->last_bill;
193 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
196 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
198 my $charge = $self->base_recur($cust_pkg);
199 if ( $self->option('sync_bill_date',1) ) {
200 my $next_bill = $cust_pkg->cust_main->next_bill_date;
201 if ( defined($next_bill) ) {
202 my $cutoff_day = (localtime($next_bill))[3];
203 $charge = $self->calc_prorate(@_, $cutoff_day);
206 elsif ( $param->{freq_override} ) {
207 # XXX not sure if this should be mutually exclusive with sync_bill_date.
208 # Given the very specific problem that freq_override is meant to 'solve',
209 # it probably should.
210 $charge *= $param->{freq_override} if $param->{freq_override};
213 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
214 return sprintf('%.2f', $charge - $discount);
218 my($self, $cust_pkg) = @_;
219 $self->option('recur_fee', 1) || 0;
222 sub base_recur_permonth {
223 my($self, $cust_pkg) = @_;
225 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
227 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
231 my ($self, $cust_pkg, %options) = @_;
234 if ($options{'time'}) {
235 $time = $options{'time'};
240 my $next_bill = $cust_pkg->getfield('bill') || 0;
242 #my $last_bill = $cust_pkg->last_bill || 0;
243 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
245 return 0 if ! $self->base_recur($cust_pkg)
246 || ! $self->option('unused_credit', 1)
249 || $next_bill < $time;
252 'h' => 3600, # 60 * 60
253 'd' => 86400, # 60 * 60 * 24
254 'w' => 604800, # 60 * 60 * 24 * 7
255 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
258 $self->freq =~ /^(\d+)([hdwm]?)$/
259 or die 'unparsable frequency: '. $self->freq;
260 my $freq_sec = $1 * $sec{$2||'m'};
261 return 0 unless $freq_sec;
263 sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
267 sub is_free_options {
268 qw( setup_fee recur_fee );
271 sub is_prepaid { 0; } #no, we're postpaid
273 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
276 $self->freq =~ /^\d+$/ && $self->freq > 0;
279 sub usage_valuehash {
281 map { $_, $self->option($_) }
282 grep { $self->option($_, 'hush') }
283 qw(seconds upbytes downbytes totalbytes);
287 my($self, $cust_pkg, %opt) = @_;
288 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
289 my %values = $self->usage_valuehash;
290 if ($self->option('usage_rollover', 1)) {
291 $cust_pkg->recharge(\%values);
293 $cust_pkg->set_usage(\%values, %opt);