Added syntactic sugar for lot pricing: {{$500.00}}

This commit is contained in:
John Wiegley 2012-03-10 02:27:33 -06:00
parent 50f202c4e8
commit 080c1d9a2d
4 changed files with 66 additions and 10 deletions

View file

@ -1065,10 +1065,6 @@ bool amount_t::parse(std::istream& in, const parse_flags_t& flags)
if (! commodity_)
commodity_ = commodity_pool_t::current_pool->create(symbol);
assert(commodity_);
if (details)
commodity_ =
commodity_pool_t::current_pool->find_or_create(*commodity_, details);
}
// Quickly scan through and verify the correctness of the amount's use of
@ -1204,6 +1200,14 @@ bool amount_t::parse(std::istream& in, const parse_flags_t& flags)
if (! flags.has_flags(PARSE_NO_REDUCE))
in_place_reduce(); // will not throw an exception
if (commodity_ && details) {
if (details.has_flags(ANNOTATION_PRICE_NOT_PER_UNIT)) {
assert(details.price);
*details.price /= this->abs();
}
set_commodity(*commodity_pool_t::current_pool->find_or_create(*commodity_, details));
}
VERIFY(valid());
return true;

View file

@ -88,6 +88,12 @@ void annotation_t::parse(std::istream& in)
throw_(amount_error, _("Commodity specifies more than one price"));
in.get(c);
c = static_cast<char>(in.peek());
if (c == '{') {
in.get(c);
add_flags(ANNOTATION_PRICE_NOT_PER_UNIT);
}
c = peek_next_nonws(in);
if (c == '=') {
in.get(c);
@ -95,10 +101,18 @@ void annotation_t::parse(std::istream& in)
}
READ_INTO(in, buf, 255, c, c != '}');
if (c == '}')
if (c == '}') {
in.get(c);
else
throw_(amount_error, _("Commodity price lacks closing brace"));
if (has_flags(ANNOTATION_PRICE_NOT_PER_UNIT)) {
c = static_cast<char>(in.peek());
if (c != '}')
throw_(amount_error, _("Commodity lot price lacks double closing brace"));
else
in.get(c);
}
} else {
throw_(amount_error, _("Commodity lot price lacks closing brace"));
}
amount_t temp;
temp.parse(buf, PARSE_NO_MIGRATE);

View file

@ -55,9 +55,10 @@ struct annotation_t : public supports_flags<>,
{
#define ANNOTATION_PRICE_CALCULATED 0x01
#define ANNOTATION_PRICE_FIXATED 0x02
#define ANNOTATION_DATE_CALCULATED 0x04
#define ANNOTATION_TAG_CALCULATED 0x08
#define ANNOTATION_VALUE_EXPR_CALCULATED 0x10
#define ANNOTATION_PRICE_NOT_PER_UNIT 0x04
#define ANNOTATION_DATE_CALCULATED 0x08
#define ANNOTATION_TAG_CALCULATED 0x10
#define ANNOTATION_VALUE_EXPR_CALCULATED 0x20
optional<amount_t> price;
optional<date_t> date;

View file

@ -0,0 +1,37 @@
2012-03-09 KFC
Expenses:Food 10 CHIK @ $50
Assets:Cash
2012-03-09 KFC
Assets:Cash $75
Expenses:Food -10 CHIK {{$50}} @ $75
Equity:Capital Gains $-25
2012-03-09 KFC
Expenses:Food 10 CHIK
Assets:Cash $-50
2012-03-09 KFC
Assets:Cash $75
Expenses:Food -10 CHIK {{$50}}
Equity:Capital Gains $-25
test print
2012/03/09 KFC
Expenses:Food 10 CHIK @ $50
Assets:Cash
2012/03/09 KFC
Assets:Cash $75
Expenses:Food -10 CHIK {$5} @ $75
Equity:Capital Gains $-25
2012/03/09 KFC
Expenses:Food 10 CHIK
Assets:Cash $-50
2012/03/09 KFC
Assets:Cash $75
Expenses:Food -10 CHIK {$5}
Equity:Capital Gains $-25
end test