@@ -15,11 +15,13 @@ using UUIDs, StructArrays, AbstractTrees
15
15
using Assets, Instruments, Currencies
16
16
17
17
export Identifier, AccountId, AccountType, AccountNumber
18
- export LedgerAccount, Ledger, Entry, Account, AccountGroup, ledgeraccount, add_account!
18
+ export LedgerAccount, Ledger, Entry, Account, AccountGroup, ledgeraccount, add_account
19
19
export id, balance, credit!, debit!, post!, instrument, currency, symbol, amount
20
20
21
21
abstract type Identifier end
22
22
23
+ Base. convert (:: Type{I} , value:: String ) where {I <: Identifier } = I (UUID (value))
24
+
23
25
struct AccountId <: Identifier
24
26
value:: UUID
25
27
end
32
34
33
35
AccountNumber () = AccountNumber (" " )
34
36
37
+ Base. convert (:: Type{AccountNumber} , value:: String ) = AccountNumber (value)
38
+
35
39
abstract type AccountType{P <: Position } end
36
40
37
- accounttype (:: A ) where {A<: AccountType } = A
41
+ accounttype (:: A ) where {A <: AccountType } = A
38
42
39
- accounttype (:: StructArray{A} ) where {A<: AccountType } = A
43
+ accounttype (:: StructArray{A} ) where {A <: AccountType } = A
40
44
41
45
mutable struct LedgerAccount{P <: Position } <: AccountType{P}
42
46
id:: AccountId
@@ -67,18 +71,7 @@ struct Account{P <: Position} <: AccountType{P}
67
71
iscontra:: Bool
68
72
end
69
73
70
- function Account (:: Type{P} , number:: AccountNumber , name, isdebit= true ; parent= nothing , ledger= nothing ) where {P <: Position }
71
- account = LedgerAccount (P, ledger= ledger)
72
- parent === nothing && return Account {P} (account, number, name, isdebit, false )
73
- acc = Account {P} (account, number, name, isdebit, parent. isdebit != = isdebit)
74
- add_account! (parent. accounts, acc)
75
- acc
76
- end
77
-
78
- Account {P} (acc:: Account{P} ) where {P<: Position } = acc
79
-
80
- Account (:: Type{P} = Assets. USD, number:: String = " " ,name:: String = " Default Account" ;ledger= nothing ) where {P <: Position } =
81
- Account (P, AccountNumber (number), name, ledger= ledger)
74
+ Account {P} (acc:: Account{P} ) where {P <: Position } = acc
82
75
83
76
ledgeraccount (acc:: Account ) = acc. account
84
77
100
93
101
94
Ledger (:: Type{P} = Assets. USD) where {P <: Position } = Ledger (Vector {Account{P}} ())
102
95
103
- function add_account! (ledger:: Ledger , acc)
96
+ function add_account (ledger:: Ledger , acc)
104
97
push! (ledger. accounts, ledgeraccount (acc))
105
98
ledger. indexes[id (acc)] = length (ledger. accounts)
106
99
ledger
@@ -112,34 +105,57 @@ struct AccountGroup{A <: AccountType}
112
105
name:: String
113
106
isdebit:: Bool
114
107
iscontra:: Bool
108
+ indexes:: Dict{AccountId,Int}
115
109
accounts:: StructArray{A}
116
110
subgroups:: StructArray{AccountGroup{A}}
117
111
end
118
112
119
113
function AccountGroup (
120
- :: Type{A} ,
121
- number ,
122
- name ,
114
+ :: Type{A} = Account{Assets . USD} ,
115
+ name :: String = " $( symbol (P)) Accounts " ,
116
+ number = " " ,
123
117
isdebit= true ;
124
118
id= AccountId (),
125
- accounts= StructArray (Vector {A} ()),
126
- subgroups= StructArray (Vector {AccountGroup{A}} ()),
127
- parent= nothing
128
- ) where {A <: AccountType }
119
+ parent:: Union{Nothing,AccountGroup{A}} = nothing
120
+ ) where {P <: Position ,A <: AccountType{P} }
121
+ indexes = Dict {AccountId,Int} ()
122
+ accounts = StructArray (Vector {A} ())
123
+ subgroups = StructArray (Vector {AccountGroup{A}} ())
129
124
if parent === nothing
130
- return AccountGroup {A} (id, number, name, isdebit, false , accounts, subgroups)
125
+ return AccountGroup {A} (id, number, name, isdebit, false , indexes, accounts, subgroups)
131
126
else
132
- acc = AccountGroup {A} (id, number, name, isdebit, parent. isdebit != = isdebit, accounts, subgroups)
127
+ acc = AccountGroup {A} (id, number, name, isdebit, parent. isdebit != = isdebit, indexes, accounts, subgroups)
133
128
push! (parent. subgroups, acc)
134
129
return acc
135
130
end
136
131
end
137
132
138
- add_account! (grp:: AccountGroup{A} , acc:: AccountType ) where {A<: AccountType } = push! (grp. accounts, A (acc))
133
+ function Account (
134
+ :: Type{P} = Assets. USD,
135
+ name:: String = " $(symbol (P)) Account" ,
136
+ number= " " ,
137
+ isdebit= true ;
138
+ parent:: Union{Nothing,<:AccountGroup} = nothing ,
139
+ ledger:: Union{Nothing,<:Ledger} = nothing
140
+ ) where {P <: Position }
141
+ account = LedgerAccount (P, ledger= ledger)
142
+ parent === nothing &&
143
+ return Account {P} (account, number, name, isdebit, false )
144
+ acc = Account {P} (account, number, name, isdebit, parent. isdebit != = isdebit)
145
+ add_account (parent, acc)
146
+ acc
147
+ end
139
148
140
- add_account! (grp:: AccountGroup , acc:: AccountGroup ) = push! (grp. subgroups, acc)
149
+ function add_account (grp:: AccountGroup{A} , acc:: AccountType ) where {A <: AccountType }
150
+ push! (grp. accounts, A (acc))
151
+ grp. indexes[id (acc)] = length (grp. accounts)
152
+ grp
153
+ end
141
154
142
- add_account! (grp:: StructArray{A} , acc:: AccountType ) where {A<: AccountType } = push! (grp,A (acc))
155
+ function add_account (grp:: AccountGroup , acc:: AccountGroup )
156
+ push! (grp. subgroups, acc)
157
+ grp
158
+ end
143
159
144
160
struct Entry{P <: Position }
145
161
debit:: Account{P}
@@ -191,6 +207,14 @@ Base.getindex(ledger::Ledger, id::AccountId) =
191
207
Base. getindex (ledger:: Ledger , array:: AbstractVector{<:AccountId} ) =
192
208
ledger. accounts[broadcast (id -> ledger. indexes[id], array)]
193
209
210
+ Base. getindex (grp:: AccountGroup , ix) = grp. accounts. accounts[ix]
211
+
212
+ Base. getindex (grp:: AccountGroup , id:: AccountId ) =
213
+ grp. accounts. accounts[grp. indexes[id]]
214
+
215
+ Base. getindex (grp:: AccountGroup , array:: AbstractVector{<:AccountId} ) =
216
+ grp. accounts[broadcast (id -> grp. indexes[id], array)]
217
+
194
218
struct EntityId <: Identifier
195
219
value:: UUID
196
220
end
0 commit comments