Class: Report::AccountingRecords::AccountingRecords

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

Overview

Service object: accounting records.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.query_beginning_netincome(company_id, account_number, start_date) ⇒ Object

Query beginning netincome.

Parameters:

  • company_id (Integer)
  • account_number (Integer)
  • start_date (Date)

Returns:

  • (Object)


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/services/report/accounting_records/accounting_records.rb', line 96

def self.query_beginning_netincome(company_id, , start_date)
  LedgerEntry
    .joins('LEFT JOIN ledger_transactions on ledger_entries.ledger_transaction_id = ledger_transactions.id')
    .joins('LEFT JOIN ledger_company_accounts on ledger_entries.ledger_company_account_id = ledger_company_accounts.id')
    .joins('LEFT JOIN ledger_accounts on ledger_company_accounts.ledger_detail_account_id = ledger_accounts.id')
    .joins('left join receipts on receipts.id = ledger_transactions.receipt_id')
    .joins('left join outgoing_payments on outgoing_payments.id = ledger_transactions.outgoing_payment_id')
    .joins('LEFT JOIN business_units on ledger_entries.business_unit_id = business_units.id')
    .joins('left join companies on companies.id = ledger_company_accounts.company_id')
    .where(ledger_company_accounts: { company_id: company_id }).where(ledger_accounts: { number:  })
    .where(ledger_transactions: { transaction_date: start_date.beginning_of_year..start_date.days_ago(1) })
    .select('sum(ledger_entries.company_amount) as total_amount').map { |r| r.attributes.symbolize_keys }.map(&:compact)
end

.query_inception_balance(company_id, account_number, start_date) ⇒ Object

Query inception balance.

Parameters:

  • company_id (Integer)
  • account_number (Integer)
  • start_date (Date)

Returns:

  • (Object)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/report/accounting_records/accounting_records.rb', line 76

def self.query_inception_balance(company_id, , start_date)
  accounts = .empty? ? LedgerAccount.order(:number).pluck(:number).map(&:to_i) : .map(&:to_i)
  LedgerEntry
    .joins('LEFT JOIN ledger_transactions on ledger_entries.ledger_transaction_id = ledger_transactions.id')
    .joins('LEFT JOIN ledger_company_accounts on ledger_entries.ledger_company_account_id = ledger_company_accounts.id')
    .joins('LEFT JOIN ledger_accounts on ledger_company_accounts.ledger_detail_account_id = ledger_accounts.id')
    .joins('left join receipts on receipts.id = ledger_transactions.receipt_id')
    .joins('left join outgoing_payments on outgoing_payments.id = ledger_transactions.outgoing_payment_id')
    .joins('LEFT JOIN business_units on ledger_entries.business_unit_id = business_units.id')
    .joins('left join companies on companies.id = ledger_company_accounts.company_id')
    .where(ledger_company_accounts: { company_id: company_id }).where(ledger_accounts: { number: accounts })
    .where(ledger_transactions: { transaction_date: ...start_date })
    .select('sum(ledger_entries.company_amount) as total_amount').map { |r| r.attributes.symbolize_keys }.map(&:compact)
end

.query_period_balance(company_id, account_number, business_unit, start_date, end_date) ⇒ Object

Query period balance.

Parameters:

  • company_id (Integer)
  • account_number (Integer)
  • business_unit (Object)
  • start_date (Date)
  • end_date (Date)

Returns:

  • (Object)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/report/accounting_records/accounting_records.rb', line 48

def self.query_period_balance(company_id, , business_unit, start_date, end_date)
  accounts = .empty? ? LedgerAccount.order(:number).pluck(:number).map(&:to_i) : .map(&:to_i)
  business_units = business_unit.empty? ? BusinessUnit.order(:number).pluck(:number).map(&:to_i) : business_unit.map(&:to_i)
  LedgerEntry
    .joins('LEFT JOIN ledger_transactions on ledger_entries.ledger_transaction_id = ledger_transactions.id')
    .joins('LEFT JOIN ledger_company_accounts on ledger_entries.ledger_company_account_id = ledger_company_accounts.id')
    .joins('LEFT JOIN ledger_accounts on ledger_company_accounts.ledger_detail_account_id = ledger_accounts.id')
    .joins('left join receipts on receipts.id = ledger_transactions.receipt_id')
    .joins('left join outgoing_payments on outgoing_payments.id = ledger_transactions.outgoing_payment_id')
    .joins('LEFT JOIN business_units on ledger_entries.business_unit_id = business_units.id')
    .joins('left join companies on companies.id = ledger_company_accounts.company_id')
    .where(ledger_company_accounts: { company_id: company_id })
    .where(ledger_accounts: { number: accounts })
    .where(business_units: { number: business_units })
    .where(ledger_transactions: { transaction_date: start_date..end_date }).order(1)
    .select('ledger_transactions.id as tr_number', 'ledger_transactions.transaction_type', 'ledger_transactions.transaction_date', 'companies.short_name',
            "(ledger_accounts.number::varchar || ' ' || ledger_company_accounts.name) as name", 'ledger_transactions.description', 'ledger_entries.company_amount',
            'coalesce(receipts.category,outgoing_payments.category) as type', 'coalesce(ledger_transactions.receipt_id,outgoing_payments.id) as document', 'ledger_entries.reconciled',
            'ledger_entries.bank_date', "('/ledger_transactions/' || ledger_transactions.id::varchar) as transaction_link",
            "coalesce(('/receipts/' || ledger_transactions.receipt_id::varchar),('/payments/' || outgoing_payments.id::varchar)) as document_link")
    .map { |r| r.attributes.symbolize_keys }.map(&:compact)
end

.result_report(options = {}) ⇒ Result

Builds the accounting records report for a period.

Parameters:

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

    configurable options

Options Hash (options):

  • company_ids (Array)

    company IDs to filter by (first is used)

  • ledger_account_ids (Array)

    ledger account numbers to filter by

  • business_units_ids (Array)

    business unit numbers to filter by

  • period1_gteq (Date)

    period start date

  • period1_lteq (Date)

    period end date

  • from_report (Integer)

    report origin (1: inception balance, 2: beginning net income)

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/report/accounting_records/accounting_records.rb', line 28

def self.result_report(options = {})
  current_amount = 0
  data = query_period_balance(options[:company_ids][0].to_i, options[:ledger_account_ids], options[:business_units_ids], options[:period1_gteq], options[:period1_lteq])
  inception_amount = query_inception_balance(options[:company_ids][0].to_i, options[:ledger_account_ids], options[:period1_gteq])[0][:total_amount] if options[:from_report] == 1
  inception_amount = query_beginning_netincome(options[:company_ids][0].to_i, options[:ledger_account_ids][0].to_i, options[:period1_gteq])[0][:total_amount] if options[:from_report] == 2
  inception_amount = 0 if inception_amount.nil?
  data.each do |r|
    current_amount += r[:company_amount]
  end

  Result.new(success: true, start_date: options[:period1_gteq], end_date: options[:period1_lteq], inception_balance: inception_amount, current_balance: current_amount, data: data)
end