r23057 - gnucash/trunk/src/gnome-utils - Register2: Improve commodity/currency handling: Try to always use account_or_default_currency lookup instead of code duplication.

Christian Stimming cstim at code.gnucash.org
Wed Jun 19 11:47:49 EDT 2013


Author: cstim
Date: 2013-06-19 11:47:49 -0400 (Wed, 19 Jun 2013)
New Revision: 23057
Trac: http://svn.gnucash.org/trac/changeset/23057

Modified:
   gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c
Log:
Register2: Improve commodity/currency handling: Try to always use account_or_default_currency lookup instead of code duplication.

The code for ensuring a commodity that is a currency was duplicated among
the file. This is now refactored into the reg_currency variable. Also, in
a non-currency register the currency is now taken from parent accounts or
the default currency, which matches the old register's policy but is different
from what register2 previously did.

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c	2013-06-19 15:47:39 UTC (rev 23056)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c	2013-06-19 15:47:49 UTC (rev 23057)
@@ -248,7 +248,8 @@
     gboolean             disposed;
   
     Account             *anchor;              // The register default Account
-    gnc_commodity       *reg_comm;            // The register commodity
+    gnc_commodity       *reg_comm;            // The register commodity (which may be a non-currency)
+    gnc_commodity       *reg_currency;        // The currency for txns in this register (guaranteed to be a currency)
 
     Transaction         *current_trans;       // The current highlighted transaction
     Split               *current_split;       // The current highlighted split
@@ -1137,6 +1138,9 @@
 
     view->priv->anchor = gnc_tree_model_split_reg_get_anchor (model);
     view->priv->reg_comm = xaccAccountGetCommodity (view->priv->anchor);
+    view->priv->reg_currency = gnc_account_or_default_currency (view->priv->anchor, NULL);
+    g_assert(view->priv->reg_currency);
+    g_assert(gnc_commodity_is_currency(view->priv->reg_currency));
     view->help_text = g_strdup ("Help Text");
 
     gnc_tree_view_split_reg_set_cols (view, gnc_tree_view_split_reg_get_colummn_list (model));
@@ -4467,48 +4471,19 @@
             /* Set the transaction currency if not set */
             if (!xaccTransGetCurrency (trans))
             {
-                gnc_commodity *split_commodity;
+                // set transaction currency to that of register (which is guaranteed to be a currency)
+                xaccTransSetCurrency (trans, view->priv->reg_currency);
 
-                // set transaction currency to that of register if a currency
-                if (gnc_commodity_is_currency (view->priv->reg_comm))
-                    xaccTransSetCurrency (trans, view->priv->reg_comm);
-                else
-                    xaccTransSetCurrency (trans, gnc_default_currency());
-
                 // We are on General ledger
                 if (!anchor)
                 {
-                    split_commodity = xaccAccountGetCommodity (xaccSplitGetAccount (split));
-
-                    if (gnc_commodity_is_currency (split_commodity))
-                        xaccTransSetCurrency (trans, xaccAccountGetCommodity (xaccSplitGetAccount (split)));
-                    else
-                        xaccTransSetCurrency (trans, gnc_default_currency());
+                    xaccTransSetCurrency (trans, gnc_account_or_default_currency (xaccSplitGetAccount (split), NULL));
                 }
             }
 
-            // if non currency register, we set the trans currency to the first currency split
-            if (xaccTransGetCurrency (trans) && !gnc_commodity_is_currency (view->priv->reg_comm))
-            {
-                if (!gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (xaccTransGetSplit (trans, 0)))))
-                {
-                    int i;
-                    Split *s = NULL;
-                    gboolean currency = FALSE;
+            // No need to check for a non-currency register because that's what
+            // was already checked when reg_currency was stored.
 
-                    for (i = 0; (s = xaccTransGetSplit (trans, i)); i++) {
-                        if (gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (s))))
-                        {
-                            currency = TRUE;
-                            break;
-                        }
-                    }
-
-                    if (currency == FALSE && gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (split))))
-                            xaccTransSetCurrency (trans, xaccAccountGetCommodity (xaccSplitGetAccount (split)));
-                }
-            }
-
             /* This computes the value if we just commit the split after entering account */
             if (!valid_input)
                 input = gnc_tree_util_split_reg_get_value_for (view, trans, split, is_blank);
@@ -4748,38 +4723,12 @@
             /* Set the transaction currency if not set */
             if (!xaccTransGetCurrency (trans))
             {
-                gnc_commodity *split_commodity;
-
-                split_commodity = xaccAccountGetCommodity (xaccSplitGetAccount (split));
-
-                if (gnc_commodity_is_currency (split_commodity))
-                    xaccTransSetCurrency (trans, xaccAccountGetCommodity (xaccSplitGetAccount (split)));
-                else
-                    xaccTransSetCurrency (trans, gnc_default_currency());
+                xaccTransSetCurrency (trans, gnc_account_or_default_currency (xaccSplitGetAccount (split), NULL));
             }
 
-            // if non currency register, we set the trans currency to the first currency split
-            if (xaccTransGetCurrency (trans))
-            {
-                if (!gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (xaccTransGetSplit (trans, 0)))))
-                {
-                    int i;
-                    Split *s = NULL;
-                    gboolean currency = FALSE;
+            // No need to check for a non-currency register because that's what
+            // was already checked when reg_currency was stored.
 
-                    for (i = 0; (s = xaccTransGetSplit (trans, i)); i++) {
-                        if (gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (s))))
-                        {
-                            currency = TRUE;
-                            break;
-                        }
-                    }
-
-                    if (currency == FALSE && gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (split))))
-                            xaccTransSetCurrency (trans, xaccAccountGetCommodity (xaccSplitGetAccount (split)));
-                }
-            }
-
             /* Setup the debit and credit fields */
             if (viewcol == COL_DEBIT)
             {



More information about the gnucash-changes mailing list