summaryrefslogtreecommitdiff
path: root/reconcile.cc
diff options
context:
space:
mode:
Diffstat (limited to 'reconcile.cc')
-rw-r--r--reconcile.cc48
1 files changed, 45 insertions, 3 deletions
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