Skip to content

Commit

Permalink
Refactor lookup of a commodity that is a currency for a specific acco…
Browse files Browse the repository at this point in the history
…unt 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
  • Loading branch information
cstim committed Jun 19, 2013
1 parent 2ed03cc commit 2bbcd77
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 36 deletions.
27 changes: 27 additions & 0 deletions src/app-utils/gnc-ui-util.c
Expand Up @@ -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)
{
Expand Down
24 changes: 24 additions & 0 deletions src/app-utils/gnc-ui-util.h
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/engine/Account.c
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/engine/Account.h
Expand Up @@ -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.
Expand Down
44 changes: 8 additions & 36 deletions src/register/ledger-core/split-register-load.c
Expand Up @@ -298,56 +298,28 @@ 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, &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 ();

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);
Expand Down

0 comments on commit 2bbcd77

Please sign in to comment.