diff --git a/src/engine/Transaction.c b/src/engine/Transaction.c index abf2efd298..72ed9c0b8c 100644 --- a/src/engine/Transaction.c +++ b/src/engine/Transaction.c @@ -133,6 +133,53 @@ MarkChanged (Transaction *trans) /********************************************************************\ \********************************************************************/ +void +xaccSplitDestroy (Split *split) +{ + Transaction *trans; + int numsplits = 0; + int ismember = 0; + Split *s; + + trans = split->parent; + assert (trans); + assert (trans->splits); + + numsplits = 0; + s = trans->splits[0]; + while (s) { + if (s == split) ismember = 1; + numsplits ++; + s = trans->splits[numsplits]; + } + assert (ismember); + + /* if the accoount has three or more splits, + * merely unlink & free the split. + */ + if (2 < numsplits) { + xaccTransRemoveSplit (trans, split); + xaccAccountRemoveSplit (split->acc, split); + xaccFreeSplit (split); + xaccSplitRebalance (trans->splits[1]); + } else { + /* if the transaction has only two splits, + * remove both of them, and them destroy the + * transaction. + */ + s = trans->splits[0]; + xaccAccountRemoveSplit (split->acc, s); + xaccFreeSplit (s); + s = trans->splits[1]; + xaccAccountRemoveSplit (split->acc, s); + xaccFreeSplit (s); + xaccFreeTransaction (trans); + } +} + +/********************************************************************\ +\********************************************************************/ + int xaccCountSplits (Split **tarray) { @@ -491,12 +538,6 @@ xaccTransRemoveSplit (Transaction *trans, Split *split) s = trans->splits[n]; } trans->splits[i] = NULL; - - /* force double entry to always be consistent */ - xaccTransRebalance (trans); - - /* hack alert -- this leaves open the possibility of a danglig split! - * */ } /********************************************************************\ diff --git a/src/engine/Transaction.h b/src/engine/Transaction.h index 8efebe5eb2..430c72657d 100644 --- a/src/engine/Transaction.h +++ b/src/engine/Transaction.h @@ -75,6 +75,14 @@ void xaccTransSetDateToday (Transaction *); void xaccTransSetNum (Transaction *, const char *); void xaccTransSetDescription (Transaction *, const char *); + +/* The xaccTransSetMemo() and xaccTransSetAction() methods are + * convenience routines to keep the memo and action fields + * of two-split trasnactions in sync. If the transaction has + * precisely two splits, then these routines will set the memo + * and action of both splits. Otherwise, they will set the + * memo and action of the first split (source split) only. + */ void xaccTransSetMemo (Transaction *, const char *); void xaccTransSetAction (Transaction *, const char *); diff --git a/src/engine/TransactionP.h b/src/engine/TransactionP.h index 8454c0dba3..7d605e7ea2 100644 --- a/src/engine/TransactionP.h +++ b/src/engine/TransactionP.h @@ -107,9 +107,15 @@ struct _transaction /* freeTransaction only does so if the transaction is not part of an * account. (i.e. if none of the member splits are in an account). */ -void xaccFreeTransaction (Transaction *); +void xaccFreeTransaction (Transaction *); -void xaccFreeSplit (Split *); /* frees memory */ +void xaccFreeSplit (Split *); /* frees memory */ + +/* The xaccTransRemoveSplit() routine will remove the indicated + * split from the transaction. It will *NOT* otherwise + * readjust balances, modify accounts, etc. + */ +void xaccTransRemoveSplit (Transaction*, Split *); /*