- my $self = shift;
- my ($cust_pkg, $sdate, $details, $param, $cutoff_day) = @_;
-
- my $charge = $self->option('recur_fee',1) || 0;
- if($cutoff_day) {
- # only works for freq >= 1 month; probably can't be fixed
- my $mnow = $$sdate;
- my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($mnow))[0..5];
- if( $self->option('prorate_round_day',1) ) {
- $mday++ if $hour >= 12;
- $mnow = timelocal(0,0,0,$mday,$mon,$year);
+ my ($self, $cust_pkg, $sdate, $details, $param, @cutoff_days) = @_;
+ die "no cutoff_day" unless @cutoff_days;
+ die "can't prorate non-monthly package\n" if $self->freq =~ /\D/;
+
+ my $money_char = FS::Conf->new->config('money_char') || '$';
+
+ my $charge = $self->base_recur($cust_pkg, $sdate) || 0;
+
+ my $add_period = $self->option('add_full_period',1);
+
+ my $mnow = $$sdate;
+
+ # if this is the first bill but the bill date has been set
+ # (by prorate_defer_bill), calculate from the setup date,
+ # append the setup fee to @$details, and make sure to bill for
+ # a full period after the bill date.
+ if ( $self->option('prorate_defer_bill',1)
+ && ! $cust_pkg->getfield('last_bill')
+ && $cust_pkg->setup
+ )
+ {
+ #warn "[calc_prorate] #".$cust_pkg->pkgnum.": running deferred setup\n";
+ $param->{'setup_fee'} = $self->calc_setup($cust_pkg, $$sdate, $details);
+ $mnow = $cust_pkg->setup;
+ $add_period = 1;
+ }
+
+ # if the customer alreqady has a billing day-of-month established,
+ # and it's a valid cutoff day, try to respect it
+ my $next_bill_day;
+ if ( my $next_bill = $cust_pkg->cust_main->next_bill_date ) {
+ $next_bill_day = (localtime($next_bill))[3];
+ if ( grep {$_ == $next_bill_day} @cutoff_days ) {
+ # by removing all other cutoff days from the list
+ @cutoff_days = ($next_bill_day);