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( $conf $money_char %info
9 %usage_recharge_fields @usage_recharge_fieldorder
12 use FS::Record qw( qsearch );
14 use List::Util qw( min );
15 use FS::UI::bytecount;
18 #ask FS::UID to run this stuff for us later
19 FS::UID->install_callback( sub {
21 $money_char = $conf->config('money_char') || '$';
24 tie my %temporalities, 'Tie::IxHash',
25 'upcoming' => "Upcoming (future)",
26 'preceding' => "Preceding (past)",
29 tie my %contract_years, 'Tie::IxHash', (
31 map { $_*12 => $_ } (1..5),
35 'name' => 'Flat rate (anniversary billing)',
36 'shortname' => 'Anniversary',
37 'inherit_fields' => [ 'prorate_Mixin', 'usage_Mixin', 'global_Mixin' ],
39 #false laziness w/voip_cdr.pm
40 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
42 'select_options' => \%temporalities,
45 #used in cust_pkg.pm so could add to any price plan
46 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
48 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out',
50 'contract_end_months'=> {
51 'name' => 'Auto-add a contract end date this number of years out',
53 'select_options' => \%contract_years,
55 #used in cust_pkg.pm so could add to any price plan where it made sense
56 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
59 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
60 'with the customer\'s other packages',
63 'prorate_defer_bill' => {
64 'name' => 'When synchronizing, defer the bill until '.
65 'the customer\'s next bill date',
68 'prorate_round_day' => {
69 'name' => 'When synchronizing, round the prorated '.
72 'select_options' => \%FS::part_pkg::prorate_Mixin::prorate_round_day_opts,
74 'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
76 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
79 'unsuspend_adjust_bill' =>
80 { 'name' => 'Adjust next bill date forward when '.
84 'bill_recur_on_cancel' => {
85 'name' => 'Bill the last period on cancellation',
88 'bill_suspend_as_cancel' => {
89 'name' => 'Bill immediately upon suspension', #desc?
92 'externalid' => { 'name' => 'Optional External ID',
96 'fieldorder' => [ qw( recur_temporality
97 expire_months adjourn_months
100 sync_bill_date prorate_defer_bill prorate_round_day
101 suspend_bill unsuspend_adjust_bill
103 bill_suspend_as_cancel
112 my $setup = $self->option('setup_fee') || 0;
113 my $recur = $self->option('recur_fee', 1) || 0;
116 $str = $money_char . $setup . ($recur ? ' setup ' : ' one-time') if $setup;
117 $str .= ', ' if ($setup && $recur);
118 $str .= $money_char. $recur. '/'. $self->freq_pretty if $recur;
123 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
125 return 0 if $self->prorate_setup($cust_pkg, $sdate);
128 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
129 while ($i < $count) {
130 push @$details, $self->option( 'additional_info' . $i++ );
133 my $charge = $self->unit_setup($cust_pkg, $sdate, $details);
137 $param->{'setup_charge'} = $charge;
138 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
139 delete $param->{'setup_charge'};
142 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
146 my($self, $cust_pkg, $sdate, $details ) = @_;
147 $self->option('setup_fee', 1) || 0;
152 my($cust_pkg, $sdate, $details, $param ) = @_;
154 #my $last_bill = $cust_pkg->last_bill;
155 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
158 if $self->recur_temporality eq 'preceding' && !$last_bill;
160 my $charge = $self->base_recur($cust_pkg, $sdate);
161 # always treat cutoff_day as a list
162 if ( my @cutoff_day = $self->cutoff_day($cust_pkg) ) {
163 $charge = $self->calc_prorate(@_, @cutoff_day);
165 elsif ( $param->{freq_override} ) {
166 # XXX not sure if this should be mutually exclusive with sync_bill_date.
167 # Given the very specific problem that freq_override is meant to 'solve',
168 # it probably should.
169 $charge *= $param->{freq_override} if $param->{freq_override};
172 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
174 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
179 my $cust_pkg = shift;
180 if ( $self->option('sync_bill_date',1) ) {
181 my $next_bill = $cust_pkg->cust_main->next_bill_date;
182 if ( defined($next_bill) ) {
183 # careful here. if the prorate calculation is going to round to
184 # the nearest day, this needs to always return the same result
185 if ( $self->option('prorate_round_day', 1) ) {
186 my $hour = (localtime($next_bill))[2];
187 $next_bill += 64800 if $hour >= 12;
189 return (localtime($next_bill))[3];
196 my($self, $cust_pkg, $sdate) = @_;
197 $self->option('recur_fee', 1) || 0;
200 sub base_recur_permonth {
201 my($self, $cust_pkg) = @_;
203 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
205 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
210 if ( $self->recur_temporality eq 'preceding'
211 and $self->option('bill_recur_on_cancel', 1) ) {
212 # run another recurring cycle
213 return $self->calc_recur(@_);
214 } elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option?
215 and $self->can('calc_usage') ) {
216 # bill for outstanding usage
217 return $self->calc_usage(@_);
219 return 'NOTHING'; # numerically zero, but has special meaning
224 my ($self, $cust_pkg, %options) = @_;
227 if ($options{'time'}) {
228 $time = $options{'time'};
233 my $next_bill = $cust_pkg->getfield('bill') || 0;
235 return 0 if ! $self->base_recur($cust_pkg, \$time)
237 || $next_bill < $time;
239 # Use actual charge for this period, not base_recur (for discounts).
240 # Use sdate < $time and edate >= $time because when billing on
241 # cancellation, edate = $time.
243 foreach my $cust_bill_pkg (
244 qsearch('cust_bill_pkg', {
245 pkgnum => $cust_pkg->pkgnum,
246 edate => {op => '>=', value => $time},
247 recur => {op => '>' , value => 0},
250 # hack to deal with the weird behavior of edate on package cancellation
251 my $edate = $cust_bill_pkg->edate;
252 if ( $self->recur_temporality eq 'preceding' ) {
253 $edate = $self->add_freq($cust_bill_pkg->sdate);
256 # this will also get any package charges that are _entirely_ after the
257 # cancellation date (can happen with advance billing). in that case,
258 # use the entire recurring charge:
259 my $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
261 # but if the cancellation happens during the interval, prorate it:
262 # (XXX obey prorate_round_day here?)
263 if ( $cust_bill_pkg->sdate < $time ) {
264 $amount = $amount * ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
269 sprintf('%.2f', $credit);
273 sub is_free_options {
274 qw( setup_fee recur_fee );
277 sub is_prepaid { 0; } #no, we're postpaid
282 return 0 if $self->start_on_hold;
284 ! $self->option('start_1st', 1) && ( ! $self->option('sync_bill_date',1)
285 || ! $self->option('prorate_defer_bill',1)
286 || ! $opt{'num_ncancelled_pkgs'}
290 sub can_discount { 1; }
292 sub recur_temporality {
294 $self->option('recur_temporality', 1);
297 sub usage_valuehash {
299 map { $_, $self->option($_) }
300 grep { $self->option($_, 'hush') }
301 qw(seconds upbytes downbytes totalbytes);
305 my($self, $cust_pkg, %opt) = @_;
306 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
307 my %values = $self->usage_valuehash;
308 if ($self->option('usage_rollover', 1)) {
309 $cust_pkg->recharge(\%values);
311 $cust_pkg->set_usage(\%values, %opt);