1 package FS::part_pkg::prorate_Mixin;
4 use vars qw(@ISA %info);
5 use Time::Local qw(timelocal);
7 @ISA = qw(FS::part_pkg);
14 FS::part_pkg::prorate_Mixin - Mixin class for part_pkg:: classes that
15 need to prorate partial months
19 package FS::part_pkg::...;
20 use base qw( FS::part_pkg::prorate_Mixin );
24 if( conditions that trigger prorate ) {
25 # sets $$sdate and $param->{'months'}, returns the prorated charge
26 $charges = $self->calc_prorate($cust_pkg, $sdate, $param, $cutoff_day);
33 =item calc_prorate CUST_PKG SDATE DETAILS PARAM CUTOFF_DAY
35 Takes all the arguments of calc_recur. Calculates a prorated charge from
36 the $sdate to the cutoff day for this package definition, and sets the $sdate
37 and $param->{months} accordingly. base_recur() will be called to determine
38 the base price per billing cycle.
41 - add_full_period: Bill for the time up to the prorate day plus one full
42 billing period after that.
43 - prorate_round_day: Round the current time to the nearest full day,
44 instead of using the exact time.
45 - prorate_defer_bill: Don't bill the prorate interval until the prorate
51 my ($self, $cust_pkg, $sdate, $details, $param, $cutoff_day) = @_;
52 die "no cutoff_day" unless $cutoff_day;
54 my $charge = $self->base_recur($cust_pkg, $sdate) || 0;
58 # if this is the first bill but the bill date has been set
59 # (by prorate_defer_bill), calculate from the setup date,
60 # and append the setup fee to @$details.
61 if ( $self->option('prorate_defer_bill',1)
62 and ! $cust_pkg->getfield('last_bill')
63 and $cust_pkg->setup ) {
64 #warn "[calc_prorate] #".$cust_pkg->pkgnum.": running deferred setup\n";
65 $param->{'setup_fee'} = $self->calc_setup($cust_pkg, $$sdate, $details);
66 $mnow = $cust_pkg->setup;
70 ($mnow, $mend, $mstart) = $self->_endpoints($mnow, $cutoff_day);
72 # next bill date will be figured as $$sdate + one period
75 my $permonth = $charge / $self->freq;
76 my $months = ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
78 # add a full period if currently billing for a partial period
79 if ( ( $self->option('add_full_period',1)
80 or $self->option('prorate_defer_bill',1) ) # necessary
81 and $months < $self->freq ) {
82 $months += $self->freq;
83 $$sdate = $self->add_freq($mstart);
86 $param->{'months'} = $months;
87 $charge = sprintf('%.2f', $permonth * $months);
92 =item prorate_setup CUST_PKG SDATE
94 Set up the package. This only has an effect if prorate_defer_bill is
95 set, in which case it postpones the next bill to the cutoff day.
101 my ($cust_pkg, $sdate) = @_;
102 my $cutoff_day = $self->cutoff_day($cust_pkg);
103 if ( ! $cust_pkg->bill
104 and $self->option('prorate_defer_bill',1)
107 my ($mnow, $mend, $mstart) = $self->_endpoints($sdate, $cutoff_day);
108 # if today is the cutoff day, set the next bill to right now instead
109 # of waiting a month.
110 if ( $mnow - $mstart < 86400 ) {
111 $cust_pkg->bill($mnow);
114 $cust_pkg->bill($mend);
121 =item _endpoints TIME CUTOFF_DAY
123 Given a current time and a day of the month to prorate to, return three
124 times: the start of the prorate interval (usually the current time), the
125 end of the prorate interval (i.e. the cutoff date), and the time one month
126 before the end of the prorate interval.
131 my ($self, $mnow, $cutoff_day) = @_;
133 # only works for freq >= 1 month; probably can't be fixed
134 my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($mnow))[0..5];
135 if( $self->option('prorate_round_day',1) ) {
136 $mday++ if $hour >= 12;
137 $mnow = timelocal(0,0,0,$mday,$mon,$year);
141 # if cutoff day > 28, force it to the 1st of next month
142 if ( $cutoff_day > 28 ) {
144 # and if we are currently after the 28th, roll the current day
145 # forward to that day
148 #set $mnow = $mend so the amount billed will be zero
149 $mnow = timelocal(0,0,0,1,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
152 if ( $mday >= $cutoff_day ) {
154 timelocal(0,0,0,$cutoff_day,$mon == 11 ? 0 : $mon + 1,$year+($mon==11));
156 timelocal(0,0,0,$cutoff_day,$mon,$year);
160 timelocal(0,0,0,$cutoff_day,$mon,$year);
162 timelocal(0,0,0,$cutoff_day,$mon == 0 ? 11 : $mon - 1,$year-($mon==0));
164 return ($mnow, $mend, $mstart);