Fix duplicate trading accounts.

In many cases GnuCash would create a new trading account hierarchy
when one already existed because gnc_account_lookup_by_type_and_commodity
didn't check the account presented and didn't recurse down the
account hierarchy correctly.
This commit is contained in:
John Ralls 2021-04-24 15:44:08 -07:00
parent 2258e7a44e
commit ed486a58a4

View File

@ -3127,16 +3127,34 @@ gnc_account_lookup_by_type_and_commodity (Account* root,
gnc_commodity* commodity)
{
auto rpriv{GET_PRIVATE(root)};
if (rpriv->type == acctype &&
gnc_commodity_equiv(rpriv->commodity, commodity))
{
if (name)
{
if (strcmp(name, rpriv->accountName) == 0)
return root;
}
else
{
return root;
}
}
/* Nope. Make sure the types are compatible */
if (!xaccAccountTypesCompatible(rpriv->type, acctype))
return nullptr;
/* Recurse */
for (auto node = rpriv->children; node; node = node->next)
{
auto account{static_cast<Account*>(node->data)};
if (xaccAccountGetType (account) == acctype &&
gnc_commodity_equiv(xaccAccountGetCommodity (account), commodity))
{
if (name && strcmp(name, xaccAccountGetName(account)))
continue; //name doesn't match so skip this one
return account;
auto child{gnc_account_lookup_by_type_and_commodity(account, name,
acctype,
commodity)};
if (child)
return child;
}
}
return nullptr;