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 );
13 use FS::cust_credit_source_bill_pkg;
15 use List::Util qw( min );
16 use FS::UI::bytecount;
18 use Time::Local 'timelocal';
20 #ask FS::UID to run this stuff for us later
21 FS::UID->install_callback( sub {
23 $money_char = $conf->config('money_char') || '$';
26 tie my %temporalities, 'Tie::IxHash',
27 'upcoming' => "Upcoming (future)",
28 'preceding' => "Preceding (past)",
31 tie my %contract_years, 'Tie::IxHash', (
33 map { $_*12 => $_ } (1..5),
37 'name' => 'Flat rate (anniversary billing)',
38 'shortname' => 'Anniversary',
39 'inherit_fields' => [ 'prorate_Mixin', 'usage_Mixin', 'global_Mixin' ],
41 #false laziness w/voip_cdr.pm
42 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
44 'select_options' => \%temporalities,
47 #used in cust_pkg.pm so could add to any price plan where it made sense
48 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
51 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
52 'with the customer\'s other packages',
55 'prorate_defer_bill' => {
56 'name' => 'When synchronizing, defer the bill until '.
57 'the customer\'s next bill date',
60 'prorate_round_day' => {
61 'name' => 'When synchronizing, round the prorated '.
64 'select_options' => \%FS::part_pkg::prorate_Mixin::prorate_round_day_opts,
66 'add_full_period' => { 'disabled' => 1 }, # doesn't make sense with sync?
68 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
71 'unsuspend_adjust_bill' =>
72 { 'name' => 'Adjust next bill date forward when '.
76 'bill_recur_on_cancel' => {
77 'name' => 'Bill the last period on cancellation',
80 'bill_suspend_as_cancel' => {
81 'name' => 'Bill immediately upon suspension', #desc?
84 'externalid' => { 'name' => 'Optional External ID',
88 'fieldorder' => [ qw( recur_temporality
90 sync_bill_date prorate_defer_bill prorate_round_day
91 suspend_bill unsuspend_adjust_bill
93 bill_suspend_as_cancel
103 my $setup = $opt{cust_pkg} ? $self->base_setup( $opt{cust_pkg} )
104 : ($self->option('setup_fee') || 0);
105 my $recur = $opt{cust_pkg} ? $self->base_recur( $opt{cust_pkg} )
106 : ($self->option('recur_fee', 1) || 0);
107 $recur += $self->usageprice_recur( $opt{cust_pkg} ) if $opt{cust_pkg};
110 $str = $money_char . $setup . ($recur ? ' setup' : ' one-time') if $setup;
111 $str .= ', ' if ($setup && $recur);
112 $str .= $money_char. $recur. '/'. $self->freq_pretty if $recur;
117 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
119 return 0 if $self->prorate_setup($cust_pkg, $sdate);
122 my $count = $self->option( 'additional_count', 'quiet' ) || 0;
123 while ($i < $count) {
124 push @$details, $self->option( 'additional_info' . $i++ );
127 my $charge = $self->base_setup($cust_pkg, $sdate, $details);
131 $param->{'setup_charge'} = $charge;
132 $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
133 delete $param->{'setup_charge'};
136 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
140 my($self, $cust_pkg, $sdate, $details ) = @_;
141 $self->option('setup_fee', 1) || 0;
146 my($cust_pkg, $sdate, $details, $param ) = @_;
148 #my $last_bill = $cust_pkg->last_bill;
149 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
152 if $self->recur_temporality eq 'preceding' && !$last_bill;
154 my $charge = $self->base_recur($cust_pkg, $sdate);
155 # always treat cutoff_day as a list
156 if ( my @cutoff_day = $self->cutoff_day($cust_pkg) ) {
157 $charge = $self->calc_prorate(@_, @cutoff_day);
159 elsif ( $param->{freq_override} ) {
160 # XXX not sure if this should be mutually exclusive with sync_bill_date.
161 # Given the very specific problem that freq_override is meant to 'solve',
162 # it probably should.
163 $charge *= $param->{freq_override} if $param->{freq_override};
166 $charge += $self->usageprice_recur($cust_pkg, $sdate);
167 $cust_pkg->apply_usageprice(); #$sdate for prorating?
169 my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
171 sprintf( '%.2f', ($cust_pkg->quantity || 1) * ($charge - $discount) );
176 my $cust_pkg = shift;
177 my $cust_main = $cust_pkg->cust_main;
178 # force it to act like a prorate package, is what this means
179 # because we made a distinction once between prorate and flat packages
180 if ( $cust_main->force_prorate_day and $cust_main->prorate_day ) {
181 return ( $cust_main->prorate_day );
183 if ( $self->option('sync_bill_date',1) ) {
184 my $next_bill = $cust_pkg->cust_main->next_bill_date;
186 return (localtime($next_bill))[3];
188 # This is the customer's only active package and hasn't been billed
189 # yet, so set the cutoff day to either today or tomorrow, whichever
190 # would result in a full period after rounding.
191 my $setup = $cust_pkg->setup; # because it's "now"
192 my $rounding_mode = $self->option('prorate_round_day',1);
193 return () if !$setup or !$rounding_mode;
194 my ($sec, $min, $hour, $mday, $mon, $year) = localtime($setup);
196 if ( ( $rounding_mode == 1 and $hour >= 12 )
197 or ( $rounding_mode == 3 and ( $sec > 0 or $min > 0 or $hour > 0 ))
199 # then the prorate period will be rounded down to start from
200 # midnight tomorrow, so the cutoff day should be the current day +
202 $setup = timelocal(59,59,23,$mday,$mon,$year) + 1;
203 $mday = (localtime($setup))[3];
205 # otherwise, it will be rounded up, so leave the cutoff day at today.
213 my($self, $cust_pkg, $sdate) = @_;
214 $self->option('recur_fee', 1) || 0;
217 sub base_recur_permonth {
218 my($self, $cust_pkg) = @_;
220 return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
222 sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
225 sub usageprice_recur {
226 my($self, $cust_pkg, $sdate) = @_;
229 $recur += $_->price foreach $cust_pkg->cust_pkg_usageprice;
231 sprintf('%.2f', $recur);
236 if ( $self->recur_temporality eq 'preceding'
237 and $self->option('bill_recur_on_cancel', 1) ) {
238 # run another recurring cycle
239 return $self->calc_recur(@_);
240 } elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option?
241 and $self->can('calc_usage') ) {
242 # bill for outstanding usage
243 return $self->calc_usage(@_);
245 return 'NOTHING'; # numerically zero, but has special meaning
249 # no longer used; see credit_remaining in FS::cust_pkg
252 my ($self, $cust_pkg, %options) = @_;
255 if ($options{'time'}) {
256 $time = $options{'time'};
261 my $next_bill = $cust_pkg->getfield('bill') || 0;
263 return 0 if ! $self->base_recur($cust_pkg, \$time)
265 || $next_bill < $time;
267 # Use actual charge for this period, not base_recur (for discounts).
268 # Use sdate < $time and edate >= $time because when billing on
269 # cancellation, edate = $time.
271 foreach my $cust_bill_pkg (
272 qsearch('cust_bill_pkg', {
273 pkgnum => $cust_pkg->pkgnum,
274 edate => {op => '>=', value => $time},
275 recur => {op => '>' , value => 0},
279 # hack to deal with the weird behavior of edate on package cancellation
280 my $edate = $cust_bill_pkg->edate;
281 if ( $self->recur_temporality eq 'preceding' ) {
282 $edate = $self->add_freq($cust_bill_pkg->sdate);
285 # this will also get any package charges that are _entirely_ after the
286 # cancellation date (can happen with advance billing). in that case,
287 # use the entire recurring charge:
288 my $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
290 # but if the cancellation happens during the interval, prorate it:
291 # (XXX obey prorate_round_day here?)
292 if ( $cust_bill_pkg->sdate < $time ) {
293 $amount = $amount * ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
298 push @{ $options{'cust_credit_source_bill_pkg'} },
299 new FS::cust_credit_source_bill_pkg {
300 'billpkgnum' => $cust_bill_pkg->billpkgnum,
301 'amount' => sprintf('%.2f', $amount),
302 'currency' => $cust_bill_pkg->cust_bill->currency,
304 if $options{'cust_credit_source_bill_pkg'};
308 sprintf('%.2f', $credit);
312 sub is_free_options {
313 qw( setup_fee recur_fee );
316 sub is_prepaid { 0; } #no, we're postpaid
321 return 0 if $self->start_on_hold;
323 ! $self->option('start_1st', 1) && ( ! $self->option('sync_bill_date',1)
324 || ! $self->option('prorate_defer_bill',1)
325 || ! $opt{'num_ncancelled_pkgs'}
329 sub can_discount { 1; }
331 sub can_usageprice { 1; }
333 sub recur_temporality {
335 $self->option('recur_temporality', 1);
338 sub usage_valuehash {
340 map { $_, $self->option($_) }
341 grep { $self->option($_, 'hush') }
342 qw(seconds upbytes downbytes totalbytes);
346 my($self, $cust_pkg, %opt) = @_;
347 warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
348 my %values = $self->usage_valuehash;
349 if ($self->option('usage_rollover', 1)) {
350 $cust_pkg->recharge(\%values);
352 $cust_pkg->set_usage(\%values, %opt);