Class: LedgerAccount

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable, Models::Lineage
Defined in:
app/models/ledger_account.rb

Overview

== Schema Information

Table name: ledger_accounts
Database name: primary

id :integer not null, primary key
classification :string(255)
closed :boolean default(FALSE), not null
description :text
name :string(50) not null
number :integer not null
type :string(255) not null
visible :boolean default(TRUE), not null
created_at :datetime
updated_at :datetime
parent_id :integer

Indexes

idx_id_type (id,type)
idx_la_parent_id (parent_id)
index_ledger_accounts_on_number (number)

Direct Known Subclasses

LedgerDetailAccount, LedgerSummaryAccount

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater

Methods included from Models::Lineage

#ancestors, #ancestors_ids, #children_and_roots, #descendants, #descendants_ids, #ensure_non_recursive_lineage, #family_members, #generate_full_name, #generate_full_name_array, #lineage, #lineage_array, #lineage_simple, #root, #root_id, #self_ancestors_and_descendants, #self_ancestors_and_descendants_ids, #self_and_ancestors, #self_and_ancestors_ids, #self_and_children, #self_and_descendants, #self_and_descendants_ids, #self_and_siblings, #self_and_siblings_ids, #siblings, #siblings_ids

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



31
# File 'app/models/ledger_account.rb', line 31

validates :number, :name, :type, presence: true

#numberObject (readonly)



31
# File 'app/models/ledger_account.rb', line 31

validates :number, :name, :type, presence: true

#typeObject (readonly)



31
# File 'app/models/ledger_account.rb', line 31

validates :number, :name, :type, presence: true

Class Method Details

.availableActiveRecord::Relation<LedgerAccount>

A relation of LedgerAccounts that are available. Active Record Scope

Returns:

See Also:



38
# File 'app/models/ledger_account.rb', line 38

scope :available, -> { where(visible: true, closed: false) }

.company_account_arrayHash{Integer=>Hash}

Pre-computed lookup of LedgerCompanyAccount ids per top-level
account, indexed by account id then by Company number (and a
convenience [USA, CAN] combo entry). Backs report dropdowns
that need to filter entries to the right company subset
without round-tripping the DB per row.

Returns:

  • (Hash{Integer=>Hash})


90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/ledger_account.rb', line 90

def self.
  accounts = []
  LedgerAccount.find_each do ||
    accounts[.id] = {}
    accounts[.id][Company::USA] = .(Company::USA)
    accounts[.id][Company::CAN] = .(Company::CAN)
    accounts[.id][Company::NLD] = .(Company::NLD)
    accounts[.id][[Company::USA, Company::CAN]] = accounts[.id][Company::USA] + accounts[.id][Company::CAN]
  end
  accounts
end

.select_accountArray<Array(String, Integer)>

[label, account_number] pairs — same as select_options but
keyed by account number, used when reports filter on the
human-known account number rather than the surrogate id.

Returns:

  • (Array<Array(String, Integer)>)


57
58
59
# File 'app/models/ledger_account.rb', line 57

def self.
  LedgerAccount.order(:number).pluck(Arel.sql("concat(number, ' ', name)"), :number)
end

.select_bank_balance_accountArray<Array(String, Integer)>

[label, account_number] pairs limited to the bank-related
accounts (cash, AR, money-market) used by the
BankBalanceStatement workflow.

Returns:

  • (Array<Array(String, Integer)>)


65
66
67
# File 'app/models/ledger_account.rb', line 65

def self.
  LedgerAccount.where(number: [1122, 1123, 1130, 1135, 1140, 1141]).order(:number).pluck(Arel.sql("concat(number, ' ', name)"), :number)
end

.select_optionsArray<Array(String, Integer)>

[label, id] pairs for any place that needs to pick an account
by record id (mostly admin form fields).

Returns:

  • (Array<Array(String, Integer)>)


49
50
51
# File 'app/models/ledger_account.rb', line 49

def self.select_options
  LedgerAccount.order(:number).pluck(Arel.sql("concat(number, ' ', name)"), :id)
end

.types_for_selectArray<Array(String, String)>

Detail vs. summary STI-class options for the new-account form.

Returns:

  • (Array<Array(String, String)>)


42
43
44
# File 'app/models/ledger_account.rb', line 42

def self.types_for_select
  [%w[Detail LedgerDetailAccount], %w[Summary LedgerSummaryAccount]]
end

Instance Method Details

#budgetsActiveRecord::Relation<Budget>

Returns:

  • (ActiveRecord::Relation<Budget>)

See Also:



36
# File 'app/models/ledger_account.rb', line 36

has_many :budgets

#ledger_company_accountsActiveRecord::Relation<LedgerCompanyAccount>

Returns:

See Also:



34
# File 'app/models/ledger_account.rb', line 34

has_many :ledger_company_accounts

#ledger_entriesActiveRecord::Relation<LedgerEntry>

Returns:

See Also:



35
# File 'app/models/ledger_account.rb', line 35

has_many :ledger_entries, through: :ledger_company_accounts

#requires_business_unit?Boolean

Whether entries on this account require a BusinessUnit tag.
All P&L accounts (number ≥ 5000) carry one.

Returns:

  • (Boolean)


79
80
81
# File 'app/models/ledger_account.rb', line 79

def requires_business_unit?
  number >= 5000
end

#summary_nameString

"<number> <name>" label used in dropdowns and breadcrumb
strings.

Returns:

  • (String)


72
73
74
# File 'app/models/ledger_account.rb', line 72

def summary_name
  "#{number} #{name}"
end