[freeside-commits] branch FREESIDE_4_BRANCH updated. 4ccd59593b88405762c05018565385bfc9bda6a3

Mark Wells mark at 420.am
Tue Nov 3 01:37:48 PST 2015


The branch, FREESIDE_4_BRANCH has been updated
       via  4ccd59593b88405762c05018565385bfc9bda6a3 (commit)
      from  14f6a6d7f3417faf1eb87aefdfbdf108aac86161 (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 4ccd59593b88405762c05018565385bfc9bda6a3
Author: Mark Wells <mark at freeside.biz>
Date:   Tue Nov 3 01:25:57 2015 -0800

    on invoice summary, include "future" payments to past invoices in balance adjustments, #38165

diff --git a/FS/FS/Template_Mixin.pm b/FS/FS/Template_Mixin.pm
index ffaef97..e02aa1f 100644
--- a/FS/FS/Template_Mixin.pm
+++ b/FS/FS/Template_Mixin.pm
@@ -712,6 +712,8 @@ sub print_generic {
       # "balance_date_range" unfortunately is unsuitable for this, since it
       # cares about application dates.  We want to know the sum of all 
       # _top-level transactions_ dated before the last invoice.
+      #
+      # still do this for the "Previous Balance" line of the summary block
       my @sql =
         map "$_ WHERE _date <= ? AND custnum = ?", (
           "SELECT      COALESCE( SUM(charged), 0 ) FROM cust_bill",
@@ -744,19 +746,31 @@ sub print_generic {
       # longer stored in the database)
       $invoice_data{'true_previous_balance'} = $last_bill_balance;
 
-      # the change in balance from immediately after that invoice
-      # to immediately before this one
-      my $before_this_bill_balance = 0;
+      # Now, get all applications of credits/payments dated on or after the
+      # previous bill, to invoices before the current bill. (The
+      # credit/payment date restriction prevents these from intersecting
+      # the "Previous Balance" set.)
+      # These are "adjustments". The past due balance will be shown as
+      # Previous Balance - Adjustments.
+      my $adjustments = 0;
+      @sql = map {
+        "SELECT COALESCE(SUM(y.amount),0) FROM $_ JOIN cust_bill USING (invnum)
+         WHERE cust_bill._date < ?
+           AND x._date >= ?
+           AND cust_bill.custnum = ?"
+        } "cust_credit AS x JOIN cust_credit_bill y USING (crednum)",
+          "cust_pay    AS x JOIN cust_bill_pay    y USING (paynum)"
+      ;
       foreach (@sql) {
         my $delta = FS::Record->scalar_sql(
           $_,
-          $self->_date - 1,
+          $self->_date,
+          $last_bill->_date,
           $self->custnum,
         );
-        $before_this_bill_balance += $delta;
+        $adjustments += $delta;
       }
-      $invoice_data{'balance_adjustments'} =
-        sprintf("%.2f", $last_bill_balance - $before_this_bill_balance);
+      $invoice_data{'balance_adjustments'} = sprintf("%.2f", $adjustments);
 
       warn sprintf("BALANCE ADJUSTMENTS: %.2f\n\n",
                    $invoice_data{'balance_adjustments'}

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

Summary of changes:
 FS/FS/Template_Mixin.pm |   28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)




More information about the freeside-commits mailing list