-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin.go
382 lines (351 loc) · 8.68 KB
/
coin.go
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Package coin implements a plaintext accounting domail model closely following that of the ledger-cli tool.
package coin
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/mkobetic/coin/check"
"github.com/mkobetic/coin/check/warn"
)
const (
// Default file names and extensions
CoinExtension = ".coin"
AccountsFilename = "accounts" + CoinExtension
CommoditiesFilename = "commodities" + CoinExtension
PricesFilename = "prices" + CoinExtension
PricesExtension = ".prices"
TransactionsFilename = "transactions" + CoinExtension
TransactionsExtension = CoinExtension
)
var (
DB = os.Getenv("COINDB")
DefaultCommodityId = "CAD"
AccountsFile = filepath.Join(DB, AccountsFilename)
CommoditiesFile = filepath.Join(DB, CommoditiesFilename)
PricesFile = filepath.Join(DB, PricesFilename)
TransactionsFile = filepath.Join(DB, TransactionsFilename)
Tests []*Test
Root *Account
Unbalanced *Account
AccountsByName = map[string]*Account{}
// Build Parameters
Built string // time built in UTC
Commit string // source commit SHA
Branch string // source branch
GoVersion string // Go version used to build
)
func DefaultCommodity() *Commodity {
return MustFindCommodity(DefaultCommodityId, "default commodity")
}
func MustFindCommodity(id string, location string) *Commodity {
if c := Commodities[id]; c != nil {
return c
}
panic(fmt.Errorf("cannot find commodity %s\n\t%s\n", id, location))
}
func LoadPrices() {
LoadFile(CommoditiesFile)
if _, err := os.Stat(PricesFile); os.IsNotExist(err) {
files, _ := filepath.Glob(filepath.Join(DB, "*.prices"))
for _, f := range files {
LoadFile(f)
}
} else {
LoadFile(PricesFile)
}
}
func LoadAll() {
LoadPrices()
LoadFile(AccountsFile)
if _, err := os.Stat(TransactionsFile); os.IsNotExist(err) {
files, _ := filepath.Glob(filepath.Join(DB, "*.coin"))
for _, f := range files {
if f != CommoditiesFile && f != AccountsFile {
LoadFile(f)
}
}
} else {
LoadFile(TransactionsFile)
}
ResolveAll()
}
func LoadFile(filename string) {
file, err := os.Open(filename)
check.NoError(err, "Failed to open %s", filename)
defer file.Close()
Load(file, filename)
}
func Load(r io.Reader, fn string) {
p := NewParser(r)
for {
i, err := p.Next(fn)
check.NoError(err, "Parsing error")
if i == nil {
return
}
switch i := i.(type) {
case *Commodity:
Commodities[i.Id] = i
if i.Symbol != "" {
CommoditiesBySymbol[i.Symbol] = i
}
case *Account:
if i.FullName == "" {
panic(fmt.Errorf("INVALID %#v", i))
}
AccountsByName[i.FullName] = i
case *Price:
Prices = append(Prices, i)
case *Transaction:
Transactions = append(Transactions, i)
case *Test:
Tests = append(Tests, i)
case *Include:
files, err := i.Files()
check.NoError(err, "Failed to resolve include %s", i.Path)
for _, f := range files {
// This won't catch nested loops, but should catch the easier to make errors.
check.If(f != fn, "Include loop detected %s", f)
LoadFile(f)
}
default:
fmt.Fprintf(os.Stderr, "Unknown entity %T", i)
os.Exit(1)
}
}
}
func ResolveAll() {
ResolvePrices()
ResolveAccounts()
ResolveTransactions(true)
}
func ResolvePrices() {
for _, p := range Prices {
p.Commodity = MustFindCommodity(p.CommodityId, p.Location())
p.Currency = MustFindCommodity(p.currencyId, p.Location())
p.Commodity.AddPrice(p)
}
// Sort commodity prices.
for _, c := range Commodities {
for _, p := range c.Prices {
sort.Slice(p, func(i, j int) bool {
return p[i].Time.After(p[j].Time)
})
}
}
sort.Slice(Prices, func(i, j int) bool {
return Prices[i].Time.Before(Prices[j].Time)
})
}
func ResolveAccounts() {
if Root == nil {
Root = AccountsByName["Root"]
if Root == nil {
Root = accountFromName("Root")
AccountsByName["Root"] = Root
}
}
if Unbalanced == nil {
Unbalanced = accountFromName("Unbalanced")
AccountsByName["Unbalanced"] = Unbalanced
}
// create parents if missing
var known []*Account
for _, a := range AccountsByName {
known = append(known, a)
}
for _, a := range known {
fn := a.FullName
for {
fn, _ = parentAndName(fn)
if fn == "" {
break
}
if AccountsByName[fn] == nil {
AccountsByName[fn] = accountFromName(fn)
}
}
}
// link parents with children,
for _, a := range AccountsByName {
if a == Root {
continue
}
pName := a.ParentName()
if pName == "" {
Root.adopt(a)
continue
}
p := AccountsByName[pName]
check.If(p != nil, "Missing account %s (%s)\n", pName, a.Location())
p.adopt(a)
}
isChild := func(p, c *Account) bool {
for _, a := range p.Children {
if a == c {
return true
}
}
return false
}
// sort children, link commodities
for _, a := range AccountsByName {
if pn := a.ParentName(); pn != "" {
warn.If(!(pn == a.Parent.FullName), "%s doesn't match parent name %s\n", a.Parent.Name, pn)
warn.If(!isChild(a.Parent, a), "%s is not a child of %s\n", a.FullName, a.Parent.FullName)
}
if a.CommodityId != "" {
a.Commodity = MustFindCommodity(a.CommodityId, a.Location())
} else {
a.Commodity = DefaultCommodity()
}
sort.Slice(a.Children, func(i, j int) bool {
return a.Children[i].Name < a.Children[j].Name
})
}
}
func ResolveTransactions(checkPostings bool) {
for _, t := range Transactions {
// t.Currency = MustFindCommodity(t.currencyId)
var commodity *Commodity
var commodities = map[*Commodity]bool{}
for _, s := range t.Postings {
s.Account = MustFindAccount(s.accountName)
s.Account.Postings = append(s.Account.Postings, s)
commodity = s.Account.Commodity
commodities[commodity] = true
}
if len(commodities) > 1 {
// Postings with different commodities, make sure amounts are set
for _, s := range t.Postings {
check.If(s.Quantity != nil, "Posting without quantity in mixed transaction: %s", t.Location())
}
continue
}
// All postings with the same commodity make sure transaction is balanced
var empty *Posting
var total = NewZeroAmount(commodity)
for _, s := range t.Postings {
if s.Quantity == nil {
check.If(empty == nil, "Multiple postings without quantity: %s", t.Location())
empty = s
} else {
err := total.AddIn(s.Quantity)
check.NoError(err, "cannot compute transaction total: %s", t.Location())
}
}
if empty == nil {
check.If(total.IsZero(), "Transaction is not balanced %f: %s", total, t.Location())
} else {
empty.Quantity = total.Negated()
}
}
for _, a := range AccountsByName {
a.sortPostings()
if checkPostings {
a.CheckPostings()
}
}
sort.Stable(Transactions)
}
// MustFindAccount returns an account matching the pattern.
// If multiple accounts match and they all have a common parent matching the pattern, return the parent.
// This is to avoid having to spell out non-leaf accounts in full.
// Otherwise panic.
func MustFindAccount(pattern string) *Account {
if a := AccountsByName[pattern]; a != nil {
return a
}
as := FindAccounts(pattern)
if len(as) == 0 {
panic(fmt.Errorf("cannot find account %s", pattern))
}
if len(as) == 1 {
return as[0]
}
parent := as[0].FullName
all := true
for _, a := range as[1:] {
if all = all && strings.HasPrefix(a.FullName, parent); all {
break
}
}
if all {
return as[0]
}
msg := fmt.Sprintf("Found %d accounts matching %s", len(as), pattern)
for _, a := range as {
msg += "\n" + a.FullName
}
panic(msg)
}
func FindAccountOfxId(acctId string) *Account {
for _, a := range AccountsByName {
if a.OFXAcctId == acctId {
return a
}
}
return nil
}
func ToRegex(pattern string) *regexp.Regexp {
multiple := `[\w/_:-]*`
single := `[\w/_-]*:[\w/_-]*`
words := strings.Split(pattern, ":")
rx := `(?i)` + words[0]
if len(words) > 1 {
wordLast := true
for _, w := range words[1:] {
if w == "" {
if wordLast {
rx += multiple
}
wordLast = false
} else {
if wordLast {
rx += single
}
rx += w
wordLast = true
}
}
}
return regexp.MustCompile(rx)
}
func FindAccounts(pattern string) (accounts []*Account) {
var names []string
rx := ToRegex(pattern)
AccountsDo(func(a *Account) {
if rx.MatchString(a.FullName) {
names = append(names, a.FullName)
}
})
sort.Strings(names)
for _, n := range names {
accounts = append(accounts, AccountsByName[n])
}
return accounts
}
func CommoditiesDo(f func(c *Commodity)) {
var names []string
for n := range Commodities {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
f(Commodities[n])
}
}
func AccountsDo(f func(c *Account)) {
var names []string
for n := range AccountsByName {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
f(AccountsByName[n])
}
}