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.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@23057 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2013-06-19 15:47:49 +00:00
parent fd9ea2d808
commit 9b093abc4a

View File

@ -248,7 +248,8 @@ struct GncTreeViewSplitRegPrivate
gboolean disposed; gboolean disposed;
Account *anchor; // The register default Account 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 Transaction *current_trans; // The current highlighted transaction
Split *current_split; // The current highlighted split Split *current_split; // The current highlighted split
@ -1137,6 +1138,9 @@ gnc_tree_view_split_reg_new_with_model (GncTreeModelSplitReg *model)
view->priv->anchor = gnc_tree_model_split_reg_get_anchor (model); view->priv->anchor = gnc_tree_model_split_reg_get_anchor (model);
view->priv->reg_comm = xaccAccountGetCommodity (view->priv->anchor); 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"); view->help_text = g_strdup ("Help Text");
gnc_tree_view_split_reg_set_cols (view, gnc_tree_view_split_reg_get_colummn_list (model)); gnc_tree_view_split_reg_set_cols (view, gnc_tree_view_split_reg_get_colummn_list (model));
@ -4467,47 +4471,18 @@ gtv_sr_edited_normal_cb (GtkCellRendererText *cell, const gchar *path_string,
/* Set the transaction currency if not set */ /* Set the transaction currency if not set */
if (!xaccTransGetCurrency (trans)) 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 // We are on General ledger
if (!anchor) if (!anchor)
{ {
split_commodity = xaccAccountGetCommodity (xaccSplitGetAccount (split)); xaccTransSetCurrency (trans, gnc_account_or_default_currency (xaccSplitGetAccount (split), NULL));
if (gnc_commodity_is_currency (split_commodity))
xaccTransSetCurrency (trans, xaccAccountGetCommodity (xaccSplitGetAccount (split)));
else
xaccTransSetCurrency (trans, gnc_default_currency());
} }
} }
// if non currency register, we set the trans currency to the first currency split // No need to check for a non-currency register because that's what
if (xaccTransGetCurrency (trans) && !gnc_commodity_is_currency (view->priv->reg_comm)) // was already checked when reg_currency was stored.
{
if (!gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (xaccTransGetSplit (trans, 0)))))
{
int i;
Split *s = NULL;
gboolean currency = FALSE;
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 */ /* This computes the value if we just commit the split after entering account */
if (!valid_input) if (!valid_input)
@ -4748,37 +4723,11 @@ gtv_sr_edited_template_cb (GtkCellRendererText *cell, const gchar *path_string,
/* Set the transaction currency if not set */ /* Set the transaction currency if not set */
if (!xaccTransGetCurrency (trans)) if (!xaccTransGetCurrency (trans))
{ {
gnc_commodity *split_commodity; xaccTransSetCurrency (trans, gnc_account_or_default_currency (xaccSplitGetAccount (split), NULL));
split_commodity = xaccAccountGetCommodity (xaccSplitGetAccount (split));
if (gnc_commodity_is_currency (split_commodity))
xaccTransSetCurrency (trans, xaccAccountGetCommodity (xaccSplitGetAccount (split)));
else
xaccTransSetCurrency (trans, gnc_default_currency());
} }
// if non currency register, we set the trans currency to the first currency split // No need to check for a non-currency register because that's what
if (xaccTransGetCurrency (trans)) // was already checked when reg_currency was stored.
{
if (!gnc_commodity_is_currency (xaccAccountGetCommodity (xaccSplitGetAccount (xaccTransGetSplit (trans, 0)))))
{
int i;
Split *s = NULL;
gboolean currency = FALSE;
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 */ /* Setup the debit and credit fields */
if (viewcol == COL_DEBIT) if (viewcol == COL_DEBIT)