+sub calc_remain {
+ my ($self, $cust_pkg) = @_;
+ my $time = time;
+ my $next_bill = $cust_pkg->getfield('bill') || 0;
+ my $last_bill = $cust_pkg->last_bill || 0;
+ return 0 if ! $self->base_recur
+ || ! $self->option('unused_credit')
+ || ! $last_bill
+ || ! $next_bill;
+
+ my $now_date = ParseDate("epoch $time");
+ my $last_date = ParseDate("epoch $last_bill");
+ my $next_date = ParseDate("epoch $next_bill");
+ my $err;
+ my $delta = DateCalc($now_date,$next_date,\$err, 0);
+ my $days_remaining = Delta_Format($delta, 4, "%dh");
+
+ my $frequency = $self->freq;
+
+ # TODO: Remove this after the frequencies are Data::Manip friendly.
+ $frequency .= "m" unless $frequency =~ /[wd]$/;
+
+ my $freq_delta = ParseDateDelta($frequency);
+ my $days = Delta_Format($freq_delta,4,"%dh");
+
+ my $recurring= $self->base_recur;
+ my $daily = $recurring/$days;
+
+ sprintf("%.2f",($daily * $days_remaining));
+}
+