gnucash maint: Multiple changes pushed

Christopher Lam clam at code.gnucash.org
Sat Mar 14 19:55:12 EDT 2020


Updated	 via  https://github.com/Gnucash/gnucash/commit/9865a996 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/8def3ba2 (commit)
	from  https://github.com/Gnucash/gnucash/commit/f182d9f9 (commit)



commit 9865a996636f373f34e2dab438df3231960f0a4a
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri Mar 13 23:05:55 2020 +0800

    [account.cpp] refactor GetBalanceAsOfDate
    
    much more compact loop.

diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index f74aaf4d1..8b0c85dd6 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -3364,56 +3364,27 @@ GetBalanceAsOfDate (Account *acc, time64 date, gboolean ignclosing)
      * xaccAccountForEachTransaction by using gpointer return
      * values rather than gints.
      */
-    AccountPrivate *priv;
-    GList   *lp;
-    gboolean found = FALSE;
-    gnc_numeric balance;
+    Split *latest = nullptr;
 
     g_return_val_if_fail(GNC_IS_ACCOUNT(acc), gnc_numeric_zero());
 
     xaccAccountSortSplits (acc, TRUE); /* just in case, normally a noop */
     xaccAccountRecomputeBalance (acc); /* just in case, normally a noop */
 
-    priv = GET_PRIVATE(acc);
-    if (ignclosing)
-        balance = priv->noclosing_balance;
-    else
-        balance = priv->balance;
-
-    lp = priv->splits;
-    while ( lp && !found )
+    for (GList *lp = GET_PRIVATE(acc)->splits; lp; lp = lp->next)
     {
-        time64 trans_time = xaccTransRetDatePosted( xaccSplitGetParent( (Split *)lp->data ));
-        if ( trans_time >= date )
-            found = TRUE;
-        else
-            lp = lp->next;
-    }
-
-    if ( lp )
-    {
-        if ( lp->prev )
-        {
-            /* Since lp is now pointing to a split which was past the reconcile
-             * date, get the running balance of the previous split.
-             */
-            if (ignclosing)
-                balance = xaccSplitGetNoclosingBalance( (Split *)lp->prev->data );
-            else
-                balance = xaccSplitGetBalance( (Split *)lp->prev->data );
-        }
-        else
-        {
-            /* AsOf date must be before any entries, return zero. */
-            balance = gnc_numeric_zero();
-        }
+        if (xaccTransGetDate (xaccSplitGetParent ((Split *)lp->data)) >= date)
+            break;
+        latest = (Split *)lp->data;
     }
 
-    /* Otherwise there were no splits posted after the given date,
-     * so the latest account balance should be good enough.
-     */
+    if (!latest)
+        return gnc_numeric_zero();
 
-    return( balance );
+    if (ignclosing)
+        return xaccSplitGetNoclosingBalance (latest);
+    else
+        return xaccSplitGetBalance (latest);
 }
 
 gnc_numeric

commit 8def3ba26e54a186bec421c585cf7b655fe90f5c
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri Mar 13 19:37:56 2020 +0800

    [account.cpp] refactor xaccAccountGetPresentBalance
    
    instead of starting from account->splits tail then backtrack, reuse
    existing code.

diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index ebe8f3851..f74aaf4d1 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -3448,32 +3448,14 @@ xaccAccountGetReconciledBalanceAsOfDate (Account *acc, time64 date)
 
 /*
  * Originally gsr_account_present_balance in gnc-split-reg.c
- *
- * How does this routine compare to xaccAccountGetBalanceAsOfDate just
- * above?  These two routines should eventually be collapsed into one.
- * Perhaps the startup logic from that one, and the logic from this
- * one that walks from the tail of the split list.
  */
 gnc_numeric
 xaccAccountGetPresentBalance (const Account *acc)
 {
-    AccountPrivate *priv;
-    GList *node;
-    time64 today;
-
     g_return_val_if_fail(GNC_IS_ACCOUNT(acc), gnc_numeric_zero());
 
-    priv = GET_PRIVATE(acc);
-    today = gnc_time64_get_today_end();
-    for (node = g_list_last(priv->splits); node; node = node->prev)
-    {
-        Split *split = static_cast<Split*>(node->data);
-
-        if (xaccTransGetDate (xaccSplitGetParent (split)) <= today)
-            return xaccSplitGetBalance (split);
-    }
-
-    return gnc_numeric_zero ();
+    return xaccAccountGetBalanceAsOfDate (GNC_ACCOUNT (acc),
+                                          gnc_time64_get_today_end ());
 }
 
 



Summary of changes:
 libgnucash/engine/Account.cpp | 73 ++++++++-----------------------------------
 1 file changed, 13 insertions(+), 60 deletions(-)



More information about the gnucash-changes mailing list