-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger.go
More file actions
266 lines (247 loc) · 6.98 KB
/
ledger.go
File metadata and controls
266 lines (247 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package luca
import (
"fmt"
"path/filepath"
"strings"
"github.com/drummonds/luca/internal/parser"
"github.com/spf13/afero"
)
// A Ledger is a working list of all entries.
// Whereas the document is a list of all the entries in a raw form
type Ledger struct {
// Raw data
Commodities []*parser.Commodity
Accounts []*Account
Transactions []*parser.Transaction
// Helper data Names are case insensitive
CommoditiesMap map[string]*parser.Commodity
AccountsMap map[string]*Account
DefaultCommodity *parser.Commodity
}
func (l *Ledger) AddCommodity(c *parser.Commodity) error {
if c == nil {
return fmt.Errorf("Can't add nil commodity to ledger")
}
if _, ok := l.CommoditiesMap[strings.ToLower(c.Symbol)]; ok {
return fmt.Errorf("commodity %s already exists in file", c.Symbol)
}
l.Commodities = append(l.Commodities, c)
l.CommoditiesMap[strings.ToLower(c.Symbol)] = c
return nil
}
// CheckCommodity used when adding an account.
// This can auto create the commodity if it doesn't exist
// It is assumed that by this point that all the commodities have been added that have been specified.
// and supplies default values if meaningful in which case it returns true
func (l *Ledger) CheckCommoditySymbol(symbol string, autoCreate bool) (bool, error) {
if symbol == "" {
return false, fmt.Errorf("Can't add empty commodity to ledger")
}
symbolKey := strings.ToLower(symbol)
if _, ok := l.CommoditiesMap[symbolKey]; ok {
return false, nil
}
var c *parser.Commodity
switch symbolKey {
case "gbp":
c = &parser.Commodity{
Symbol: "£",
Name: "British Pound",
Sign: "£",
SubUnit: 100,
}
default:
return false, fmt.Errorf("unknown commodity %s", symbol)
}
err := l.AddCommodity(c)
if err != nil {
return false, err
}
return true, nil
}
func (l *Ledger) AddAccount(pa *parser.Account, autoCreate bool) error {
if pa == nil {
return fmt.Errorf("can't add account with empty id")
}
_, err := l.CheckCommoditySymbol(pa.Commodity, autoCreate)
if err != nil {
return err
}
// Now the commoddity is in place so can check and add the account
if _, ok := l.AccountsMap[strings.ToLower(pa.Name)]; ok {
return fmt.Errorf("duplicate account %s already exists in file %s", pa.Name, pa.Filename)
}
a, err := NewAccount(pa, l)
if err != nil {
return err
}
l.Accounts = append(l.Accounts, a)
l.AccountsMap[strings.ToLower(pa.Name)] = a
return nil
}
func (l *Ledger) CheckAccountName(name string, autoCreate bool) (bool, error) {
if name == "" {
return false, fmt.Errorf("can't add account with empty id")
}
if _, ok := l.AccountsMap[strings.ToLower(name)]; ok {
return false, nil
}
if !autoCreate {
return false, fmt.Errorf("account %s not found", name)
}
a := &parser.Account{
Name: name,
}
err := l.AddAccount(a, autoCreate)
if err != nil {
return false, err
}
return true, nil
}
func (l *Ledger) AddTransaction(t *parser.Transaction, autoCreate bool) error {
if t == nil {
return fmt.Errorf("can't add transaction with empty id")
}
// Check that all the accounts are in place
for _, m := range t.Movements {
_, err := l.CheckAccountName(m.From, autoCreate)
if err != nil {
return err
}
_, err = l.CheckAccountName(m.To, autoCreate)
if err != nil {
return err
}
}
// Now the accounts are in place can add transaction
// Todo create sorted list of transactions
l.Transactions = append(l.Transactions, t)
return nil
}
// Assume that all entires are now in the document
// so if in mutiple files they need to be merged or added in
// sequence Commodities --> Accounts --> Transactions
func (l *Ledger) AddDocument(doc *parser.Document, filename string, autoCreate bool) error {
var err error
for _, c := range doc.Commodities {
err = l.AddCommodity(c)
if err != nil {
return err
}
}
l.SetDefaultCommodity()
for _, pa := range doc.Accounts {
if pa.Commodity == "" {
pa.Commodity = l.DefaultCommodity.Symbol
}
err = l.AddAccount(pa, autoCreate)
if err != nil {
return err
}
}
for _, t := range doc.Transactions {
err = l.AddTransaction(t, autoCreate)
if err != nil {
return err
}
}
l.LinkAccountsTransactions()
return nil
}
// Given a directory read all the .luca files and return a Ledger
func NewLedger() (*Ledger, error) {
ledger := &Ledger{}
ledger.CommoditiesMap = make(map[string]*parser.Commodity)
ledger.AccountsMap = make(map[string]*Account)
ledger.Commodities = make([]*parser.Commodity, 0)
ledger.Accounts = make([]*Account, 0)
ledger.Transactions = make([]*parser.Transaction, 0)
return ledger, nil
}
// Given a directory read all the .luca files and return a Ledger
func NewLedgerFrom(dir string, autoCreate bool) (*Ledger, error) {
return NewLedgerFromFs(afero.NewOsFs(), dir, autoCreate)
}
// NewLedgerFromFs creates a new ledger from files in the given directory using the provided filesystem
func NewLedgerFromFs(fs afero.Fs, dir string, autoCreate bool) (*Ledger, error) {
ledger, err := NewLedger()
if err != nil {
return nil, err
}
pattern := filepath.Join(dir, "*.luca")
files, err := afero.Glob(fs, pattern)
if err != nil {
return nil, err
}
for _, file := range files {
content, err := afero.ReadFile(fs, file)
if err != nil {
return nil, err
}
doc, err := parser.Parse(string(content), file)
if err != nil {
return nil, err
}
ledger.AddDocument(doc, file, autoCreate)
}
return ledger, nil
}
// SetDefaultCommodity checks for a single default commodity and sets it
func (l *Ledger) SetDefaultCommodity() error {
switch len(l.Commodities) {
case 0:
// If no commodities, create GBP as default
gbp := &parser.Commodity{
Symbol: "GBP",
Name: "British Pound",
Sign: "£",
SubUnit: 100,
Default: true,
}
if err := l.AddCommodity(gbp); err != nil {
return err
}
l.DefaultCommodity = gbp
return nil
case 1:
// If only one commodity, make it default
l.Commodities[0].Default = true
l.DefaultCommodity = l.Commodities[0]
return nil
default:
// Original logic for multiple commodities
var defaultCommodity *parser.Commodity
for _, c := range l.Commodities {
if c.Default {
if defaultCommodity != nil {
return fmt.Errorf("multiple default commodities found: %s and %s",
defaultCommodity.Symbol, c.Symbol)
}
defaultCommodity = c
}
}
if defaultCommodity != nil {
if l.DefaultCommodity != nil && l.DefaultCommodity != defaultCommodity {
return fmt.Errorf("conflicting default commodity: existing %s vs new %s",
l.DefaultCommodity.Symbol, defaultCommodity.Symbol)
}
l.DefaultCommodity = defaultCommodity
}
}
return nil
}
// linkAccountsTransactions links the accounts to the transactions
// and sorts the movements so that you can get easy
func (l *Ledger) LinkAccountsTransactions() {
for _, t := range l.Transactions {
for _, m := range t.Movements {
fromAccount := l.AccountsMap[strings.ToLower(m.From)]
toAccount := l.AccountsMap[strings.ToLower(m.To)]
fromAccount.LinkMovement(m, t)
toAccount.LinkMovement(m, t)
}
}
for _, a := range l.Accounts {
a.SortMovements()
}
}