summaryrefslogtreecommitdiff
path: root/src/pool.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/pool.cc')
-rw-r--r--src/pool.cc27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/pool.cc b/src/pool.cc
index b08c8fad..f895a8bc 100644
--- a/src/pool.cc
+++ b/src/pool.cc
@@ -39,6 +39,8 @@
namespace ledger {
+shared_ptr<commodity_pool_t> commodity_pool_t::current_pool;
+
commodity_pool_t::commodity_pool_t()
: default_commodity(NULL), keep_base(false),
quote_leeway(86400), get_quotes(false),
@@ -245,7 +247,7 @@ commodity_pool_t::exchange(const amount_t& amount,
current_annotation = &as_annotated_commodity(commodity).details;
amount_t per_unit_cost =
- (is_per_unit || amount.is_realzero() ? cost : cost / amount).abs();
+ (is_per_unit || amount.is_realzero()) ? cost.abs() : (cost / amount).abs();
DEBUG("commodity.prices.add", "exchange: per-unit-cost = " << per_unit_cost);
@@ -253,7 +255,7 @@ commodity_pool_t::exchange(const amount_t& amount,
exchange(commodity, per_unit_cost, moment ? *moment : CURRENT_TIME());
cost_breakdown_t breakdown;
- breakdown.final_cost = ! is_per_unit ? cost : cost * amount;
+ breakdown.final_cost = ! is_per_unit ? cost : cost * amount.abs();
DEBUG("commodity.prices.add",
"exchange: final-cost = " << breakdown.final_cost);
@@ -284,7 +286,8 @@ commodity_pool_t::exchange(const amount_t& amount,
return breakdown;
}
-optional<price_point_t> commodity_pool_t::parse_price_directive(char * line)
+optional<std::pair<commodity_t *, price_point_t> >
+commodity_pool_t::parse_price_directive(char * line, bool do_not_add_price)
{
char * date_field_ptr = line;
char * time_field_ptr = next_element(date_field_ptr);
@@ -293,6 +296,7 @@ optional<price_point_t> commodity_pool_t::parse_price_directive(char * line)
char * symbol_and_price;
datetime_t datetime;
+ string symbol;
if (std::isdigit(time_field_ptr[0])) {
symbol_and_price = next_element(time_field_ptr);
@@ -305,26 +309,27 @@ optional<price_point_t> commodity_pool_t::parse_price_directive(char * line)
datetime = datetime_t(parse_date(date_field));
}
else {
- symbol_and_price = date_field_ptr;
+ symbol = date_field_ptr;
+ symbol_and_price = time_field_ptr;
datetime = CURRENT_TIME();
}
- string symbol;
- commodity_t::parse_symbol(symbol_and_price, symbol);
+ if (symbol.empty())
+ commodity_t::parse_symbol(symbol_and_price, symbol);
price_point_t point;
point.when = datetime;
- point.price.parse(symbol_and_price);
+ point.price.parse(symbol_and_price, PARSE_NO_MIGRATE);
VERIFY(point.price.valid());
DEBUG("commodity.download", "Looking up symbol: " << symbol);
- if (commodity_t * commodity =
- amount_t::current_pool->find_or_create(symbol)) {
+ if (commodity_t * commodity = find_or_create(symbol)) {
DEBUG("commodity.download", "Adding price for " << symbol << ": "
<< point.when << " " << point.price);
- commodity->add_price(point.when, point.price, true);
+ if (! do_not_add_price)
+ commodity->add_price(point.when, point.price, true);
commodity->add_flags(COMMODITY_KNOWN);
- return point;
+ return std::pair<commodity_t *, price_point_t>(commodity, point);
}
return none;