X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg%2Fprorate_Mixin.pm;h=7eac2af5be411cb06138cf09116fcdd43cbdfcf2;hb=57bb423fe457ba4e13726877f53bcdf944f828f8;hp=26fdc35580c65f6cad51bf95b11b3326abf55f80;hpb=0cd91e6625848b1ccf72b4b80bd18b3a1c66bbee;p=freeside.git diff --git a/FS/FS/part_pkg/prorate_Mixin.pm b/FS/FS/part_pkg/prorate_Mixin.pm index 26fdc3558..7eac2af5b 100644 --- a/FS/FS/part_pkg/prorate_Mixin.pm +++ b/FS/FS/part_pkg/prorate_Mixin.pm @@ -2,6 +2,7 @@ package FS::part_pkg::prorate_Mixin; use strict; use vars qw( %info ); +use Tie::IxHash; use Time::Local qw( timelocal timelocal_nocheck ); use Date::Format qw( time2str ); use List::Util qw( min ); @@ -29,7 +30,7 @@ tie our %prorate_round_day_opts, 'Tie::IxHash', }, 'prorate_defer_bill' => { 'name' => 'When prorating, defer the first bill until the '. - 'billing day', + 'billing day or customers next bill date if synchronizing.', 'type' => 'checkbox', }, 'prorate_verbose' => { @@ -107,7 +108,8 @@ sub calc_prorate { ) { #warn "[calc_prorate] #".$cust_pkg->pkgnum.": running deferred setup\n"; - $param->{'setup_fee'} = $self->calc_setup($cust_pkg, $$sdate, $details); + $param->{'setup_fee'} = $self->calc_setup($cust_pkg, $$sdate, $details) + unless $cust_pkg->{'Hash'}->{'waive_setup'}; $mnow = $cust_pkg->setup; $add_period = 1; } @@ -188,25 +190,37 @@ set, in which case it postpones the next bill to the cutoff day. =cut sub prorate_setup { - my $self = shift; - my ($cust_pkg, $sdate) = @_; + my ($self, $cust_pkg, $time) = @_; my @cutoff_days = $self->cutoff_day($cust_pkg); - if ( ! $cust_pkg->bill - and $self->option('prorate_defer_bill',1) - and @cutoff_days - ) { - my ($mnow, $mend, $mstart) = $self->_endpoints($sdate, @cutoff_days); - # If today is the cutoff day, set the next bill and setup both to - # midnight today, so that the customer will be billed normally for a - # month starting today. - if ( $mnow - $mstart < 86400 ) { - $cust_pkg->setup($mstart); - $cust_pkg->bill($mstart); - } - else { - $cust_pkg->bill($mend); + if ( @cutoff_days and $self->option('prorate_defer_bill', 1) ) { + if ( $cust_pkg->setup ) { + # Setup date is already set. Then we're being called indirectly via calc_prorate + # to calculate the deferred setup fee. Allow that to happen normally. + return 0; + } else { + # We're going to set the setup date (so that the deferred billing knows when + # the package started) and suppress charging the setup fee. + if ( $cust_pkg->bill ) { + # For some reason (probably user override), the bill date has been set even + # though the package isn't billing yet. Start billing as though that was the + # start date. + $time = $cust_pkg->bill; + $cust_pkg->setup($cust_pkg->bill); + } + # Now figure the start and end of the period that contains the start date. + my ($mnow, $mend, $mstart) = $self->_endpoints($time, @cutoff_days); + # If today is the cutoff day, set the next bill and setup both to + # midnight today, so that the customer will be billed normally for a + # month starting today. + if ( $mnow - $mstart < 86400 ) { + $cust_pkg->setup($mstart); + $cust_pkg->bill($mstart); + } + else { + $cust_pkg->bill($mend); + } + return 1; } - return 1; } return 0; }