Class: Report::BalanceSheet::BalanceSheet

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

Overview

Service object: balance sheet.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.balance_data(year, month, company_id) ⇒ Object

Balance data.

Parameters:

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

Returns:

  • (Object)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/report/balance_sheet/balance_sheet.rb', line 83

def self.balance_data(year, month, company_id)
  ViewFinancialAccountBalance
    .where(year: 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(case when monthly_amount <= 0 then monthly_amount else 0 end) as debit',
            'sum(case when monthly_amount > 0 then monthly_amount else 0 end) as credit',
            'sum(case when beginning_balance_amount <= 0 then beginning_balance_amount else 0 end) as ytd_debit',
            'sum(case when beginning_balance_amount > 0 then beginning_balance_amount else 0 end) as ytd_credit')
    .map { |r| r.attributes.symbolize_keys }.map(&:compact)
end

.result_report(options = {}) ⇒ Result

Returns trial balance 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)

    trial balance 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
68
69
70
71
72
73
74
75
76
# File 'app/services/report/balance_sheet/balance_sheet.rb', line 24

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

  accounts.each do |acc|
    debit = 0
    credit = 0
    ytd_debit = 0
    ytd_credit = 0
    data.each do |r|
      next unless r[:account_number] == acc[:number]

      acc[:number] == 4980 ? debit = r[:debit] : debit += r[:debit]
      acc[:number] == 4980 ? credit = r[:credit] : credit += r[:credit]
      ytd_debit += r[:ytd_debit]
      ytd_credit += r[:ytd_credit]
    end
    debit -= ytd_credit if acc[:number] == 4999
    credit -= ytd_debit if acc[:number] == 4999
    trial_balance << { id: acc[:id], number: acc[:number], name: acc[:name], acc_debit: debit, acc_credit: credit, ytd_debit: ytd_debit, ytd_credit: ytd_credit }
  end
  trial_balance.delete_if { |d| d[:acc_debit].zero? && d[:acc_credit].zero? && d[:ytd_debit].zero? && d[:ytd_credit] == 0 }

  accounts.each do |acc|
    ytd_value = 0
    data.each do |r|
      ytd_value += (r[:ytd_debit] + r[:ytd_credit]) if r[:account_number] == acc[:number]
    end
    by_month << { id: acc[:id], number: acc[:number], name: acc[:name], ytd_value: ytd_value }

    (1..12).each do |e|
      value = 0
      data.each do |r|
        if r[:account_number] == acc[:number] && r[:month] <= e
          acc[:number] == 4980 ? value = (r[:debit] + r[:credit]) : value += (r[:debit] + r[:credit])
        end
      end
      by_month.map { |r| r[:number] == acc[:number] ? r[e] = (value + r[:ytd_value]) : r }
      by_month.map { |r| r[:number] == acc[:number] ? r[e] = value : r } if acc[:number] == 4999
    end
  end

  if options[:months][0].to_i < 12
    ((options[:months][0].to_i + 1)..12).each do |v|
      by_month.map { |r| r[v] = 0 }
    end
  end
  by_month.delete_if { |d| d[:ytd_value].zero? && d[1].zero? && d[2].zero? && d[3].zero? && d[4].zero? && d[5].zero? && d[6].zero? && d[7].zero? && d[8].zero? && d[9].zero? && d[10].zero? && d[11].zero? && d[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_balance: trial_balance, by_month: by_month)
end