1 package FS::part_pkg::flat;
4 use vars qw(@ISA %info);
6 #use FS::Record qw(qsearch);
10 @ISA = qw(FS::part_pkg);
12 tie my %temporalities, 'Tie::IxHash',
13 'upcoming' => "Upcoming (future)",
14 'preceding' => "Preceding (past)",
18 'name' => 'Flat rate (anniversary billing)',
20 'setup_fee' => { 'name' => 'Setup fee for this package',
23 'recur_fee' => { 'name' => 'Recurring fee for this package',
27 #false laziness w/voip_cdr.pm
28 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
30 'select_options' => \%temporalities,
33 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
34 ' of service at cancellation',
37 'externalid' => { 'name' => 'Optional External ID',
40 'seconds' => { 'name' => 'Time limit for this package',
42 'check' => sub { shift =~ /^\d*$/ },
44 'upbytes' => { 'name' => 'Upload limit for this package',
46 'check' => sub { shift =~ /^\d*$/ },
47 'format' => \&FS::UI::bytecount::display_bytecount,
48 'parse' => \&FS::UI::bytecount::parse_bytecount,
50 'downbytes' => { 'name' => 'Download limit for this package',
52 'check' => sub { shift =~ /^\d*$/ },
53 'format' => \&FS::UI::bytecount::display_bytecount,
54 'parse' => \&FS::UI::bytecount::parse_bytecount,
56 'totalbytes' => { 'name' => 'Transfer limit for this package',
58 'check' => sub { shift =~ /^\d*$/ },
59 'format' => \&FS::UI::bytecount::display_bytecount,
60 'parse' => \&FS::UI::bytecount::parse_bytecount,
62 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
64 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
66 'recharge_seconds' => { 'name' => 'Recharge time for this package',
68 'check' => sub { shift =~ /^\d*$/ },
70 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
72 'check' => sub { shift =~ /^\d*$/ },
73 'format' => \&FS::UI::bytecount::display_bytecount,
74 'parse' => \&FS::UI::bytecount::parse_bytecount,
76 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
78 'check' => sub { shift =~ /^\d*$/ },
79 'format' => \&FS::UI::bytecount::display_bytecount,
80 'parse' => \&FS::UI::bytecount::parse_bytecount,
82 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
84 'check' => sub { shift =~ /^\d*$/ },
85 'format' => \&FS::UI::bytecount::display_bytecount,
86 'parse' => \&FS::UI::bytecount::parse_bytecount,
88 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
89 ' over into current period',
92 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
97 'fieldorder' => [qw( setup_fee recur_fee recur_temporality unused_credit
98 seconds upbytes downbytes totalbytes
99 recharge_amount recharge_seconds recharge_upbytes
100 recharge_downbytes recharge_totalbytes
101 usage_rollover recharge_reset externalid
108 my($self, $cust_pkg, $sdate, $details ) = @_;
111 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
112 while ($i < $count) {
113 push @$details, $self->option( 'additional_info' . $i++ );
116 my $quantity = $cust_pkg->quantity || 1;
118 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
122 my($self, $cust_pkg, $sdate, $details ) = @_;
124 $self->option('setup_fee');
128 my($self, $cust_pkg) = @_;
130 #my $last_bill = $cust_pkg->last_bill;
131 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
134 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
136 $self->base_recur($cust_pkg);
140 my($self, $cust_pkg) = @_;
141 $self->option('recur_fee', 1) || 0;
145 my ($self, $cust_pkg, %options) = @_;
148 if ($options{'time'}) {
149 $time = $options{'time'};
154 my $next_bill = $cust_pkg->getfield('bill') || 0;
156 #my $last_bill = $cust_pkg->last_bill || 0;
157 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
159 return 0 if ! $self->base_recur
160 || ! $self->option('unused_credit', 1)
163 || $next_bill < $time;
166 'h' => 3600, # 60 * 60
167 'd' => 86400, # 60 * 60 * 24
168 'w' => 604800, # 60 * 60 * 24 * 7
169 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
172 $self->freq =~ /^(\d+)([hdwm]?)$/
173 or die 'unparsable frequency: '. $self->freq;
174 my $freq_sec = $1 * $sec{$2||'m'};
175 return 0 unless $freq_sec;
177 sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
181 sub is_free_options {
182 qw( setup_fee recur_fee );
186 0; #no, we're postpaid
190 my($self, $cust_pkg) = @_;
191 my %values = map { $_, $self->option($_) }
192 grep { $self->option($_, 'hush') }
193 qw(seconds upbytes downbytes totalbytes);
194 if ($self->option('usage_rollover', 1)) {
195 $cust_pkg->recharge(\%values);
197 $cust_pkg->set_usage(\%values);