gnucash stable: Imbalance fraction testing: Check all of the account SCUs too.

John Ralls jralls at code.gnucash.org
Mon Oct 7 20:13:41 EDT 2024


Updated	 via  https://github.com/Gnucash/gnucash/commit/c63d2dcf (commit)
	from  https://github.com/Gnucash/gnucash/commit/7f8d5456 (commit)



commit c63d2dcf6d227c8bff0848807a3e9445d23c9443
Author: John Ralls <jralls at ceridwen.us>
Date:   Mon Oct 7 17:11:16 2024 -0700

    Imbalance fraction testing: Check all of the account SCUs too.
    
    An account might have a fixed SCU denominator that's less
    than the commodity's denominator and that can also create an
    unbalanceable transaction.

diff --git a/gnucash/register/ledger-core/split-register-control.cpp b/gnucash/register/ledger-core/split-register-control.cpp
index 070a4c5f39..76fa25b1ea 100644
--- a/gnucash/register/ledger-core/split-register-control.cpp
+++ b/gnucash/register/ledger-core/split-register-control.cpp
@@ -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)
@@ -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



Summary of changes:
 .../ledger-core/split-register-control.cpp         | 35 ++++++++++++++++++----
 1 file changed, 30 insertions(+), 5 deletions(-)



More information about the gnucash-changes mailing list