summaryrefslogtreecommitdiff
path: root/src/post.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/post.cc')
-rw-r--r--src/post.cc94
1 files changed, 80 insertions, 14 deletions
diff --git a/src/post.cc b/src/post.cc
index 183fb901..7dc15830 100644
--- a/src/post.cc
+++ b/src/post.cc
@@ -142,11 +142,15 @@ namespace {
return value_t(static_cast<scope_t *>(post.xact));
}
+ value_t get_xact_id(post_t& post) {
+ return static_cast<long>(post.xact_id());
+ }
+
value_t get_code(post_t& post) {
if (post.xact->code)
return string_value(*post.xact->code);
else
- return string_value(empty_string);
+ return NULL_VALUE;
}
value_t get_payee(post_t& post) {
@@ -154,9 +158,13 @@ namespace {
}
value_t get_note(post_t& post) {
- string note = post.note ? *post.note : empty_string;
- note += post.xact->note ? *post.xact->note : empty_string;
- return string_value(note);
+ if (post.note || post.xact->note) {
+ string note = post.note ? *post.note : empty_string;
+ note += post.xact->note ? *post.xact->note : empty_string;
+ return string_value(note);
+ } else {
+ return NULL_VALUE;
+ }
}
value_t get_magnitude(post_t& post) {
@@ -183,11 +191,21 @@ namespace {
}
value_t get_commodity(post_t& post) {
- return string_value(post.amount.commodity().symbol());
+ if (post.has_xdata() &&
+ post.xdata().has_flags(POST_EXT_COMPOUND))
+ return string_value(post.xdata().compound_value.to_amount()
+ .commodity().symbol());
+ else
+ return string_value(post.amount.commodity().symbol());
}
value_t get_commodity_is_primary(post_t& post) {
- return post.amount.commodity().has_flags(COMMODITY_PRIMARY);
+ if (post.has_xdata() &&
+ post.xdata().has_flags(POST_EXT_COMPOUND))
+ return post.xdata().compound_value.to_amount()
+ .commodity().has_flags(COMMODITY_PRIMARY);
+ else
+ return post.amount.commodity().has_flags(COMMODITY_PRIMARY);
}
value_t get_has_cost(post_t& post) {
@@ -222,7 +240,7 @@ namespace {
return 1L;
}
- value_t get_account(call_scope_t& scope)
+ value_t account_name(call_scope_t& scope)
{
in_context_t<post_t> env(scope, "&v");
@@ -265,14 +283,32 @@ namespace {
} else {
name = env->reported_account()->fullname();
}
+ return string_value(name);
+ }
- if (env->has_flags(POST_VIRTUAL)) {
- if (env->must_balance())
- name = string("[") + name + "]";
- else
- name = string("(") + name + ")";
+ value_t get_display_account(call_scope_t& scope)
+ {
+ in_context_t<post_t> env(scope, "&v");
+
+ value_t acct = account_name(scope);
+ if (acct.is_string()) {
+ if (env->has_flags(POST_VIRTUAL)) {
+ if (env->must_balance())
+ acct = string_value(string("[") + acct.as_string() + "]");
+ else
+ acct = string_value(string("(") + acct.as_string() + ")");
+ }
}
- return string_value(name);
+ return acct;
+ }
+
+ value_t get_account(call_scope_t& scope)
+ {
+ return account_name(scope);
+ }
+
+ value_t get_account_id(post_t& post) {
+ return static_cast<long>(post.account_id());
}
value_t get_account_base(post_t& post) {
@@ -351,6 +387,8 @@ expr_t::ptr_op_t post_t::lookup(const symbol_t::kind_t kind,
return WRAP_FUNCTOR(get_account);
else if (name == "account_base")
return WRAP_FUNCTOR(get_wrapper<&get_account_base>);
+ else if (name == "account_id")
+ return WRAP_FUNCTOR(get_wrapper<&get_account_id>);
else if (name == "any")
return WRAP_FUNCTOR(&fn_any);
else if (name == "all")
@@ -378,7 +416,9 @@ expr_t::ptr_op_t post_t::lookup(const symbol_t::kind_t kind,
break;
case 'd':
- if (name == "depth")
+ if (name == "display_account")
+ return WRAP_FUNCTOR(get_display_account);
+ else if (name == "depth")
return WRAP_FUNCTOR(get_wrapper<&get_account_depth>);
else if (name == "datetime")
return WRAP_FUNCTOR(get_wrapper<&get_datetime>);
@@ -444,6 +484,8 @@ expr_t::ptr_op_t post_t::lookup(const symbol_t::kind_t kind,
case 'x':
if (name == "xact")
return WRAP_FUNCTOR(get_wrapper<&get_xact>);
+ else if (name == "xact_id")
+ return WRAP_FUNCTOR(get_wrapper<&get_xact_id>);
break;
case 'N':
@@ -479,6 +521,30 @@ amount_t post_t::resolve_expr(scope_t& scope, expr_t& expr)
}
}
+std::size_t post_t::xact_id() const
+{
+ std::size_t id = 1;
+ foreach (post_t * p, xact->posts) {
+ if (p == this)
+ return id;
+ id++;
+ }
+ assert(! "Failed to find posting within its transaction");
+ return 0;
+}
+
+std::size_t post_t::account_id() const
+{
+ std::size_t id = 1;
+ foreach (post_t * p, account->posts) {
+ if (p == this)
+ return id;
+ id++;
+ }
+ assert(! "Failed to find posting within its transaction");
+ return 0;
+}
+
bool post_t::valid() const
{
if (! xact) {