when crediting unused time on packages, also credit any tax that was charged, #42729
[freeside.git] / FS / FS / part_pkg / flat.pm
index e10094e..f2f1b3f 100644 (file)
@@ -264,8 +264,22 @@ sub calc_remain {
       $amount = $amount * ($edate - $time) / ($edate - $cust_bill_pkg->sdate);
     }
 
+    # calculate tax adjustment. we're not doing full credit_lineitems here
+    # (e.g. not applying the credit to the past billing of this package)
+    # so just include the adjustment in the source record with the rest
+    # of the credit
+    my %tax_adjust = FS::cust_credit->calculate_tax_adjustment(
+      'custnum'     => $cust_pkg->custnum,
+      'billpkgnums' => [ $cust_bill_pkg->billpkgnum ],
+      'setuprecurs' => [ 'recur' ],
+      'amounts'     => [ $amount ],
+    );
+    $amount += $tax_adjust{taxtotal};
+
+    $amount = sprintf('%.2f', $amount); # ensure that amounts add up right
     $credit += $amount;
-  } 
+  } # foreach $cust_bill_pkg
+
   sprintf('%.2f', $credit);
 
 }