Skip to content

Commit ed93080

Browse files
Eric ForgyEric Forgy
authored andcommitted
Update
- Change AccountGroup parameter to <: AccountType - Add accounttype method - Improve printing
1 parent 563295e commit ed93080

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["Finance", "Ledger"]
44
license = "MIT"
55
desc = "Financial ledgers"
66
authors = ["Eric Forgy <[email protected]>", "ScottPJones <[email protected]>"]
7-
version = "0.5.0"
7+
version = "0.6.0"
88

99
[deps]
1010
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/Ledgers.jl

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,24 @@ AccountNumber() = AccountNumber("")
3434

3535
abstract type AccountType{P <: Position} end
3636

37+
accounttype(::A) where {A<:AccountType} = A
38+
39+
accounttype(::StructArray{A}) where {A<:AccountType} = A
40+
3741
mutable struct LedgerAccount{P <: Position} <: AccountType{P}
3842
id::AccountId
3943
balance::P
4044
end
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
4751
end
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

5256
ledgeraccount(acc::LedgerAccount) = acc
5357

@@ -64,15 +68,17 @@ struct Account{P <: Position} <: AccountType{P}
6468
end
6569

6670
function 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
7276
end
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

7783
ledgeraccount(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)))
9399
end
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

97103
function add_account!(ledger::Ledger, acc)
98104
push!(ledger.accounts, ledgeraccount(acc))
99105
ledger.indexes[id(acc)] = length(ledger.accounts)
100106
ledger
101107
end
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}}
111117
end
112118

113119
function 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
130136
end
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

134140
add_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+
136144
struct Entry{P <: Position}
137145
debit::Account{P}
138146
credit::Account{P}
@@ -144,7 +152,7 @@ id(acc::AccountGroup) = acc.id
144152

145153
balance(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

183191
Base.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

186194
struct EntityId <: Identifier
187195
value::UUID
@@ -192,7 +200,7 @@ EntityId() = EntityId(uuid4())
192200
struct 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}
196204
end
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+
240253
Base.show(io::IO, id::Identifier) = print(io, id.value)
241254

242255
Base.show(io::IO, number::AccountNumber) = print(io, number.value)
243256

244257
Base.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+
246261
Base.show(io::IO, acc::AccountGroup) = print_tree(io, acc)
247262

248263
Base.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

254272
AbstractTrees.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

259276
AbstractTrees.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-
268278
AbstractTrees.printnode(io::IO, b::Vector{<:AccountType}) =
269279
isempty(b) ? print(io, "Accounts: None") : print(io, "Accounts:")
270280

0 commit comments

Comments
 (0)