r23055 - gnucash/trunk/src - Refactor lookup of a commodity that is a currency for a specific account into general function gnc_account_or_default_currency().

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


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

Modified:
   gnucash/trunk/src/app-utils/gnc-ui-util.c
   gnucash/trunk/src/app-utils/gnc-ui-util.h
   gnucash/trunk/src/engine/Account.c
   gnucash/trunk/src/engine/Account.h
   gnucash/trunk/src/register/ledger-core/split-register-load.c
Log:
Refactor lookup of a commodity that is a currency for a specific account into general function gnc_account_or_default_currency().

Modified: gnucash/trunk/src/app-utils/gnc-ui-util.c
===================================================================
--- gnucash/trunk/src/app-utils/gnc-ui-util.c	2013-06-19 15:47:13 UTC (rev 23054)
+++ gnucash/trunk/src/app-utils/gnc-ui-util.c	2013-06-19 15:47:27 UTC (rev 23055)
@@ -940,6 +940,33 @@
     return gnc_default_currency_common (user_default_currency, GCONF_GENERAL);
 }
 
+gnc_commodity * gnc_account_or_default_currency(const Account* account, gboolean * currency_from_account_found)
+{
+    gnc_commodity *currency;
+    if (!account)
+    {
+        if (currency_from_account_found)
+            *currency_from_account_found = FALSE;
+        return gnc_default_currency();
+    }
+
+    currency = gnc_account_get_currency_or_parent(account);
+    if (currency)
+    {
+        if (currency_from_account_found)
+            *currency_from_account_found = TRUE;
+    }
+    else
+    {
+        if (currency_from_account_found)
+            *currency_from_account_found = FALSE;
+        currency = gnc_default_currency();
+    }
+    return currency;
+}
+
+
+
 gnc_commodity *
 gnc_default_report_currency (void)
 {

Modified: gnucash/trunk/src/app-utils/gnc-ui-util.h
===================================================================
--- gnucash/trunk/src/app-utils/gnc-ui-util.h	2013-06-19 15:47:13 UTC (rev 23054)
+++ gnucash/trunk/src/app-utils/gnc-ui-util.h	2013-06-19 15:47:27 UTC (rev 23055)
@@ -168,7 +168,31 @@
  */
 gnc_commodity * gnc_default_currency (void);
 
+/** Returns a gnc_commodity that is a currency, suitable for being a
+Transaction's currency. The gnc_commodity is taken either from the current
+account, or from the next parent account that has a gnc_commodity that is a
+currency, or from gnc_default_currency().
 
+If the given account or any of its parent account have a commodity that is a
+currency, it is returned and the gboolean currency_from_account_found is set to
+TRUE (if non-NULL). If neither this account nor any of its parent accounts have
+such a commodity, gnc_default_currency() is returned and the gboolean
+currency_from_account_found is set to FALSE (if non-NULL). This can be used to
+show an appropriate warning message.
+
+If account is NULL, gnc_default_currency() is returned and
+currency_from_account_found is set to FALSE.
+
+ at param account The account where the currency should be looked up. May be NULL.
+
+ at param currency_from_account_found A gboolean pointer that takes the output
+argument of whether the returned currency was found in the account. May be
+NULL.
+
+ at return A currency pointer (and never NULL).
+*/
+gnc_commodity * gnc_account_or_default_currency(const Account* account, gboolean * currency_from_account_found);
+
 /** Return the default currency for use in reports, as set by the
  *  user.  If the user's preference is invalid, then this routine will
  *  return the default currency for the user's locale.

Modified: gnucash/trunk/src/engine/Account.c
===================================================================
--- gnucash/trunk/src/engine/Account.c	2013-06-19 15:47:13 UTC (rev 23054)
+++ gnucash/trunk/src/engine/Account.c	2013-06-19 15:47:27 UTC (rev 23055)
@@ -2972,6 +2972,39 @@
     return GET_PRIVATE(acc)->commodity;
 }
 
+gnc_commodity * gnc_account_get_currency_or_parent(const Account* account)
+{
+    gnc_commodity * commodity;
+    g_assert(account);
+
+    commodity = xaccAccountGetCommodity (account);
+    if (gnc_commodity_is_currency(commodity))
+        return commodity;
+    else
+    {
+        const Account *parent_account = account;
+        /* Account commodity is not a currency, walk up the tree until
+         * we find a parent account that is a currency account and use
+         * it's currency.
+         */
+        do
+        {
+            parent_account = gnc_account_get_parent (parent_account);
+            if (parent_account)
+            {
+                commodity = xaccAccountGetCommodity (parent_account);
+                if (gnc_commodity_is_currency(commodity))
+                {
+                    return commodity;
+                    //break;
+                }
+            }
+        }
+        while (parent_account);
+    }
+    return NULL; // no suitable commodity found.
+}
+
 /********************************************************************\
 \********************************************************************/
 void

Modified: gnucash/trunk/src/engine/Account.h
===================================================================
--- gnucash/trunk/src/engine/Account.h	2013-06-19 15:47:13 UTC (rev 23054)
+++ gnucash/trunk/src/engine/Account.h	2013-06-19 15:47:27 UTC (rev 23055)
@@ -483,6 +483,14 @@
 /*@ dependent @*/
 gnc_commodity * xaccAccountGetCommodity (const Account *account);
 
+/** Returns a gnc_commodity that is a currency, suitable for being a
+Transaction's currency. The gnc_commodity is taken either from the current
+account, or from the next parent account that has a gnc_commodity that is a
+currency. If neither this account nor any of its parent has such a commodity
+that is a currency, NULL is returned. In that case, you can use
+gnc_default_currency() but you might want to show a warning dialog first. */
+gnc_commodity * gnc_account_get_currency_or_parent(const Account* account);
+
 /** Return the SCU for the account.  If a non-standard SCU has been
  *   set for the account, that is returned; else the default SCU for
  *   the account commodity is returned.

Modified: gnucash/trunk/src/register/ledger-core/split-register-load.c
===================================================================
--- gnucash/trunk/src/register/ledger-core/split-register-load.c	2013-06-19 15:47:13 UTC (rev 23054)
+++ gnucash/trunk/src/register/ledger-core/split-register-load.c	2013-06-19 15:47:27 UTC (rev 23055)
@@ -298,47 +298,20 @@
     if (blank_split == NULL)
     {
         Transaction *new_trans;
-        gnc_commodity * currency = NULL;
+        gboolean currency_from_account = TRUE;
 
         /* Determine the proper currency to use for this transaction.
          * if default_account != NULL and default_account->commodity is
          * a currency, then use that.  Otherwise use the default currency.
          */
-        if (default_account != NULL)
-        {
-            gnc_commodity * commodity = xaccAccountGetCommodity (default_account);
-            if (gnc_commodity_is_currency(commodity))
-                currency = commodity;
-            else
-            {
-                Account *parent_account = default_account;
-                /* Account commodity is not a currency, walk up the tree until
-                 * we find a parent account that is a currency account and use
-                 * it's currency.
-                 */
-                do
-                {
-                    parent_account = gnc_account_get_parent (parent_account);
-                    if (parent_account)
-                    {
-                        commodity = xaccAccountGetCommodity (parent_account);
-                        if (gnc_commodity_is_currency(commodity))
-                        {
-                            currency = commodity;
-                            break;
-                        }
-                    }
-                }
-                while (parent_account && !currency);
-            }
+        gnc_commodity * currency = gnc_account_or_default_currency(default_account, &currency_from_account);
 
+        if (default_account != NULL && !currency_from_account)
+        {
             /* If we don't have a currency then pop up a warning dialog */
-            if (!currency)
-            {
-                gnc_info_dialog(NULL, "%s",
-                                _("Could not determine the account currency.  "
-                                  "Using the default currency provided by your system."));
-            }
+            gnc_info_dialog(NULL, "%s",
+                            _("Could not determine the account currency.  "
+                              "Using the default currency provided by your system."));
         }
 
         gnc_suspend_gui_refresh ();
@@ -346,8 +319,7 @@
         new_trans = xaccMallocTransaction (gnc_get_current_book ());
 
         xaccTransBeginEdit (new_trans);
-        xaccTransSetCurrency (new_trans,
-                              currency ? currency : gnc_default_currency());
+        xaccTransSetCurrency (new_trans, currency);
         xaccTransSetDatePostedSecsNormalized(new_trans, info->last_date_entered);
         blank_split = xaccMallocSplit (gnc_get_current_book ());
         xaccSplitSetParent(blank_split, new_trans);



More information about the gnucash-changes mailing list