[freeside-commits] freeside/FS/FS/part_pkg flat.pm, 1.63, 1.64 discount_Mixin.pm, 1.4, 1.5

Erik Levinson levinse at wavetail.420.am
Thu May 5 17:30:09 PDT 2011


Update of /home/cvs/cvsroot/freeside/FS/FS/part_pkg
In directory wavetail.420.am:/tmp/cvs-serv32622/FS/FS/part_pkg

Modified Files:
	flat.pm discount_Mixin.pm 
Log Message:
discounts on setup fees, part 2 of 2, RT11512

Index: flat.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_pkg/flat.pm,v
retrieving revision 1.63
retrieving revision 1.64
diff -u -w -d -r1.63 -r1.64
--- flat.pm	29 Apr 2011 12:12:45 -0000	1.63
+++ flat.pm	6 May 2011 00:30:07 -0000	1.64
@@ -93,7 +93,7 @@
 }
 
 sub calc_setup {
-  my($self, $cust_pkg, $sdate, $details ) = @_;
+  my($self, $cust_pkg, $sdate, $details, $param ) = @_;
 
   return 0 if $self->prorate_setup($cust_pkg, $sdate);
 
@@ -107,8 +107,12 @@
 
   my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
 
-  my $param = { 'setup_charge' => $charge };
-  my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
+  my $discount = 0;
+  if ( $charge > 0 ) {
+      $param->{'setup_charge'} = $charge;
+      $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
+      delete $param->{'setup_charge'};
+  }
 
   sprintf('%.2f', $charge - $discount);
 }

Index: discount_Mixin.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_pkg/discount_Mixin.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -d -r1.4 -r1.5
--- discount_Mixin.pm	29 Apr 2011 12:10:18 -0000	1.4
+++ discount_Mixin.pm	6 May 2011 00:30:07 -0000	1.5
@@ -44,8 +44,6 @@
   my $br = $self->base_recur($cust_pkg, $sdate);
   $br += $param->{'override_charges'} if $param->{'override_charges'};
   
-  return 0 if defined $param->{'setup_charge'} && $param->{'setup_charge'} == 0;
-
   my $tot_discount = 0;
   #UI enforces just 1 for now, will need ordering when they can be stacked
 
@@ -80,11 +78,12 @@
 
   my @cust_pkg_discount = $cust_pkg->cust_pkg_discount_active;
   foreach my $cust_pkg_discount ( @cust_pkg_discount ) {
+    my $discount_left;
     my $discount = $cust_pkg_discount->discount;
     #UI enforces one or the other (for now?  probably for good)
     my $amount = 0;
     $amount += $discount->amount
-    if $cust_pkg->pkgpart == $param->{real_pkgpart};
+        if defined $param->{'real_pkgpart'} && $cust_pkg->pkgpart == $param->{'real_pkgpart'};
     $amount += sprintf('%.2f', $discount->percent * $br / 100 );
     my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
 
@@ -100,10 +99,31 @@
             $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
             $months = 1;
         }
+        elsif ( $discount->amount && $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;
     }
 
     my $error = $cust_pkg_discount->increment_months_used($months)
-        if ($cust_pkg->pkgpart == $param->{real_pkgpart} 
+        if (defined $param->{'real_pkgpart'} 
+            && $cust_pkg->pkgpart == $param->{'real_pkgpart'} 
             && ! defined $param->{'setup_charge'});
     die "error discounting: $error" if $error;
 
@@ -112,6 +132,16 @@
 
     next unless $amount > 0;
 
+    # transfer remainder of discount, if any, to setup
+    if ( $discount->setup && $discount->amount
+        && (!$discount->months || $discount->months != 1)
+        && !defined $param->{'setup_charge'}) {
+        $discount_left = $br - $amount;
+        $amount = $br if $discount_left < 0;
+        $param->{'discount_left_setup'}{$discount->discountnum} = 
+                                    0 - $discount_left if $discount_left < 0;
+    }
+
     #record details in cust_bill_pkg_discount
     my $cust_bill_pkg_discount = new FS::cust_bill_pkg_discount {
       'pkgdiscountnum' => $cust_pkg_discount->pkgdiscountnum,



More information about the freeside-commits mailing list