diff --git a/src/SplitLedger.c b/src/SplitLedger.c index 9a809ad9bb..10c0d609c0 100644 --- a/src/SplitLedger.c +++ b/src/SplitLedger.c @@ -2596,11 +2596,11 @@ xaccSRSaveChangedCells (SplitRegister *reg, Transaction *trans, Split *split) const char *security = NULL; currency = xaccAccountGetCurrency(new_acc); - currency = xaccTransIsCommonCurrency(trans, currency); + currency = xaccTransIsCommonExclSCurrency(trans, currency, split); if (currency == NULL) { security = xaccAccountGetSecurity(new_acc); - security = xaccTransIsCommonCurrency(trans, security); + security = xaccTransIsCommonExclSCurrency(trans, security, split); } if ((currency != NULL) || (security != NULL)) { @@ -2669,11 +2669,11 @@ xaccSRSaveChangedCells (SplitRegister *reg, Transaction *trans, Split *split) const char *security = NULL; currency = xaccAccountGetCurrency(new_acc); - currency = xaccTransIsCommonCurrency(trans, currency); + currency = xaccTransIsCommonExclSCurrency(trans, currency, split); if (currency == NULL) { security = xaccAccountGetSecurity(new_acc); - security = xaccTransIsCommonCurrency(trans, security); + security = xaccTransIsCommonExclSCurrency(trans, security, split); } if ((currency != NULL) || (security != NULL)) { diff --git a/src/engine/Transaction.c b/src/engine/Transaction.c index 318d3121a4..65fce6d0bd 100644 --- a/src/engine/Transaction.c +++ b/src/engine/Transaction.c @@ -846,7 +846,9 @@ xaccIsCommonCurrency(const char *currency_1, const char *security_1, } static const char * -FindCommonCurrency (Split **slist, const char * ra, const char * rb) +FindCommonExclSCurrency (Split **slist, + const char * ra, const char * rb, + Split *excl_split) { Split *s; int i = 0; @@ -855,10 +857,17 @@ FindCommonCurrency (Split **slist, const char * ra, const char * rb) if (rb && ('\0' == rb[0])) rb = NULL; - i=0; s = slist[0]; + i = 0; + s = slist[0]; + + /* If s is to be excluded, go ahead in the list until one split is + not excluded or is NULL. */ + while (s && (s == excl_split)) + { i++; s = slist[i]; } + while (s) { char *sa, *sb; - + /* Novice/casual users may not want or use the double entry * features of this engine. Because of this, there * may be the occasional split without a parent account. @@ -901,12 +910,28 @@ FindCommonCurrency (Split **slist, const char * ra, const char * rb) } if ((!ra) && (!rb)) return NULL; - i++; s = slist[i]; + + i++; + s = slist[i]; + + /* If s is to be excluded, go ahead in the list until one split is + not excluded or is NULL. */ + while (s && (s == excl_split)) + { i++; s = slist[i]; } } return (ra); } +/* This is the wrapper for those calls (i.e. the older ones) which + * don't exclude one split from the splitlist when looking for a + * common currency. + */ +static const char * +FindCommonCurrency (Split **slist, const char * ra, const char * rb) +{ + return FindCommonExclSCurrency(slist, ra, rb, NULL); +} const char * xaccTransFindCommonCurrency (Transaction *trans) @@ -929,6 +954,13 @@ xaccTransIsCommonCurrency (Transaction *trans, const char * ra) return FindCommonCurrency (trans->splits, ra, NULL); } +const char * +xaccTransIsCommonExclSCurrency (Transaction *trans, + const char * ra, Split *excl_split) +{ + return FindCommonExclSCurrency (trans->splits, ra, NULL, excl_split); +} + /********************************************************************\ \********************************************************************/ diff --git a/src/engine/Transaction.h b/src/engine/Transaction.h index a89721b30a..2100db8e85 100644 --- a/src/engine/Transaction.h +++ b/src/engine/Transaction.h @@ -295,6 +295,20 @@ const char * xaccTransFindCommonCurrency (Transaction *trans); const char * xaccTransIsCommonCurrency (Transaction *trans, const char * currency); +/* The xaccTransIsCommonExclSCurrency () method compares the input + * string to the currency/security denominations of all splits in + * the transaction except the one given as parameter, and returns + * the input string if it is common with all splits except the one + * given, otherwise, it returns NULL. + * + * This is useful when changing one split such that the old entry in + * that split is of no relevance when determining whether the new entry + * has a common currency with the other splits. + */ +const char * xaccTransIsCommonExclSCurrency (Transaction *trans, + const char * currency, + Split *excl_split); + /* The xaccTransGetImbalance() method returns the total value of the * transaction. In a pure double-entry system, this imbalance * should be exactly zero, and if it is not, something is broken.