+ # decide whether to show active discounts here
+ if (
+ # case 1: we are showing a single line for the package
+ ( !$type )
+ # case 2: we are showing a setup line for a package that has
+ # no base recurring fee
+ or ( $type eq 'S' and $cust_bill_pkg->unitrecur == 0 )
+ # case 3: we are showing a recur line for a package that has
+ # a base recurring fee
+ or ( $type eq 'R' and $cust_bill_pkg->unitrecur > 0 )
+ ) {
+
+ my @discounts = $cust_bill_pkg->cust_bill_pkg_discount;
+ # special case: if there are old "discount details" on this line
+ # item, don't show discount line items
+ if ( FS::cust_bill_pkg_detail->count(
+ "detail LIKE 'Includes discount%' AND billpkgnum = " .
+ $cust_bill_pkg->billpkgnum
+ ) > 0 ) {
+ @discounts = ();
+ }
+ if( @discounts ) {
+ warn "$me _items_cust_bill_pkg including discounts for ".
+ $cust_bill_pkg->billpkgnum."\n"
+ if $DEBUG;
+ my $discount_amount = sum( map {$_->amount} @discounts );
+ my $orig_amount = $cust_bill_pkg->setup + $cust_bill_pkg->recur
+ + $discount_amount;
+ # if multiple discounts apply to the same package, how to display
+ # them? ext_description lines, apparently
+ if ( $d and $cust_bill_pkg->hidden ) {
+ $d->{amount} += $discount_amount;
+ $d->{orig_amount} += $orig_amount;
+ } else {
+ my @ext;
+ # make a placeholder for the original price, if necessary
+ # (if unit prices are enabled, it won't be necessary)
+ push @ext, '' if !$conf->exists('invoice-unitprice');
+ $d = {
+ _is_discount => 1,
+ description => $self->mt('Discount included'),
+ amount => $discount_amount,
+ orig_amount => $orig_amount,
+ ext_description => \@ext,
+ };
+ foreach my $cust_bill_pkg_discount (@discounts) {
+ my $def = $cust_bill_pkg_discount->cust_pkg_discount->discount;
+ push @ext, &{$escape_function}( $def->description );
+ }
+ }
+
+ # update the placeholder to show the original price in the
+ # first ext_description line
+ if ( !$conf->exists('invoice-unitprice') ) {
+ $d->{ext_description}->[0] =
+ sprintf('Original price: %.2f', $d->{orig_amount});
+ }
+ } # if there are any discounts
+ } # if this is an appropriate place to show discounts
+