Added "fixated commodity pricing"

If you put '=' before an annotated commodity's price, it will cause any
future market valuation of that commodity to use that price, and ignore
whatever changes may have happened since in the market price.  This can
be useful if you are tracking gas expenses based on a standard rate
which, although it changes over time, should not adjust the historical
valuation of how much the gas cost at the time it was purchased:

    2009/01/01 Payee
        Expenses:Gas                100 GAL {=$2}
        Liabilities:MasterCard        $-200
This commit is contained in:
John Wiegley 2009-03-02 16:39:26 -04:00
parent c10c01e5a5
commit 1c7de0f9e4
4 changed files with 35 additions and 3 deletions

View file

@ -546,6 +546,10 @@ amount_t::value(const bool primary_only,
if (in_terms_of && commodity() == *in_terms_of) { if (in_terms_of && commodity() == *in_terms_of) {
return *this; return *this;
} }
else if (is_annotated() && annotation().price &&
annotation().has_flags(ANNOTATION_PRICE_FIXATED)) {
return (*annotation().price * number()).rounded();
}
else if (optional<price_point_t> point = else if (optional<price_point_t> point =
commodity().find_price(in_terms_of, moment)) { commodity().find_price(in_terms_of, moment)) {
return (point->price * number()).rounded(); return (point->price * number()).rounded();

View file

@ -638,6 +638,12 @@ void annotation_t::parse(std::istream& in)
throw_(amount_error, _("Commodity specifies more than one price")); throw_(amount_error, _("Commodity specifies more than one price"));
in.get(c); in.get(c);
c = peek_next_nonws(in);
if (c == '=') {
in.get(c);
add_flags(ANNOTATION_PRICE_FIXATED);
}
READ_INTO(in, buf, 255, c, c != '}'); READ_INTO(in, buf, 255, c, c != '}');
if (c == '}') if (c == '}')
in.get(c); in.get(c);
@ -761,7 +767,10 @@ void annotated_commodity_t::write_annotations(std::ostream& out) const
void annotation_t::print(std::ostream& out, bool keep_base) const void annotation_t::print(std::ostream& out, bool keep_base) const
{ {
if (price) if (price)
out << " {" << (keep_base ? *price : price->unreduced()).rounded() << '}'; out << " {"
<< (has_flags(ANNOTATION_PRICE_FIXATED) ? "=" : "")
<< (keep_base ? *price : price->unreduced()).rounded()
<< '}';
if (date) if (date)
out << " [" << format_date(*date, string("%Y/%m/%d")) << ']'; out << " [" << format_date(*date, string("%Y/%m/%d")) << ']';

View file

@ -363,8 +363,9 @@ struct annotation_t : public supports_flags<>,
public equality_comparable<annotation_t> public equality_comparable<annotation_t>
{ {
#define ANNOTATION_PRICE_CALCULATED 0x01 #define ANNOTATION_PRICE_CALCULATED 0x01
#define ANNOTATION_DATE_CALCULATED 0x02 #define ANNOTATION_PRICE_FIXATED 0x02
#define ANNOTATION_TAG_CALCULATED 0x04 #define ANNOTATION_DATE_CALCULATED 0x04
#define ANNOTATION_TAG_CALCULATED 0x08
optional<amount_t> price; optional<amount_t> price;
optional<date_t> date; optional<date_t> date;

View file

@ -0,0 +1,18 @@
reg -V gas
<<<
2009/01/01 Payee
Expenses:Gas 100 GAL {=$2}
Liabilities:MasterCard $-200
2009/01/01 Payee
Expenses:Gas 100 FOO {$2}
Liabilities:MasterCard $-200
P 2009/03/01 12:00:00 GAL $3
P 2009/03/01 12:00:00 FOO $3
>>>1
09-Jan-01 Payee Expenses:Gas $200 $200
09-Jan-01 Payee Expenses:Gas $200 $400
09-Mar-02 Commodities revalued <Revalued> $100 $500
>>>2
=== 0