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;
14 use FS::cust_bill_pkg_discount;
16 @ISA = qw(FS::part_pkg FS::part_pkg::prorate_Mixin);
18 tie my %temporalities, 'Tie::IxHash',
19 'upcoming' => "Upcoming (future)",
20 'preceding' => "Preceding (past)",
23 tie my %contract_years, 'Tie::IxHash', (
25 map { $_*12 => $_ } (1..5),
30 'seconds' => { 'name' => 'Time limit for this package',
32 'check' => sub { shift =~ /^\d*$/ },
34 'upbytes' => { 'name' => 'Upload limit for this package',
36 'check' => sub { shift =~ /^\d*$/ },
37 'format' => \&FS::UI::bytecount::display_bytecount,
38 'parse' => \&FS::UI::bytecount::parse_bytecount,
40 'downbytes' => { 'name' => 'Download limit for this package',
42 'check' => sub { shift =~ /^\d*$/ },
43 'format' => \&FS::UI::bytecount::display_bytecount,
44 'parse' => \&FS::UI::bytecount::parse_bytecount,
46 'totalbytes' => { 'name' => 'Transfer limit for this package',
48 'check' => sub { shift =~ /^\d*$/ },
49 'format' => \&FS::UI::bytecount::display_bytecount,
50 'parse' => \&FS::UI::bytecount::parse_bytecount,
54 %usage_recharge_fields = (
56 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
58 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
60 'recharge_seconds' => { 'name' => 'Recharge time for this package',
62 'check' => sub { shift =~ /^\d*$/ },
64 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
66 'check' => sub { shift =~ /^\d*$/ },
67 'format' => \&FS::UI::bytecount::display_bytecount,
68 'parse' => \&FS::UI::bytecount::parse_bytecount,
70 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
72 'check' => sub { shift =~ /^\d*$/ },
73 'format' => \&FS::UI::bytecount::display_bytecount,
74 'parse' => \&FS::UI::bytecount::parse_bytecount,
76 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
78 'check' => sub { shift =~ /^\d*$/ },
79 'format' => \&FS::UI::bytecount::display_bytecount,
80 'parse' => \&FS::UI::bytecount::parse_bytecount,
82 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
83 ' over into current period',
86 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
92 @usage_fieldorder = qw( seconds upbytes downbytes totalbytes );
93 @usage_recharge_fieldorder = qw(
94 recharge_amount recharge_seconds recharge_upbytes
95 recharge_downbytes recharge_totalbytes
96 usage_rollover recharge_reset
100 'name' => 'Flat rate (anniversary billing)',
101 'shortname' => 'Anniversary',
103 'setup_fee' => { 'name' => 'Setup fee for this package',
106 'recur_fee' => { 'name' => 'Recurring fee for this package',
110 #false laziness w/voip_cdr.pm
111 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
113 'select_options' => \%temporalities,
115 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
116 ' of service at cancellation',
117 'type' => 'checkbox',
120 #used in cust_pkg.pm so could add to any price plan
121 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
123 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
125 'contract_end_months'=> {
126 'name' => 'Auto-add a contract end date this number of years out',
128 'select_options' => \%contract_years,
130 #used in cust_pkg.pm so could add to any price plan where it made sense
131 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
132 'type' => 'checkbox',
134 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
135 'with the customer\'s other packages',
136 'type' => 'checkbox',
138 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
139 'type' => 'checkbox',
141 'unsuspend_adjust_bill' =>
142 { 'name' => 'Adjust next bill date forward when '.
144 'type' => 'checkbox',
148 %usage_recharge_fields,
150 'externalid' => { 'name' => 'Optional External ID',
154 'fieldorder' => [ qw( setup_fee recur_fee
155 recur_temporality unused_credit
156 expire_months adjourn_months
158 start_1st sync_bill_date
159 suspend_bill unsuspend_adjust_bill
161 @usage_fieldorder, @usage_recharge_fieldorder,
168 my($self, $cust_pkg, $sdate, $details ) = @_;
171 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
172 while ($i < $count) {
173 push @$details, $self->option( 'additional_info' . $i++ );
176 my $quantity = $cust_pkg->quantity || 1;
178 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
182 my($self, $cust_pkg, $sdate, $details ) = @_;
184 $self->option('setup_fee') || 0;
189 my($cust_pkg, $sdate, $details, $param ) = @_;
191 #my $last_bill = $cust_pkg->last_bill;
192 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
195 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
197 if( $self->option('sync_bill_date',1) ) {
198 return $self->calc_prorate(@_);
201 my $charge = $self->base_recur($cust_pkg);
202 $charge *= $param->{freq_override} if $param->{freq_override};
203 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
205 return sprintf('%.2f', $charge - $discount);
210 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
212 my $br = $self->base_recur($cust_pkg);
214 my $tot_discount = 0;
215 #UI enforces just 1 for now, will need ordering when they can be stacked
217 if ( $param->{freq_override} ) {
218 my $real_part_pkg = new FS::part_pkg { $self->hash };
219 $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
220 my @discount = grep { $_->months == $param->{freq_override} }
222 $real_part_pkg->part_pkg_discount;
223 my $discount = shift @discount;
224 $param->{months} = $param->{freq_override} unless $param->{months};
227 if ($discount->months == $param->{months}) {
228 $cust_pkg->discountnum($discount->discountnum);
229 $error = $cust_pkg->insert_discount;
231 $cust_pkg->discountnum(-1);
232 foreach ( qw( amount percent months ) ) {
233 my $method = "discountnum_$_";
234 $cust_pkg->$method($discount->$_);
236 $error = $cust_pkg->insert_discount;
238 die "error discounting using part_pkg_discount: $error" if $error;
242 my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
243 foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
244 my $discount = $cust_pkg_discount->discount;
245 #UI enforces one or the other (for now? probably for good)
247 $amount += $discount->amount
248 if $cust_pkg->pkgpart == $param->{real_pkgpart};
249 $amount += sprintf('%.2f', $discount->percent * $br / 100 );
251 my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
253 my $months = $discount->months
255 $discount->months - $cust_pkg_discount->months_used )
258 my $error = $cust_pkg_discount->increment_months_used($months)
259 if $cust_pkg->pkgpart == $param->{real_pkgpart};
260 die "error discounting: $error" if $error;
263 $amount = sprintf('%.2f', $amount);
265 next unless $amount > 0;
267 #record details in cust_bill_pkg_discount
268 my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
269 'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
273 push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
275 #add details on discount to invoice
276 my $conf = new FS::Conf;
277 my $money_char = $conf->config('money_char') || '$';
278 $months = sprintf('%.2f', $months) if $months =~ /\./;
281 $d .= $discount->name. ' ' if $discount->name;
282 $d .= 'discount of '. $discount->description_short;
283 $d .= " for $months month". ( $months!=1 ? 's' : '' );
284 $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
287 $tot_discount += $amount;
290 sprintf('%.2f', $tot_discount);
294 my($self, $cust_pkg) = @_;
295 $self->option('recur_fee', 1) || 0;
298 sub base_recur_permonth {
299 my($self, $cust_pkg) = @_;
301 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
303 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
307 my ($self, $cust_pkg, %options) = @_;
310 if ($options{'time'}) {
311 $time = $options{'time'};
316 my $next_bill = $cust_pkg->getfield('bill') || 0;
318 #my $last_bill = $cust_pkg->last_bill || 0;
319 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
321 return 0 if ! $self->base_recur($cust_pkg)
322 || ! $self->option('unused_credit', 1)
325 || $next_bill < $time;
328 'h' => 3600, # 60 * 60
329 'd' => 86400, # 60 * 60 * 24
330 'w' => 604800, # 60 * 60 * 24 * 7
331 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
334 $self->freq =~ /^(\d+)([hdwm]?)$/
335 or die 'unparsable frequency: '. $self->freq;
336 my $freq_sec = $1 * $sec{$2||'m'};
337 return 0 unless $freq_sec;
339 sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
343 sub is_free_options {
344 qw( setup_fee recur_fee );
347 sub is_prepaid { 0; } #no, we're postpaid
349 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
352 $self->freq =~ /^\d+$/ && $self->freq > 0;
355 sub usage_valuehash {
357 map { $_, $self->option($_) }
358 grep { $self->option($_, 'hush') }
359 qw(seconds upbytes downbytes totalbytes);
363 my($self, $cust_pkg, %opt) = @_;
364 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
365 my %values = $self->usage_valuehash;
366 if ($self->option('usage_rollover', 1)) {
367 $cust_pkg->recharge(\%values);
369 $cust_pkg->set_usage(\%values, %opt);