amount_t -> balance_t.

This commit is contained in:
Pascal Fleury 2018-07-17 21:59:01 +02:00
parent e2bad41fe6
commit 3186ff514a

View file

@ -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);