rationalize split deletiton, make it work right.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@1242 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 1998-09-30 06:05:52 +00:00
parent c7b28fd6cb
commit 33c331a418
3 changed files with 63 additions and 42 deletions

View File

@ -110,6 +110,7 @@ xaccFreeAccount( Account *acc )
{ {
int i=0; int i=0;
Split *s; Split *s;
Transaction *t;
if (NULL == acc) return; if (NULL == acc) return;
@ -129,12 +130,17 @@ xaccFreeAccount( Account *acc )
s->acc = NULL; s->acc = NULL;
} }
/* search for orphaned transactions, and delete them */ /* destroy all of the splits. The xaccCommitEdit() call
* will automatically clean up orphaned transactions.
*/
acc->open |= ACC_BEING_DESTROYED; acc->open |= ACC_BEING_DESTROYED;
acc->open |= ACC_DEFER_REBALANCE; acc->open |= ACC_DEFER_REBALANCE;
for (i=0; i<acc->numSplits; i++) { for (i=0; i<acc->numSplits; i++) {
s = acc->splits[i]; s = acc->splits[i];
t = s->parent;
xaccTransBeginEdit (t, 1);
xaccSplitDestroy (s); xaccSplitDestroy (s);
xaccTransCommitEdit (t);
} }
/* free up array of split pointers */ /* free up array of split pointers */

View File

@ -677,6 +677,37 @@ xaccTransCommitEdit (Transaction *trans)
if (!trans) return; if (!trans) return;
CHECK_OPEN (trans); CHECK_OPEN (trans);
/* At this point, we check to see if we have a valid transaction.
* As a result of editing, we could end up with a transaction that
* has no splits in it, in which case we delete the transaction and
* return. Alternately the transaction may have only one split in
* it, in which case ... that's OK if and only if the split has no
* value (i.e. is only recording a price). Otherwise, a single
* split with a value can't possibly balance, thus violating the
* rules of double-entry, and that's way bogus. So delete
* the split, delete the transaction. I suppose we could
* generate an error or something like that at this point,
* to let the user know that we blew away a split.
*/
split = trans->splits[0];
if (!split ||
((NULL == trans->splits[1]) && (!(DEQ(0.0, split->damount)))))
{
/* Make a log in the journal before destruction. */
xaccTransWriteLog (trans, 'D');
if (split) {
acc = split->acc;
MARK_SPLIT (split);
xaccAccountRemoveSplit (acc, split);
xaccAccountRecomputeBalance (acc);
xaccFreeSplit (split);
trans->splits[0] = NULL;
}
xaccFreeTransaction (trans);
return;
}
trans->open &= ~DEFER_REBALANCE; trans->open &= ~DEFER_REBALANCE;
xaccTransRebalance (trans); xaccTransRebalance (trans);
@ -749,8 +780,7 @@ xaccSplitDestroy (Split *split)
trans = split->parent; trans = split->parent;
assert (trans); assert (trans);
assert (trans->splits); assert (trans->splits);
// temp hack alert -- get rid of annoying error messages CHECK_OPEN (trans);
// CHECK_OPEN (trans);
numsplits = 0; numsplits = 0;
s = trans->splits[0]; s = trans->splits[0];
@ -765,42 +795,20 @@ xaccSplitDestroy (Split *split)
/* If the account has three or more splits, /* If the account has three or more splits,
* merely unlink & free the split. * merely unlink & free the split.
* *
* Or if the account has only two splits, and the * Or if the account has only two splits,
* split to be destroyed is a price split (i.e. * then this destroy will leave only one split.
* has a damount value of zero), then * Don't rebalance, as this will goof up the
* merely unlink & free the split. * value of teh remaining split.
*/ */
if ((2 < numsplits) ||
((2 == numsplits) && (DEQ(0.0, split->damount))))
{
MARK_SPLIT (split); MARK_SPLIT (split);
xaccTransRemoveSplit (trans, split); xaccTransRemoveSplit (trans, split);
acc = split->acc; acc = split->acc;
xaccAccountRemoveSplit (acc, split); xaccAccountRemoveSplit (acc, split);
xaccAccountRecomputeBalance (acc); xaccAccountRecomputeBalance (acc);
xaccFreeSplit (split); xaccFreeSplit (split);
xaccSplitRebalance (trans->splits[0]);
} else {
/* if the transaction has only two splits,
* remove both of them, and them destroy the
* transaction. Make a log in the journal before
* destruction.
*/
xaccTransWriteLog (trans, 'D');
s = trans->splits[0];
acc = s->acc;
MARK_SPLIT (s);
xaccAccountRemoveSplit (acc, s);
xaccAccountRecomputeBalance (acc);
s = trans->splits[1]; if (2 < numsplits) {
if (s) { xaccSplitRebalance (trans->splits[0]);
acc = s->acc;
MARK_SPLIT (s);
xaccAccountRemoveSplit (acc, s);
xaccAccountRecomputeBalance (acc);
xaccFreeTransaction (trans);
}
} }
} }

View File

@ -72,9 +72,13 @@ void xaccInitTransaction (Transaction *);/* clears a trans struct */
*/ */
void xaccTransDestroy (Transaction *); void xaccTransDestroy (Transaction *);
/* The xaccTransBegineEdit() ... /* The xaccTransBeginEdit() ...
* If the defer flag is set, then automated balancing * If the defer flag is set, then automated balancing
* is defered until the commit ... * is defered until the commit ...
*
* The xaccTransCommitEdit() routine may result in the deletion of the
* transaction, if the transaction is "empty" (has no splits, or
* has a single split in it whose value is non-zero.)
*/ */
void xaccTransBeginEdit (Transaction *, int defer); void xaccTransBeginEdit (Transaction *, int defer);
void xaccTransCommitEdit (Transaction *); void xaccTransCommitEdit (Transaction *);
@ -115,10 +119,13 @@ void xaccTransAppendSplit (Transaction *, Split *);
* leaving the accounting structure out-of-balance or otherwise * leaving the accounting structure out-of-balance or otherwise
* inconsistent. * inconsistent.
* *
* If the parent transaction of the split has three or more splits * If the deletion of the split leaves the transaction "empty",
* in it, then only this one split is unlinked. If the parent * then the transaction will be marked for deletion. (It will
* transaction has only two splits in it (and thus, this is one of * not be deleted until the xaccTransCommitEdit() routine is called.)
* them), then both splits and the transaction are destroyed. * The transaction is considered "empty" if it has no splits in it,
* or it has only one split left, and that split is not a price split
* (i.e. has a non-zero value). Transactions with only one split in
* them are valid if and only if the value of that split is zero.
*/ */
void xaccSplitDestroy (Split *); void xaccSplitDestroy (Split *);