diff options
author | John Wiegley <johnw@newartisans.com> | 2009-06-26 17:09:34 +0100 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-06-26 17:15:35 +0100 |
commit | 2ef1934bb0dbcd08dfe8d4dc68bfd1509c8f51ce (patch) | |
tree | 5225fc7f164feed6a0af36f552b9fa713a3d8840 /src | |
parent | 4574c30fcfd3b03bab9a368a6ff2928af7e11e28 (diff) | |
download | fork-ledger-2ef1934bb0dbcd08dfe8d4dc68bfd1509c8f51ce.tar.gz fork-ledger-2ef1934bb0dbcd08dfe8d4dc68bfd1509c8f51ce.tar.bz2 fork-ledger-2ef1934bb0dbcd08dfe8d4dc68bfd1509c8f51ce.zip |
Restored --price option, added baseline test
This option reports only in terms of the annotated price of the
commodities involved, otherwise it reports the amounts themselves. It
can be used in conjunction with other reports, as it applies to the
displayed amounts, not the actual amounts being calculated.
Diffstat (limited to 'src')
-rw-r--r-- | src/amount.cc | 16 | ||||
-rw-r--r-- | src/amount.h | 2 | ||||
-rw-r--r-- | src/balance.cc | 10 | ||||
-rw-r--r-- | src/balance.h | 2 | ||||
-rw-r--r-- | src/report.cc | 16 | ||||
-rw-r--r-- | src/report.h | 7 | ||||
-rw-r--r-- | src/value.cc | 12 | ||||
-rw-r--r-- | src/value.h | 2 |
8 files changed, 60 insertions, 7 deletions
diff --git a/src/amount.cc b/src/amount.cc index 0b798686..2434f110 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -576,6 +576,17 @@ amount_t::value(const bool primary_only, return none; } +amount_t amount_t::price() const +{ + if (is_annotated() && annotation().price) { + amount_t temp(*annotation().price); + temp *= *this; + DEBUG("amount.price", "Returning price of " << *this << " = " << temp); + return temp; + } + return *this; +} + int amount_t::sign() const { @@ -784,8 +795,9 @@ bool amount_t::is_annotated() const throw_(amount_error, _("Cannot determine if an uninitialized amount's commodity is annotated")); - assert(! commodity().annotated || as_annotated_commodity(commodity()).details); - return commodity().annotated; + assert(! has_commodity() || ! commodity().annotated || + as_annotated_commodity(commodity()).details); + return has_commodity() && commodity().annotated; } annotation_t& amount_t::annotation() diff --git a/src/amount.h b/src/amount.h index 990b326f..b3c632af 100644 --- a/src/amount.h +++ b/src/amount.h @@ -384,6 +384,8 @@ public: const optional<datetime_t>& moment = none, const optional<commodity_t&>& in_terms_of = none) const; + amount_t price() const; + /*@}*/ /** @name Truth tests diff --git a/src/balance.cc b/src/balance.cc index 07657287..274f860a 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -203,6 +203,16 @@ balance_t::value(const bool primary_only, return resolved ? temp : optional<balance_t>(); } +balance_t balance_t::price() const +{ + balance_t temp; + + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.price(); + + return temp; +} + optional<amount_t> balance_t::commodity_amount(const optional<const commodity_t&>& commodity) const { diff --git a/src/balance.h b/src/balance.h index 142621f8..765c46dd 100644 --- a/src/balance.h +++ b/src/balance.h @@ -367,6 +367,8 @@ public: const optional<datetime_t>& moment = none, const optional<commodity_t&>& in_terms_of = none) const; + balance_t price() const; + /** * Truth tests. An balance may be truth test in two ways: * diff --git a/src/report.cc b/src/report.cc index 3e38aef2..4c8a40e6 100644 --- a/src/report.cc +++ b/src/report.cc @@ -246,16 +246,18 @@ value_t report_t::fn_quoted(call_scope_t& args) return string_value(out.str()); } -value_t report_t::fn_join(call_scope_t& args) +value_t report_t::fn_join(call_scope_t& scope) { + interactive_t args(scope, "s"); + std::ostringstream out; - foreach (const char ch, args[0].to_string()) + foreach (const char ch, args.get<string>(0)) { if (ch != '\n') out << ch; else out << "\\n"; - + } return string_value(out.str()); } @@ -299,6 +301,12 @@ value_t report_t::fn_percent(call_scope_t& scope) (args.get<amount_t>(0) / args.get<amount_t>(1)).number()); } +value_t report_t::fn_price(call_scope_t& scope) +{ + interactive_t args(scope, "v"); + return args.value_at(0).price(); +} + namespace { value_t fn_black(call_scope_t&) { return string_value("black"); @@ -846,6 +854,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name) return WRAP_FUNCTOR(fn_false); else if (is_eq(p, "percent")) return MAKE_FUNCTOR(report_t::fn_percent); + else if (is_eq(p, "price")) + return MAKE_FUNCTOR(report_t::fn_price); break; case 'q': diff --git a/src/report.h b/src/report.h index 6f442b5d..71dc4ca2 100644 --- a/src/report.h +++ b/src/report.h @@ -158,6 +158,7 @@ public: value_t fn_format_date(call_scope_t& scope); value_t fn_ansify_if(call_scope_t& scope); value_t fn_percent(call_scope_t& scope); + value_t fn_price(call_scope_t& scope); value_t fn_now(call_scope_t&) { return terminus; @@ -613,8 +614,10 @@ public: }); OPTION_(report_t, price, DO() { // -I - parent->HANDLER(revalued).off(); - parent->HANDLER(amount_).set_expr(string("--price"), "price"); + parent->HANDLER(display_amount_) + .set_expr(string("--price"), "price(amount_expr)"); + parent->HANDLER(display_total_) + .set_expr(string("--price"), "price(total_expr)"); }); OPTION__(report_t, prices_format_, CTOR(report_t, prices_format_) { diff --git a/src/value.cc b/src/value.cc index 54d4bfc2..b19ad956 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1230,6 +1230,18 @@ value_t value_t::value(const bool primary_only, return NULL_VALUE; } +value_t value_t::price() const +{ + switch (type()) { + case AMOUNT: + return as_amount().price(); + case BALANCE: + return as_balance().price(); + default: + return *this; + } +} + value_t value_t::exchange_commodities(const std::string& commodities, const bool add_prices, const optional<datetime_t>& moment) diff --git a/src/value.h b/src/value.h index 31ed1d58..62943e62 100644 --- a/src/value.h +++ b/src/value.h @@ -449,6 +449,8 @@ public: const optional<datetime_t>& moment = none, const optional<commodity_t&>& in_terms_of = none) const; + value_t price() const; + value_t exchange_commodities(const std::string& commodities, const bool add_prices = false, const optional<datetime_t>& moment = none); |