+ my $self = shift;
+ my($cust_pkg, $sdate, $details, $param ) = @_;
+
+ #my $last_bill = $cust_pkg->last_bill;
+ my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
+
+ return 0
+ if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
+
+ my $charge = $self->base_recur($cust_pkg, $sdate);
+ if ( $self->option('sync_bill_date',1) ) {
+ my $next_bill = $cust_pkg->cust_main->next_bill_date;
+ if ( defined($next_bill) ) {
+ my $cutoff_day = (localtime($next_bill))[3];
+ $charge = $self->calc_prorate(@_, $cutoff_day);
+ }
+ }
+ elsif ( $param->{freq_override} ) {
+ # XXX not sure if this should be mutually exclusive with sync_bill_date.
+ # Given the very specific problem that freq_override is meant to 'solve',
+ # it probably should.
+ $charge *= $param->{freq_override} if $param->{freq_override};
+ }
+
+ my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
+ return sprintf('%.2f', $charge - $discount);
+}
+
+sub base_recur {
+ my($self, $cust_pkg, $sdate) = @_;
+ $self->option('recur_fee', 1) || 0;
+}
+
+sub base_recur_permonth {
+ my($self, $cust_pkg) = @_;
+
+ return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
+
+ sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
+}
+
+sub calc_remain {
+ my ($self, $cust_pkg, %options) = @_;
+
+ my $time;
+ if ($options{'time'}) {
+ $time = $options{'time'};
+ } else {
+ $time = time;
+ }
+
+ my $next_bill = $cust_pkg->getfield('bill') || 0;
+
+ return 0 if ! $self->base_recur($cust_pkg, \$time)
+ || ! $next_bill
+ || $next_bill < $time;
+
+ my %sec = (
+ 'h' => 3600, # 60 * 60
+ 'd' => 86400, # 60 * 60 * 24
+ 'w' => 604800, # 60 * 60 * 24 * 7
+ 'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12
+ );
+
+ $self->freq =~ /^(\d+)([hdwm]?)$/
+ or die 'unparsable frequency: '. $self->freq;
+ my $freq_sec = $1 * $sec{$2||'m'};
+ return 0 unless $freq_sec;
+
+ sprintf("%.2f", $self->base_recur($cust_pkg, \$time) * ( $next_bill - $time ) / $freq_sec );
+
+}
+
+sub is_free_options {
+ qw( setup_fee recur_fee );
+}
+
+sub is_prepaid { 0; } #no, we're postpaid
+
+sub can_start_date { ! shift->option('start_1st', 1) }
+
+#XXX discounts only on recurring fees for now (no setup/one-time or usage)
+sub can_discount {
+ my $self = shift;
+ $self->freq =~ /^\d+$/ && $self->freq > 0;
+}
+
+sub usage_valuehash {
+ my $self = shift;
+ map { $_, $self->option($_) }
+ grep { $self->option($_, 'hush') }
+ qw(seconds upbytes downbytes totalbytes);
+}
+
+sub reset_usage {
+ my($self, $cust_pkg, %opt) = @_;
+ warn " resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
+ my %values = $self->usage_valuehash;
+ if ($self->option('usage_rollover', 1)) {
+ $cust_pkg->recharge(\%values);
+ }else{
+ $cust_pkg->set_usage(\%values, %opt);
+ }