diff options
author | Pascal Fleury <pascal@telefleuries.com> | 2018-07-17 21:59:01 +0200 |
---|---|---|
committer | Pascal Fleury <pascal@telefleuries.com> | 2018-07-17 21:59:01 +0200 |
commit | 3186ff514a19851266b5be63773a54086e2ae2d9 (patch) | |
tree | 3386d01afa399a99eafa1a45b98c6805d1eab0c9 /src/textual.cc | |
parent | e2bad41fe61c934556cf5bf6550da39d7b6a3817 (diff) | |
download | fork-ledger-3186ff514a19851266b5be63773a54086e2ae2d9.tar.gz fork-ledger-3186ff514a19851266b5be63773a54086e2ae2d9.tar.bz2 fork-ledger-3186ff514a19851266b5be63773a54086e2ae2d9.zip |
amount_t -> balance_t.
Diffstat (limited to 'src/textual.cc')
-rw-r--r-- | src/textual.cc | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/src/textual.cc b/src/textual.cc index 8fbc5c08..5d7706ec 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -1644,9 +1644,9 @@ post_t * instance_t::parse_post(char * line, } DEBUG("textual.parse", "line " << context.linenum << ": " - << "POST assign: parsed amt = " << *post->assigned_amount); + << "POST assign: parsed balance amount = " << *post->assigned_amount); - amount_t& amt(*post->assigned_amount); + const amount_t& amt(*post->assigned_amount); value_t account_total (post->account->amount().strip_annotations(keep_details_t())); @@ -1655,18 +1655,19 @@ post_t * instance_t::parse_post(char * line, DEBUG("post.assign", "line " << context.linenum << ": " << "post amount = " << amt); - amount_t diff = amt; + balance_t diff = amt; switch (account_total.type()) { case value_t::AMOUNT: - if (account_total.as_amount().commodity_ptr() == diff.commodity_ptr()) - diff -= account_total.as_amount(); + diff -= account_total.as_amount(); + DEBUG("textual.parse", "line " << context.linenum << ": " + << "Subtracting amount " << account_total.as_amount() << " from diff, yielding " << diff); break; case value_t::BALANCE: - if (optional<amount_t> comm_bal = - account_total.as_balance().commodity_amount(amt.commodity())) - diff -= *comm_bal; + diff -= account_total.as_balance(); + DEBUG("textual.parse", "line " << context.linenum << ": " + << "Subtracting balance " << account_total.as_balance() << " from diff, yielding " << diff); break; default: @@ -1680,26 +1681,42 @@ post_t * instance_t::parse_post(char * line, // Subtract amounts from previous posts to this account in the xact. for (post_t* p : xact->posts) { - if (p->account == post->account && - p->amount.commodity_ptr() == diff.commodity_ptr()) { + if (p->account == post->account) { diff -= p->amount; DEBUG("textual.parse", "line " << context.linenum << ": " - << "Subtract " << p->amount << ", diff = " << diff); + << "Subtracting " << p->amount << ", diff = " << diff); } } if (post->amount.is_null()) { // balance assignment if (! diff.is_zero()) { - post->amount = diff; + // This will fail if there are more than 1 commodity in diff, which is wanted, + // as amount cannot store more than 1 commodity. + post->amount = diff.to_amount(); DEBUG("textual.parse", "line " << context.linenum << ": " << "Overwrite null posting"); } } else { // balance assertion diff -= post->amount; + // If amt has a commodity, restrict balancing to that. Otherwise, it's the blanket '0' and + // check that all of them are zero. + if (amt.has_commodity()) { + DEBUG("textual.parse", "line " << context.linenum << ": " + << "Finding commodity " << amt.commodity() << " (" << amt << ") in balance " << diff); + optional<amount_t> wanted_commodity = diff.commodity_amount(amt.commodity()); + if (!wanted_commodity) { + diff = amt - amt; // this is '0' with the correct commodity. + } else { + diff = *wanted_commodity; + } + DEBUG("textual.parse", "line " << context.linenum << ": " + << "Diff is now " << diff); + } if (! no_assertions && ! diff.is_zero()) { - amount_t tot = amt - diff; + balance_t tot = -diff + amt; + DEBUG("textual.parse", "Balance assertion: off by " << diff << " (expected to see " << tot << ")"); throw_(parse_error, _f("Balance assertion off by %1% (expected to see %2%)") % diff % tot); |