Skip to content

Commit

Permalink
[scrub.c] xaccTransScrubSplits calls Begin/Commit only if required
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Jun 2, 2023
1 parent 872e20c commit 81902ba
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions libgnucash/engine/Scrub.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "Account.h"
#include "AccountP.h"
Expand Down Expand Up @@ -231,23 +232,25 @@ xaccAccountScrubSplits (Account *account)
scrub_depth--;
}

void
xaccSplitScrub (Split *split)
/* if dry_run is true, this function will analyze the split and
return true if the split will be modified during the actual scrub. */
static bool
split_scrub_or_dry_run (Split *split, bool dry_run)
{
Account *account;
Transaction *trans;
gnc_numeric value, amount;
gnc_commodity *currency, *acc_commodity;
int scu;

if (!split) return;
if (!split) return false;
ENTER ("(split=%p)", split);

trans = xaccSplitGetParent (split);
if (!trans)
{
LEAVE("no trans");
return;
return false;
}

account = xaccSplitGetAccount (split);
Expand All @@ -257,7 +260,10 @@ xaccSplitScrub (Split *split)
*/
if (!account)
{
xaccTransScrubOrphans (trans);
if (dry_run)
return true;
else
xaccTransScrubOrphans (trans);
account = xaccSplitGetAccount (split);
}

Expand All @@ -269,22 +275,28 @@ xaccSplitScrub (Split *split)
{
PINFO ("Free Floating Transaction!");
LEAVE ("no account");
return;
return false;
}

/* Split amounts and values should be valid numbers */
value = xaccSplitGetValue (split);
if (gnc_numeric_check (value))
{
value = gnc_numeric_zero();
xaccSplitSetValue (split, value);
if (dry_run)
return true;
else
xaccSplitSetValue (split, value);
}

amount = xaccSplitGetAmount (split);
if (gnc_numeric_check (amount))
{
amount = gnc_numeric_zero();
xaccSplitSetAmount (split, amount);
if (dry_run)
return true;
else
xaccSplitSetAmount (split, amount);
}

currency = xaccTransGetCurrency (trans);
Expand All @@ -295,12 +307,15 @@ xaccSplitScrub (Split *split)
acc_commodity = xaccAccountGetCommodity(account);
if (!acc_commodity)
{
xaccAccountScrubCommodity (account);
if (dry_run)
return true;
else
xaccAccountScrubCommodity (account);
}
if (!acc_commodity || !gnc_commodity_equiv(acc_commodity, currency))
{
LEAVE ("(split=%p) inequiv currency", split);
return;
return false;
}

scu = MIN (xaccAccountGetCommoditySCU (account),
Expand All @@ -309,9 +324,12 @@ xaccSplitScrub (Split *split)
if (gnc_numeric_same (amount, value, scu, GNC_HOW_RND_ROUND_HALF_UP))
{
LEAVE("(split=%p) different values", split);
return;
return false;
}

if (dry_run)
return true;

/*
* This will be hit every time you answer yes to the dialog "The
* current transaction has changed. Would you like to record it.
Expand All @@ -327,6 +345,7 @@ xaccSplitScrub (Split *split)
xaccSplitSetAmount (split, value);
xaccTransCommitEdit (trans);
LEAVE ("(split=%p)", split);
return true;
}

/* ================================================================ */
Expand All @@ -340,6 +359,15 @@ xaccTransScrubSplits (Transaction *trans)
if (!currency)
PERR ("Transaction doesn't have a currency!");

bool must_scrub = false;

for (GList *n = xaccTransGetSplitList (trans); !must_scrub && n; n = g_list_next (n))
if (split_scrub_or_dry_run (n->data, true))
must_scrub = true;

if (!must_scrub)
return;

xaccTransBeginEdit(trans);
/* The split scrub expects the transaction to have a currency! */

Expand All @@ -349,6 +377,13 @@ xaccTransScrubSplits (Transaction *trans)
xaccTransCommitEdit(trans);
}

/* ================================================================ */

void
xaccSplitScrub (Split *split)
{
split_scrub_or_dry_run (split, false);
}

/* ================================================================ */

Expand Down

0 comments on commit 81902ba

Please sign in to comment.