From 2bbcd7733f4fdf40f57f4a6af72db6ba6295cb23 Mon Sep 17 00:00:00 2001 From: Christian Stimming Date: Wed, 19 Jun 2013 15:47:27 +0000 Subject: [PATCH] Refactor lookup of a commodity that is a currency for a specific account into general function gnc_account_or_default_currency(). git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@23055 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/app-utils/gnc-ui-util.c | 27 ++++++++++++ src/app-utils/gnc-ui-util.h | 24 ++++++++++ src/engine/Account.c | 33 ++++++++++++++ src/engine/Account.h | 8 ++++ .../ledger-core/split-register-load.c | 44 ++++--------------- 5 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/app-utils/gnc-ui-util.c b/src/app-utils/gnc-ui-util.c index 9531054cb8..5332a4b227 100644 --- a/src/app-utils/gnc-ui-util.c +++ b/src/app-utils/gnc-ui-util.c @@ -940,6 +940,33 @@ gnc_default_currency (void) 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) { diff --git a/src/app-utils/gnc-ui-util.h b/src/app-utils/gnc-ui-util.h index 63a4c8b2e5..3a7a36205c 100644 --- a/src/app-utils/gnc-ui-util.h +++ b/src/app-utils/gnc-ui-util.h @@ -168,6 +168,30 @@ const char * gnc_locale_default_iso_currency_code (void); */ 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. + +@param account The account where the currency should be looked up. May be NULL. + +@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. + +@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 diff --git a/src/engine/Account.c b/src/engine/Account.c index fec882a4a9..997dbfef00 100644 --- a/src/engine/Account.c +++ b/src/engine/Account.c @@ -2972,6 +2972,39 @@ xaccAccountGetCommodity (const Account *acc) 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 diff --git a/src/engine/Account.h b/src/engine/Account.h index d6b3868c3b..e999a1e759 100644 --- a/src/engine/Account.h +++ b/src/engine/Account.h @@ -483,6 +483,14 @@ void xaccAccountSetCommodity (Account *account, gnc_commodity *comm); /*@ 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. diff --git a/src/register/ledger-core/split-register-load.c b/src/register/ledger-core/split-register-load.c index dc2b1f8d4e..498d3bcb6c 100644 --- a/src/register/ledger-core/split-register-load.c +++ b/src/register/ledger-core/split-register-load.c @@ -298,47 +298,20 @@ gnc_split_register_load (SplitRegister *reg, GList * slist, 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, ¤cy_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 @@ gnc_split_register_load (SplitRegister *reg, GList * slist, 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);