Class: Report::AccountingRecords::AccountingRecords
- Inherits:
-
Object
- Object
- Report::AccountingRecords::AccountingRecords
- Defined in:
- app/services/report/accounting_records/accounting_records.rb
Overview
Service object: accounting records.
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .query_beginning_netincome(company_id, account_number, start_date) ⇒ Object
- .query_inception_balance(company_id, account_number, start_date) ⇒ Object
- .query_period_balance(company_id, account_number, business_unit, start_date, end_date) ⇒ Object
- .result_report(options = {}) ⇒ Object
Class Method Details
.query_beginning_netincome(company_id, account_number, start_date) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/services/report/accounting_records/accounting_records.rb', line 61 def self.query_beginning_netincome(company_id, account_number, 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: account_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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/report/accounting_records/accounting_records.rb', line 46 def self.query_inception_balance(company_id, account_number, start_date) accounts = account_number.empty? ? LedgerAccount.order(:number).pluck(:number).map(&:to_i) : account_number.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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/report/accounting_records/accounting_records.rb', line 23 def self.query_period_balance(company_id, account_number, business_unit, start_date, end_date) accounts = account_number.empty? ? LedgerAccount.order(:number).pluck(:number).map(&:to_i) : account_number.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 = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/services/report/accounting_records/accounting_records.rb', line 10 def self.result_report( = {}) current_amount = 0 data = query_period_balance([:company_ids][0].to_i, [:ledger_account_ids], [:business_units_ids], [:period1_gteq], [:period1_lteq]) inception_amount = query_inception_balance([:company_ids][0].to_i, [:ledger_account_ids], [:period1_gteq])[0][:total_amount] if [:from_report] == 1 inception_amount = query_beginning_netincome([:company_ids][0].to_i, [:ledger_account_ids][0].to_i, [:period1_gteq])[0][:total_amount] if [: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: [:period1_gteq], end_date: [:period1_lteq], inception_balance: inception_amount, current_balance: current_amount, data: data) end |