summaryrefslogtreecommitdiff
path: root/reports.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2003-10-11 22:59:43 +0000
committerJohn Wiegley <johnw@newartisans.com>2003-10-11 22:59:43 +0000
commit854b719f1ec704a7a84bee629bc759b25d19ca6f (patch)
tree14602d9d63c0ab5bb944f1e82ed84e4ac884f8b6 /reports.cc
parent7ebe72c054575af9771644c8164ef7be7e52e388 (diff)
downloadfork-ledger-854b719f1ec704a7a84bee629bc759b25d19ca6f.tar.gz
fork-ledger-854b719f1ec704a7a84bee629bc759b25d19ca6f.tar.bz2
fork-ledger-854b719f1ec704a7a84bee629bc759b25d19ca6f.zip
c
Diffstat (limited to 'reports.cc')
-rw-r--r--reports.cc119
1 files changed, 117 insertions, 2 deletions
diff --git a/reports.cc b/reports.cc
index b050d33f..ad53e270 100644
--- a/reports.cc
+++ b/reports.cc
@@ -535,8 +535,9 @@ int main(int argc, char * argv[])
// Compile the list of specified regular expressions, which can be
// specified after the command, or using the '-i FILE' option
- for (; index < argc; index++)
- regexps.push_back(mask(argv[index]));
+ if (command != "add")
+ for (; index < argc; index++)
+ regexps.push_back(mask(argv[index]));
// Parse the ledger
@@ -590,6 +591,120 @@ int main(int argc, char * argv[])
else if (command == "equity") {
equity_ledger(std::cout, regexps);
}
+ else if (command == "add") {
+ entry added, * matching = NULL;
+
+ if (! parse_date(argv[index++], &added.date)) {
+ std::cerr << "Error: Bad add date: " << argv[index - 1]
+ << std::endl;
+ return 1;
+ }
+
+ added.cleared = show_cleared;
+
+ if (index == argc) {
+ std::cerr << "Error: Too few arguments to 'add'." << std::endl;
+ return 1;
+ }
+
+ regexps.clear();
+ regexps.push_back(mask(argv[index++]));
+
+ for (entries_list_reverse_iterator i = main_ledger->entries.rbegin();
+ i != main_ledger->entries.rend();
+ i++) {
+ if ((*i)->matches(regexps)) {
+ matching = *i;
+ break;
+ }
+ }
+
+ added.desc = matching ? matching->desc : regexps.front().pattern;
+
+ if (index == argc) {
+ std::cerr << "Error: Too few arguments to 'add'." << std::endl;
+ return 1;
+ }
+
+ if (argv[index][0] == '-' || std::isdigit(argv[index][0])) {
+ if (! matching) {
+ std::cerr << "Error: Missing account name for non-matching entry."
+ << std::endl;
+ return 1;
+ }
+
+ transaction * m_xact, * xact, * first;
+
+ m_xact = matching->xacts.front();
+
+ first = xact = new transaction();
+ xact->acct = m_xact->acct;
+ xact->cost = create_amount(argv[index++]);
+ xact->cost->set_commdty(m_xact->cost->commdty());
+
+ added.xacts.push_back(xact);
+
+ m_xact = matching->xacts.back();
+
+ xact = new transaction();
+ xact->acct = m_xact->acct;
+ xact->cost = first->cost->copy();
+ xact->cost->negate();
+
+ added.xacts.push_back(xact);
+
+ if ((index + 1) < argc && std::string(argv[index]) == "-from")
+ if (account * acct = main_ledger->re_find_account(argv[++index]))
+ added.xacts.back()->acct = acct;
+ } else {
+ while (index < argc && std::string(argv[index]) != "-from") {
+ transaction * xact = new transaction();
+
+ mask acct_regex(argv[index++]);
+
+ account * acct = NULL;
+ for (std::list<transaction *>::iterator x = matching->xacts.begin();
+ x != matching->xacts.end();
+ x++) {
+ if (acct_regex.match((*x)->acct->as_str())) {
+ acct = (*x)->acct;
+ break;
+ }
+ }
+
+ if (acct)
+ xact->acct = acct;
+ else
+ xact->acct = main_ledger->re_find_account(acct_regex.pattern);
+
+ if (! xact->acct) {
+ std::cerr << "Error: Could not find account name '"
+ << acct_regex.pattern << "'." << std::endl;
+ return 1;
+ }
+
+ if (index == argc) {
+ std::cerr << "Error: Too few arguments to 'add'." << std::endl;
+ return 1;
+ }
+
+ xact->cost = create_amount(argv[index++]);
+ added.xacts.push_back(xact);
+ }
+
+ if ((index + 1) < argc && std::string(argv[index]) == "-from")
+ if (account * acct = main_ledger->re_find_account(argv[++index])) {
+ transaction * xact = new transaction();
+ xact->acct = acct;
+ xact->cost = NULL;
+
+ added.xacts.push_back(xact);
+ }
+ }
+
+ if (added.finalize())
+ added.print(std::cout);
+ }
#ifdef DEBUG
// Ordinarily, deleting the main ledger isn't necessary, since the