[freeside-commits] branch FREESIDE_2_3_BRANCH updated. 788f67e1d0a83e5554273dc946d80f5badccda4c

Mark Wells mark at 420.am
Thu Mar 7 14:45:40 PST 2013


The branch, FREESIDE_2_3_BRANCH has been updated
       via  788f67e1d0a83e5554273dc946d80f5badccda4c (commit)
       via  f80829f3cee324b94974bdf2b2d7682e411715b9 (commit)
      from  e07eba0d44fbe13505c3df373fd7d29515b74fec (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 788f67e1d0a83e5554273dc946d80f5badccda4c
Merge: f80829f e07eba0
Author: Mark Wells <mark at freeside.biz>
Date:   Thu Mar 7 14:45:13 2013 -0800

    Merge branch 'FREESIDE_2_3_BRANCH' of git.freeside.biz:/home/git/freeside into 2.3


commit f80829f3cee324b94974bdf2b2d7682e411715b9
Author: Mark Wells <mark at freeside.biz>
Date:   Thu Mar 7 13:50:13 2013 -0800

    application of line item credits, #20629, #21752

diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm
index 2dfd081..f1d125c 100644
--- a/FS/FS/cust_credit.pm
+++ b/FS/FS/cust_credit.pm
@@ -717,7 +717,7 @@ sub credit_lineitems {
   my %cust_credit_bill = ();
   my %cust_bill_pkg = ();
   my %cust_credit_bill_pkg = ();
-  my %unapplied_payments = (); # except here they're billpaynums
+  my %unapplied_payments = (); #invoice numbers, and then billpaynums
   foreach my $billpkgnum ( @{$arg{billpkgnums}} ) {
     my $setuprecur = shift @{$arg{setuprecurs}};
     my $amount = shift @{$arg{amounts}};
@@ -752,7 +752,7 @@ sub credit_lineitems {
         $dbh->rollback if $oldAutoCommit;
         return "Error unapplying payment: $error";
       }
-      $unapplied_payments{$cust_bill_pay_pkg->billpaynum}
+      $unapplied_payments{$invnum}{$cust_bill_pay_pkg->billpaynum}
         += $cust_bill_pay_pkg->amount;
     }
 
@@ -912,7 +912,7 @@ sub credit_lineitems {
             $dbh->rollback if $oldAutoCommit;
             return "Error unapplying payment: $error";
           }
-          $unapplied_payments{$cust_bill_pay_pkg->billpaynum}
+          $unapplied_payments{$invnum}{$cust_bill_pay_pkg->billpaynum}
             += $cust_bill_pay_pkg->amount;
         }
       } #foreach $taxline
@@ -921,20 +921,30 @@ sub credit_lineitems {
 
     # if we unapplied any payments from line items, also unapply that
     # amount from the invoice
-    foreach my $billpaynum (keys %unapplied_payments) {
+    foreach my $billpaynum (keys %{$unapplied_payments{$invnum}}) {
       my $cust_bill_pay = FS::cust_bill_pay->by_key($billpaynum)
         or die "broken payment application $billpaynum";
+      my @subapps = $cust_bill_pay->lineitem_applications;
       $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})
+              $cust_bill_pay->get('amount')
+              - $unapplied_payments{$invnum}{$billpaynum})
       });
 
       if ( $new_cust_bill_pay->amount > 0 ) {
         $error ||= $new_cust_bill_pay->insert;
+        # Also reapply it to everything it was applied to before.
+        # Note that we've already deleted cust_bill_pay_pkg records for the
+        # items we're crediting, so they aren't on this list.
+        foreach my $cust_bill_pay_pkg (@subapps) {
+          $cust_bill_pay_pkg->billpaypkgnum('');
+          $cust_bill_pay_pkg->billpaynum($new_cust_bill_pay->billpaynum);
+          $error ||= $cust_bill_pay_pkg->insert;
+        }
       }
       if ( $error ) {
         $dbh->rollback if $oldAutoCommit;

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

Summary of changes:
 FS/FS/cust_credit.pm |   20 +++++++++++++++-----
 1 files changed, 15 insertions(+), 5 deletions(-)




More information about the freeside-commits mailing list