Added --date, similar to --account and --payee

This lets you do things like store a date as the value of a tag, then
run:

  ledger --date='has_tag("Foo") ? to_date(tag("Foo")) : date' reg
This commit is contained in:
John Wiegley 2009-11-11 21:32:18 -05:00
parent c22b8457ef
commit 67c9cf134d
7 changed files with 31 additions and 5 deletions

View file

@ -159,6 +159,11 @@ post_handler_ptr chain_post_handlers(report_t& report,
handler.reset(new sort_posts(handler, "date")); handler.reset(new sort_posts(handler, "date"));
} }
if (report.HANDLED(date_))
handler.reset(new transfer_details(handler, transfer_details::SET_DATE,
report.session.journal->master,
report.HANDLER(date_).str(),
report));
if (report.HANDLED(account_)) if (report.HANDLED(account_))
handler.reset(new transfer_details(handler, transfer_details::SET_ACCOUNT, handler.reset(new transfer_details(handler, transfer_details::SET_ACCOUNT,
report.session.journal->master, report.session.journal->master,

View file

@ -706,22 +706,27 @@ void transfer_details::operator()(post_t& post)
temp.set_state(post.state()); temp.set_state(post.state());
bind_scope_t bound_scope(scope, temp); bind_scope_t bound_scope(scope, temp);
value_t substitute(expr.calc(bound_scope));
switch (which_element) { switch (which_element) {
case SET_PAYEE: case SET_DATE:
xact.payee = expr.calc(bound_scope).to_string(); xact.set_date(substitute.to_date());
break; break;
case SET_ACCOUNT: { case SET_ACCOUNT: {
std::list<string> account_names; std::list<string> account_names;
temp.account->remove_post(&temp); temp.account->remove_post(&temp);
split_string(expr.calc(bound_scope).to_string(), ':', account_names); split_string(substitute.to_string(), ':', account_names);
temp.account = create_temp_account_from_path(account_names, temps, temp.account = create_temp_account_from_path(account_names, temps,
xact.journal->master); xact.journal->master);
temp.account->add_post(&temp); temp.account->add_post(&temp);
break; break;
} }
case SET_PAYEE:
xact.payee = substitute.to_string();
break;
default: default:
assert(false); assert(false);
break; break;

View file

@ -550,8 +550,9 @@ class transfer_details : public item_handler<post_t>
public: public:
enum element_t { enum element_t {
SET_PAYEE, SET_DATE,
SET_ACCOUNT SET_ACCOUNT,
SET_PAYEE
} which_element; } which_element;
transfer_details(post_handler_ptr handler, transfer_details(post_handler_ptr handler,

View file

@ -172,6 +172,12 @@ public:
virtual optional<date_t> effective_date() const { virtual optional<date_t> effective_date() const {
return _date_eff; return _date_eff;
} }
virtual void set_date(const date_t& date) {
if (use_effective_date)
_date_eff = date;
else
_date = date;
}
void set_state(state_t new_state) { void set_state(state_t new_state) {
_state = new_state; _state = new_state;

View file

@ -561,6 +561,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
break; break;
case 'd': case 'd':
OPT(daily); OPT(daily);
else OPT(date_);
else OPT(date_format_); else OPT(date_format_);
else OPT(datetime_format_); else OPT(datetime_format_);
else OPT(depth_); else OPT(depth_);

View file

@ -217,6 +217,7 @@ public:
HANDLER(csv_format_).report(out); HANDLER(csv_format_).report(out);
HANDLER(current).report(out); HANDLER(current).report(out);
HANDLER(daily).report(out); HANDLER(daily).report(out);
HANDLER(date_).report(out);
HANDLER(date_format_).report(out); HANDLER(date_format_).report(out);
HANDLER(datetime_format_).report(out); HANDLER(datetime_format_).report(out);
HANDLER(depth_).report(out); HANDLER(depth_).report(out);
@ -450,6 +451,7 @@ public:
parent->HANDLER(period_).on(string("--daily"), "daily"); parent->HANDLER(period_).on(string("--daily"), "daily");
}); });
OPTION(report_t, date_);
OPTION(report_t, date_format_); OPTION(report_t, date_format_);
OPTION(report_t, datetime_format_); OPTION(report_t, datetime_format_);

View file

@ -1146,6 +1146,12 @@ void value_t::in_place_cast(type_t cast_type)
case AMOUNT: case AMOUNT:
set_amount(amount_t(as_string())); set_amount(amount_t(as_string()));
return; return;
case DATE:
set_date(parse_date(as_string()));
return;
case DATETIME:
set_datetime(parse_datetime(as_string()));
return;
case MASK: case MASK:
set_mask(as_string()); set_mask(as_string());
return; return;