add back-office xmlrpc api and daemon, RT#27958
[freeside.git] / FS / FS / discount_plan.pm
index 081f240..6a45676 100644 (file)
@@ -47,10 +47,12 @@ sub new {
 
   my ($previous_balance) = $cust_bill->previous;
   my $self = {
+    cust_bill     => $cust_bill,
+    months        => $months,
     pkgnums       => [],
     base          => $previous_balance || 0, # sum of charges before discount
     discounted    => $previous_balance || 0, # sum of charges after discount
-    list_pkgnums  => 0, # whether any packages are not discounted
+    list_pkgnums  => undef, # whether any packages are not discounted
   };
 
   foreach my $cust_bill_pkg ( $cust_bill->cust_bill_pkg ) {
@@ -61,7 +63,7 @@ sub new {
     my $recur = $cust_bill_pkg->recur || 0;
 
     if ( $freq eq '1' ) { # monthly recurring package
-      my $permonth = $part_pkg->base_recur_permonth || 0;
+      my $permonth = $part_pkg->base_recur_permonth($cust_pkg) || 0;
 
       my ($discount) = grep { $_->months == $months }
       map { $_->discount } $part_pkg->part_pkg_discount;
@@ -78,19 +80,24 @@ sub new {
           $self->{discounted} += $setup;
         }
 
-        if ( $discount->percent ) {
+        if ( $discount->percent > 0 ) {
           $discountable += $months * $permonth;
           $discountable -= ($discountable * $discount->percent / 100);
           $discountable -= ($permonth - $recur); # correct for prorate
           $self->{discounted} += $discountable;
         }
-        else {
+        elsif ( $discount->amount > 0 ) {
           $discountable += $recur;
           $discountable -= $discount->amount * $recur/$permonth;
           $discountable += ($months - 1) * max($permonth - $discount->amount,0);
+          $self->{discounted} += $discountable;
+        }
+        else {
+          warn "discountnum ".$discount->discountnum.
+            " has no amount or percentage, ignored\n";
+          $self->{discounted} = $self->{base};
         }
 
-        $self->{discounted} += $discountable;
         push @{ $self->{pkgnums} }, $cust_pkg->pkgnum;
       }
       else { #no discount
@@ -182,6 +189,19 @@ sub pkgnums {
   @{ $self->{pkgnums} };
 }
 
+=item list_pkgnums
+
+Returns a true value if any packages listed on the invoice do not 
+receive a discount, either because there isn't one at the specified
+term length or because they're not monthly recurring packages.
+
+=cut
+
+sub list_pkgnums {
+  my $self = shift;
+  $self->{list_pkgnums};
+}
+
 # any others?  don't think so
 
 1;