Class: MudratProjector::ChartOfAccounts
- Inherits:
-
Object
- Object
- MudratProjector::ChartOfAccounts
- Includes:
- Enumerable
- Defined in:
- lib/mudrat_projector/chart_of_accounts.rb
Instance Method Summary collapse
- #account_balance(id) ⇒ Object
- #accounts ⇒ Object
- #add_account(id, **params) ⇒ Object
- #apply_transaction(transaction) ⇒ Object
- #balance ⇒ Object
- #each(&block) ⇒ Object
- #exists?(account_id) ⇒ Boolean
- #fetch(account_id) ⇒ Object
-
#initialize ⇒ ChartOfAccounts
constructor
A new instance of ChartOfAccounts.
- #net_worth ⇒ Object
- #serialize ⇒ Object
- #size ⇒ Object
- #split_account(id, into: {}) ⇒ Object
- #subaccounts_balance(id) ⇒ Object
- #validate_entry!(transaction_date, entry) ⇒ Object
- #validate_transaction!(transaction) ⇒ Object
Constructor Details
#initialize ⇒ ChartOfAccounts
Returns a new instance of ChartOfAccounts.
5 6 7 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 5 def initialize @accounts = {} end |
Instance Method Details
#account_balance(id) ⇒ Object
9 10 11 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 9 def account_balance id fetch(id).balance + subaccounts_balance(id) end |
#accounts ⇒ Object
13 14 15 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 13 def accounts @accounts.keys end |
#add_account(id, **params) ⇒ Object
17 18 19 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 17 def add_account id, **params @accounts[id] = Account.new params end |
#apply_transaction(transaction) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 21 def apply_transaction transaction validate_transaction! transaction transaction.entries.each do |entry| entry.calculate self; end transaction.entries.each do |entry| fetch(entry.account_id).add_entry entry end transaction end |
#balance ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 30 def balance inject 0 do |sum, account| if account.parent? sum else method = %i(asset expense).include?(account.type) ? :+ : :- sum.public_send method, account.balance end end end |
#each(&block) ⇒ Object
41 42 43 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 41 def each &block @accounts.values.each &block end |
#exists?(account_id) ⇒ Boolean
45 46 47 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 45 def exists? account_id @accounts.has_key? account_id end |
#fetch(account_id) ⇒ Object
49 50 51 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 49 def fetch account_id @accounts.fetch account_id end |
#net_worth ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 53 def net_worth @accounts.reduce 0 do |sum, (_, account)| if account.type == :asset sum + account.balance elsif account.type == :liability sum - account.balance else sum end end end |
#serialize ⇒ Object
69 70 71 72 73 74 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 69 def serialize @accounts.reduce Hash.new do |hash, (id, account)| hash[id] = account.serialize hash end end |
#size ⇒ Object
65 66 67 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 65 def size @accounts.size end |
#split_account(id, into: {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 76 def split_account id, into: {} parent = fetch id into.map do |sub_account_id, hash| @accounts[sub_account_id] = if hash parent.create_child( opening_balance: hash[:amount], parent_id: id, tags: hash[:tags], ) else parent.create_child parent_id: id end end end |
#subaccounts_balance(id) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 92 def subaccounts_balance id subaccount_ids = @accounts.reduce [] do |ary, (subaccount_id, account)| ary.push subaccount_id if account.parent_id == id ary end subaccount_ids.reduce 0 do |sum, id| sum + account_balance(id); end end |
#validate_entry!(transaction_date, entry) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 106 def validate_entry! transaction_date, entry unless @accounts.has_key? entry.account_id raise Projector::AccountDoesNotExist, "Transaction references non "\ "existent account #{entry.account_id.inspect}" end open_date = fetch(entry.account_id).open_date unless open_date <= transaction_date raise Projector::AccountDoesNotExist, "Transaction references account "\ "#{entry.account_id.inspect} which does not open until #{open_date}, "\ "but transaction is set for #{transaction_date}" end end |
#validate_transaction!(transaction) ⇒ Object
100 101 102 103 104 |
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 100 def validate_transaction! transaction transaction.each do |entry| validate_entry! transaction.date, entry end end |