Report sought magnitude when balancing errors occur

This commit is contained in:
John Wiegley 2009-11-05 17:40:07 -05:00
parent 1bdb9330e5
commit 5e3f3d7f47
8 changed files with 63 additions and 16 deletions

View file

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

View file

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

View file

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

View file

@ -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()) {

View file

@ -761,6 +761,8 @@ public:
}
void in_place_simplify();
value_t number() const;
/**
* Annotated commodity methods.
*/

View file

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

View file

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

View file

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