diff options
-rw-r--r-- | src/balance.h | 7 | ||||
-rw-r--r-- | src/py_balance.cc | 2 | ||||
-rw-r--r-- | src/py_value.cc | 2 | ||||
-rw-r--r-- | src/value.cc | 29 | ||||
-rw-r--r-- | src/value.h | 2 | ||||
-rw-r--r-- | src/xact.cc | 32 | ||||
-rw-r--r-- | src/xact.h | 3 | ||||
-rw-r--r-- | test/regress/2E3496BD.test | 2 |
8 files changed, 63 insertions, 16 deletions
diff --git a/src/balance.h b/src/balance.h index 36a6907a..a4f922a8 100644 --- a/src/balance.h +++ b/src/balance.h @@ -480,6 +480,13 @@ public: optional<amount_t> commodity_amount(const optional<const commodity_t&>& commodity = none) const; + balance_t number() const { + balance_t temp; + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.number(); + return temp; + } + /** * Annotated commodity methods. The amounts contained by a balance * may use annotated commodities. The `strip_annotations' method diff --git a/src/py_balance.cc b/src/py_balance.cc index 60df1317..73049c99 100644 --- a/src/py_balance.cc +++ b/src/py_balance.cc @@ -209,6 +209,8 @@ void export_balance() .def("commodity_amount", py_commodity_amount_0) .def("commodity_amount", py_commodity_amount_1) + .def("number", &balance_t::number) + .def("strip_annotations", &balance_t::strip_annotations) .def("valid", &balance_t::valid) diff --git a/src/py_value.cc b/src/py_value.cc index 6e4afaf7..9653b0e1 100644 --- a/src/py_value.cc +++ b/src/py_value.cc @@ -285,6 +285,8 @@ void export_value() .def("simplified", &value_t::simplified) .def("in_place_simplify", &value_t::in_place_simplify) + .def("number", &value_t::number) + .def("annotate", &value_t::annotate) .def("is_annotated", &value_t::is_annotated) #if 0 diff --git a/src/value.cc b/src/value.cc index 4529300a..e2c9dc8b 100644 --- a/src/value.cc +++ b/src/value.cc @@ -279,6 +279,35 @@ void value_t::in_place_simplify() #endif } +value_t value_t::number() const +{ + switch (type()) { + case VOID: + return 0L; + case BOOLEAN: + return as_boolean() ? 1L : 0L; + case INTEGER: + return as_long(); + case AMOUNT: + return as_amount().number(); + case BALANCE: + return as_balance().number(); + case SEQUENCE: + if (! as_sequence().empty()) { + value_t temp; + foreach (const value_t& value, as_sequence()) + temp += value.number(); + return temp; + } + break; + default: + break; + } + + throw_(value_error, _("Cannot determine numeric value of %1") << label()); + return false; +} + value_t& value_t::operator+=(const value_t& val) { if (is_string()) { diff --git a/src/value.h b/src/value.h index 0993305e..bd681b07 100644 --- a/src/value.h +++ b/src/value.h @@ -761,6 +761,8 @@ public: } void in_place_simplify(); + value_t number() const; + /** * Annotated commodity methods. */ diff --git a/src/xact.cc b/src/xact.cc index 9e5f504e..67dad155 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -83,6 +83,20 @@ void xact_base_t::clear_xdata() post->clear_xdata(); } +value_t xact_base_t::magnitude() const +{ + value_t halfbal = 0L; + foreach (const post_t * post, posts) { + if (post->amount.sign() > 0) { + if (post->cost) + halfbal += *post->cost; + else + halfbal += post->amount; + } + } + return halfbal; +} + bool xact_base_t::finalize() { // Scan through and compute the total balance for the xact. This is used @@ -321,6 +335,8 @@ bool xact_base_t::finalize() add_error_context(item_context(*this, _("While balancing transaction"))); add_error_context(_("Unbalanced remainder is:")); add_error_context(value_context(balance)); + add_error_context(_("Amount to balance against:")); + add_error_context(value_context(magnitude())); throw_(balance_error, _("Transaction does not balance")); } @@ -368,26 +384,12 @@ void xact_t::add_post(post_t * post) xact_base_t::add_post(post); } -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; -} - string xact_t::idstring() const { std::ostringstream buf; buf << format_date(*_date, FMT_WRITTEN); buf << payee; - magnitude().print(buf); + magnitude().number().print(buf); return buf.str(); } @@ -84,6 +84,8 @@ public: return posts.end(); } + value_t magnitude() const; + virtual bool finalize(); void clear_xdata(); @@ -129,7 +131,6 @@ public: virtual void add_post(post_t * post); - value_t magnitude() const; string idstring() const; string id() const; diff --git a/test/regress/2E3496BD.test b/test/regress/2E3496BD.test index f4226bb5..cb105f08 100644 --- a/test/regress/2E3496BD.test +++ b/test/regress/2E3496BD.test @@ -14,5 +14,7 @@ While balancing transaction from "$FILE", lines 3-5: > Account2 -1000 EUR @ 1.5 USD Unbalanced remainder is: 100.00 USD +Amount to balance against: + 1,600.0 USD Error: Transaction does not balance === 1 |