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');
147 my($self, $cust_pkg) = @_;
149 #my $last_bill = $cust_pkg->last_bill;
150 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
153 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
155 $self->base_recur($cust_pkg);
159 my($self, $cust_pkg) = @_;
160 $self->option('recur_fee', 1) || 0;
163 sub base_recur_permonth {
164 my($self, $cust_pkg) = @_; #$cust_pkg?
166 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
168 sprintf('%.2f', $self->base_recur / $self->freq );
172 my ($self, $cust_pkg, %options) = @_;
175 if ($options{'time'}) {
176 $time = $options{'time'};
181 my $next_bill = $cust_pkg->getfield('bill') || 0;
183 #my $last_bill = $cust_pkg->last_bill || 0;
184 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
186 return 0 if ! $self->base_recur
187 || ! $self->option('unused_credit', 1)
190 || $next_bill < $time;
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
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;
204 sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
208 sub is_free_options {
209 qw( setup_fee recur_fee );
213 0; #no, we're postpaid
216 sub usage_valuehash {
218 map { $_, $self->option($_) }
219 grep { $self->option($_, 'hush') }
220 qw(seconds upbytes downbytes totalbytes);
224 my($self, $cust_pkg, %opt) = @_;
225 warn " resetting usage counters" if $opt{debug} > 1;
226 my %values = $self->usage_valuehash;
227 if ($self->option('usage_rollover', 1)) {
228 $cust_pkg->recharge(\%values);
230 $cust_pkg->set_usage(\%values, %opt);