Class: Report::CostCenter::CostCenter

Inherits:
Object
  • Object
show all
Defined in:
app/services/report/cost_center/cost_center.rb

Overview

Service object: cost center.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.cost_center_data(year, month, company_id, units) ⇒ Object

Cost center data.

Parameters:

  • year (Object)
  • month (Object)
  • company_id (Integer)
  • units (Object)

Returns:

  • (Object)


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/services/report/cost_center/cost_center.rb', line 93

def self.cost_center_data(year, month, company_id, units)
  ViewFinancialAccountBalance
    .where(year: (year - 1)..year)
    .where(month: ..month)
    .where(company_id: company_id)
    .where(ViewFinancialAccountBalance[:account_number].gt(4999))
    .where(business_unit: units)
    .group(:account_id, :account_number, :business_unit, :account_name, :year, :month)
    .order(:account_number)
    .select(:account_id, :account_number, :business_unit, :account_name, :year, :month,
            'sum(monthly_amount * -1) as value')
    .map { |r| r.attributes.symbolize_keys }.map(&:compact)
end

.gross_profit_data(year, month, company_id) ⇒ Object

Gross profit data.

Parameters:

  • year (Object)
  • month (Object)
  • company_id (Integer)

Returns:

  • (Object)


74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/report/cost_center/cost_center.rb', line 74

def self.gross_profit_data(year, month, company_id)
  ViewFinancialAccountBalance
    .where(year: (year - 1)..year)
    .where(month: ..month)
    .where(company_id: company_id)
    .where(ViewFinancialAccountBalance[:account_number].gt(4999))
    .group(:account_id, :account_number, :account_name, :year, :month)
    .order(:account_number)
    .select(:account_id, :account_number, :account_name, :year, :month,
            'sum(monthly_amount * -1) as value')
    .map { |r| r.attributes.symbolize_keys }.map(&:compact)
end

.result_report(options = {}) ⇒ Result

Returns gross profit and cost center data by month.

Parameters:

  • options (Hash) (defaults to: {})

    report filter options

Options Hash (options):

  • years (Array<String>)

    first element is the report year

  • months (Array<String>)

    first element is the report month

  • company_ids (Array<String>)

    first element is the company to report on

Returns:

  • (Result)

    gross profit and cost center data by month



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
66
67
# File 'app/services/report/cost_center/cost_center.rb', line 24

def self.result_report(options = {})
  gp_by_month = []
  gp_data = gross_profit_data(options[:years][0].to_i, options[:months][0].to_i, options[:company_ids][0].to_i)
  gp_accounts = gp_data.map { |r| { id: r[:account_id], number: r[:account_number], name: r[:account_name] } }.uniq.sort_by { |e| e[:number] }
  gp_accounts.each do |acc|
    acum = 0
    gp_data.each do |r|
      acum += r[:value] if r[:account_number] == acc[:number] && r[:year] == options[:years][0].to_i && r[:month] <= options[:months][0].to_i
    end
    gp_by_month << { id: acc[:id], number: acc[:number], name: acc[:name], acum: acum }

    (1..12).each do |e|
      monthly = 0
      gp_data.each do |r|
        monthly += r[:value] if r[:account_number] == acc[:number] && r[:year] == options[:years][0].to_i && r[:month] == e
      end
      gp_by_month.map { |r| r[:number] == acc[:number] ? r[e] = monthly : r }
    end
  end

  cc_by_month = []
  units = [10_100, 10_110, 10_120, 10_130, 10_140, 10_160, 10_170, 10_180, 10_190]
  cc_data = cost_center_data(options[:years][0].to_i, options[:months][0].to_i, options[:company_ids][0].to_i, units)
  cc_accounts = cc_data.map { |r| { id: r[:account_id], number: r[:account_number], name: r[:account_name], business_unit: r[:business_unit] } }.uniq.sort_by { |e| e[:number] }
  cc_accounts.each do |acc|
    acum = 0
    cc_data.each do |r|
      acum += r[:value] if r[:account_number] == acc[:number] && r[:business_unit] == acc[:business_unit] && r[:year] == options[:years][0].to_i && r[:month] <= options[:months][0].to_i
    end
    cc_by_month << { id: acc[:id], number: acc[:number], name: acc[:name], business_unit: acc[:business_unit], acum: acum }

    (1..12).each do |e|
      monthly = 0
      cc_data.each do |r|
        monthly += r[:value] if r[:account_number] == acc[:number] && r[:business_unit] == acc[:business_unit] && r[:year] == options[:years][0].to_i && r[:month] == e
      end
      cc_by_month.map { |r| r[:number] == acc[:number] && r[:business_unit] == acc[:business_unit] ? r[e] = monthly : r }
    end
  end
  gp_by_month.delete_if { |b| b[:acum].zero? && b[1].zero? && b[2].zero? && b[3].zero? && b[4].zero? && b[5].zero? && b[6].zero? && b[7].zero? && b[8].zero? && b[9].zero? && b[10].zero? && b[11].zero? && b[12].zero? }
  cc_by_month.delete_if { |b| b[:acum].zero? && b[1].zero? && b[2].zero? && b[3].zero? && b[4].zero? && b[5].zero? && b[6].zero? && b[7].zero? && b[8].zero? && b[9].zero? && b[10].zero? && b[11].zero? && b[12].zero? }

  Result.new(success: true, year: options[:years][0].to_i, month: options[:months][0].to_i, company_id: options[:company_ids][0].to_i, gp_by_month: gp_by_month, cc_by_month: cc_by_month)
end