diff options
Diffstat (limited to 'src/xact.cc')
-rw-r--r-- | src/xact.cc | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/src/xact.cc b/src/xact.cc index 175a1467..6ea8d8f9 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -65,7 +65,7 @@ void xact_base_t::add_post(post_t * post) bool xact_base_t::remove_post(post_t * post) { posts.remove(post); - post->xact = NULL; + post->xact = NULL; return true; } @@ -163,11 +163,14 @@ bool xact_base_t::finalize() null_post->amount = balance.as_amount().negated(); null_post->add_flags(POST_CALCULATED); } + else if (balance.is_long()) { + null_post->amount = amount_t(- balance.as_long()); + null_post->add_flags(POST_CALCULATED); + } else if (! balance.is_null() && ! balance.is_realzero()) { throw_(balance_error, _("Transaction does not balance")); } balance = NULL_VALUE; - } else if (balance.is_balance() && balance.as_balance().amounts.size() == 2) { @@ -314,6 +317,7 @@ bool xact_base_t::finalize() if (dynamic_cast<xact_t *>(this)) { bool all_null = true; bool some_null = false; + foreach (post_t * post, posts) { if (! post->amount.is_null()) { all_null = false; @@ -327,6 +331,7 @@ bool xact_base_t::finalize() post->xdata().add_flags(POST_EXT_VISITED); post->account->xdata().add_flags(ACCOUNT_EXT_VISITED); } + if (all_null) return false; // ignore this xact completely else if (some_null) @@ -351,32 +356,48 @@ void xact_t::add_post(post_t * post) xact_base_t::add_post(post); } -namespace { - value_t get_magnitude(xact_t& xact) { - balance_t halfbal; - foreach (post_t * post, xact.posts) - if (post->amount.sign() > 0) +value_t xact_t::magnitude() const +{ + value_t halfbal = 0L; + foreach (const post_t * post, posts) { + if (post->amount.sign() > 0) { + if (post->cost) + halfbal += post->cost->number(); + else halfbal += post->amount.number(); - return halfbal; + } } + return halfbal; +} - value_t get_idstring(xact_t& xact) { - std::ostringstream buf; - buf << *xact._date; - buf << xact.payee; +string xact_t::idstring() const +{ + std::ostringstream buf; + buf << format_date(*_date, FMT_WRITTEN); + buf << payee; + magnitude().print(buf); + return buf.str(); +} - get_magnitude(xact).print(buf); +string xact_t::id() const +{ + SHA1 sha; + sha.Reset(); + sha << idstring().c_str(); + uint_least32_t message_digest[5]; + sha.Result(message_digest); + return to_hex(message_digest, 5); +} - return string_value(buf.str()); +namespace { + value_t get_magnitude(xact_t& xact) { + return xact.magnitude(); + } + value_t get_idstring(xact_t& xact) { + return string_value(xact.idstring()); } value_t get_id(xact_t& xact) { - SHA1 sha; - sha.Reset(); - sha << get_idstring(xact).as_string().c_str(); - - uint_least32_t message_digest[5]; - sha.Result(message_digest); - return string_value(to_hex(message_digest)); + return string_value(xact.id()); } value_t get_code(xact_t& xact) { |