-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
176 lines (153 loc) · 4.44 KB
/
bucket.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
package api
import (
"database/sql"
"sort"
"github.com/pkg/errors"
"github.com/lieut-data/go-moneywell/api/money"
)
// Bucket represents an income or expense bucket in a MoneyWell document. A bucket correlates 1:1
// with a record in the ZBUCKET table. Not all columns are exported.
//
// The MoneyWell SQLite schema for the ZBUCKET table is as follows:
// > .schema ZBUCKET
// CREATE TABLE ZBUCKET (
// Z_PK INTEGER PRIMARY KEY,
// Z_ENT INTEGER,
// Z_OPT INTEGER,
// ZINCLUDEINAUTOFILL INTEGER,
// ZISHIDDEN INTEGER,
// ZISSELECTED INTEGER,
// ZISTAXRELATED INTEGER,
// ZSEQUENCE INTEGER,
// ZTYPE INTEGER,
// ZBUCKETGROUP INTEGER,
// ZOVERFLOWBUCKET INTEGER,
// ZSOURCEBUCKET INTEGER,
// ZCURRENCYCODE VARCHAR,
// ZMEMO VARCHAR,
// ZNAME VARCHAR,
// ZTICDSSYNCID VARCHAR,
// ZUNIQUEID VARCHAR
// );
type Bucket struct {
PrimaryKey int64
Type int64
BucketGroup int64
Name string
StartingBalance money.Money
CurrencyCode string
}
// GetBuckets fetches the set of buckets in a MoneyWell document, sorted by the display order
// as MoneyWell itself would render.
func GetBuckets(database *sql.DB) ([]Bucket, error) {
rows, err := database.Query(`
SELECT
zb.Z_PK,
zb.ZTYPE,
zb.ZBUCKETGROUP,
zb.ZNAME,
CAST(ROUND(zbsb.ZAMOUNT * 100) AS INTEGER),
zb.ZCURRENCYCODE
FROM
ZBUCKET zb
LEFT JOIN
ZBUCKETGROUP zbg ON ( zbg.Z_PK = zb.ZBUCKETGROUP )
LEFT JOIN
ZBUCKETSTARTINGBALANCE zbsb ON ( zbsb.ZBUCKET = zb.Z_PK )
ORDER BY
-- Sort income buckets before expense buckets
zb.ZTYPE ASC,
-- Sort by the bucket group, if any
zbg.ZSEQUENCE ASC,
-- Sort within the bucket group, if any
zb.ZSEQUENCE ASC
`)
if err != nil {
return nil, errors.Wrap(err, "failed to query buckets")
}
defer rows.Close()
buckets := []Bucket{}
var primaryKey, bucketType int64
var name, currencyCode string
var bucketGroup, startingBalance sql.NullInt64
for rows.Next() {
err := rows.Scan(
&primaryKey,
&bucketType,
&bucketGroup,
&name,
&startingBalance,
¤cyCode,
)
if err != nil {
return nil, errors.Wrap(err, "failed to scan bucket")
}
buckets = append(buckets, Bucket{
PrimaryKey: primaryKey,
Type: bucketType,
BucketGroup: bucketGroup.Int64,
Name: name,
StartingBalance: money.Money{
Currency: currencyCode,
Amount: startingBalance.Int64,
},
})
}
return buckets, nil
}
// GetBucketsMap gets a map from the bucket primary key to the bucket.
func GetBucketsMap(database *sql.DB) (map[int64]Bucket, error) {
buckets, err := GetBuckets(database)
if err != nil {
return nil, errors.WithStack(err)
}
bucketsMap := make(map[int64]Bucket, len(buckets))
for _, bucket := range buckets {
bucketsMap[bucket.PrimaryKey] = bucket
}
return bucketsMap, nil
}
// GetBucketEvents uses the given transactions and bucket transfers to generate a list of events
// against the bucket sorted by date, generally matching the display of a bucket in MoneyWell.
func GetBucketEvents(
bucket Bucket,
transactions []Transaction,
bucketTransfers []BucketTransfer,
) ([]Event, error) {
events := []Event{}
for _, transaction := range transactions {
transaction := transaction
// Ignore voided and pending transactions.
switch transaction.Status {
case TransactionStatusVoided:
fallthrough
case TransactionStatusPending:
continue
}
if transaction.Bucket == bucket.PrimaryKey {
events = append(events, &transaction)
}
}
for _, bucketTransfer := range bucketTransfers {
bucketTransfer := bucketTransfer
if bucketTransfer.Bucket == bucket.PrimaryKey {
events = append(events, &bucketTransfer)
}
}
sort.Sort(ByEventDate(events))
return events, nil
}
// GetBucketBalance uses the given events to compute the current balance of a bucket.
func GetBucketBalance(bucket Bucket, events []Event, settings Settings) (money.Money, error) {
balance := bucket.StartingBalance
for _, event := range events {
if event.GetDate().Before(settings.CashFlowStartDate) {
continue
}
if event.GetBucket() != bucket.PrimaryKey {
continue
}
balance = balance.Add(event.GetAmount())
}
return balance, nil
}