Class: Report::ProfitLoss::ProfitLoss

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

Overview

Service object: profit loss.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.profit_and_loss_data(year, month, company_id, option) ⇒ Object

Profit and loss data.

Parameters:

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

Returns:

  • (Object)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/report/profit_loss/profit_loss.rb', line 91

def self.profit_and_loss_data(year, month, company_id, option)
  if option == 0
    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(&:compact)
  elsif option == 1
    ViewFinancialAccountBalance
      .where("year between #{year - 1} and #{year}")
      .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(&:compact)
  end
end

.result_report(options = {}) ⇒ Result

Builds the profit & loss report result for a company, year, and month.

Parameters:

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

    configurable options

Options Hash (options):

  • years (Array<String>)

    report years; the first element is the selected year

  • months (Array<String>)

    report months; the first element is the selected month

  • company_ids (Array<String>)

    company ids; the first element is the selected company

Returns:

  • (Result)

    the report data (trial P&L, consolidated, and by-month rows)



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/services/report/profit_loss/profit_loss.rb', line 26

def self.result_report(options = {})
  trial_pl = []
  consolidated = []
  by_month = []
  data = profit_and_loss_data(options[:years][0].to_i, options[:months][0].to_i, options[:company_ids][0].to_i, 0)
  data_trial = profit_and_loss_data(options[:years][0].to_i, options[:months][0].to_i, options[:company_ids][0].to_i, 1)
  accounts = data_trial.map { |r| { id: r[:account_id], number: r[:account_number], name: r[:account_name] } }.uniq.sort_by { |e| e[:number] }

  accounts.each do |acc|
    bb_debit = 0
    bb_credit = 0
    debit = 0
    credit = 0
    data_trial.each do |r|
      next unless r[:account_number] == acc[:number]

      bb_debit += r[:value] if r[:year] == (options[:years][0].to_i - 1) && r[:value] < 0
      bb_credit += r[:value] if r[:year] == (options[:years][0].to_i - 1) && r[:value] > 0
      debit += r[:value] if r[:year] == options[:years][0].to_i && r[:month] <= options[:months][0].to_i && r[:value] < 0
      credit += r[:value] if r[:year] == options[:years][0].to_i && r[:month] <= options[:months][0].to_i && r[:value] > 0
    end
    trial_pl << { id: acc[:id], number: acc[:number], name: acc[:name], bb_debit: bb_debit, bb_credit: bb_credit, debit: debit, credit: credit }

    c_month = 0
    p_month = 0
    c_year = 0
    p_year = 0
    data.each do |r|
      next unless r[:account_number] == acc[:number]

      c_month += r[:value] if r[:year] == options[:years][0].to_i && r[:month] == options[:months][0].to_i
      p_month += r[:value] if r[:year] == (options[:years][0].to_i - 1) && r[:month] == options[:months][0].to_i
      c_year += r[:value] if r[:year] == options[:years][0].to_i && r[:month] <= options[:months][0].to_i
      p_year += r[:value] if r[:year] == (options[:years][0].to_i - 1) && r[:month] <= options[:months][0].to_i
    end
    consolidated << { id: acc[:id], number: acc[:number], name: acc[:name], c_month: c_month, p_month: p_month, c_year: c_year, p_year: p_year }

    acum = 0
    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
    by_month << { id: acc[:id], number: acc[:number], name: acc[:name], acum: acum }

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

  trial_pl.delete_if { |c| c[:bb_debit].zero? && c[:bb_credit].zero? && c[:debit].zero? && c[:credit].zero? }
  consolidated.delete_if { |c| c[:c_month].zero? && c[:p_month].zero? && c[:c_year].zero? && c[:p_year].zero? }
  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, trial_pl: trial_pl, profit_loss_data: consolidated, by_month: by_month)
end