Skip to content

Commit

Permalink
Imbalance fraction testing: Check all of the account SCUs too.
Browse files Browse the repository at this point in the history
An account might have a fixed SCU denominator that's less
than the commodity's denominator and that can also create an
unbalanceable transaction.
  • Loading branch information
jralls committed Oct 8, 2024
1 parent 7f8d545 commit c63d2dc
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions gnucash/register/ledger-core/split-register-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_LEDGER;

static inline bool
check_imbalance_fraction (const SplitRegister* reg,
const gnc_monetary* imbal_mon,
const Transaction* trans)
{
auto commodity_fraction{gnc_commodity_get_fraction (imbal_mon->commodity)};
auto denom_diff = imbal_mon->value.denom > commodity_fraction;
if (!denom_diff)
{
const auto imbal_comm = imbal_mon->commodity;
for (auto node = xaccTransGetSplitList(trans); node;
node = g_list_next(node))
{
auto acc = xaccSplitGetAccount(GNC_SPLIT (node->data));
if (xaccAccountGetCommodity(acc) == imbal_comm &&
imbal_mon->value.denom > xaccAccountGetCommoditySCU (acc))
{
denom_diff = true;
break;
}
}
}

if (denom_diff)
gnc_error_dialog(gnc_ui_get_main_window(GTK_WIDGET(reg)), "%s", _("This transaction cannot be balanced: The imbalance is a fraction smaller than the commodity allows."));

return denom_diff;
}

static gboolean
gnc_split_register_balance_trans (SplitRegister *reg, Transaction *trans)
Expand Down Expand Up @@ -91,12 +119,9 @@ gnc_split_register_balance_trans (SplitRegister *reg, Transaction *trans)
multi_currency = FALSE;
else
multi_currency = TRUE;
if (multi_currency &&
imbal_mon->value.denom > gnc_commodity_get_fraction(imbal_mon->commodity))
{
gnc_error_dialog(gnc_ui_get_main_window(GTK_WIDGET(reg)), "%s", _("This transaction cannot be balanced: The imbalance is a fraction smaller than the commodity allows."));

if (multi_currency && check_imbalance_fraction (reg, imbal_mon, trans))
return FALSE;
}
}

/* We're done with the imbalance list, the real work will be done
Expand Down

0 comments on commit c63d2dc

Please sign in to comment.