diff options
author | John Wiegley <johnw@newartisans.com> | 2012-03-06 03:18:10 -0600 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2012-03-06 03:24:06 -0600 |
commit | 97d68ebc8cf2bf88feffaedd6873934dc785c411 (patch) | |
tree | b19e73eb8860dda1bbf8358141ddf1afc2060b50 /src/commodity.cc | |
parent | eb3591f898e194be0cb6c15107e8e41e9dd67206 (diff) | |
download | fork-ledger-97d68ebc8cf2bf88feffaedd6873934dc785c411.tar.gz fork-ledger-97d68ebc8cf2bf88feffaedd6873934dc785c411.tar.bz2 fork-ledger-97d68ebc8cf2bf88feffaedd6873934dc785c411.zip |
Added "value" sub-directive for commodity directive
Diffstat (limited to 'src/commodity.cc')
-rw-r--r-- | src/commodity.cc | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/commodity.cc b/src/commodity.cc index 565204fd..0543c973 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -35,6 +35,7 @@ #include "commodity.h" #include "annotate.h" #include "pool.h" +#include "scope.h" namespace ledger { @@ -85,6 +86,28 @@ void commodity_t::map_prices(function<void(datetime_t, const amount_t&)> fn, } optional<price_point_t> +commodity_t::find_price_from_expr(expr_t& expr, + const optional<commodity_t&>& commodity, + const datetime_t& moment) const +{ +#if defined(DEBUG_ON) + if (SHOW_DEBUG("commodity.price.find")) { + ledger::_log_buffer << "valuation expr: "; + expr.dump(ledger::_log_buffer); + DEBUG("commodity.price.find", ""); + } +#endif + call_scope_t call_args(*scope_t::default_scope); + + call_args.push_back(string_value(base_symbol())); + call_args.push_back(moment); + if (commodity) + call_args.push_back(string_value(commodity->symbol())); + + return price_point_t(moment, expr.calc(call_args).to_amount()); +} + +optional<price_point_t> commodity_t::find_price(const optional<commodity_t&>& commodity, const optional<datetime_t>& moment, const optional<datetime_t>& oldest) const @@ -125,6 +148,9 @@ commodity_t::find_price(const optional<commodity_t&>& commodity, else when = CURRENT_TIME(); + if (base->value_expr) + return find_price_from_expr(*base->value_expr, commodity, when); + optional<price_point_t> point = target ? pool().commodity_price_history.find_price(*this, *target, when, oldest) : |