From d7c0ade60a185281aa96ecb5237359df8e76b0de Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sat, 29 Nov 1997 06:43:58 +0000 Subject: [PATCH] implement account merge git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@179 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/Account.c | 1 + src/Data.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++- src/MainWindow.c | 3 ++- src/QIFIO.c | 18 ++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/Account.c b/src/Account.c index 1b1d3e4833..3588185f00 100644 --- a/src/Account.c +++ b/src/Account.c @@ -105,6 +105,7 @@ freeAccount( Account *acc ) Transaction *trans = acc->transaction[i]; struct _account * _acc = (struct _account *) acc; + if (!trans) continue; /* free the transaction only if its not * a part of a double entry */ if (_acc == trans->credit) trans->credit = NULL; diff --git a/src/Data.c b/src/Data.c index 884f0802d3..ff7b03beff 100644 --- a/src/Data.c +++ b/src/Data.c @@ -499,7 +499,7 @@ xaccGetRootGroupOfAcct (Account *acc) \********************************************************************/ void -xaccMergeGroup (AccountGroup *togrp, AccountGroup *fromgrp) +xaccConcatGroups (AccountGroup *togrp, AccountGroup *fromgrp) { Account * acc; int i; @@ -517,4 +517,65 @@ xaccMergeGroup (AccountGroup *togrp, AccountGroup *fromgrp) fromgrp->numAcc = 0; } +/********************************************************************\ +\********************************************************************/ + +void +xaccMergeAccounts (AccountGroup *grp) +{ + Account *acc_a, *acc_b; + int i,j, k; + + if (!grp) return; + + for (i=0; inumAcc; i++) { + acc_a = grp->account[i]; + for (j=i+1; jnumAcc; j++) { + acc_b = grp->account[j]; + if ((0 == strcmp(acc_a->accountName, acc_b->accountName)) && + (0 == strcmp(acc_a->description, acc_b->description)) && + (0 == strcmp(acc_a->notes, acc_b->notes)) && + (acc_a->type == acc_b->type)) { + + AccountGroup *ga, *gb; + + /* consolidate children */ + ga = (AccountGroup *) acc_a->children; + gb = (AccountGroup *) acc_b->children; + if (gb) { + if (!ga) { + acc_a->children = (struct _account_group *) gb; + gb->parent = acc_a; + acc_b->children = NULL; + } else { + xaccConcatGroups (ga, gb); + freeAccountGroup (gb); + acc_b->children = NULL; + } + } + + /* recurse to do the children's children */ + xaccMergeAccounts (ga); + + /* consolidate transactions */ + for (k=0; knumTrans; k++) { + Transaction *trans; + trans = acc_b->transaction[k]; + acc_b->transaction[k] = NULL; + if (acc_b == (Account *) trans->debit) trans->debit = (struct _account *) acc_a; + if (acc_b == (Account *) trans->credit) trans->credit = (struct _account *) acc_a; + insertTransaction (acc_a, trans); + } + + /* free the account structure itself */ + acc_b->numTrans = 0; + freeAccount (acc_b); + grp->account[j] = grp->account[grp->numAcc -1]; + grp->account[grp->numAcc -1] = NULL; + grp->numAcc --; + } + } + } +} + /****************** END OF FILE *************************************/ diff --git a/src/MainWindow.c b/src/MainWindow.c index a9c5cf67ad..23f1e1b3b0 100644 --- a/src/MainWindow.c +++ b/src/MainWindow.c @@ -886,7 +886,8 @@ fileMenubarCB( Widget mw, XtPointer cd, XtPointer cb ) /* since quicken will not export all accounts * into one file, we must merge them in one by one */ - xaccMergeGroup (topgroup, grp); + xaccConcatGroups (topgroup, grp); + xaccMergeAccounts (topgroup); } break; } diff --git a/src/QIFIO.c b/src/QIFIO.c index bdb223bff6..09cbf6ce52 100644 --- a/src/QIFIO.c +++ b/src/QIFIO.c @@ -140,6 +140,7 @@ char * xaccReadQIFCategory (int fd, Account * acc) acc -> type = -1; /* type is byte */ acc -> accountName = 0x0; /* string */ acc -> description = 0x0; /* string */ + acc -> notes = 0x0; /* string */ qifline = xaccReadQIFLine (fd); if (!qifline) return NULL; @@ -187,6 +188,7 @@ char * xaccReadQIFCategory (int fd, Account * acc) XACC_PREP_NULL_STRING (acc->accountName); XACC_PREP_NULL_STRING (acc->description); + XACC_PREP_NULL_STRING (acc->notes); return qifline; } @@ -210,6 +212,7 @@ char * xaccReadQIFAccount (int fd, Account * acc) acc -> type = -1; /* type is byte */ acc -> accountName = 0x0; /* string */ acc -> description = 0x0; /* string */ + acc -> notes = 0x0; /* string */ qifline = xaccReadQIFLine (fd); if (!qifline) return NULL; @@ -259,6 +262,7 @@ char * xaccReadQIFAccount (int fd, Account * acc) XACC_PREP_NULL_STRING (acc->accountName); XACC_PREP_NULL_STRING (acc->description); + XACC_PREP_NULL_STRING (acc->notes); return qifline; } @@ -483,6 +487,8 @@ xaccGetXferQIFAccount (Account *acc, char *qifline) if (!xfer_acc) { xfer_acc = mallocAccount (); xfer_acc->accountName = XtNewString (qifline); + xfer_acc->description = XtNewString (""); + xfer_acc->notes = XtNewString (""); xfer_acc->type = BANK; insertAccount (rootgrp, xfer_acc); } @@ -520,6 +526,8 @@ xaccGetSecurityQIFAccount (Account *acc, char *qifline) if (!xfer_acc) { xfer_acc = mallocAccount (); xfer_acc->accountName = XtNewString (qifline); + xfer_acc->description = XtNewString (""); + xfer_acc->notes = XtNewString (""); xfer_acc->type = STOCK; xaccInsertSubAccount (acc, xfer_acc); } @@ -723,8 +731,12 @@ char * xaccReadQIFTransaction (int fd, Account *acc) } else if ('!' == qifline [0]) break; qifline = xaccReadQIFLine (fd); + } + /* at this point, we should see an end-of-transaction marker + * if we see something else, assume the worst, free the last + * transaction, and return */ if ('!' == qifline[0]) { xaccRemoveTransaction ((Account *) trans->debit, trans); xaccRemoveTransaction ((Account *) trans->credit, trans); @@ -808,6 +820,8 @@ char * xaccReadQIFTransList (int fd, Account *acc) if (!acc) return 0x0; do { qifline = xaccReadQIFTransaction (fd, acc); + + if ('!' == qifline[0]) break; } while (qifline); return qifline; } @@ -856,6 +870,8 @@ xaccReadQIFData( char *datafile ) acc->type = BANK; acc->accountName = XtNewString ("Quicken Bank Account"); + acc->description = XtNewString (""); + acc->notes = XtNewString (""); insertAccount( grp, acc ); qifline = xaccReadQIFTransList (fd, acc); @@ -880,6 +896,8 @@ xaccReadQIFData( char *datafile ) acc->type = BANK; acc->accountName = XtNewString ("Quicken Investment Account"); + acc->description = XtNewString (""); + acc->notes = XtNewString (""); insertAccount( grp, acc ); qifline = xaccReadQIFTransList (fd, acc);