From d492bb51af64fc85678cd73e8dfe6dd2cdcf8748 Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 27 Nov 2011 00:49:40 +0000 Subject: [PATCH] correct display of prepay discount info with non-monthly packages, #15040 --- FS/FS/cust_bill.pm | 66 ++++++++++++++++++++++++++-------------------- FS/FS/cust_main/Billing.pm | 9 ++++++- FS/FS/cust_pay.pm | 9 ++++++- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 77c571b8c..d9a5e42fb 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -5203,44 +5203,52 @@ sub _items_discounts_avail { foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) { my $cust_pkg = $cust_bill_pkg->cust_pkg or next; my $part_pkg = $cust_pkg->part_pkg or next; - - next if $part_pkg->freq ne '1'; + my $freq = $part_pkg->freq; my $setup = $cust_bill_pkg->setup || 0; my $recur = $cust_bill_pkg->recur || 0; - my $permonth = $part_pkg->base_recur_permonth || 0; - my ($discount) = grep { $_->months == $months } - map { $_->discount } $part_pkg->part_pkg_discount; + if ( $freq eq '1' ) { #monthly + my $permonth = $part_pkg->base_recur_permonth || 0; - $hash->{base} += $setup + $recur + ($months - 1) * $permonth; - if ($discount) { + my ($discount) = grep { $_->months == $months } + map { $_->discount } $part_pkg->part_pkg_discount; - my $discountable; - if ( $discount->setup ) { - $discountable += $setup; - } - else { - $hash->{discounted} += $setup; - } + $hash->{base} += $setup + $recur + ($months - 1) * $permonth; + + if ( $discount ) { + + my $discountable; + if ( $discount->setup ) { + $discountable += $setup; + } + else { + $hash->{discounted} += $setup; + } + + if ( $discount->percent ) { + $discountable += $months * $permonth; + $discountable -= ($discountable * $discount->percent / 100); + $discountable -= ($permonth - $recur); # correct for prorate + $hash->{discounted} += $discountable; + } + else { + $discountable += $recur; + $discountable -= $discount->amount * $recur/$permonth; + + $discountable += ($months - 1) * max($permonth - $discount->amount,0); + } - if ( $discount->percent ) { - $discountable += $months * $permonth; - $discountable -= ($discountable * $discount->percent / 100); - $discountable -= ($permonth - $recur); # correct for prorate $hash->{discounted} += $discountable; + push @{ $hash->{pkgnums} }, $cust_pkg->pkgnum; } - else { - $discountable += $recur; - $discountable -= $discount->amount * $recur/$permonth; - - $discountable += ($months - 1) * max($permonth - $discount->amount,0); + else { #no discount + $hash->{discounted} += $setup + $recur + ($months - 1) * $permonth; + $hash->{list_pkgnums} = 1; } - - $hash->{discounted} += $discountable; - push @{ $hash->{pkgnums} }, $cust_pkg->pkgnum; - } - else { #no discount - $hash->{discounted} += $setup + $recur + ($months - 1) * $permonth; + } #if $freq eq '1' + else { # all non-monthly packages: include current charges only + $hash->{discounted} += $setup + $recur; + $hash->{base} += $setup + $recur; $hash->{list_pkgnums} = 1; } } #foreach $cust_bill_pkg diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index dca4ab7c7..eb5bd0ba0 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -253,7 +253,8 @@ charges, etc. =item freq_override If set, then override the normal frequency and look for a part_pkg_discount -to take at that frequency. +to take at that frequency. This will exclude any packages that aren't billed +on a monthly cycle. =item time @@ -846,6 +847,12 @@ sub _make_lines { my $time = $params{'time'} or die "no time specified"; my (%options) = %{$params{options}}; + if ( $part_pkg->freq ne '1' and ($options{'freq_override'} || 0) > 0 ) { + # this should never happen + die 'freq_override billing attempted on non-monthly package '. + $cust_pkg->pkgnum; + } + my $dbh = dbh; my $real_pkgpart = $params{real_pkgpart}; my %hash = $cust_pkg->hash; diff --git a/FS/FS/cust_pay.pm b/FS/FS/cust_pay.pm index 60c437a92..d98d11ecb 100644 --- a/FS/FS/cust_pay.pm +++ b/FS/FS/cust_pay.pm @@ -194,7 +194,14 @@ sub insert { my ($cust_bill) = ($cust_main->cust_bill)[-1]; # most recent invoice return "can't accept prepayment for an unbilled customer" if !$cust_bill; - my %billing_pkgs = map { $_->pkgnum => $_ } $cust_main->billing_pkgs; + # %billing_pkgs contains this customer's active monthly packages. + # Recurring fees for those packages will be credited and then rebilled + # for the full discount term. Other packages on the last invoice + # (canceled, non-monthly recurring, or one-time charges) will be + # left as they are. + my %billing_pkgs = map { $_->pkgnum => $_ } + grep { $_->part_pkg->freq eq '1' } + $cust_main->billing_pkgs; my $credit = 0; # sum of recurring charges from that invoice my $last_bill_date = 0; # the real bill date foreach my $item ( $cust_bill->cust_bill_pkg ) { -- 2.11.0