[freeside-commits] branch FREESIDE_2_3_BRANCH updated. ce69838d46f32a59b54afb529f6517074e0cbd31

Mark Wells mark at 420.am
Fri Dec 21 16:57:54 PST 2012


The branch, FREESIDE_2_3_BRANCH has been updated
       via  ce69838d46f32a59b54afb529f6517074e0cbd31 (commit)
      from  4af7fdd6eb6a6fb2d1ab40116080308241f0baf3 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit ce69838d46f32a59b54afb529f6517074e0cbd31
Author: Mark Wells <mark at freeside.biz>
Date:   Fri Dec 21 16:36:49 2012 -0800

    correctly unapply payments when applying a lineitem credit with tax, #18676

diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm
index 4553aff..560be20 100644
--- a/FS/FS/cust_credit.pm
+++ b/FS/FS/cust_credit.pm
@@ -717,6 +717,8 @@ sub credit_lineitems {
   my %cust_credit_bill = ();
   my %cust_bill_pkg = ();
   my %cust_credit_bill_pkg = ();
+  # except here they're billpaynums
+  my %unapplied_payments;
   foreach my $billpkgnum ( @{$arg{billpkgnums}} ) {
     my $setuprecur = shift @{$arg{setuprecurs}};
     my $amount = shift @{$arg{amounts}};
@@ -744,7 +746,6 @@ sub credit_lineitems {
 
     push @{$cust_bill_pkg{$invnum}}, $cust_bill_pkg;
 
-    my %unapplied_payments; # billpaynum => amount
     #unapply any payments applied to this line item (other credits too?)
     foreach my $cust_bill_pay_pkg ( $cust_bill_pkg->cust_bill_pay_pkg($setuprecur) ) {
       $error = $cust_bill_pay_pkg->delete;
@@ -755,25 +756,6 @@ sub credit_lineitems {
       $unapplied_payments{$cust_bill_pay_pkg->billpaynum}
         += $cust_bill_pay_pkg->amount;
     }
-    # also unapply that amount from the invoice so it doesn't screw up 
-    # application of the credit
-    foreach my $billpaynum (keys %unapplied_payments) {
-      my $cust_bill_pay = FS::cust_bill_pay->by_key($billpaynum)
-        or die "broken payment application $billpaynum";
-      $cust_bill_pay->set('amount',
-        sprintf('%.2f',
-          $cust_bill_pay->get('amount') - $unapplied_payments{$billpaynum})
-      );
-      if ( $cust_bill_pay->amount >= 0.005 ) {
-        $error = $cust_bill_pay->replace;
-      } else {
-        $error = $cust_bill_pay->delete;
-      }
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return "Error unapplying payment: $error";
-      }
-    }
 
     #$subtotal += $amount;
     $cust_credit_bill{$invnum} += $amount;
@@ -921,11 +903,47 @@ sub credit_lineitems {
             };
 
         } # if $amount > 0
+
+        #unapply any payments applied to the tax
+        foreach my $cust_bill_pay_pkg 
+          ( $tax_cust_bill_pkg->cust_bill_pay_pkg('setup') )
+        {
+          $error = $cust_bill_pay_pkg->delete;
+          if ( $error ) {
+            $dbh->rollback if $oldAutoCommit;
+            return "Error unapplying payment: $error";
+          }
+          $unapplied_payments{$cust_bill_pay_pkg->billpaynum}
+            += $cust_bill_pay_pkg->amount;
+        }
       } #foreach $taxline
 
     } # if @{ $cust_bill_pkg{$invnum} }
 
-    #insert cust_credit_bill
+    # if we unapplied any payments from line items, also unapply that
+    # amount from the invoice
+    foreach my $billpaynum (keys %unapplied_payments) {
+      my $cust_bill_pay = FS::cust_bill_pay->by_key($billpaynum)
+        or die "broken payment application $billpaynum";
+      $error = $cust_bill_pay->delete; # can't replace
+
+      my $new_cust_bill_pay = FS::cust_bill_pay->new({
+          $cust_bill_pay->hash,
+          billpaynum => '',
+          amount => sprintf('%.2f',
+              $cust_bill_pay->get('amount') - $unapplied_payments{$billpaynum})
+      });
+
+      if ( $new_cust_bill_pay->amount > 0 ) {
+        $error ||= $new_cust_bill_pay->insert;
+      }
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "Error unapplying payment: $error";
+      }
+    }
+
+    #NOW insert cust_credit_bill
 
     my $cust_credit_bill = new FS::cust_credit_bill {
       'crednum' => $cust_credit->crednum,

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/cust_credit.pm |   60 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 39 insertions(+), 21 deletions(-)




More information about the freeside-commits mailing list