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);
17 tie my %temporalities, 'Tie::IxHash',
18 'upcoming' => "Upcoming (future)",
19 'preceding' => "Preceding (past)",
24 'seconds' => { 'name' => 'Time limit for this package',
26 'check' => sub { shift =~ /^\d*$/ },
28 'upbytes' => { 'name' => 'Upload limit for this package',
30 'check' => sub { shift =~ /^\d*$/ },
31 'format' => \&FS::UI::bytecount::display_bytecount,
32 'parse' => \&FS::UI::bytecount::parse_bytecount,
34 'downbytes' => { 'name' => 'Download limit for this package',
36 'check' => sub { shift =~ /^\d*$/ },
37 'format' => \&FS::UI::bytecount::display_bytecount,
38 'parse' => \&FS::UI::bytecount::parse_bytecount,
40 'totalbytes' => { 'name' => 'Transfer limit for this package',
42 'check' => sub { shift =~ /^\d*$/ },
43 'format' => \&FS::UI::bytecount::display_bytecount,
44 'parse' => \&FS::UI::bytecount::parse_bytecount,
48 %usage_recharge_fields = (
50 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
52 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
54 'recharge_seconds' => { 'name' => 'Recharge time for this package',
56 'check' => sub { shift =~ /^\d*$/ },
58 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
60 'check' => sub { shift =~ /^\d*$/ },
61 'format' => \&FS::UI::bytecount::display_bytecount,
62 'parse' => \&FS::UI::bytecount::parse_bytecount,
64 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
66 'check' => sub { shift =~ /^\d*$/ },
67 'format' => \&FS::UI::bytecount::display_bytecount,
68 'parse' => \&FS::UI::bytecount::parse_bytecount,
70 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
72 'check' => sub { shift =~ /^\d*$/ },
73 'format' => \&FS::UI::bytecount::display_bytecount,
74 'parse' => \&FS::UI::bytecount::parse_bytecount,
76 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
77 ' over into current period',
80 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
86 @usage_fieldorder = qw( seconds upbytes downbytes totalbytes );
87 @usage_recharge_fieldorder = qw(
88 recharge_amount recharge_seconds recharge_upbytes
89 recharge_downbytes recharge_totalbytes
90 usage_rollover recharge_reset
94 'name' => 'Flat rate (anniversary billing)',
95 'shortname' => 'Anniversary',
97 'setup_fee' => { 'name' => 'Setup fee for this package',
100 'recur_fee' => { 'name' => 'Recurring fee for this package',
104 #false laziness w/voip_cdr.pm
105 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
107 'select_options' => \%temporalities,
111 %usage_recharge_fields,
113 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
114 ' of service at cancellation',
115 'type' => 'checkbox',
117 'externalid' => { 'name' => 'Optional External ID',
121 'fieldorder' => [ qw( setup_fee recur_fee recur_temporality unused_credit ),
122 @usage_fieldorder, @usage_recharge_fieldorder,
129 my($self, $cust_pkg, $sdate, $details ) = @_;
132 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
133 while ($i < $count) {
134 push @$details, $self->option( 'additional_info' . $i++ );
137 my $quantity = $cust_pkg->quantity || 1;
139 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
143 my($self, $cust_pkg, $sdate, $details ) = @_;
145 $self->option('setup_fee');
152 #my $last_bill = $cust_pkg->last_bill;
153 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
156 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
158 my $br = $self->base_recur(@_);
160 my $discount = $self->calc_discount(@_);
162 sprintf('%.2f', $br - $discount);
167 my($cust_pkg, $sdate, $details, $param ) = @_;
169 my $br = $self->base_recur(@_);
171 my $tot_discount = 0;
172 #UI enforces just 1 for now, will need ordering when they can be stacked
173 foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
174 my $discount = $cust_pkg_discount->discount;
175 #UI enforces one or the other (for now? probably for good)
177 $amount += $discount->amount;
178 $amount += sprintf('%.2f', $discount->percent * $br / 100 );
180 my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
182 my $months = $discount->months
184 $discount->months - $cust_pkg->months_used )
187 my $error = $cust_pkg_discount->increment_months_used($months);
188 die "error discounting: $error" if $error;
191 $amount = sprintf('%.2f', $amount);
193 #add details on discount to invoice
194 my $conf = new FS::Conf;
195 my $money_char = $conf->config('money_char') || '$';
196 $months = sprintf('%.2f', $months) if $months =~ /\./;
199 $d .= $discount->name. ' ' if $discount->name;
200 $d .= 'discount of '. $discount->description_short;
201 $d .= " for $months month". ( $months!=1 ? 's' : '' );
202 $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
205 $tot_discount += $amount;
208 sprintf('%.2f', $tot_discount);
212 my($self, $cust_pkg) = @_;
213 $self->option('recur_fee', 1) || 0;
216 sub base_recur_permonth {
217 my($self, $cust_pkg) = @_;
219 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
221 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
225 my ($self, $cust_pkg, %options) = @_;
228 if ($options{'time'}) {
229 $time = $options{'time'};
234 my $next_bill = $cust_pkg->getfield('bill') || 0;
236 #my $last_bill = $cust_pkg->last_bill || 0;
237 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
239 return 0 if ! $self->base_recur($cust_pkg)
240 || ! $self->option('unused_credit', 1)
243 || $next_bill < $time;
246 'h' => 3600, # 60 * 60
247 'd' => 86400, # 60 * 60 * 24
248 'w' => 604800, # 60 * 60 * 24 * 7
249 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
252 $self->freq =~ /^(\d+)([hdwm]?)$/
253 or die 'unparsable frequency: '. $self->freq;
254 my $freq_sec = $1 * $sec{$2||'m'};
255 return 0 unless $freq_sec;
257 sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
261 sub is_free_options {
262 qw( setup_fee recur_fee );
265 sub is_prepaid { 0; } #no, we're postpaid
267 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
270 $self->freq =~ /^\d+$/ && $self->freq > 0;
273 sub usage_valuehash {
275 map { $_, $self->option($_) }
276 grep { $self->option($_, 'hush') }
277 qw(seconds upbytes downbytes totalbytes);
281 my($self, $cust_pkg, %opt) = @_;
282 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
283 my %values = $self->usage_valuehash;
284 if ($self->option('usage_rollover', 1)) {
285 $cust_pkg->recharge(\%values);
287 $cust_pkg->set_usage(\%values, %opt);