Module: Crm::Reports::BudgetHelper
- Defined in:
- app/helpers/crm/reports/budget_helper.rb
Overview
View helpers for the CRM Budget report — formatters and per-cell
aggregators that turn budgeted-vs-actual numbers into the favourable/
unfavourable colour-coded grid users see in /reports/budget.
Instance Method Summary collapse
-
#add_business_unit(q, business_unit_id = nil) ⇒ ActiveRecord::Relation
Append a
business_unit_id =filter toqwhen given, otherwise pass through. -
#build_actual_accumulated(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
Year-to-date actuals total.
-
#build_actual_accumulated_previous(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
YTD-prior-year actuals total.
-
#build_actual_month(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
Same shape as #build_budget_month but sums actuals from
@ledger_entries_monthfor the givenLedgerCompanyAccountids. -
#build_actual_month_previous(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
Same-month-previous-year actuals total.
-
#build_budget_accumulated(amount_field, ledger_account_id, company_id, business_unit_id = nil) ⇒ Integer
Year-to-date budget total — same as #build_budget_month on the
@budgets_accumulatedrelation. -
#build_budget_month(amount_field, ledger_account_id, company_id, business_unit_id = nil) ⇒ Integer
Sum of
amount_fieldover the budget rows for one(account, company, [BU])for the report's current month. -
#build_ledger_company_account_ids(budget, company_id) ⇒ Array<Integer>
Read the right pre-computed
all_ledger_company_account_ids_*cache offbudgetfor the givencompany_idset (single or USA+CAN combo). -
#build_percentage(diff, original) ⇒ Numeric
diff / original × 100, with the corner cases that report writers expect:0%when the diff is zero,100%whenoriginalis zero. -
#q_sum(q, amount_field) ⇒ Integer
SUM(amount_field)overq, negated and rounded — the report convention is to show revenue/income as positive.
Instance Method Details
#add_business_unit(q, business_unit_id = nil) ⇒ ActiveRecord::Relation
Append a business_unit_id = filter to q when given,
otherwise pass through.
133 134 135 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 133 def add_business_unit(q, business_unit_id = nil) business_unit_id.nil? ? q : q.where(:business_unit_id => business_unit_id) end |
#build_actual_accumulated(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
Year-to-date actuals total.
57 58 59 60 61 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 57 def build_actual_accumulated(amount_field, ledger_company_account_ids, business_unit_id = nil) q = @ledger_entries_accumulated.where(:ledger_company_account_id => ledger_company_account_ids) q = add_business_unit(q, business_unit_id) q_sum(q, amount_field) end |
#build_actual_accumulated_previous(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
YTD-prior-year actuals total.
81 82 83 84 85 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 81 def build_actual_accumulated_previous(amount_field, ledger_company_account_ids, business_unit_id = nil) q = @ledger_entries_accumulated_previous.where(:ledger_company_account_id => ledger_company_account_ids) q = add_business_unit(q, business_unit_id) q_sum(q, amount_field) end |
#build_actual_month(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
Same shape as #build_budget_month but sums actuals from
@ledger_entries_month for the given
LedgerCompanyAccount ids.
31 32 33 34 35 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 31 def build_actual_month(amount_field, ledger_company_account_ids, business_unit_id = nil) q = @ledger_entries_month.where(:ledger_company_account_id => ledger_company_account_ids) q = add_business_unit(q, business_unit_id) q_sum(q, amount_field) end |
#build_actual_month_previous(amount_field, ledger_company_account_ids, business_unit_id = nil) ⇒ Integer
Same-month-previous-year actuals total.
69 70 71 72 73 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 69 def build_actual_month_previous(amount_field, ledger_company_account_ids, business_unit_id = nil) q = @ledger_entries_month_previous.where(:ledger_company_account_id => ledger_company_account_ids) q = add_business_unit(q, business_unit_id) q_sum(q, amount_field) end |
#build_budget_accumulated(amount_field, ledger_account_id, company_id, business_unit_id = nil) ⇒ Integer
Year-to-date budget total — same as #build_budget_month on
the @budgets_accumulated relation.
45 46 47 48 49 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 45 def build_budget_accumulated(amount_field, ledger_account_id, company_id, business_unit_id = nil) q = @budgets_accumulated.where(:ledger_account_id => ledger_account_id, :company_id => company_id) q = add_business_unit(q, business_unit_id) q_sum(q, amount_field) end |
#build_budget_month(amount_field, ledger_account_id, company_id, business_unit_id = nil) ⇒ Integer
Sum of amount_field over the budget rows for one
(account, company, [BU]) for the report's current month.
Negated by #q_sum so a positive number is "favourable".
17 18 19 20 21 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 17 def build_budget_month(amount_field, ledger_account_id, company_id, business_unit_id = nil) q = @budgets.where(:ledger_account_id => ledger_account_id, :company_id => company_id) q = add_business_unit(q, business_unit_id) q_sum(q, amount_field) end |
#build_ledger_company_account_ids(budget, company_id) ⇒ Array<Integer>
Read the right pre-computed all_ledger_company_account_ids_*
cache off budget for the given company_id set (single or
USA+CAN combo). See the Budget#set_company_accounts cache.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 104 def build_ledger_company_account_ids(budget, company_id) ids = case company_id when [Company::USA] budget.all_ledger_company_account_ids_1 when [Company::CAN] budget.all_ledger_company_account_ids_2 when [Company::NLD] budget.all_ledger_company_account_ids_4 when [Company::USA, Company::CAN] budget.all_ledger_company_account_ids_1 + budget.all_ledger_company_account_ids_2 end ids.collect(&:to_i) end |
#build_percentage(diff, original) ⇒ Numeric
diff / original × 100, with the corner cases that report
writers expect: 0% when the diff is zero, 100% when
original is zero.
93 94 95 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 93 def build_percentage(diff, original) diff.zero? ? 0 : ( original.zero? ? 100 : ((diff / original) * 100)) end |
#q_sum(q, amount_field) ⇒ Integer
SUM(amount_field) over q, negated and rounded — the
report convention is to show revenue/income as positive.
123 124 125 126 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 123 def q_sum(q, amount_field) q = q.sum(amount_field) -q.round end |