Skip to content

Commit ba73719

Browse files
authored
Merge pull request #8 from JuliaFinance/spj/wip
WIP changes
2 parents 5cf0b1e + f783638 commit ba73719

File tree

4 files changed

+62
-67
lines changed

4 files changed

+62
-67
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
1414
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1515

1616
[compat]
17-
Instruments = ">= 0.7"
18-
Assets = ">= 0.7"
17+
Instruments = ">= 0.8"
18+
Assets = ">= 0.9"
1919

2020
[extras]
2121
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
[julia-url]: https://github.com/JuliaLang/Julia
2-
[julia-release]:https://img.shields.io/github/release/JuliaLang/julia.svg
1+
### WORK IN PROGRESS - DO NOT USE ###
32

43
[travis-url]: https://travis-ci.com/JuliaFinance/Ledgers.jl
5-
[travis-s-img]: https://travis-ci.com/JuliaFinance/Ledgers.jl.svg
6-
[travis-m-img]: https://travis-ci.com/JuliaFinance/Ledgers.jl.svg?branch=main
4+
[travis-img]: https://travis-ci.com/JuliaFinance/Ledgers.jl.svg
75

86
[codecov-url]: https://codecov.io/gh/JuliaFinance/Ledgers.jl
9-
[codecov-img]: https://codecov.io/gh/JuliaFinance/Ledgers.jl/branch/master/graph/badge.svg
7+
[codecov-img]: https://codecov.io/gh/JuliaFinance/Ledgers.jl/branch/main/graph/badge.svg
108

11-
| **Julia Version** | **Unit Tests** | **Coverage** |
12-
|:------------------:|:---------------------:|:---------------------:|
13-
| [![][julia-release]][julia-url] | [![][travis-s-img]][travis-url] | [![][codecov-img]][codecov-url]
14-
| Latest | [![][travis-m-img]][travis-url] | [![][codecov-img]][codecov-url]
9+
[![][travis-img]][travis-url] [![][codecov-img]][codecov-url]
1510

1611
# Ledgers

src/Ledgers.jl

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module Ledgers
1414
using UUIDs, StructArrays, AbstractTrees
1515
using Instruments
1616
import Instruments: instrument, symbol, amount, name, currency
17-
using Assets: USD
17+
using Assets
18+
@cash USD
1819

1920
export Account, Ledger, Entry, AccountId, AccountCode, AccountInfo, AccountGroup
2021
export id, balance, credit!, debit!, post!, instrument, symbol, amount, code, name, currency
@@ -32,65 +33,60 @@ struct AccountCode
3233
value::String
3334
end
3435

35-
abstract type AccountType{B <: Position} end
36+
abstract type AccountType{P<:Position} end
3637

37-
mutable struct Account{B <: Position} <: AccountType{B}
38+
mutable struct Account{P<:Position} <: AccountType{P}
3839
id::AccountId
39-
balance::B
40+
balance::P
4041
end
4142

4243
Account(balance::Position) = Account{typeof(balance)}(AccountId(), balance)
4344

44-
abstract type AccountNode{B <: Position} <: AccountType{B} end
45+
abstract type AccountNode{P<:Position} <: AccountType{P} end
4546

46-
struct AccountInfo{B <: Position} <: AccountNode{B}
47-
account::Account{B}
47+
struct AccountInfo{P<:Position} <: AccountNode{P}
48+
account::Account{P}
4849
code::AccountCode
4950
name::String
5051
isdebit::Bool
5152

52-
function AccountInfo{B}(account::Account{B}, code, name, isdebit=true, parent=nothing) where {B <: Position}
53-
acc = new{B}(account, code, name, isdebit)
53+
function AccountInfo{P}(account::Account{P}, code, name, isdebit=true, parent=nothing
54+
) where {P<:Position}
55+
#println("Create a new AccountInfo{$P}, id=$id, code=$code, name=$name")
56+
acc = new{P}(account, code, name, isdebit)
5457
parent === nothing || push!(parent.subaccounts, acc)
5558
acc
5659
end
5760
end
5861

59-
AccountInfo(account::Account{B}, code, name, isdebit=true, parent=nothing) where {B <: Position} =
60-
AccountInfo{B}(account, code, name, isdebit, parent)
62+
AccountInfo(account::Account{P}, code, name, args...) where {P<:Position} =
63+
AccountInfo{P}(account, code, name, args...)
6164

62-
struct AccountGroup{B <: Position} <: AccountNode{B}
65+
mutable struct AccountGroup{P<:Position} <: AccountNode{P}
6366
id::AccountId
6467
code::AccountCode
6568
name::String
6669
isdebit::Bool
67-
parent::Union{Nothing,AccountGroup{B}}
68-
subaccounts::StructArray{AccountInfo{B}}
69-
subgroups::StructArray{AccountGroup{B}}
70-
71-
function AccountGroup{B}(
72-
id,
73-
code,
74-
name,
75-
isdebit=true,
76-
parent=nothing,
77-
subaccounts=StructArray(Vector{AccountInfo{B}}()),
78-
subgroups=StructArray(Vector{AccountGroup{B}}())) where {B <: Position}
79-
acc = new{B}(id, code, name, isdebit, parent, subaccounts, subgroups)
70+
71+
parent::Union{Nothing,AccountGroup{P}}
72+
subaccounts::Vector{AccountInfo{P}}
73+
subgroups::Vector{AccountGroup{P}}
74+
75+
function AccountGroup{P}(id, code, name,
76+
isdebit=true,
77+
parent=nothing,
78+
subaccounts=Vector{AccountInfo{P}}(),
79+
subgroups=Vector{AccountGroup{P}}()
80+
) where {P<:Position}
81+
#println("Create a new AccountGroup{$P}, id=$id, code=$code, name=$name")
82+
acc = new{P}(id, code, name, isdebit, parent, subaccounts, subgroups)
8083
parent === nothing || push!(parent.subgroups, acc)
8184
acc
8285
end
8386
end
8487

85-
AccountGroup(
86-
::B,
87-
code,
88-
name,
89-
isdebit=true,
90-
parent=nothing,
91-
subaccounts=StructArray(Vector{AccountInfo{B}}()),
92-
subgroups=StructArray(Vector{AccountGroup{B}}())) where {B <: Position} =
93-
AccountGroup{B}(AccountId(), code, name, isdebit, parent, subaccounts, subgroups)
88+
AccountGroup(::P, code, name, args...) where {P<:Position} =
89+
AccountGroup{P}(AccountId(), code, name, args...)
9490

9591
# Identity function (to make code more generic)
9692
account(acc::AccountType) = acc
@@ -107,24 +103,27 @@ subgroups(group::AccountGroup) = group.subgroups
107103
id(acc::AccountType) = account(acc).id
108104

109105
balance(acc) = account(acc).balance
110-
balance(group::AccountGroup) = isempty(subgroups(group)) ?
111-
sum(balance.(subaccounts(group))) :
112-
sum(balance.(subaccounts(group))) + sum(balance.(subgroups(group)))
113106

114-
instrument(::AccountType{B}) where {B <: Position} = instrument(B)
107+
function balance(grp::AccountGroup{P}) where {P<:Position}
108+
sa = subaccounts(grp)
109+
sg = subgroups(grp)
110+
(isempty(sa) ? zero(P) : sum(balance, sa)) + (isempty(sg) ? zero(P) : sum(balance, sg))
111+
end
112+
113+
instrument(::AccountType{P}) where {P<:Position} = instrument(P)
115114

116-
symbol(::AccountType{B}) where {B <: Position} = symbol(B)
115+
symbol(::AccountType{P}) where {P<:Position} = symbol(P)
117116

118-
currency(::AccountType{B}) where {B <: Position} = currency(B)
117+
currency(::AccountType{P}) where {P<:Position} = currency(P)
119118

120119
amount(acc::AccountType) = amount(balance(acc))
121120

122121
debit!(acc::Account, amt::Position) = (acc.balance += amt)
123122
credit!(acc::Account, amt::Position) = (acc.balance -= amt)
124123

125-
struct Entry{B <: Position}
126-
debit::AccountInfo{B}
127-
credit::AccountInfo{B}
124+
struct Entry{P<:Position}
125+
debit::AccountInfo{P}
126+
credit::AccountInfo{P}
128127
end
129128

130129
function post!(entry::Entry, amt::Position)
@@ -133,11 +132,12 @@ function post!(entry::Entry, amt::Position)
133132
entry
134133
end
135134

136-
struct Ledger{P <: Position}
135+
# SPJ: we should probably retain the id of the Ledger here
136+
struct Ledger{P<:Position}
137137
indexes::Dict{AccountId,Int}
138138
accounts::StructArray{Account{P}}
139139

140-
function Ledger(accounts::Vector{Account{P}}) where {P <: Position}
140+
function Ledger(accounts::Vector{Account{P}}) where {P<:Position}
141141
indexes = Dict{AccountId,Int}()
142142
for (index, account) in enumerate(accounts)
143143
indexes[id(account)] = index

test/runtests.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
using Ledgers, Test
2-
using Assets: USD
1+
using Ledgers, Test, Assets
2+
@cash USD
33

4-
group, assets, liabilities, cash, payable, entry = Ledgers.example()
4+
group, assets, liabilities, mycash, payable, entry = Ledgers.example()
55

66
@testset "Account creation" begin
77
@test id(group) isa AccountId
88
@test id(assets) isa AccountId
9-
@test code(cash).value === "1010000"
9+
@test code(mycash).value === "1010000"
1010
@test name(payable) === "Accounts Payable"
11-
@test balance(group) === 0USD
12-
@test balance(assets) === 0USD
13-
@test balance(liabilities) === 0USD
11+
@test balance(group) === 0usd
12+
@test balance(assets) === 0usd
13+
@test balance(liabilities) === 0usd
1414
end
1515

1616
@testset "Post entry" begin
17-
amt = 10USD
17+
amt = 10usd
1818
post!(entry, amt)
19-
@test balance(group) === 0USD
19+
@test balance(group) === 0usd
2020
@test balance(assets) === amt
2121
@test balance(liabilities) === -amt
22-
@test balance(cash) === amt
22+
@test balance(mycash) === amt
2323
@test balance(payable) === -amt
24-
end
24+
end

0 commit comments

Comments
 (0)