Class: Report::CostCenter::CostCenter

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

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



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

def self.cost_center_data(year,month,company_id,units)
  ViewFinancialAccountBalance
    .where("year between #{(year - 1)} and #{year}")
    .where("month <= #{month}")
    .where(company_id: company_id)
    .where('account_number > 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{ |a| a.compact }
end

.gross_profit_data(year, month, company_id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/report/cost_center/cost_center.rb', line 61

def self.gross_profit_data(year,month,company_id)
  ViewFinancialAccountBalance
    .where("year between #{(year - 1)} and #{year}")
    .where("month <= #{month}")
    .where(company_id: company_id)
    .where('account_number > 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{ |a| a.compact }
end

.result_report(options = {}) ⇒ Object



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

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|
      if r[:account_number] == acc[:number] && r[:year] == options[:years][0].to_i && r[:month]  <= options[:months][0].to_i
        acum += r[:value]
      end
    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|
        if r[:account_number] == acc[:number] && r[:year] == options[:years][0].to_i && r[:month] == e
          monthly += r[:value]
        end
      end
      gp_by_month.map{ |r| r[:number] == acc[:number] ? r[e] = monthly : r }
    end
  end # Gross Profit By month

  cc_by_month = []
  units = [10100,10110,10120,10130,10140,10160,10170,10180,10190]
  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|
      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
        acum += r[:value]
      end
    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|
        if r[:account_number] == acc[:number] && r[:business_unit] == acc[:business_unit] && r[:year] == options[:years][0].to_i && r[:month] == e
          monthly += r[:value]
        end
      end
      cc_by_month.map{ |r| r[:number] == acc[:number] && r[:business_unit] == acc[:business_unit] ? r[e] = monthly : r }
    end
  end # Cost Center By month
  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