Class: Report::AccountingRecords::AccountingRecords

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.query_beginning_netincome(company_id, account_number, start_date) ⇒ Object



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

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 between ? and ?',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{ |a| a.compact }
end

.query_inception_balance(company_id, account_number, start_date) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/report/accounting_records/accounting_records.rb', line 44

def self.query_inception_balance(company_id,,start_date)
  .empty? ? accounts = LedgerAccount.all.order(:number).pluck(:number).map{ |r| r.to_i } : accounts = .map{ |r| r.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 in (?)', accounts)
    .where("ledger_transactions.transaction_date < ?",start_date)
    .select('sum(ledger_entries.company_amount) as total_amount').map { |r| r.attributes.symbolize_keys }.map{ |a| a.compact }
end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/report/accounting_records/accounting_records.rb', line 21

def self.query_period_balance(company_id,,business_unit,start_date,end_date)
  .empty? ? accounts = LedgerAccount.all.order(:number).pluck(:number).map{ |r| r.to_i } : accounts = .map{ |r| r.to_i }
  business_unit.empty? ? business_units = BusinessUnit.all.order(:number).pluck(:number).map{ |r| r.to_i } : business_units = business_unit.map{ |r| r.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 in (?)', accounts)
    .where('business_units.number in (?)', business_units)
    .where("ledger_transactions.transaction_date between ? and ?",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{ |a| a.compact }
end

.result_report(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/report/accounting_records/accounting_records.rb', line 8

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 == nil ? inception_amount = 0 : inception_amount = inception_amount
  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