From 0fdb900c9975b97b43fe85c2691a9d5e69006fab Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 27 Oct 2009 07:15:41 -0400 Subject: Redid the way temporaries are handled in filtering --- src/filters.h | 92 ++++++++++++++++++++++++----------------------------------- 1 file changed, 37 insertions(+), 55 deletions(-) (limited to 'src/filters.h') diff --git a/src/filters.h b/src/filters.h index a03d3160..23a06482 100644 --- a/src/filters.h +++ b/src/filters.h @@ -50,6 +50,7 @@ #include "xact.h" #include "post.h" #include "account.h" +#include "temps.h" namespace ledger { @@ -279,11 +280,6 @@ public: } }; -inline void clear_xacts_posts(std::list& xacts_list) { - foreach (xact_t& xact, xacts_list) - xact.posts.clear(); -} - /** * @brief Brief * @@ -291,8 +287,7 @@ inline void clear_xacts_posts(std::list& xacts_list) { */ class anonymize_posts : public item_handler { - std::list xact_temps; - std::list post_temps; + temporaries_t temps; xact_t * last_xact; @@ -305,7 +300,6 @@ public: } virtual ~anonymize_posts() { TRACE_DTOR(anonymize_posts); - clear_xacts_posts(xact_temps); } virtual void operator()(post_t& post); @@ -346,18 +340,16 @@ public: */ class collapse_posts : public item_handler { - expr_t& amount_expr; - item_predicate display_predicate; - item_predicate only_predicate; - value_t subtotal; - std::size_t count; - xact_t * last_xact; - post_t * last_post; - account_t totals_account; - bool only_collapse_if_zero; - - std::list xact_temps; - std::list post_temps; + expr_t& amount_expr; + item_predicate display_predicate; + item_predicate only_predicate; + value_t subtotal; + std::size_t count; + xact_t * last_xact; + post_t * last_post; + temporaries_t temps; + account_t& totals_account; + bool only_collapse_if_zero; std::list component_posts; collapse_posts(); @@ -372,13 +364,12 @@ public: display_predicate(_display_predicate), only_predicate(_only_predicate), count(0), last_xact(NULL), last_post(NULL), - totals_account(NULL, _("")), + totals_account(temps.create_account(_(""))), only_collapse_if_zero(_only_collapse_if_zero) { TRACE_CTOR(collapse_posts, "post_handler_ptr"); } virtual ~collapse_posts() { TRACE_DTOR(collapse_posts); - clear_xacts_posts(xact_temps); } virtual void flush() { @@ -432,19 +423,17 @@ class changed_value_posts : public item_handler // This filter requires that calc_posts be used at some point // later in the chain. - expr_t display_amount_expr; - expr_t total_expr; - expr_t display_total_expr; - report_t& report; - bool changed_values_only; - post_t * last_post; - value_t last_total; - value_t last_display_total; - account_t revalued_account; - account_t rounding_account; - - std::list xact_temps; - std::list post_temps; + expr_t display_amount_expr; + expr_t total_expr; + expr_t display_total_expr; + report_t& report; + bool changed_values_only; + post_t * last_post; + value_t last_total; + value_t last_display_total; + temporaries_t temps; + account_t& revalued_account; + account_t& rounding_account; changed_value_posts(); @@ -459,14 +448,13 @@ public: display_amount_expr(_display_amount_expr), total_expr(_total_expr), display_total_expr(_display_total_expr), report(_report), changed_values_only(_changed_values_only), last_post(NULL), - revalued_account(NULL, _("")), - rounding_account(NULL, _("")){ + revalued_account(temps.create_account(_(""))), + rounding_account(temps.create_account(_(""))) { TRACE_CTOR(changed_value_posts, "post_handler_ptr, const expr_t&, const expr_t&, report_t&, bool"); } virtual ~changed_value_posts() { TRACE_DTOR(changed_value_posts); - clear_xacts_posts(xact_temps); } virtual void flush(); @@ -517,8 +505,7 @@ protected: expr_t& amount_expr; values_map values; optional date_format; - std::list xact_temps; - std::list post_temps; + temporaries_t temps; std::list component_posts; public: @@ -531,7 +518,6 @@ public: } virtual ~subtotal_posts() { TRACE_DTOR(subtotal_posts); - clear_xacts_posts(xact_temps); } void report_subtotal(const char * spec_fmt = NULL, @@ -555,7 +541,7 @@ class interval_posts : public subtotal_posts date_interval_t interval; date_interval_t last_interval; post_t * last_post; - account_t empty_account; + account_t& empty_account; bool exact_periods; bool generate_empty_posts; @@ -569,11 +555,11 @@ public: bool _exact_periods = false, bool _generate_empty_posts = false) : subtotal_posts(_handler, amount_expr), interval(_interval), - last_post(NULL), empty_account(NULL, _("")), + last_post(NULL), empty_account(temps.create_account(_(""))), exact_periods(_exact_periods), generate_empty_posts(_generate_empty_posts) { TRACE_CTOR(interval_posts, - "post_handler_ptr, expr_t&, interval_t, account_t *, bool, bool"); + "post_handler_ptr, expr_t&, date_interval_t, bool, bool"); } virtual ~interval_posts() throw() { TRACE_DTOR(interval_posts); @@ -594,7 +580,7 @@ public: class posts_as_equity : public subtotal_posts { post_t * last_post; - account_t equity_account; + account_t& equity_account; account_t * balance_account; posts_as_equity(); @@ -602,7 +588,7 @@ class posts_as_equity : public subtotal_posts public: posts_as_equity(post_handler_ptr _handler, expr_t& amount_expr) : subtotal_posts(_handler, amount_expr), - equity_account(NULL, _("Equity")) { + equity_account(temps.create_account(_("Equity"))) { TRACE_CTOR(posts_as_equity, "post_handler_ptr, expr_t&"); balance_account = equity_account.find_account(_("Opening Balances")); } @@ -653,11 +639,10 @@ class by_payee_posts : public item_handler */ class transfer_details : public item_handler { - std::list xact_temps; - std::list post_temps; - account_t * master; - expr_t expr; - scope_t& scope; + account_t * master; + expr_t expr; + scope_t& scope; + temporaries_t temps; transfer_details(); @@ -679,7 +664,6 @@ public: } virtual ~transfer_details() { TRACE_DTOR(transfer_details); - clear_xacts_posts(xact_temps); } virtual void operator()(post_t& post); @@ -725,8 +709,7 @@ protected: typedef std::list pending_posts_list; pending_posts_list pending_posts; - std::list xact_temps; - std::list post_temps; + temporaries_t temps; public: generate_posts(post_handler_ptr handler) @@ -736,7 +719,6 @@ public: virtual ~generate_posts() { TRACE_DTOR(generate_posts); - clear_xacts_posts(xact_temps); } void add_period_xacts(period_xacts_list& period_xacts); -- cgit v1.2.3 From 88460db2fbb0ab76c0ab3705cc4043e7fb2726d4 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 27 Oct 2009 08:04:31 -0400 Subject: Added a new "budget" report This is a balance report with four columns: Amount spent | Budgeted Amount | Difference | Percentage --- src/filters.cc | 9 +++++++++ src/filters.h | 7 ++++--- src/report.cc | 15 ++++++++++++++- src/report.h | 30 ++++++++++++++++++++++++------ 4 files changed, 51 insertions(+), 10 deletions(-) (limited to 'src/filters.h') diff --git a/src/filters.cc b/src/filters.cc index 3667c213..25649ad0 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -752,6 +752,15 @@ void budget_posts::report_budget_items(const date_t& date) post_t& temp = temps.copy_post(post, xact); temp.amount.in_place_negate(); + if (flags & BUDGET_WRAP_VALUES) { + value_t seq; + seq.push_back(0L); + seq.push_back(temp.amount); + + temp.xdata().compound_value = seq; + temp.xdata().add_flags(POST_EXT_COMPOUND); + } + ++pair.first; begin = *pair.first.start; diff --git a/src/filters.h b/src/filters.h index 23a06482..3eb5da8d 100644 --- a/src/filters.h +++ b/src/filters.h @@ -733,9 +733,10 @@ public: */ class budget_posts : public generate_posts { -#define BUDGET_NO_BUDGET 0x00 -#define BUDGET_BUDGETED 0x01 -#define BUDGET_UNBUDGETED 0x02 +#define BUDGET_NO_BUDGET 0x00 +#define BUDGET_BUDGETED 0x01 +#define BUDGET_UNBUDGETED 0x02 +#define BUDGET_WRAP_VALUES 0x04 uint_least8_t flags; diff --git a/src/report.cc b/src/report.cc index 6fc7626f..2f932115 100644 --- a/src/report.cc +++ b/src/report.cc @@ -700,11 +700,24 @@ expr_t::ptr_op_t report_t::lookup(const string& name) if (WANT_CMD()) { const char * q = p + CMD_PREFIX_LEN; switch (*q) { case 'b': - if (*(q + 1) == '\0' || is_eq(q, "bal") || is_eq(q, "balance")) + if (*(q + 1) == '\0' || is_eq(q, "bal") || is_eq(q, "balance")) { return expr_t::op_t::wrap_functor (reporter (new format_accounts(*this, report_format(HANDLER(balance_format_))), *this, "#balance")); + } + else if (is_eq(q, "budget")) { + HANDLER(amount_).set_expr(string("#budget"), "(amount, 0)"); + + budget_flags |= BUDGET_WRAP_VALUES; + if (! (budget_flags & ~BUDGET_WRAP_VALUES)) + budget_flags |= BUDGET_BUDGETED; + + return expr_t::op_t::wrap_functor + (reporter + (new format_accounts(*this, report_format(HANDLER(budget_format_))), + *this, "#budget")); + } break; case 'c': diff --git a/src/report.h b/src/report.h index f0052128..baacd28c 100644 --- a/src/report.h +++ b/src/report.h @@ -117,9 +117,10 @@ public: session_t& session; output_stream_t output_stream; -#define BUDGET_NO_BUDGET 0x00 -#define BUDGET_BUDGETED 0x01 -#define BUDGET_UNBUDGETED 0x02 +#define BUDGET_NO_BUDGET 0x00 +#define BUDGET_BUDGETED 0x01 +#define BUDGET_UNBUDGETED 0x02 +#define BUDGET_WRAP_VALUES 0x04 datetime_t terminus; uint_least8_t budget_flags; @@ -205,6 +206,7 @@ public: HANDLER(basis).report(out); HANDLER(begin_).report(out); HANDLER(budget).report(out); + HANDLER(budget_format_).report(out); HANDLER(by_payee).report(out); HANDLER(cleared).report(out); HANDLER(code_as_payee).report(out); @@ -314,7 +316,7 @@ public: }); OPTION_(report_t, add_budget, DO() { - parent->budget_flags = BUDGET_BUDGETED | BUDGET_UNBUDGETED; + parent->budget_flags |= BUDGET_BUDGETED | BUDGET_UNBUDGETED; }); OPTION__ @@ -368,7 +370,23 @@ public: }); OPTION_(report_t, budget, DO() { - parent->budget_flags = BUDGET_BUDGETED; + parent->budget_flags |= BUDGET_BUDGETED; + }); + + OPTION__(report_t, budget_format_, CTOR(report_t, budget_format_) { + on(none, + "%(justify(scrub(get_at(total_expr, 0)), 12, -1, true, color))" + " %(justify(scrub(- get_at(total_expr, 1)), 12, -1, true, color))" + " %(justify(scrub(get_at(total_expr, 1) + get_at(total_expr, 0)), 12, -1, true, color))" + " %(justify(scrub((100% * get_at(total_expr, 0)) / - get_at(total_expr, 1)), 5, -1, true, color))" + " %(!options.flat ? depth_spacer : \"\")" + "%-(ansify_if(partial_account(options.flat), blue if color))\n" + "%/" + "%(justify(scrub(get_at(total_expr, 0)), 12, -1, true, color))" + " %(justify(scrub(- get_at(total_expr, 1)), 12, -1, true, color))" + " %(justify(scrub(get_at(total_expr, 1) + get_at(total_expr, 0)), 12, -1, true, color))" + " %(justify(scrub((100% * get_at(total_expr, 0)) / - get_at(total_expr, 1)), 5, -1, true, color))\n%/" + "------------ ------------ ------------ -----\n"); }); OPTION(report_t, by_payee); // -P @@ -772,7 +790,7 @@ public: }); OPTION_(report_t, unbudgeted, DO() { - parent->budget_flags = BUDGET_UNBUDGETED; + parent->budget_flags |= BUDGET_UNBUDGETED; }); OPTION_(report_t, uncleared, DO() { // -U -- cgit v1.2.3