+ if (defined $param->{'setup_charge'}) {
+ next unless $discount->setup;
+
+ if ( $discount->percent > 0 ) {
+ $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
+ $months = 1;
+ } elsif ( $discount->amount > 0 && $discount->months == 1) {
+ $discount_left = $param->{'setup_charge'} - $discount->amount;
+ $amount = $param->{'setup_charge'} if $discount_left < 0;
+ $amount = $discount->amount if $discount_left >= 0;
+ $months = 1;
+
+ # transfer remainder of discount, if any, to recur
+ $param->{'discount_left_recur'}{$discount->discountnum} =
+ 0 - $discount_left if $discount_left < 0;
+ } else {
+ next;
+ }
+ } elsif ( defined $param->{'discount_left_recur'}{$discount->discountnum}
+ && $param->{'discount_left_recur'}{$discount->discountnum} > 0
+ ) {
+ # use up transferred remainder of discount from setup
+ $amount = $param->{'discount_left_recur'}{$discount->discountnum};
+ $param->{'discount_left_recur'}{$discount->discountnum} = 0;
+ $months = 1;
+ } elsif ( $discount->setup
+ && $discount->months == 1
+ && $discount->amount > 0
+ ) {
+ next;
+ }
+
+ if ( ! defined $param->{'setup_charge'} ) {
+ if ( $cust_pkg->pkgpart == $param->{'real_pkgpart'} ) {
+ push @{ $param->{precommit_hooks} }, sub {
+ my $error = $cust_pkg_discount->increment_months_used($months);
+ die "error discounting: $error" if $error;
+ };
+ }
+
+ $amount = min($amount, $br);
+ $amount *= $months;
+ }