class MudratProjector::ChartOfAccounts
Public Class Methods
new()
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 5 def initialize @accounts = {} end
Public Instance Methods
account_balance(id)
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 9 def account_balance id fetch(id).balance + subaccounts_balance(id) end
accounts()
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 13 def accounts @accounts.keys end
add_account(id, **params)
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 17 def add_account id, **params @accounts[id] = Account.new params end
apply_transaction(transaction)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 41 def each &block @accounts.values.each &block end
exists?(account_id)
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 45 def exists? account_id @accounts.has_key? account_id end
fetch(account_id)
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 49 def fetch account_id @accounts.fetch account_id end
net_worth()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/mudrat_projector/chart_of_accounts.rb, line 65 def size @accounts.size end
split_account(id, into: {})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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