X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg%2Fflat.pm;h=d11b99b1aa57837fbe43da89a8a8fe26b5217381;hb=98f6d91ec7eaa907204afbfeb90ede1e3bff656d;hp=1594a132b0c3ef252f1b8f7d71a120d2a49f6b1d;hpb=ad4bf730d6e96f0acf589c00ff31eaa64f60b0b7;p=freeside.git diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 1594a132b..d11b99b1a 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -7,6 +7,7 @@ use base qw( FS::part_pkg::prorate_Mixin use strict; use vars qw( %info %usage_recharge_fields @usage_recharge_fieldorder ); use FS::Record qw( qsearch ); +use FS::cust_credit_source_bill_pkg; use Tie::IxHash; use List::Util qw( min ); use FS::UI::bytecount; @@ -33,16 +34,6 @@ tie my %contract_years, 'Tie::IxHash', ( 'select_options' => \%temporalities, }, - #used in cust_pkg.pm so could add to any price plan - 'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out', - }, - 'adjourn_months'=> { 'name' => 'Auto-add a suspension date this number of months out', - }, - 'contract_end_months'=> { - 'name' => 'Auto-add a contract end date this number of years out', - 'type' => 'select', - 'select_options' => \%contract_years, - }, #used in cust_pkg.pm so could add to any price plan where it made sense 'start_1st' => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.', 'type' => 'checkbox', @@ -84,8 +75,6 @@ tie my %contract_years, 'Tie::IxHash', ( }, }, 'fieldorder' => [ qw( recur_temporality - expire_months adjourn_months - contract_end_months start_1st sync_bill_date prorate_defer_bill prorate_round_day suspend_bill unsuspend_adjust_bill @@ -178,6 +167,12 @@ sub cutoff_day { if ( $self->option('sync_bill_date',1) ) { my $next_bill = $cust_pkg->cust_main->next_bill_date; if ( defined($next_bill) ) { + # careful here. if the prorate calculation is going to round to + # the nearest day, this needs to always return the same result + if ( $self->option('prorate_round_day', 1) ) { + my $hour = (localtime($next_bill))[2]; + $next_bill += 64800 if $hour >= 12; + } return (localtime($next_bill))[3]; } } @@ -213,13 +208,13 @@ sub calc_cancel { and $self->option('bill_recur_on_cancel', 1) ) { # run another recurring cycle return $self->calc_recur(@_); - } - elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option? + } elsif ( $conf->exists('bill_usage_on_cancel') # should be a package option? and $self->can('calc_usage') ) { # bill for outstanding usage return $self->calc_usage(@_); + } else { + return 'NOTHING'; # numerically zero, but has special meaning } - 0; } sub calc_remain { @@ -242,7 +237,7 @@ sub calc_remain { # Use sdate < $time and edate >= $time because when billing on # cancellation, edate = $time. my $credit = 0; - foreach my $item ( + foreach my $cust_bill_pkg ( qsearch('cust_bill_pkg', { pkgnum => $cust_pkg->pkgnum, sdate => {op => '<' , value => $time}, @@ -250,16 +245,28 @@ sub calc_remain { recur => {op => '>' , value => 0}, }) ) { + # hack to deal with the weird behavior of edate on package cancellation - my $edate = $item->edate; + my $edate = $cust_bill_pkg->edate; if ( $self->recur_temporality eq 'preceding' ) { - $edate = $self->add_freq($item->sdate); + $edate = $self->add_freq($cust_bill_pkg->sdate); } - $credit += ($item->recur - $item->usage) * - ($edate - $time) / ($edate - $item->sdate); + + my $amount = ($cust_bill_pkg->recur - $cust_bill_pkg->usage) * + ($edate - $time) / ($edate - $cust_bill_pkg->sdate); + $credit += $amount; + + push @{ $options{'cust_credit_source_bill_pkg'} }, + new FS::cust_credit_source_bill_pkg { + 'billpkgnum' => $cust_bill_pkg->billpkgnum, + 'amount' => sprintf('%.2f', $amount), + 'currency' => $cust_bill_pkg->cust_bill->currency, + } + if $options{'cust_credit_source_bill_pkg'}; + } + sprintf('%.2f', $credit); - #sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec ); } @@ -272,6 +279,7 @@ sub is_prepaid { 0; } #no, we're postpaid sub can_start_date { my $self = shift; my %opt = @_; + return 0 if $self->start_on_hold; ! $self->option('start_1st', 1) && ( ! $self->option('sync_bill_date',1) || ! $self->option('prorate_defer_bill',1)