+
+ return $charge;
+}
+
+=item prorate_setup CUST_PKG SDATE
+
+Set up the package. This only has an effect if prorate_defer_bill is
+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 $cutoff_day = $self->cutoff_day($cust_pkg);
+ if ( ! $cust_pkg->bill
+ and $self->option('prorate_defer_bill',1)
+ and $cutoff_day
+ ) {
+ my ($mnow, $mend, $mstart) = $self->_endpoints($sdate, $cutoff_day);
+ # if today is the cutoff day, set the next bill to right now instead
+ # of waiting a month.
+ if ( $mnow - $mstart < 86400 ) {
+ $cust_pkg->bill($mnow);
+ }
+ else {
+ $cust_pkg->bill($mend);
+ }
+ return 1;
+ }
+ return 0;
+}
+
+=item _endpoints TIME CUTOFF_DAY
+
+Given a current time and a day of the month to prorate to, return three
+times: the start of the prorate interval (usually the current time), the
+end of the prorate interval (i.e. the cutoff date), and the time one month
+before the end of the prorate interval.
+
+=cut
+
+sub _endpoints {
+ my ($self, $mnow, $cutoff_day) = @_;
+
+ # only works for freq >= 1 month; probably can't be fixed
+ 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 $mend;
+ my $mstart;
+ # if cutoff day > 28, force it to the 1st of next month
+ if ( $cutoff_day > 28 ) {
+ $cutoff_day = 1;
+ # and if we are currently after the 28th, roll the current day
+ # forward to that day
+ if ( $mday > 28 ) {
+ $mday = 1;
+ #set $mnow = $mend so the amount billed will be zero
+ $mnow = timelocal(0,0,0,1,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
+ }
+ }
+ if ( $mday >= $cutoff_day ) {
+ $mend =
+ timelocal(0,0,0,$cutoff_day,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
+ $mstart =
+ timelocal(0,0,0,$cutoff_day,$mon,$year);
+ }
+ else {
+ $mend =
+ timelocal(0,0,0,$cutoff_day,$mon,$year);
+ $mstart =
+ timelocal(0,0,0,$cutoff_day,$mon == 0 ? 11 : $mon - 1,$year-($mon==0));