diff options
author | John Wiegley <johnw@newartisans.com> | 2010-03-07 22:53:57 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2010-03-08 01:11:55 -0500 |
commit | 1bf0220f246c5e984d9a43e5d8b3979b9091bc5a (patch) | |
tree | 710291f43415b9aed741a948ee8687316df13bda /src/convert.cc | |
parent | e070cdfc8ddcf9d6a25b593502f1c5ade56c849c (diff) | |
download | fork-ledger-1bf0220f246c5e984d9a43e5d8b3979b9091bc5a.tar.gz fork-ledger-1bf0220f246c5e984d9a43e5d8b3979b9091bc5a.tar.bz2 fork-ledger-1bf0220f246c5e984d9a43e5d8b3979b9091bc5a.zip |
Added experimental account lookup logic
This is used by the "xact" command, and the CSV importer. It is very
slow O(xacts*records).
Diffstat (limited to 'src/convert.cc')
-rw-r--r-- | src/convert.cc | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/src/convert.cc b/src/convert.cc index e47fb6c0..6c02cff3 100644 --- a/src/convert.cc +++ b/src/convert.cc @@ -38,7 +38,8 @@ #include "iterators.h" #include "report.h" #include "xact.h" -#include "output.h" +#include "print.h" +#include "lookup.h" namespace ledger { @@ -54,7 +55,8 @@ value_t convert_command(call_scope_t& scope) else bucket_name = "Equity:Unknown"; - account_t * bucket = journal.master->find_account(bucket_name); + account_t * bucket = journal.master->find_account(bucket_name); + account_t * unknown = journal.master->find_account(_("Expenses:Unknown")); // Make an amounts mapping for the account under consideration @@ -81,14 +83,15 @@ value_t convert_command(call_scope_t& scope) } } + // Create a flat list o + xacts_list current_xacts(journal.xacts_begin(), journal.xacts_end()); + // Read in the series of transactions from the CSV file - format_posts formatter(report, - report.report_format(report.HANDLER(print_format_)), - false); + print_xacts formatter(report); + ifstream data(path(args.get<string>(0))); + csv_reader reader(data); - ifstream data(path(args.get<string>(0))); - csv_reader reader(data); while (xact_t * xact = reader.read_xact(journal, bucket)) { bool matched = false; post_map_t::iterator i = post_map.find(- xact->posts.front()->amount); @@ -111,17 +114,34 @@ value_t convert_command(call_scope_t& scope) DEBUG("convert.csv", "Ignored xact with code: " << *xact->code); delete xact; // ignore it } - else if (! journal.add_xact(xact)) { - delete xact; - throw_(std::runtime_error, - _("Failed to finalize derived transaction (check commodities)")); - } else { - xact_posts_iterator xact_iter(*xact); - while (post_t * post = xact_iter()) - formatter(*post); + if (xact->posts.front()->account == NULL) { + xacts_iterator xi; + xi.xacts_i = current_xacts.begin(); + xi.xacts_end = current_xacts.end(); + xi.xacts_uninitialized = false; + + // jww (2010-03-07): Bind this logic to an option: --auto-match + if (account_t * acct = + lookup_probable_account(xact->payee, xi, bucket).second) + xact->posts.front()->account = acct; + else + xact->posts.front()->account = unknown; + } + + if (! journal.add_xact(xact)) { + delete xact; + throw_(std::runtime_error, + _("Failed to finalize derived transaction (check commodities)")); + } + else { + xact_posts_iterator xact_iter(*xact); + while (post_t * post = xact_iter()) + formatter(*post); + } } } + formatter.flush(); // If not, transform the payee according to regexps |