From 4ca26fbd73e2619d7de75363b294163a16832982 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 9 Feb 2005 11:50:31 +0000 Subject: *** empty log message *** --- reconcile.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'reconcile.cc') diff --git a/reconcile.cc b/reconcile.cc index 894175c2..5579da21 100644 --- a/reconcile.cc +++ b/reconcile.cc @@ -2,6 +2,27 @@ namespace ledger { +unsigned long called = 0; +bool search_for_balance(const value_t& balance, + const value_t& amount, + transactions_list::iterator beg, + transactions_list::iterator end) +{ + called++; + if (balance == amount) + return true; + + for (transactions_list::iterator i = beg; i != end; ) { + transactions_list::iterator x = i; + (*x)->data = (void *)true; + if (search_for_balance(balance, amount + (*x)->amount, ++i, end)) + return true; + (*x)->data = NULL; + } + + return false; +} + reconcile_results_t reconcile_account(journal_t& journal, account_t& account, const value_t& balance) @@ -53,17 +74,38 @@ reconcile_results_t reconcile_account(journal_t& journal, results.previous_balance = cleared_balance; - value_t to_reconcile = balance - cleared_balance; - // If the amount to reconcile is the same as the pending balance, // then assume an exact match and return the results right away. + value_t to_reconcile = balance - cleared_balance; if (to_reconcile == pending_balance) { results.remaining_balance = 0L; + results.pending_balance = pending_balance; results.pending_xacts = pending_xacts; return results; } - throw error("Could not reconcile account"); + if (search_for_balance(to_reconcile, value_t(), + pending_xacts.begin(), pending_xacts.end())) { + results.remaining_balance = pending_balance - to_reconcile; + results.pending_balance = to_reconcile; + + for (transactions_list::iterator i = pending_xacts.begin(); + i != pending_xacts.end(); + i++) + if ((*i)->data) { + (*i)->data = NULL; + results.pending_xacts.push_back(*i); + } + return results; + } + + // At this point we have an uncleared amount X, and a known desired + // amount of Y. X != Y because not all of the transactions in + // `pending_xacts' are desired, or some are missing, or both. In + // the case that none are missing, we now attempt a permutative + // search to discover which should be removed to yield the amount Y. + + throw error("Could not reconcile account!"); } } // namespace ledger -- cgit v1.2.3