correct display of prepay discount info with non-monthly packages, #15040
authormark <mark>
Sun, 27 Nov 2011 00:49:40 +0000 (00:49 +0000)
committermark <mark>
Sun, 27 Nov 2011 00:49:40 +0000 (00:49 +0000)
FS/FS/cust_bill.pm
FS/FS/cust_main/Billing.pm
FS/FS/cust_pay.pm

index 77c571b..d9a5e42 100644 (file)
@@ -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;
     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 $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;
           $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
         $hash->{list_pkgnums} = 1;
       }
     } #foreach $cust_bill_pkg
index dca4ab7..eb5bd0b 100644 (file)
@@ -253,7 +253,8 @@ charges, etc.
 =item freq_override
 
 If set, then override the normal frequency and look for a part_pkg_discount
 =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
 
 
 =item time
 
@@ -846,6 +847,12 @@ sub _make_lines {
   my $time = $params{'time'} or die "no time specified";
   my (%options) = %{$params{options}};
 
   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;
   my $dbh = dbh;
   my $real_pkgpart = $params{real_pkgpart};
   my %hash = $cust_pkg->hash;
index 60c437a..d98d11e 100644 (file)
@@ -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 ($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 ) {
       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 ) {