=item cust_pkg
+Returns the customer package (see L<FS::cust_pkg>).
+
=cut
sub cust_pkg {
=item discount
+Returns the discount (see L<FS::discount>).
+
=cut
sub discount {
qsearchs('discount', { 'discountnum' => $self->discountnum } );
}
+=item increment_months_used
+
+Increments months_used by the given parameter
+
+=cut
+
+sub increment_months_used {
+ my( $self, $used ) = @_;
+ #UPDATE cust_pkg_discount SET months_used = months_used + ?
+ #leaves no history, and billing is mutexed per-customer, so the dum way is ok
+ $self->months_used( $self->months_used + $used );
+ $self->replace();
+}
+
=back
=head1 BUGS
}
+sub can_discount { 0; }
+
sub hide_svc_detail {
1;
}
sprintf('%.2f', $self->base_recur($cust_pkg) + $total_svc_charge );
}
+sub can_discount { 0; }
+
sub hide_svc_detail {
1;
}
@usage_fieldorder @usage_recharge_fieldorder
);
use Tie::IxHash;
+use List::Util qw(min); # max);
#use FS::Record qw(qsearch);
use FS::UI::bytecount;
+use FS::Conf;
use FS::part_pkg;
@ISA = qw(FS::part_pkg);
return 0
if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
- $self->base_recur(@_);
+ my $br = $self->base_recur(@_);
+
+ my $discount = $self->calc_discount(@_);
+
+ sprintf('%.2f', $br - $discount);
+}
+
+sub calc_discount {
+ my $self = shift;
+ my($cust_pkg, $sdate, $details, $param ) = @_;
+
+ my $br = $self->base_recur(@_);
+
+ my $tot_discount = 0;
+ #UI enforces just 1 for now, will need ordering when they can be stacked
+ foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
+ my $discount = $cust_pkg_discount->discount;
+ #UI enforces one or the other (for now? probably for good)
+ my $amount = 0;
+ $amount += $discount->amount;
+ $amount += sprintf('%.2f', $discount->percent * $br / 100 );
+
+ my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
+
+ my $months = $discount->months
+ ? min( $chg_months,
+ $discount->months - $cust_pkg->months_used )
+ : $chg_months;
+
+ my $error = $cust_pkg_discount->increment_months_used($months);
+ die "error discounting: $error" if $error;
+
+ $amount *= $months;
+
+ #add details on discount to invoice
+ my $conf = new FS::Conf;
+ my $money_char = $conf->config('money_char') || '$';
+
+ my $d = 'Includes ';
+ $d .= $discount->name. ' ' if $discount->name;
+ $d .= 'discount of '. $discount->description_short;
+ $d .= " for $months month". ( $months>1 ? 's' : '' );
+ $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
+ push @$details, $d;
+
+ $tot_discount += $amount;
+ }
+
+ sprintf('%.2f', $tot_discount);
}
sub base_recur {
sub is_prepaid { 0; } #no, we're postpaid
-sub can_discount { 1; } #and anything that inherits from us
+#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;
$self->option('recur_fee');
}
+sub can_discount { 0; }
+
1;
$self->option('recur_fee');
}
+sub can_discount { 0; }
+
1;
$self->option('recur_fee');
}
+sub can_discount { 0; }
+
1;
);
sub calc_recur {
- my($self, $cust_pkg, $sdate ) = @_;
+ my($self, $cust_pkg, $sdate, $details, $param ) = @_;
my $cutoff_day = $self->option('cutoff_day', 1) || 1;
my $mnow = $$sdate;
my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
$$sdate = $mstart;
my $permonth = $self->option('recur_fee') / $self->freq;
- $permonth * ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
+ my $months = ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
+
+ $param->{'months'} = $months;
+ my $discount = $self->calc_discount( $cust_pkg, $sdate, $details, $param);
+
+ sprintf('%.2f', $permonth * $months - $discount);
}
1;
}#$recur_method eq 'subscription'
+ $charges -= $self->calc_discount( $cust_pkg, $sdate, $details, $param );
+
}#$recur_method eq 'prorate'
}#increment_next_bill
}
+sub can_discount { 0; }
+
sub calc_cancel {
my $self = shift;
my($cust_pkg, $sdate, $details, $param ) = @_;
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_hourly_charge );
}
$self->option('recur_fee') + $min * $self->option('recur_minly_charge');
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_minly_charge );
}
$price;
}
-sub is_free {
- 0;
-}
+sub can_discount { 0; }
+
+sub is_free { 0; }
sub base_recur {
my($self, $cust_pkg) = @_;
$self->option('recur_fee') + $units * $self->option('recur_unit_charge');
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_unit_charge );
}
$self->option('recur_fee') + $charges;
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_hourly_charge
recur_input_charge recur_output_charge recur_total_charge );
$$sdate = timelocal(0,0,0,$cutoff_day,$mon,$year);
- $self->option('recur_fee');
+ my $br = $self->base_recur(@_);
+
+ my $discount = $self->calc_discount(@_);
+
+ sprintf('%.2f', $br - $discount);
}
1;
}
-sub is_free {
- 0;
-}
+sub can_discount { 0; }
+
+sub is_free { 0; }
sub base_recur {
my($self, $cust_pkg) = @_;