-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
210 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,13 @@ authors = ["Eric Forgy <[email protected]>"] | |
version = "0.1.0" | ||
|
||
[deps] | ||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
Countries = "d5396ca0-2cf9-11e9-2c96-2532c40e11ac" | ||
ISOCurrencies = "5683cba2-2ced-11e9-0d3f-33b1fe4e9d91" | ||
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" | ||
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" | ||
Positions = "81d999f0-3725-11e9-2196-fd457307c398" | ||
Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# GeneralLedgers.jl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,11 @@ | ||
module GeneralLedgers | ||
|
||
import ISOCurrencies.Currencies: Currency, USD | ||
using Reexport, AbstractTrees | ||
@reexport using Positions | ||
|
||
# There should be three types of accounts: | ||
# 1. GeneralLedger - Has no parent. Has children. Cannot post journal entries directly. | ||
# 2. AccountGroup - Has a parent. Has children. Cannot post journal entries directly. | ||
# 3. LedgerAccount - Has a parent. Has no children. Can post journal entries directly. | ||
|
||
# What about "Modules"? | ||
|
||
# - Financial Reports | ||
# - Audit Controls | ||
# - Inventory Management | ||
# - Invoice / Customer / Purchase Order / Vendor / Receipts Management | ||
|
||
# Account types | ||
# - Balance Sheet | ||
# - Income Statement | ||
# - Retained Earnings | ||
|
||
abstract type Ledger end | ||
abstract type Account end | ||
|
||
struct GeneralLedger <: Ledger | ||
name::String | ||
currency::Currency | ||
children::Dict{String,Union{Ledger,Account}} | ||
|
||
GeneralLedger(name::String,currency::Currency,children=Dict{String,Union{Ledger,Account}}()) = new(name,currency,children) | ||
end | ||
|
||
struct AccountGroup <: Ledger | ||
parent::Ledger | ||
name::String | ||
code::String | ||
children::Dict{String,Union{Ledger,Account}} | ||
|
||
function AccountGroup(parent,name,code) | ||
haskey(parent.children,code) && error("Account code $(code) already exists.") | ||
account = new(parent,name,code,Dict{String,Union{Ledger,Account}}()) | ||
parent.children[code] = account | ||
return account | ||
end | ||
end | ||
|
||
struct DebitAccount <: Account | ||
parent::Ledger | ||
name::String | ||
code::String | ||
|
||
function DebitAccount(parent,name,code) | ||
haskey(parent.children,code) && error("Account code $(code) already exists.") | ||
account = new(parent,name,code) | ||
parent.children[code] = account | ||
return account | ||
end | ||
end | ||
|
||
struct CreditAccount <: Account | ||
parent::Ledger | ||
name::String | ||
code::String | ||
|
||
function CreditAccount(parent,name,code) | ||
haskey(parent.children,code) && error("Account code $(code) already exists.") | ||
account = new(parent,name,code) | ||
parent.children[code] = account | ||
return account | ||
end | ||
end | ||
|
||
include("show.jl") | ||
|
||
function example() | ||
ledger = GeneralLedger("NewCo",USD) | ||
assets = DebitAccount(ledger,"Assets","1") | ||
liabilities = CreditAccount(ledger,"Liabilities","2") | ||
return ledger | ||
end | ||
include("account.jl") | ||
include("entries.jl") | ||
include("display.jl") | ||
include("example.jl") | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
struct GeneralLedger{C} end | ||
struct DebitGroup{C} end | ||
struct CreditGroup{C} end | ||
struct DebitAccount{C} end | ||
struct CreditAccount{C} end | ||
|
||
mutable struct Account{C<:Currency} | ||
parent::Union{Account,Nothing} | ||
name::String | ||
code::Union{String,Nothing} | ||
isdebit::Union{Bool,Nothing} | ||
balance::Union{Position{C},Nothing} | ||
subaccounts::Union{Vector{Account},Nothing} | ||
chart::Union{Dict{String,Account},Nothing} | ||
|
||
function Account{C}(parent,name,code,isdebit,balance,subaccounts,chart) where C<:Currency | ||
a = new(parent,name,code,isdebit,balance,subaccounts,chart) | ||
if !isnothing(parent) | ||
push!(parent.subaccounts,a) | ||
if isnothing(subaccounts) | ||
add(a) | ||
end | ||
end | ||
return a | ||
end | ||
end | ||
|
||
function get_ledger(a::Account) | ||
p = a | ||
while !isnothing(p.parent) | ||
p = p.parent | ||
end | ||
return p | ||
end | ||
|
||
function add(a::Account) | ||
ledger = get_ledger(a) | ||
ledger.chart[a.code] = a | ||
end | ||
|
||
function accounttype(a::Account{C}) where C | ||
if isnothing(a.parent) | ||
return GeneralLedger{C}() | ||
elseif a.isdebit | ||
if isnothing(a.subaccounts) | ||
return DebitAccount{C}() | ||
else | ||
return DebitGroup{C}() | ||
end | ||
else | ||
if isnothing(a.subaccounts) | ||
return CreditAccount{C}() | ||
else | ||
return CreditGroup{C}() | ||
end | ||
end | ||
end | ||
|
||
DebitAccount(p,n,c,b::Position{C}=Position(Currencies.USD,0.)) where C = Account{C}(p,n,c,true,b,nothing,nothing) | ||
CreditAccount(p,n,c,b::Position{C}=Position(Currencies.USD,0.)) where C = Account{C}(p,n,c,false,b,nothing,nothing) | ||
DebitGroup(p,n,c,::C=Currencies.USD) where C = Account{C}(p,n,c,true,nothing,Vector{Account}(),nothing) | ||
CreditGroup(p,n,c,::C=Currencies.USD) where C = Account{C}(p,n,c,false,nothing,Vector{Account}(),nothing) | ||
GeneralLedger(n,::C=Currencies.USD) where C = Account{C}(nothing,n,nothing,true,nothing,Vector{Account}(),Dict{String,Account}()) | ||
|
||
currency(a::Account{C}) where C = C() | ||
|
||
balance(a::Account) = balance(a,accounttype(a)) | ||
balance(a::Account{C}, ::Union{DebitAccount{C},CreditAccount{C}}) where C = a.balance | ||
function balance(a::Account{C}, ::Any) where C | ||
b = Position{C}(0.) | ||
for account in a.subaccounts | ||
if isequal(a.isdebit,account.isdebit) | ||
b += balance(account) | ||
else | ||
b -= balance(account) | ||
end | ||
end | ||
return b | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
AbstractTrees.printnode(x) = AbstractTrees.printnode(stdout,x) | ||
|
||
AbstractTrees.children(a::Account) = _children(a,accounttype(a)) | ||
_children(a::Account,::GeneralLedger) = isnothing(a.subaccounts) ? Vector{Account}() : [a.subaccounts;[a.chart]] | ||
_children(a::Account,::Any) = isnothing(a.subaccounts) ? Vector{Account}() : a.subaccounts | ||
|
||
AbstractTrees.printnode(io::IO,a::Account) = _printnode(io,a,accounttype(a)) | ||
_printnode(io::IO,a::Account,::GeneralLedger{C}) where C = print(io,a.name) | ||
_printnode(io::IO,a::Account,::Union{DebitGroup,CreditGroup}) = print(io,a.name) | ||
_printnode(io::IO,a::Account,::Union{DebitAccount,CreditAccount}) = print(io,"$(a.name): ",balance(a)) | ||
|
||
AbstractTrees.printnode(io::IO,c::Dict{String,Account}) = print(io,"Chart of Accounts:") | ||
|
||
Base.show(io::IO,a::Account) = _show(io,a,accounttype(a)) | ||
_show(io::IO,a::Account,::Union{GeneralLedger,DebitGroup,CreditGroup}) = print_tree(io,a) | ||
_show(io::IO,a::Account,t::Union{DebitAccount,CreditAccount}) = _printnode(io,a,t) | ||
|
||
Base.show(io::IO,a::DebitGroup{C}) where C = print(io,"DebitGroup{",C(),"}") | ||
Base.show(io::IO,a::CreditGroup{C}) where C = print(io,"CreditGroup{",C(),"}") | ||
Base.show(io::IO,a::DebitAccount{C}) where C = print(io,"DebitAccount{",C(),"}") | ||
Base.show(io::IO,a::CreditAccount{C}) where C = print(io,"CreditAccount{",C(),"}") | ||
|
||
AbstractTrees.children(e::Entry) = [e.debit,e.credit,e.amount] | ||
AbstractTrees.printnode(io::IO,c::Entry) = print(io,"Entry") | ||
AbstractTrees.printnode(io::IO,c::Position{C}) where C = print(io,c) | ||
|
||
Base.show(io::IO,c::Position{C}) where C<:Currency = print(io,c.amount," ",C()) | ||
Base.show(io::IO,e::Entry) = print_tree(io,e) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
struct Entry{C<:Currency} | ||
debit::Account{C} | ||
credit::Account{C} | ||
amount::Position{C} | ||
end | ||
|
||
debit!(a::Account{C},c::Position{C}) where C = debit!(a,c,accounttype(a)) | ||
credit!(a::Account{C},c::Position{C}) where C = credit!(a,c,accounttype(a)) | ||
|
||
function debit!(a::Account{C},c::Position{C},::DebitAccount{C}) where C | ||
a.balance += c | ||
return a | ||
end | ||
function debit!(a::Account{C},c::Position{C},::CreditAccount{C}) where C | ||
a.balance -= c | ||
return a | ||
end | ||
function credit!(a::Account{C},c::Position{C},::CreditAccount{C}) where C | ||
a.balance += c | ||
return a | ||
end | ||
function credit!(a::Account{C},c::Position{C},::DebitAccount{C}) where C | ||
a.balance -= c | ||
return a | ||
end | ||
|
||
function post!(e::Entry) | ||
debit!(e.debit,e.amount) | ||
credit!(e.credit,e.amount) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function example() | ||
ledger = GeneralLedger("NewCo") | ||
assets = DebitGroup(ledger,"Assets","1000000") | ||
liabilities = CreditGroup(ledger,"Liabilities","2000000") | ||
cash = DebitAccount(assets,"Cash","1010000") | ||
payable = CreditAccount(liabilities,"Accounts Payable","2010000") | ||
|
||
entry = Entry(cash,payable,Position(Currencies.USD,10.)) | ||
return ledger, assets, liabilities, cash, payable, entry | ||
end |
Oops, something went wrong.