1 package FS::part_pkg::flat;
2 use base qw( FS::part_pkg::prorate_Mixin
3 FS::part_pkg::discount_Mixin
8 use vars qw( %info %usage_recharge_fields @usage_recharge_fieldorder );
9 use FS::Record qw( qsearch );
10 use FS::cust_credit_source_bill_pkg;
12 use List::Util qw( min );
13 use FS::UI::bytecount;
16 tie my %temporalities, 'Tie::IxHash',
17 'upcoming' => "Upcoming (future)",
18 'preceding' => "Preceding (past)",
21 tie my %contract_years, 'Tie::IxHash', (
23 map { $_*12 => $_ } (1..5),
27 'name' => 'Flat rate (anniversary billing)',
28 'shortname' => 'Anniversary',
29 'inherit_fields' => [ 'prorate_Mixin', 'usage_Mixin', 'global_Mixin' ],
31 #false laziness w/voip_cdr.pm
32 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
34 'select_options' => \%temporalities,
37 #used in cust_pkg.pm so could add to any price plan where it made sense
38 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
41 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
42 'with the customer\'s other packages',
45 'prorate_defer_bill' => {
46 'name' => 'When synchronizing, defer the bill until '.
47 'the customer\'s next bill date',
50 'prorate_round_day' => {
51 'name' => 'When synchronizing, round the prorated '.
52 'period to the nearest full day',
55 'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
57 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
60 'unsuspend_adjust_bill' =>
61 { 'name' => 'Adjust next bill date forward when '.
65 'bill_recur_on_cancel' => {
66 'name' => 'Bill the last period on cancellation',
69 'bill_suspend_as_cancel' => {
70 'name' => 'Bill immediately upon suspension', #desc?
73 'externalid' => { 'name' => 'Optional External ID',
77 'fieldorder' => [ qw( recur_temporality
79 sync_bill_date prorate_defer_bill prorate_round_day
80 suspend_bill unsuspend_adjust_bill
82 bill_suspend_as_cancel
91 my $conf = new FS::Conf;
92 my $money_char = $conf->config('money_char') || '$';
93 my $setup = $opt{cust_pkg} ? $self->base_setup( $opt{cust_pkg} )
94 : ($self->option('setup_fee') || 0);
95 my $recur = $opt{cust_pkg} ? $self->base_recur( $opt{cust_pkg} )
96 : ($self->option('recur_fee', 1) || 0);
97 $recur += $self->usageprice_recur( $opt{cust_pkg} ) if $opt{cust_pkg};
99 $str = $money_char . $setup . ($recur ? ' setup' : ' one-time') if $setup;
100 $str .= ', ' if ($setup && $recur);
101 $str .= $money_char. $recur. '/'. $self->freq_pretty if $recur;
106 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
108 return 0 if $self->prorate_setup($cust_pkg, $sdate);
111 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
112 while ($i < $count) {
113 push @$details, $self->option( 'additional_info' . $i++ );
116 my $charge = $self->base_setup($cust_pkg, $sdate, $details);
120 $param->{'setup_charge'} = $charge;
121 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
122 delete $param->{'setup_charge'};
125 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
129 my($self, $cust_pkg, $sdate, $details ) = @_;
130 ( exists( $self->{'Hash'}{'_opt_setup_fee'} )
131 ? $self->{'Hash'}{'_opt_setup_fee'}
132 : $self->option('setup_fee', 1)
139 my($cust_pkg, $sdate, $details, $param ) = @_;
141 #my $last_bill = $cust_pkg->last_bill;
142 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
145 if $self->recur_temporality eq 'preceding' && !$last_bill;
147 my $charge = $self->base_recur($cust_pkg, $sdate);
148 # always treat cutoff_day as a list
149 if ( my @cutoff_day = $self->cutoff_day($cust_pkg) ) {
150 $charge = $self->calc_prorate(@_, @cutoff_day);
152 elsif ( $param->{freq_override} ) {
153 # XXX not sure if this should be mutually exclusive with sync_bill_date.
154 # Given the very specific problem that freq_override is meant to 'solve',
155 # it probably should.
156 $charge *= $param->{freq_override} if $param->{freq_override};
159 $charge += $self->usageprice_recur($cust_pkg, $sdate);
160 $cust_pkg->apply_usageprice(); #$sdate for prorating?
162 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
164 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
169 my $cust_pkg = shift;
170 if ( $self->option('sync_bill_date',1) ) {
171 my $next_bill = $cust_pkg->cust_main->next_bill_date;
172 if ( defined($next_bill) ) {
173 # careful here. if the prorate calculation is going to round to
174 # the nearest day, this needs to always return the same result
175 if ( $self->option('prorate_round_day', 1) ) {
176 my $hour = (localtime($next_bill))[2];
177 $next_bill += 64800 if $hour >= 12;
179 return (localtime($next_bill))[3];
186 my($self, $cust_pkg, $sdate) = @_;
187 ( exists( $self->{'Hash'}{'_opt_recur_fee'} )
188 ? $self->{'Hash'}{'_opt_recur_fee'}
189 : $self->option('recur_fee', 1)
194 sub base_recur_permonth {
195 my($self, $cust_pkg) = @_;
197 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
199 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
202 sub usageprice_recur {
203 my($self, $cust_pkg, $sdate) = @_;
206 $recur += $_->price foreach $cust_pkg->cust_pkg_usageprice;
208 sprintf('%.2f', $recur);
213 my $conf = new FS::Conf;
214 if ( $self->recur_temporality eq 'preceding'
215 and $self->option('bill_recur_on_cancel', 1) ) {
216 # run another recurring cycle
217 return $self->calc_recur(@_);
218 } elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option?
219 and $self->can('calc_usage') ) {
220 # bill for outstanding usage
221 return $self->calc_usage(@_);
223 return 'NOTHING'; # numerically zero, but has special meaning
228 my ($self, $cust_pkg, %options) = @_;
231 if ($options{'time'}) {
232 $time = $options{'time'};
237 my $next_bill = $cust_pkg->getfield('bill') || 0;
239 return 0 if ! $self->base_recur($cust_pkg, \$time)
241 || $next_bill < $time;
243 # Use actual charge for this period, not base_recur (for discounts).
244 # Use sdate < $time and edate >= $time because when billing on
245 # cancellation, edate = $time.
247 foreach my $cust_bill_pkg (
248 qsearch('cust_bill_pkg', {
249 pkgnum => $cust_pkg->pkgnum,
250 edate => {op => '>=', value => $time},
251 recur => {op => '>' , value => 0},
255 # hack to deal with the weird behavior of edate on package cancellation
256 my $edate = $cust_bill_pkg->edate;
257 if ( $self->recur_temporality eq 'preceding' ) {
258 $edate = $self->add_freq($cust_bill_pkg->sdate);
261 # this will also get any package charges that are _entirely_ after the
262 # cancellation date (can happen with advance billing). in that case,
263 # use the entire recurring charge:
264 my $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
266 # but if the cancellation happens during the interval, prorate it:
267 # (XXX obey prorate_round_day here?)
268 if ( $cust_bill_pkg->sdate < $time ) {
269 $amount = $amount * ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
274 push @{ $options{'cust_credit_source_bill_pkg'} },
275 new FS::cust_credit_source_bill_pkg {
276 'billpkgnum' => $cust_bill_pkg->billpkgnum,
277 'amount' => sprintf('%.2f', $amount),
278 'currency' => $cust_bill_pkg->cust_bill->currency,
280 if $options{'cust_credit_source_bill_pkg'};
284 sprintf('%.2f', $credit);
288 sub is_free_options {
289 qw( setup_fee recur_fee );
292 sub is_prepaid { 0; } #no, we're postpaid
297 return 0 if $self->start_on_hold;
299 ! $self->option('start_1st', 1) && ( ! $self->option('sync_bill_date',1)
300 || ! $self->option('prorate_defer_bill',1)
301 || ! $opt{'num_ncancelled_pkgs'}
305 sub can_discount { 1; }
307 sub can_usageprice { 1; }
309 sub recur_temporality {
311 $self->option('recur_temporality', 1);
314 sub usage_valuehash {
316 map { $_, $self->option($_) }
317 grep { $self->option($_, 'hush') }
318 qw(seconds upbytes downbytes totalbytes);
322 my($self, $cust_pkg, %opt) = @_;
323 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
324 my %values = $self->usage_valuehash;
325 if ($self->option('usage_rollover', 1)) {
326 $cust_pkg->recharge(\%values);
328 $cust_pkg->set_usage(\%values, %opt);