Skip to content

Commit

Permalink
Ensure numeric errors aren't stored in split amounts or values.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Dec 4, 2014
1 parent c4d649b commit 701d803
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/engine/Split.c
Expand Up @@ -1265,10 +1265,13 @@ xaccSplitSetAmount (Split *s, gnc_numeric amt)

xaccTransBeginEdit (s->parent);
if (s->acc)
{
s->amount = gnc_numeric_convert(amt, get_commodity_denom(s),
GNC_HOW_RND_ROUND_HALF_UP);
g_assert (gnc_numeric_check (s->amount) == GNC_ERROR_OK);
}
else
s->amount = amt;
s->amount = amt;

SET_GAINS_ADIRTY(s);
mark_split (s);
Expand All @@ -1283,6 +1286,7 @@ qofSplitSetValue (Split *split, gnc_numeric amt)
g_return_if_fail(split);
split->value = gnc_numeric_convert(amt,
get_currency_denom(split), GNC_HOW_RND_ROUND_HALF_UP);
g_assert(gnc_numeric_check (split->value) != GNC_ERROR_OK);
}

/* The value of the split in the _transaction's_ currency. */
Expand All @@ -1300,7 +1304,8 @@ xaccSplitSetValue (Split *s, gnc_numeric amt)
xaccTransBeginEdit (s->parent);
new_val = gnc_numeric_convert(amt, get_currency_denom(s),
GNC_HOW_RND_ROUND_HALF_UP);
if (gnc_numeric_check(new_val) == GNC_ERROR_OK)
if (gnc_numeric_check(new_val) == GNC_ERROR_OK &&
!(gnc_numeric_zero_p (new_val) && !gnc_numeric_zero_p (amt)))
s->value = new_val;
else PERR("numeric error %s in converting the split value's denominator with amount %s and denom %d", gnc_numeric_errorCode_to_string(gnc_numeric_check(new_val)), gnc_numeric_to_string(amt), get_currency_denom(s));

Expand Down
3 changes: 3 additions & 0 deletions src/engine/cap-gains.c
Expand Up @@ -338,6 +338,7 @@ xaccSplitAssignToLot (Split *split, GNCLot *lot)
amt_tot = split->amount;
amt_a = gnc_numeric_neg (baln);
amt_b = gnc_numeric_sub_fixed (amt_tot, amt_a);
g_assert (gnc_numeric_check(amt_b) == GNC_ERROR_OK);

PINFO ("++++++++++++++ splitting split=%p into amt = %s + %s",
split,
Expand Down Expand Up @@ -377,6 +378,8 @@ xaccSplitAssignToLot (Split *split, GNCLot *lot)
gnc_num_dbg_to_string(val_a),
gnc_num_dbg_to_string(val_b) );

g_assert (!gnc_numeric_zero_p (amt_a));
g_assert (!gnc_numeric_zero_p (val_a));
xaccSplitSetAmount (split, amt_a);
xaccSplitSetValue (split, val_a);

Expand Down
1 change: 1 addition & 0 deletions src/engine/gnc-lot.c
Expand Up @@ -497,6 +497,7 @@ gnc_lot_get_balance (GNCLot *lot)
Split *s = node->data;
gnc_numeric amt = xaccSplitGetAmount (s);
baln = gnc_numeric_add_fixed (baln, amt);
g_assert (gnc_numeric_check (baln) == GNC_ERROR_OK);
}

/* cache a zero balance as a closed lot */
Expand Down

0 comments on commit 701d803

Please sign in to comment.