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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/report/cost_center/cost_center.rb', line 68

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



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/report/cost_center/cost_center.rb', line 55

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 = {}) ⇒ Object



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

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