r18355 - gnucash/trunk/src/register/ledger-core - Improve original register running balance implementation.

Christian Stimming cstim at code.gnucash.org
Mon Sep 28 16:32:06 EDT 2009


Author: cstim
Date: 2009-09-28 16:32:05 -0400 (Mon, 28 Sep 2009)
New Revision: 18355
Trac: http://svn.gnucash.org/trac/changeset/18355

Modified:
   gnucash/trunk/src/register/ledger-core/split-register-model.c
Log:
Improve original register running balance implementation.

Patch by Tim M:

The attached patch is an enhancement to the original reg_run_balance patch
that was implemented.  This patch has been created off of a recent trunk (in
the last week).  It makes the following changes:

1. Add a gboolean to the argument list of the gnc_split_register_get_rbaln()
function.  If the new boolean value is set to TRUE, then the function will
return the running balance for the parent account and all subaccounts.  If
set to FALSE, the function will only return the running balance of the
parent account and will not include the subaccounts.  The function has been
modified also to accomodate this change.

2. Update existing calls to the function to add the boolean value.

This patch does not actually implement a runtime balance for single account
register view or normal balance column because of potential issues that will
need to be addressed, but it opens up the possibility for easy
implementation of such a change.

Signed-off-by: Christian Stimming <stimming at tuhh.de>

Modified: gnucash/trunk/src/register/ledger-core/split-register-model.c
===================================================================
--- gnucash/trunk/src/register/ledger-core/split-register-model.c	2009-09-28 20:31:42 UTC (rev 18354)
+++ gnucash/trunk/src/register/ledger-core/split-register-model.c	2009-09-28 20:32:05 UTC (rev 18355)
@@ -55,9 +55,12 @@
 /* Flag for determining colorization of negative amounts. */
 static gboolean use_red_for_negative = TRUE;
 
-
+/* This returns the balance at runtime of a register at the split defined by virt_loc regardless of
+ * sort order. It always assumes that the first txn in the register is starting from a 0 balance. 
+ * If gboolean subaccounts is TRUE, then it will return the total balance of the parent account
+ * and all its subaccounts. FALSE will return the balance of just the parent account of the register. */
 static gnc_numeric
-gnc_split_register_get_rbaln (VirtualLocation virt_loc, gpointer user_data)
+gnc_split_register_get_rbaln (VirtualLocation virt_loc, gpointer user_data, gboolean subaccounts)
 {
   SplitRegister *reg = user_data;
   Split *split;
@@ -68,8 +71,6 @@
   GList *node, *children, *child;
   int i, row;
 
-  /* This function calculates the register balance for a particular split at runtime.
-   * It works regardless of the sort order. */
   balance = gnc_numeric_zero();
 
     /* Return NULL if this is a blank transaction. */
@@ -82,9 +83,13 @@
     if (!trans)
       return gnc_numeric_zero();
 
-    /* Get a list of all subaccounts for matching */
-    children = gnc_account_get_descendants(gnc_split_register_get_default_account(reg));
-    children = g_list_append(children, gnc_split_register_get_default_account(reg));
+    /* Get a list of accounts for matching */
+    if (subaccounts) {
+      children = gnc_account_get_descendants(gnc_split_register_get_default_account(reg));
+      children = g_list_append(children, gnc_split_register_get_default_account(reg));
+    } else
+      account = gnc_split_register_get_default_account(reg);
+      
 
     /* Get the row number we're on, then start with the first row. */
     row = virt_loc.vcell_loc.virt_row;
@@ -100,15 +105,20 @@
         Split *secondary = node->data;
         i++;
 
-        /* Add up the splits that belong to the transaction if they are
-         * from the lead account or one of the subaccounts. */
-        account = xaccSplitGetAccount (secondary);
+        if (subaccounts) {
+          /* Add up the splits that belong to the transaction if they are
+           * from the lead account or one of the subaccounts. */
+          account = xaccSplitGetAccount (secondary);
 
-        for (child = children; child; child = child->next) {
-          if (account == child->data) {
-            balance = gnc_numeric_add_fixed(balance, xaccSplitGetAmount(secondary));
-            break;
+          for (child = children; child; child = child->next) {
+            if (account == child->data) {
+              balance = gnc_numeric_add_fixed(balance, xaccSplitGetAmount(secondary));
+              break;
+            }
           }
+        } else {
+          if ( account == xaccSplitGetAccount(secondary) )
+            balance = gnc_numeric_add_fixed( balance, xaccSplitGetAmount(secondary) );
         }
       }
       virt_loc.vcell_loc.virt_row+=i;
@@ -464,7 +474,7 @@
   if (gnc_cell_name_equal (cell_name, BALN_CELL))
     balance = xaccSplitGetBalance (split);
   else if (gnc_cell_name_equal (cell_name, RBALN_CELL))
-    balance = gnc_split_register_get_rbaln (virt_loc,user_data);
+    balance = gnc_split_register_get_rbaln (virt_loc,user_data, TRUE);
   else
     balance = get_trans_total_balance (reg, xaccSplitGetParent (split));
 
@@ -1615,7 +1625,7 @@
   if (!trans)
     return NULL;
 
-  balance = gnc_split_register_get_rbaln (virt_loc,user_data);
+  balance = gnc_split_register_get_rbaln (virt_loc,user_data, TRUE);
 
   account = xaccSplitGetAccount (split);
   if (!account)



More information about the gnucash-changes mailing list