@@ -34,20 +34,24 @@ AccountNumber() = AccountNumber("")
3434
3535abstract type AccountType{P <: Position } end
3636
37+ accounttype (:: A ) where {A<: AccountType } = A
38+
39+ accounttype (:: StructArray{A} ) where {A<: AccountType } = A
40+
3741mutable struct LedgerAccount{P <: Position } <: AccountType{P}
3842 id:: AccountId
3943 balance:: P
4044end
4145
42- function LedgerAccount (balance:: P ; ledger= nothing ) where {P<: Position }
46+ function LedgerAccount (balance:: P ; ledger= nothing ) where {P <: Position }
4347 ledger === nothing && return LedgerAccount {P} (AccountId (), balance)
4448 acc = LedgerAccount {P} (AccountId (), balance)
45- push! (ledger. accounts,acc)
49+ push! (ledger. accounts, acc)
4650 acc
4751end
4852
49- LedgerAccount (:: Type{P} = Assets. USD; ledger= nothing ) where {P<: Position } =
50- LedgerAccount (P (0 ),ledger= ledger)
53+ LedgerAccount (:: Type{P} = Assets. USD; ledger= nothing ) where {P <: Position } =
54+ LedgerAccount (P (0 ), ledger= ledger)
5155
5256ledgeraccount (acc:: LedgerAccount ) = acc
5357
@@ -64,15 +68,17 @@ struct Account{P <: Position} <: AccountType{P}
6468end
6569
6670function Account (:: Type{P} , number:: AccountNumber , name, isdebit= true ; parent= nothing , ledger= nothing ) where {P <: Position }
67- account = LedgerAccount (P,ledger= ledger)
71+ account = LedgerAccount (P, ledger= ledger)
6872 parent === nothing && return Account {P} (account, number, name, isdebit, false )
6973 acc = Account {P} (account, number, name, isdebit, parent. isdebit != = isdebit)
70- push ! (parent. accounts, acc)
74+ add_account ! (parent. accounts, acc)
7175 acc
7276end
7377
74- Account (:: Type{P} = Assets. USD, number:: String = " " ,name:: String = " Default Account" ;ledger= nothing ) where {P<: Position } =
75- Account (P,AccountNumber (number),name,ledger= ledger)
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)
7682
7783ledgeraccount (acc:: Account ) = acc. account
7884
@@ -92,47 +98,49 @@ function Ledger(accounts::Vector{Account{P}}; id=LedgerId()) where {P <: Positio
9298 Ledger {P} (id, indexes, StructArray (ledgeraccount .(accounts)))
9399end
94100
95- Ledger (:: Type{P} = Assets. USD) where {P<: Position } = Ledger (Vector {Account{P}} ())
101+ Ledger (:: Type{P} = Assets. USD) where {P <: Position } = Ledger (Vector {Account{P}} ())
96102
97103function add_account! (ledger:: Ledger , acc)
98104 push! (ledger. accounts, ledgeraccount (acc))
99105 ledger. indexes[id (acc)] = length (ledger. accounts)
100106 ledger
101107end
102108
103- struct AccountGroup{P <: Position }
109+ struct AccountGroup{A <: AccountType }
104110 id:: AccountId
105111 number:: AccountNumber
106112 name:: String
107113 isdebit:: Bool
108114 iscontra:: Bool
109- accounts:: StructArray{Account{P} }
110- subgroups:: StructArray{AccountGroup{P }}
115+ accounts:: StructArray{A }
116+ subgroups:: StructArray{AccountGroup{A }}
111117end
112118
113119function AccountGroup (
114- :: Type{P } ,
120+ :: Type{A } ,
115121 number,
116122 name,
117123 isdebit= true ;
118124 id= AccountId (),
119- accounts= StructArray (Vector {Account{P} } ()),
120- subgroups= StructArray (Vector {AccountGroup{P }} ()),
125+ accounts= StructArray (Vector {A } ()),
126+ subgroups= StructArray (Vector {AccountGroup{A }} ()),
121127 parent= nothing
122- ) where {P <: Position }
128+ ) where {A <: AccountType }
123129 if parent === nothing
124- return AccountGroup {P } (id, number, name, isdebit, false , accounts, subgroups)
130+ return AccountGroup {A } (id, number, name, isdebit, false , accounts, subgroups)
125131 else
126- acc = AccountGroup {P } (id, number, name, isdebit, parent. isdebit != = isdebit, accounts, subgroups)
132+ acc = AccountGroup {A } (id, number, name, isdebit, parent. isdebit != = isdebit, accounts, subgroups)
127133 push! (parent. subgroups, acc)
128134 return acc
129135 end
130136end
131137
132- add_account! (grp:: AccountGroup , acc:: AccountType ) = push! (grp. accounts, acc)
138+ add_account! (grp:: AccountGroup{A} , acc:: AccountType ) where {A <: AccountType } = push! (grp. accounts, A ( acc) )
133139
134140add_account! (grp:: AccountGroup , acc:: AccountGroup ) = push! (grp. subgroups, acc)
135141
142+ add_account! (grp:: StructArray{A} , acc:: AccountType ) where {A<: AccountType } = push! (grp,A (acc))
143+
136144struct Entry{P <: Position }
137145 debit:: Account{P}
138146 credit:: Account{P}
@@ -144,7 +152,7 @@ id(acc::AccountGroup) = acc.id
144152
145153balance (acc) = ledgeraccount (acc). balance
146154
147- function balance (group:: AccountGroup{P } ) where {P <: Position }
155+ function balance (group:: AccountGroup{<:AccountType{P} } ) where {P <: Position }
148156 btot = zero (P)
149157 for acc in group. accounts. account
150158 btot += balance (acc)
@@ -181,7 +189,7 @@ Base.getindex(ledger::Ledger, id::AccountId) =
181189 ledger. accounts[ledger. indexes[id]]
182190
183191Base. getindex (ledger:: Ledger , array:: AbstractVector{<:AccountId} ) =
184- ledger. accounts[broadcast (id-> ledger. indexes[id], array)]
192+ ledger. accounts[broadcast (id -> ledger. indexes[id], array)]
185193
186194struct EntityId <: Identifier
187195 value:: UUID
@@ -192,7 +200,7 @@ EntityId() = EntityId(uuid4())
192200struct Entity
193201 id:: EntityId
194202 name:: String
195- ledgers:: Dict{P,Ledger{P}} where {P<: Position }
203+ ledgers:: Dict{P,Ledger{P}} where {P <: Position }
196204end
197205
198206
@@ -237,34 +245,36 @@ end
237245# return newaccount
238246# end
239247
248+ _print_account (io:: IO , acc) = print (io,
249+ isempty (acc. number. value) ? " " : " [$(acc. number) ] " ,
250+ " $(acc. name) : $(acc. isdebit ? balance (acc) : - balance (acc)) "
251+ )
252+
240253Base. show (io:: IO , id:: Identifier ) = print (io, id. value)
241254
242255Base. show (io:: IO , number:: AccountNumber ) = print (io, number. value)
243256
244257Base. show (io:: IO , acc:: LedgerAccount ) = print (io, " $(string (id (acc))) : $(balance (acc)) " )
245258
259+ Base. show (io:: IO , acc:: Account ) = _print_account (io, acc)
260+
246261Base. show (io:: IO , acc:: AccountGroup ) = print_tree (io, acc)
247262
248263Base. show (io:: IO , entry:: Entry ) = print_tree (io, entry)
249264
250- Base. show (io:: IO , ledger:: Ledger ) = print_tree (io,ledger)
265+ Base. show (io:: IO , ledger:: Ledger ) = print_tree (io, ledger)
251266
252- Base. show (io:: IO , a:: Vector{<:AccountType} ) = print_tree (io,a)
267+ Base. show (io:: IO , a:: Vector{<:AccountType} ) = print_tree (io, a)
268+
269+ AbstractTrees. printnode (io:: IO , acc:: Ledger{P} ) where {P <: Position } =
270+ print (io, " $(symbol (P)) Ledger: [$(acc. id) ]" )
253271
254272AbstractTrees. children (acc:: Ledger ) = Vector (acc. accounts)
255273
256- AbstractTrees. printnode (io:: IO , acc:: Ledger{P} ) where {P<: Position } =
257- print (io, " $(symbol (P)) Ledger: [$(acc. id) ]" )
274+ AbstractTrees. printnode (io:: IO , acc:: AccountGroup ) = _print_account (io, acc)
258275
259276AbstractTrees. children (acc:: AccountGroup ) = vcat (Vector (acc. subgroups), Vector (acc. accounts))
260277
261- function AbstractTrees. printnode (io:: IO , acc:: AccountGroup )
262- print (io,
263- isempty (acc. number. value) ? " " : " [$(acc. number) ] " ,
264- " $(acc. name) : $(acc. isdebit ? balance (acc) : - balance (acc)) "
265- )
266- end
267-
268278AbstractTrees. printnode (io:: IO , b:: Vector{<:AccountType} ) =
269279 isempty (b) ? print (io, " Accounts: None" ) : print (io, " Accounts:" )
270280
0 commit comments