summaryrefslogtreecommitdiff
path: root/src/xact.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-27 03:28:14 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-27 03:45:32 -0400
commitf1795e628b0027aed2dc344c757167887c337bc1 (patch)
tree4fea3644081c427daf247f34327510834ce28d5e /src/xact.cc
parent501949a364d3a26418bd59957be4015207312191 (diff)
downloadfork-ledger-f1795e628b0027aed2dc344c757167887c337bc1.tar.gz
fork-ledger-f1795e628b0027aed2dc344c757167887c337bc1.tar.bz2
fork-ledger-f1795e628b0027aed2dc344c757167887c337bc1.zip
Fixed bugs relating to sign and rounding of costs
Diffstat (limited to 'src/xact.cc')
-rw-r--r--src/xact.cc20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/xact.cc b/src/xact.cc
index 52ac68a6..e9d22da1 100644
--- a/src/xact.cc
+++ b/src/xact.cc
@@ -78,15 +78,16 @@ bool xact_base_t::finalize()
foreach (post_t * post, posts) {
if (! post->must_balance())
continue;
-
+
amount_t& p(post->cost ? *post->cost : post->amount);
- DEBUG("xact.finalize", "post must balance = " << p.reduced());
if (! p.is_null()) {
+ DEBUG("xact.finalize", "post must balance = " << p.reduced());
if (! post->cost && post->amount.is_annotated() &&
post->amount.annotation().price) {
// If the amount has no cost, but is annotated with a per-unit
// price, use the price times the amount as the cost
- post->cost = *post->amount.annotation().price * post->amount;
+ post->cost = (*post->amount.annotation().price *
+ post->amount).unrounded();
DEBUG("xact.finalize",
"annotation price = " << *post->amount.annotation().price);
DEBUG("xact.finalize", "amount = " << post->amount);
@@ -145,10 +146,11 @@ bool xact_base_t::finalize()
foreach (const balance_t::amounts_map::value_type& pair, bal.amounts) {
if (first) {
null_post->amount = pair.second.negated();
+ null_post->add_flags(POST_CALCULATED);
first = false;
} else {
post_t * p = new post_t(null_post->account, pair.second.negated(),
- ITEM_GENERATED);
+ ITEM_GENERATED | POST_CALCULATED);
p->set_state(null_post->state());
add_post(p);
}
@@ -228,7 +230,7 @@ bool xact_base_t::finalize()
DEBUG("xact.finalize", "total_cost = " << total_cost);
}
}
- per_unit_cost = (*y / *x).abs();
+ per_unit_cost = (*y / *x).abs().unrounded();
DEBUG("xact.finalize", "per_unit_cost = " << per_unit_cost);
@@ -254,7 +256,9 @@ bool xact_base_t::finalize()
DEBUG("xact.finalize", "resolved balance = " << balance);
- foreach (post_t * post, posts) {
+ posts_list copy(posts);
+
+ foreach (post_t * post, copy) {
if (! post->cost)
continue;
@@ -281,7 +285,7 @@ bool xact_base_t::finalize()
else
account = journal->find_account(_("Equity:Capital Losses"));
- post_t * p = new post_t(account, gain_loss.rounded(), ITEM_GENERATED);
+ post_t * p = new post_t(account, gain_loss, ITEM_GENERATED);
p->set_state(post->state());
add_post(p);
DEBUG("xact.finalize", "added gain_loss, balance = " << balance);
@@ -328,6 +332,8 @@ bool xact_base_t::finalize()
_("There cannot be null amounts after balancing a transaction"));
}
+ VERIFY(valid());
+
return true;
}