Module: Backend::EntitiesHelper

Defined in:
app/helpers/backend/entities_helper.rb

Overview

Helpers for backend/entities controller

Instance Method Summary collapse

Instance Method Details

#transactions_by_month_chart(entity, options = {}) ⇒ Object

Compute a column chart with main transactions of an entity


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/backend/entities_helper.rb', line 5

def transactions_by_month_chart(entity, options = {})
  stopped_on = options[:stopped_on] || Time.zone.today.end_of_month
  started_on = options[:started_on] || (stopped_on.beginning_of_month - 12.months)

  series = []
  categories = {}

  date = started_on
  stopped_on = started_on + 1 if started_on >= stopped_on
  while date <= stopped_on
    categories[date.year.to_s + date.month.to_s.rjust(3, '0')] = date.l(format: '%b %Y')
    date = date >> 1
  end

  sale_items = entity.sale_items.between(started_on, stopped_on)
  if sale_items.any?
    item_h = sale_items.sums_of_periods(column: :amount).sort.each_with_object({}) do |pair, hash|
      hash[pair.expr.to_i.to_s] = pair.sum.to_d
      hash
    end
    series << { type: 'column', name: :sales.tl, data: normalize_serie(item_h, categories.keys) }
  end

  purchase_items = entity.purchase_items.between(started_on, stopped_on)
  if purchase_items.any?
    item_h = purchase_items.sums_of_periods(column: :amount).sort.each_with_object({}) do |pair, hash|
      hash[pair.expr.to_i.to_s] = pair.sum.to_d
      hash
    end
    series << { type: 'column', name: :purchases.tl, data: normalize_serie(item_h, categories.keys) }
  end

  outgoing_payments = entity.outgoing_payments.between(started_on, stopped_on)
  if outgoing_payments.any?
    item_h = outgoing_payments.sums_of_periods.sort.each_with_object({}) do |pair, hash|
      hash[pair.expr.to_i.to_s] = pair.sum.to_d
      hash
    end
    series << { type: 'column', name: :outgoing_payments.tl, data: normalize_serie(item_h, categories.keys) }
  end

  incoming_payments = entity.incoming_payments.between(started_on, stopped_on)
  if incoming_payments.any?
    item_h = incoming_payments.sums_of_periods.sort.each_with_object({}) do |pair, hash|
      hash[pair.expr.to_i.to_s] = pair.sum.to_d
      hash
    end
    series << { type: 'column', name: :incoming_payments.tl, data: normalize_serie(item_h, categories.keys) }
  end

  return nil unless options[:blank].is_a?(FalseClass) || series.any?

  html = column_highcharts(series, y_axis: { title: { text: :pretax_amount.tl } }, x_axis: { categories: categories.values }, legend: true)

  return html unless options[:cobbler]

  options[:cobbler].cobble(options[:title] || :transactions_by_month) do
    html
  end
  nil
end