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)",
25 'seconds' => { 'name' => 'Time limit for this package',
27 'check' => sub { shift =~ /^\d*$/ },
29 'upbytes' => { 'name' => 'Upload limit for this package',
31 'check' => sub { shift =~ /^\d*$/ },
32 'format' => \&FS::UI::bytecount::display_bytecount,
33 'parse' => \&FS::UI::bytecount::parse_bytecount,
35 'downbytes' => { 'name' => 'Download limit for this package',
37 'check' => sub { shift =~ /^\d*$/ },
38 'format' => \&FS::UI::bytecount::display_bytecount,
39 'parse' => \&FS::UI::bytecount::parse_bytecount,
41 'totalbytes' => { 'name' => 'Transfer limit for this package',
43 'check' => sub { shift =~ /^\d*$/ },
44 'format' => \&FS::UI::bytecount::display_bytecount,
45 'parse' => \&FS::UI::bytecount::parse_bytecount,
49 %usage_recharge_fields = (
51 'recharge_amount' => { 'name' => 'Cost of recharge for this package',
53 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
55 'recharge_seconds' => { 'name' => 'Recharge time for this package',
57 'check' => sub { shift =~ /^\d*$/ },
59 'recharge_upbytes' => { 'name' => 'Recharge upload for this package',
61 'check' => sub { shift =~ /^\d*$/ },
62 'format' => \&FS::UI::bytecount::display_bytecount,
63 'parse' => \&FS::UI::bytecount::parse_bytecount,
65 'recharge_downbytes' => { 'name' => 'Recharge download for this package',
67 'check' => sub { shift =~ /^\d*$/ },
68 'format' => \&FS::UI::bytecount::display_bytecount,
69 'parse' => \&FS::UI::bytecount::parse_bytecount,
71 'recharge_totalbytes' => { 'name' => 'Recharge transfer for this package',
73 'check' => sub { shift =~ /^\d*$/ },
74 'format' => \&FS::UI::bytecount::display_bytecount,
75 'parse' => \&FS::UI::bytecount::parse_bytecount,
77 'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
78 ' over into current period',
81 'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
87 @usage_fieldorder = qw( seconds upbytes downbytes totalbytes );
88 @usage_recharge_fieldorder = qw(
89 recharge_amount recharge_seconds recharge_upbytes
90 recharge_downbytes recharge_totalbytes
91 usage_rollover recharge_reset
95 'name' => 'Flat rate (anniversary billing)',
96 'shortname' => 'Anniversary',
98 'setup_fee' => { 'name' => 'Setup fee for this package',
101 'recur_fee' => { 'name' => 'Recurring fee for this package',
105 #false laziness w/voip_cdr.pm
106 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
108 'select_options' => \%temporalities,
110 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
111 ' of service at cancellation',
112 'type' => 'checkbox',
115 #used in cust_pkg.pm so could add to any price plan
116 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
118 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
120 #used in cust_pkg.pm so could add to any price plan where it made sense
121 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
122 'type' => 'checkbox',
124 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
125 'with the customer\'s other packages',
126 'type' => 'checkbox',
128 'unsuspend_adjust_bill' =>
129 { 'name' => 'Adjust next bill date forward when '.
131 'type' => 'checkbox',
135 %usage_recharge_fields,
137 'externalid' => { 'name' => 'Optional External ID',
141 'fieldorder' => [ qw( setup_fee recur_fee
142 recur_temporality unused_credit
143 expire_months adjourn_months
144 start_1st sync_bill_date
145 unsuspend_adjust_bill
147 @usage_fieldorder, @usage_recharge_fieldorder,
154 my($self, $cust_pkg, $sdate, $details ) = @_;
157 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
158 while ($i < $count) {
159 push @$details, $self->option( 'additional_info' . $i++ );
162 my $quantity = $cust_pkg->quantity || 1;
164 sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
168 my($self, $cust_pkg, $sdate, $details ) = @_;
170 $self->option('setup_fee') || 0;
175 my($cust_pkg, $sdate, $details, $param ) = @_;
177 #my $last_bill = $cust_pkg->last_bill;
178 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
181 if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
183 if( $self->option('sync_bill_date',1) ) {
184 return $self->calc_prorate(@_);
187 my $charge = $self->base_recur($cust_pkg);
188 $charge *= $param->{freq_override} if $param->{freq_override};
189 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
191 return sprintf('%.2f', $charge - $discount);
196 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
198 my $br = $self->base_recur($cust_pkg);
200 my $tot_discount = 0;
201 #UI enforces just 1 for now, will need ordering when they can be stacked
203 if ( $param->{freq_override} ) {
204 my $real_part_pkg = new FS::part_pkg { $self->hash };
205 $real_part_pkg->pkgpart($param->{real_pkgpart} || $self->pkgpart);
206 my @discount = grep { $_->months == $param->{freq_override} }
208 $real_part_pkg->part_pkg_discount;
209 my $discount = shift @discount;
210 $param->{months} = $param->{freq_override} unless $param->{months};
213 if ($discount->months == $param->{months}) {
214 $cust_pkg->discountnum($discount->discountnum);
215 $error = $cust_pkg->insert_discount;
217 $cust_pkg->discountnum(-1);
218 foreach ( qw( amount percent months ) ) {
219 my $method = "discountnum_$_";
220 $cust_pkg->$method($discount->$_);
222 $error = $cust_pkg->insert_discount;
224 die "error discounting using part_pkg_discount: $error" if $error;
228 my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
229 foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
230 my $discount = $cust_pkg_discount->discount;
231 #UI enforces one or the other (for now? probably for good)
233 $amount += $discount->amount
234 if $cust_pkg->pkgpart == $param->{real_pkgpart};
235 $amount += sprintf('%.2f', $discount->percent * $br / 100 );
237 my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
239 my $months = $discount->months
241 $discount->months - $cust_pkg_discount->months_used )
244 my $error = $cust_pkg_discount->increment_months_used($months)
245 if $cust_pkg->pkgpart == $param->{real_pkgpart};
246 die "error discounting: $error" if $error;
249 $amount = sprintf('%.2f', $amount);
251 next unless $amount > 0;
253 #record details in cust_bill_pkg_discount
254 my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
255 'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,
259 push @{ $param->{'discounts'} }, $cust_bill_pkg_discount;
261 #add details on discount to invoice
262 my $conf = new FS::Conf;
263 my $money_char = $conf->config('money_char') || '$';
264 $months = sprintf('%.2f', $months) if $months =~ /\./;
267 $d .= $discount->name. ' ' if $discount->name;
268 $d .= 'discount of '. $discount->description_short;
269 $d .= " for $months month". ( $months!=1 ? 's' : '' );
270 $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
273 $tot_discount += $amount;
276 sprintf('%.2f', $tot_discount);
280 my($self, $cust_pkg) = @_;
281 $self->option('recur_fee', 1) || 0;
284 sub base_recur_permonth {
285 my($self, $cust_pkg) = @_;
287 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
289 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
293 my ($self, $cust_pkg, %options) = @_;
296 if ($options{'time'}) {
297 $time = $options{'time'};
302 my $next_bill = $cust_pkg->getfield('bill') || 0;
304 #my $last_bill = $cust_pkg->last_bill || 0;
305 my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
307 return 0 if ! $self->base_recur($cust_pkg)
308 || ! $self->option('unused_credit', 1)
311 || $next_bill < $time;
314 'h' => 3600, # 60 * 60
315 'd' => 86400, # 60 * 60 * 24
316 'w' => 604800, # 60 * 60 * 24 * 7
317 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
320 $self->freq =~ /^(\d+)([hdwm]?)$/
321 or die 'unparsable frequency: '. $self->freq;
322 my $freq_sec = $1 * $sec{$2||'m'};
323 return 0 unless $freq_sec;
325 sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
329 sub is_free_options {
330 qw( setup_fee recur_fee );
333 sub is_prepaid { 0; } #no, we're postpaid
335 #XXX discounts only on recurring fees for now (no setup/one-time or usage)
338 $self->freq =~ /^\d+$/ && $self->freq > 0;
341 sub usage_valuehash {
343 map { $_, $self->option($_) }
344 grep { $self->option($_, 'hush') }
345 qw(seconds upbytes downbytes totalbytes);
349 my($self, $cust_pkg, %opt) = @_;
350 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
351 my %values = $self->usage_valuehash;
352 if ($self->option('usage_rollover', 1)) {
353 $cust_pkg->recharge(\%values);
355 $cust_pkg->set_usage(\%values, %opt);