summaryrefslogtreecommitdiff
path: root/src/amount.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2010-06-07 09:49:17 -0400
committerJohn Wiegley <johnw@newartisans.com>2010-06-07 09:49:17 -0400
commitbe6cef93c479056169ab499d03ea212ff22db435 (patch)
treefba78156e5187bd690f519846427284cdc0b0193 /src/amount.cc
parentae8ab8106218167036ef386159450b56c328f1b9 (diff)
downloadfork-ledger-be6cef93c479056169ab499d03ea212ff22db435.tar.gz
fork-ledger-be6cef93c479056169ab499d03ea212ff22db435.tar.bz2
fork-ledger-be6cef93c479056169ab499d03ea212ff22db435.zip
A further simplification of -V and -X
With -X COMM, all values are computed in terms of COMM, regardless. With -V, only secondary commodities will ever be computed, never primaries. Further, if a secondary commodities has an associated price, the valuation is done in terms of that price's commodity.
Diffstat (limited to 'src/amount.cc')
-rw-r--r--src/amount.cc53
1 files changed, 28 insertions, 25 deletions
diff --git a/src/amount.cc b/src/amount.cc
index ed8f09d1..105b54ef 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -724,8 +724,7 @@ void amount_t::in_place_unreduce()
}
optional<amount_t>
-amount_t::value(const bool primary_only,
- const optional<datetime_t>& moment,
+amount_t::value(const optional<datetime_t>& moment,
const optional<commodity_t&>& in_terms_of) const
{
if (quantity) {
@@ -740,35 +739,39 @@ amount_t::value(const bool primary_only,
"amount_t::value: in_terms_of = " << in_terms_of->symbol());
#endif
if (has_commodity() &&
- (! primary_only || ! commodity().has_flags(COMMODITY_PRIMARY))) {
- if (in_terms_of &&
- commodity().referent() == in_terms_of->referent()) {
+ (in_terms_of || ! commodity().has_flags(COMMODITY_PRIMARY))) {
+ optional<price_point_t> point;
+ optional<commodity_t&> comm(in_terms_of);
+
+ if (comm && commodity().referent() == comm->referent()) {
return *this;
}
- else if (has_annotation() && annotation().price &&
- annotation().has_flags(ANNOTATION_PRICE_FIXATED)) {
- amount_t price(*annotation().price);
+ else if (has_annotation() && annotation().price) {
+ if (annotation().has_flags(ANNOTATION_PRICE_FIXATED)) {
+ point = price_point_t();
+ point->price = *annotation().price;
+ }
+ else if (! in_terms_of) {
+ comm = annotation().price->commodity();
+ }
+ }
+
+ if (! point) {
+ point = commodity().find_price(comm, moment);
+ // Whether a price was found or not, check whether we should attempt
+ // to download a price from the Internet. This is done if (a) no
+ // price was found, or (b) the price is "stale" according to the
+ // setting of --price-exp.
+ if (point)
+ point = commodity().check_for_updated_price(point, moment, comm);
+ }
+
+ if (point) {
+ amount_t price(point->price);
price.multiply(*this, true);
price.in_place_round();
return price;
}
- else {
- optional<price_point_t> point =
- commodity().find_price(in_terms_of, moment);
-
- // Whether a price was found or not, check whether we should
- // attempt to download a price from the Internet. This is done
- // if (a) no price was found, or (b) the price is "stale"
- // according to the setting of --price-exp.
- point = commodity().check_for_updated_price(point, moment,
- in_terms_of);
- if (point) {
- amount_t price(point->price);
- price.multiply(*this, true);
- price.in_place_round();
- return price;
- }
- }
}
} else {
throw_(amount_error,