Class: MudratProjector::ChartOfAccounts

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mudrat_projector/chart_of_accounts.rb

Instance Method Summary collapse

Constructor Details

#initializeChartOfAccounts

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  id
  fetch(id).balance + subaccounts_balance(id)
end

#accountsObject



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  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.).add_entry entry
  end
  transaction
end

#balanceObject



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, |
    if .parent?
      sum
    else
      method = %i(asset expense).include?(.type) ? :+ : :-
      sum.public_send method, .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

Returns:

  • (Boolean)


45
46
47
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 45

def exists? 
  @accounts.has_key? 
end

#fetch(account_id) ⇒ Object



49
50
51
# File 'lib/mudrat_projector/chart_of_accounts.rb', line 49

def fetch 
  @accounts.fetch 
end

#net_worthObject



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, (_, )|
    if .type == :asset
      sum + .balance
    elsif .type == :liability
      sum - .balance
    else
      sum
    end
  end
end

#serializeObject



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, )|
    hash[id] = .serialize
    hash
  end
end

#sizeObject



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  id, into: {}
  parent = fetch id
  into.map do |, hash|
    @accounts[] = 
      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, )|
    ary.push subaccount_id if .parent_id == id
    ary
  end
  subaccount_ids.reduce 0 do |sum, id| sum + (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.
    raise Projector::AccountDoesNotExist, "Transaction references non "\
      "existent account #{entry..inspect}"
  end
  open_date = fetch(entry.).open_date
  unless open_date <= transaction_date
    raise Projector::AccountDoesNotExist, "Transaction references account "\
      "#{entry..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