From 288a062c13470c8e9ff5da22b0a26944c02c66e6 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sun, 21 Sep 2003 17:44:29 +0000 Subject: [PATCH] restructure lot code, part one git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@9388 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/Scrub2.c | 39 -------------------------------- src/engine/Scrub2.h | 17 -------------- src/engine/Scrub3.c | 54 ++++++++++++++++++++++++++++++++++++++++++++- src/engine/Scrub3.h | 53 ++++++++++++++++++++++++++++++++------------ 4 files changed, 92 insertions(+), 71 deletions(-) diff --git a/src/engine/Scrub2.c b/src/engine/Scrub2.c index 1015c8da17..bb604252d6 100644 --- a/src/engine/Scrub2.c +++ b/src/engine/Scrub2.c @@ -221,43 +221,4 @@ xaccLotScrubDoubleBalance (GNCLot *lot) LEAVE ("lot=%s", kvp_frame_get_string (gnc_lot_get_slots (lot), "/title")); } -/* ============================================================== */ - -static gpointer -lot_scrub_cb (Account *acc, gpointer data) -{ - if (FALSE == xaccAccountHasTrades (acc)) return NULL; - xaccAccountAssignLots (acc); - xaccAccountScrubDoubleBalance (acc); - return NULL; -} - -void -xaccGroupScrubLotsBalance (AccountGroup *grp) -{ - if (!grp) return; - xaccGroupForEachAccount (grp, lot_scrub_cb, NULL, TRUE); -} - -void -xaccAccountScrubLotsBalance (Account *acc) -{ - if (!acc) return; - if (FALSE == xaccAccountHasTrades (acc)) return; - xaccAccountAssignLots (acc); - xaccAccountScrubDoubleBalance (acc); -} - -void -xaccAccountTreeScrubLotsBalance (Account *acc) -{ - if (!acc) return; - - xaccGroupScrubLotsBalance (acc->children); - - if (FALSE == xaccAccountHasTrades (acc)) return; - xaccAccountAssignLots (acc); - xaccAccountScrubDoubleBalance (acc); -} - /* =========================== END OF FILE ======================= */ diff --git a/src/engine/Scrub2.h b/src/engine/Scrub2.h index 339abbb4ed..5e180facdf 100644 --- a/src/engine/Scrub2.h +++ b/src/engine/Scrub2.h @@ -38,23 +38,6 @@ #include "gnc-engine.h" -/** The xaccGroupScrubLotsBalance() routine walks the - * account tree, and invokes xaccAccountScrubLots() - * and xaccAccountScrubDoubleBalance() on all accounts - * that are trading accounts. - * The xaccAccountTreeScrubLotsBalance() does the same. - * The xaccAccountScrubLotsBalance() will do the same, - * except that it won't descend down to the account - * children. - * - * Most GUI routines will want to use one of these - * xacc[*]ScrubLotsBalance() routines, instead of the - * component ScrubLots() and ScrubDoubleBalance() routines, - * since it usually makes sense to call these together. - */ -void xaccGroupScrubLotsBalance (AccountGroup *grp); -void xaccAccountScrubLotsBalance (Account *acc); -void xaccAccountTreeScrubLotsBalance (Account *acc); /** The xaccAccountAssignLots() routine will walk over all of * the splits in an account, and make sure that each belongs diff --git a/src/engine/Scrub3.c b/src/engine/Scrub3.c index cb1ff30160..39cd8b22d3 100644 --- a/src/engine/Scrub3.c +++ b/src/engine/Scrub3.c @@ -139,7 +139,11 @@ remove_guids (Split *sa, Split *sb) "peer_guid", &sb->guid); if (!ksub) { - PERR ("merging splits that didn't have correct gemini values!"); + PERR ("merging splits that didn't have correct gemini values!\n" + "looking for guid=%s\n" + "bag held: %s", + guid_to_string (&sb->guid), + kvp_frame_to_string (sa->kvp_data)); return; } gnc_kvp_bag_remove_frame (sa->kvp_data, "lot-split", ksub); @@ -333,4 +337,52 @@ rethin: return splits_deleted; } +/* ============================================================== */ + +void +xaccAccountScrubLots (Account *acc) +{ + LotList *node; + if (!acc) return; + if (FALSE == xaccAccountHasTrades (acc)) return; + + ENTER ("acc=%s", acc->accountName); + xaccAccountBeginEdit(acc); + xaccAccountAssignLots (acc); + + for (node = acc->lots; node; node=node->next) + { + GNCLot *lot = node->data; + xaccScrubLot (lot); + } + xaccAccountCommitEdit(acc); + LEAVE ("acc=%s", acc->accountName); +} + +/* ============================================================== */ + +static gpointer +lot_scrub_cb (Account *acc, gpointer data) +{ + if (FALSE == xaccAccountHasTrades (acc)) return NULL; + xaccAccountScrubLots (acc); + return NULL; +} + +void +xaccGroupScrubLots (AccountGroup *grp) +{ + if (!grp) return; + xaccGroupForEachAccount (grp, lot_scrub_cb, NULL, TRUE); +} + +void +xaccAccountTreeScrubLots (Account *acc) +{ + if (!acc) return; + + xaccGroupScrubLots (acc->children); + xaccAccountScrubLots (acc); +} + /* ========================== END OF FILE ========================= */ diff --git a/src/engine/Scrub3.h b/src/engine/Scrub3.h index 54e2e60350..7ac5578d7b 100644 --- a/src/engine/Scrub3.h +++ b/src/engine/Scrub3.h @@ -29,12 +29,51 @@ * Provides a set of functions and utilities for checking and * repairing ('scrubbing clean') the usage of Cap Gains * transactions in stock and commodity accounts. + * + * NOTE: Unless you have special needs, the functions you are looking + * for and almost certainly want to use are either xaccScrubLot() or + * xaccAccountScrubLots(). */ #ifndef XACC_SCRUB3_H #define XACC_SCRUB3_H #include "gnc-engine.h" +/** The xaccScrubLot() routine makes sure that the indicated lot is + * self-consistent and properly balanced, and fixes it if its not. + * This is an important routine to call if the amount of any split + * in the lot is changed. That's because (obviously) changing + * split values is gaurenteed to throw off lot balances. + * This routine may end up closing the lot, or at least trying + * to. It will also cause cap gains to be recomputed. + * + * Scrubbing the lot may cause subsplits to be merged together, + * i.e. for splits to be deleted. This routine returns true if + * any splits were deleted. + */ +gboolean xaccScrubLot (GNCLot *lot); + +/** The xaccAccountScrubLots() routine makes sure that every split + * in the account is assigned to a lot, and that then, every + * lot is self-consistent (by calling xaccScrubLot() on each lot). + * + * This routine is the primary routine for ensuring that the + * lot structure, and the cap-gains for an account are in good + * order. + * + * The xaccGroupScrubLots() routine walks the account tree, and invokes + * xaccAccountScrubLots() on all accounts that are trading accounts. + * The xaccAccountTreeScrubLots() does the same. + * + * Most GUI routines will want to use one of these xacc[*]ScrubLots() + * routines, instead of the various component routines, since it will + * usually makes sense to work only with these high-level routines. + */ +void xaccAccountScrubLots (Account *acc); +void xaccGroupScrubLots (AccountGroup *grp); +void xaccAccountTreeScrubLots (Account *acc); + + /** If a split has been pulled apart to make it fit into two (or more) * lots, then it becomes theoretically possible for each subsplit to * have a distinct price. But this would be wrong: each subsplit should @@ -71,19 +110,5 @@ gboolean xaccScrubMergeSubSplits (Split *split); gboolean xaccScrubMergeTransSubSplits (Transaction *txn); gboolean xaccScrubMergeLotSubSplits (GNCLot *lot); -/** The xaccScrubLot() routine makes sure that the indicated lot is - * self-consistent and properly balanced, and fixes it if its not. - * This is an important routine to call if the amount of any split - * in the lot is changed. That's because (obviously) changing - * split values is gaurenteed to throw off lot balances. - * This routine may end up closing the lot, or at least trying - * to. It will also cause cap gains to be recomputed. - * - * Scrubbing the lot may cause subsplits to be merged together, - * i.e. for splits to be deleted. This routine returns true if - * any splits were deleted. - */ -gboolean xaccScrubLot (GNCLot *lot); - #endif /* XACC_SCRUB3_H */ /** @} */