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

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.

Parameters:

  • q (ActiveRecord::Relation)
  • business_unit_id (Integer, nil) (defaults to: nil)

Returns:

  • (ActiveRecord::Relation)


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.

Returns:

  • (Integer)


43
44
45
46
47
# File 'app/helpers/crm/reports/budget_helper.rb', line 43

def build_actual_accumulated(amount_field, , business_unit_id = nil)
	q = @ledger_entries_accumulated.where(:ledger_company_account_id => )
	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.

Returns:

  • (Integer)


59
60
61
62
63
# File 'app/helpers/crm/reports/budget_helper.rb', line 59

def build_actual_accumulated_previous(amount_field, , business_unit_id = nil)
	q = @ledger_entries_accumulated_previous.where(:ledger_company_account_id => )
	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.

Returns:

  • (Integer)


26
27
28
29
30
# File 'app/helpers/crm/reports/budget_helper.rb', line 26

def build_actual_month(amount_field, , business_unit_id = nil)
	q = @ledger_entries_month.where(:ledger_company_account_id => )
	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.

Returns:

  • (Integer)


51
52
53
54
55
# File 'app/helpers/crm/reports/budget_helper.rb', line 51

def build_actual_month_previous(amount_field, , business_unit_id = nil)
	q = @ledger_entries_month_previous.where(:ledger_company_account_id => )
	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.

Returns:

  • (Integer)


35
36
37
38
39
# File 'app/helpers/crm/reports/budget_helper.rb', line 35

def build_budget_accumulated(amount_field, , company_id, business_unit_id = nil)
	q = @budgets_accumulated.where(: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".

Parameters:

  • amount_field (Symbol)

    e.g. :amount or :consolidated_amount

  • ledger_account_id (Integer)
  • company_id (Integer, Array<Integer>)
  • business_unit_id (Integer, nil) (defaults to: nil)

Returns:

  • (Integer)


16
17
18
19
20
# File 'app/helpers/crm/reports/budget_helper.rb', line 16

def build_budget_month(amount_field, , company_id, business_unit_id = nil)
	q = @budgets.where(: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.

Parameters:

  • budget (Budget)
  • company_id (Array<Integer>)

Returns:

  • (Array<Integer>)


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/crm/reports/budget_helper.rb', line 82

def (budget, company_id)
  ids = case company_id
        when [Company::USA]
          budget.
        when [Company::CAN]
          budget.
        when [Company::NLD]
          budget.
        when [Company::USA, Company::CAN]
          budget. + budget.
        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.

Parameters:

  • diff (Numeric)
  • original (Numeric)

Returns:

  • (Numeric)


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.

Parameters:

  • q (ActiveRecord::Relation)
  • amount_field (Symbol)

Returns:

  • (Integer)


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