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)',
19 'shortname' => 'Anniversary',
21 'setup_fee' => { 'name' => 'Setup fee for this package',
24 'recur_fee' => { 'name' => 'Recurring fee for this package',
28 #false laziness w/voip_cdr.pm
29 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
31 'select_options' => \%temporalities,
34 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
35 ' of service at cancellation',
38 'externalid' => { 'name' => 'Optional External ID',
41 'seconds' => { 'name' => 'Time limit for this package',
43 'check' => sub { shift =~ /^\d*$/ },
45 'upbytes' => { 'name' => 'Upload limit for this package',
47 'check' => sub { shift =~ /^\d*$/ },
48 'format' => \&FS::UI::bytecount::display_bytecount,
49 'parse' => \&FS::UI::bytecount::parse_bytecount,
51 'downbytes' => { 'name' => 'Download limit for this package',
53 'check' => sub { shift =~ /^\d*$/ },
54 'format' => \&FS::UI::bytecount::display_bytecount,
55 'parse' => \&FS::UI::bytecount::parse_bytecount,
57 'totalbytes' => { 'name' => 'Transfer limit for this package',
59 'check' => sub { shift =~ /^\d*$/ },
60 'format' => \&FS::UI::bytecount::display_bytecount,
61 'parse' => \&FS::UI::bytecount::parse_bytecount,
63 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
65 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
67 'recharge_seconds' => { 'name' => 'Recharge time for this package',
69 'check' => sub { shift =~ /^\d*$/ },
71 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
73 'check' => sub { shift =~ /^\d*$/ },
74 'format' => \&FS::UI::bytecount::display_bytecount,
75 'parse' => \&FS::UI::bytecount::parse_bytecount,
77 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
79 'check' => sub { shift =~ /^\d*$/ },
80 'format' => \&FS::UI::bytecount::display_bytecount,
81 'parse' => \&FS::UI::bytecount::parse_bytecount,
83 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
85 'check' => sub { shift =~ /^\d*$/ },
86 'format' => \&FS::UI::bytecount::display_bytecount,
87 'parse' => \&FS::UI::bytecount::parse_bytecount,
89 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
90 ' over into current period',
93 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
98 'fieldorder' => [qw( setup_fee recur_fee recur_temporality unused_credit
99 seconds upbytes downbytes totalbytes
100 recharge_amount recharge_seconds recharge_upbytes
101 recharge_downbytes recharge_totalbytes
102 usage_rollover recharge_reset externalid
109 my($self, $cust_pkg, $sdate, $details ) = @_;
112 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
113 while ($i < $count) {
114 push @$details, $self->option( 'additional_info' . $i++ );
117 my $quantity = $cust_pkg->quantity || 1;
119 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
123 my($self, $cust_pkg, $sdate, $details ) = @_;
125 $self->option('setup_fee');
129 my($self, $cust_pkg) = @_;
131 #my $last_bill = $cust_pkg->last_bill;
132 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
135 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
137 $self->base_recur($cust_pkg);
141 my($self, $cust_pkg) = @_;
142 $self->option('recur_fee', 1) || 0;
146 my ($self, $cust_pkg, %options) = @_;
149 if ($options{'time'}) {
150 $time = $options{'time'};
155 my $next_bill = $cust_pkg->getfield('bill') || 0;
156 my $last_bill = $cust_pkg->last_bill || 0;
157 return 0 if ! $self->base_recur
158 || ! $self->option('unused_credit', 1)
161 || $next_bill < $time;
164 'h' => 3600, # 60 * 60
165 'd' => 86400, # 60 * 60 * 24
166 'w' => 604800, # 60 * 60 * 24 * 7
167 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
170 $self->freq =~ /^(\d+)([hdwm]?)$/
171 or die 'unparsable frequency: '. $self->freq;
172 my $freq_sec = $1 * $sec{$2||'m'};
173 return 0 unless $freq_sec;
175 sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
179 sub is_free_options {
180 qw( setup_fee recur_fee );
184 0; #no, we're postpaid
188 my($self, $cust_pkg, %opt) = @_;
189 warn " resetting usage counters" if $opt{debug} > 1;
190 my %values = map { $_, $self->option($_) }
191 grep { $self->option($_, 'hush') }
192 qw(seconds upbytes downbytes totalbytes);
193 if ($self->option('usage_rollover', 1)) {
194 $cust_pkg->recharge(\%values);
196 $cust_pkg->set_usage(\%values);