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 FS::Record qw(qsearch);
10 use FS::UI::bytecount;
13 @ISA = qw(FS::part_pkg);
15 tie my %temporalities, 'Tie::IxHash',
16 'upcoming' => "Upcoming (future)",
17 'preceding' => "Preceding (past)",
22 'seconds' => { 'name' => 'Time limit for this package',
24 'check' => sub { shift =~ /^\d*$/ },
26 'upbytes' => { 'name' => 'Upload limit for this package',
28 'check' => sub { shift =~ /^\d*$/ },
29 'format' => \&FS::UI::bytecount::display_bytecount,
30 'parse' => \&FS::UI::bytecount::parse_bytecount,
32 'downbytes' => { 'name' => 'Download limit for this package',
34 'check' => sub { shift =~ /^\d*$/ },
35 'format' => \&FS::UI::bytecount::display_bytecount,
36 'parse' => \&FS::UI::bytecount::parse_bytecount,
38 'totalbytes' => { 'name' => 'Transfer limit for this package',
40 'check' => sub { shift =~ /^\d*$/ },
41 'format' => \&FS::UI::bytecount::display_bytecount,
42 'parse' => \&FS::UI::bytecount::parse_bytecount,
46 %usage_recharge_fields = (
48 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
50 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
52 'recharge_seconds' => { 'name' => 'Recharge time for this package',
54 'check' => sub { shift =~ /^\d*$/ },
56 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
58 'check' => sub { shift =~ /^\d*$/ },
59 'format' => \&FS::UI::bytecount::display_bytecount,
60 'parse' => \&FS::UI::bytecount::parse_bytecount,
62 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
64 'check' => sub { shift =~ /^\d*$/ },
65 'format' => \&FS::UI::bytecount::display_bytecount,
66 'parse' => \&FS::UI::bytecount::parse_bytecount,
68 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
70 'check' => sub { shift =~ /^\d*$/ },
71 'format' => \&FS::UI::bytecount::display_bytecount,
72 'parse' => \&FS::UI::bytecount::parse_bytecount,
74 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
75 ' over into current period',
78 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
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
92 'name' => 'Flat rate (anniversary billing)',
93 'shortname' => 'Anniversary',
95 'setup_fee' => { 'name' => 'Setup fee for this package',
98 'recur_fee' => { 'name' => 'Recurring fee for this package',
102 #false laziness w/voip_cdr.pm
103 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
105 'select_options' => \%temporalities,
109 %usage_recharge_fields,
111 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
112 ' of service at cancellation',
113 'type' => 'checkbox',
115 'externalid' => { 'name' => 'Optional External ID',
119 'fieldorder' => [ qw( setup_fee recur_fee recur_temporality unused_credit ),
120 @usage_fieldorder, @usage_recharge_fieldorder,
127 my($self, $cust_pkg, $sdate, $details ) = @_;
130 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
131 while ($i < $count) {
132 push @$details, $self->option( 'additional_info' . $i++ );
135 my $quantity = $cust_pkg->quantity || 1;
137 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
141 my($self, $cust_pkg, $sdate, $details ) = @_;
143 $self->option('setup_fee');
150 #my $last_bill = $cust_pkg->last_bill;
151 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
154 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
156 $self->base_recur(@_);
160 my($self, $cust_pkg) = @_;
161 $self->option('recur_fee', 1) || 0;
164 sub base_recur_permonth {
165 my($self, $cust_pkg) = @_; #$cust_pkg?
167 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
169 sprintf('%.2f', $self->base_recur / $self->freq );
173 my ($self, $cust_pkg, %options) = @_;
176 if ($options{'time'}) {
177 $time = $options{'time'};
182 my $next_bill = $cust_pkg->getfield('bill') || 0;
184 #my $last_bill = $cust_pkg->last_bill || 0;
185 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
187 return 0 if ! $self->base_recur
188 || ! $self->option('unused_credit', 1)
191 || $next_bill < $time;
194 'h' => 3600, # 60 * 60
195 'd' => 86400, # 60 * 60 * 24
196 'w' => 604800, # 60 * 60 * 24 * 7
197 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
200 $self->freq =~ /^(\d+)([hdwm]?)$/
201 or die 'unparsable frequency: '. $self->freq;
202 my $freq_sec = $1 * $sec{$2||'m'};
203 return 0 unless $freq_sec;
205 sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
209 sub is_free_options {
210 qw( setup_fee recur_fee );
214 0; #no, we're postpaid
217 sub usage_valuehash {
219 map { $_, $self->option($_) }
220 grep { $self->option($_, 'hush') }
221 qw(seconds upbytes downbytes totalbytes);
225 my($self, $cust_pkg, %opt) = @_;
226 warn " resetting usage counters" if $opt{debug} > 1;
227 my %values = $self->usage_valuehash;
228 if ($self->option('usage_rollover', 1)) {
229 $cust_pkg->recharge(\%values);
231 $cust_pkg->set_usage(\%values, %opt);