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.
111 112 113 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 111 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.
43 44 45 46 47 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 43 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.
59 60 61 62 63 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 59 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.
26 27 28 29 30 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 26 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.
51 52 53 54 55 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 51 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.
35 36 37 38 39 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 35 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".
16 17 18 19 20 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 16 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.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 82 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.
71 72 73 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 71 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.
101 102 103 104 |
# File 'app/helpers/crm/reports/budget_helper.rb', line 101 def q_sum(q, amount_field) q = q.sum(amount_field) -q.round end |