-
Notifications
You must be signed in to change notification settings - Fork 0
/
kakebo.go
302 lines (249 loc) · 5.88 KB
/
kakebo.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
package kakebo
import (
"fmt"
"sort"
"strings"
"time"
"github.com/shopspring/decimal"
)
// FormatEntries
//
// Input example:
//
// 1,2 foo
// 3,45 bar
// 6 baz
// 78.09 xyzzy
//
// Output example:
//
// Foo 1.20
// Bar 3.45
// Baz 6.00
// Xyzzy 78.09
func FormatEntries(entryData string) (string, error) {
var formattedEntries []string
for _, line := range lines(entryData) {
entry, err := formatEntry(strings.Fields(line))
if err != nil {
return "", err
}
formattedEntries = append(formattedEntries, entry)
}
return strings.Join(formattedEntries, ""), nil
}
func formatEntry(fields []string) (string, error) {
if len(fields) < 2 {
return "", fmt.Errorf("at least 2 fields required")
}
s := strings.Replace(fields[0], ",", ".", 1)
amount, err := decimal.NewFromString(s)
if err != nil {
return "", err
}
return fmt.Sprintf("%s\t%s\n", strings.Title(fields[1]), money(amount)), nil
}
// CalcMonth
//
// Input example:
//
// Foo 1.20
// Bar 3.45
// Baz 6.00
// Xyzzy 78.09
//
// Output example:
//
// 88.74
func CalcMonth(monthData string) (decimal.Decimal, error) {
return sumValues(monthData, extractFormattedEntryValue)
}
func extractFormattedEntryValue(fields []string) (decimal.Decimal, error) {
return decimal.NewFromString(fields[1])
}
// CalcBalance
//
// Input example:
//
// -120 y foo
// -34.5 m bar
// -6 M baz
// 789 Y xyzzy
//
// Output example:
//
// 15.25
func CalcBalance(dueData string) (decimal.Decimal, error) {
return sumValues(dueData, extractDueValue)
}
const (
monthly int64 = 1
yearly int64 = 12
)
var intervalDictionary = map[string]int64{
"m": monthly,
"M": monthly,
"y": yearly,
"Y": yearly,
}
func extractDueValue(fields []string) (decimal.Decimal, error) {
if len(fields) < 2 {
return decimal.Decimal{}, fmt.Errorf("at least 2 fields required")
}
amount, err := decimal.NewFromString(fields[0])
if err != nil {
return decimal.Decimal{}, err
}
interval, ok := intervalDictionary[fields[1]]
if !ok {
return decimal.Decimal{}, fmt.Errorf("unknown interval '%s'", fields[1])
}
return amount.Div(decimal.NewFromInt(interval)), nil
}
// DisplayMonth
//
// Input example:
//
// Foo 1.20
// Bar 3.45
// Baz 6.00
//
// Output example:
//
// January 2020
//
// Foo 1,20
// Bar 3,45
// Baz 6,00
//
// Tot 10,65
func DisplayMonth(date time.Time, monthData string, monthTot decimal.Decimal) string {
var lines []string
lines = append(lines, fmt.Sprintln(date.Month(), date.Year())) // header
lines = append(lines, monthData) // body
lines = append(lines, fmt.Sprintf("Tot\t%s\n", money(monthTot))) // footer
text := strings.Join(lines, "\n")
return replaceDotsWithCommas(text)
}
// DisplayStats
//
// Input example:
//
// time.Time{2009/11/10}, decimal.Decimal{1000}, decimal.Decimal{100}, 10
//
// Output example:
//
// 10 November 2009
//
// Save goal 100,00
// Monthly budget 900,00
// Daily budget 30,00
//
// End of month 33%
// Amount spent 11%
func DisplayStats(date time.Time, balance, monthTot decimal.Decimal, savePercentage int) string {
var lines []string
hundred := decimal.NewFromInt(100)
savePercent := decimal.NewFromInt(int64(savePercentage))
today := decimal.NewFromInt(int64(date.Day()))
saveGoal := balance.Div(hundred).Mul(savePercent)
monthlyBudget := balance.Sub(saveGoal)
monthDays := daysOfMonth(date)
dailyBudget := monthlyBudget.DivRound(monthDays, 2)
endOfMonthPercentage := hundred.Mul(today).DivRound(monthDays, 0)
spentAmountPercentage := hundred.Mul(monthTot).DivRound(monthlyBudget, 0)
y, m, d := date.Date()
body := [][]string{
{"Save goal", money(saveGoal)},
{"Monthly budget", money(monthlyBudget)},
{"Daily budget", money(dailyBudget)},
}
footer := [][]string{
{"End of month", percentage(endOfMonthPercentage)},
{"Amount spent", percentage(spentAmountPercentage)},
}
lines = append(lines, fmt.Sprintln(d, m, y))
lines = append(lines, formatStats(body))
lines = append(lines, formatStats(footer))
text := strings.Join(lines, "\n")
return replaceDotsWithCommas(text)
}
func daysOfMonth(date time.Time) decimal.Decimal {
y, m, _ := date.Date()
endOfMonth := time.Date(y, m+1, 0, 0, 0, 0, 0, date.Location())
return decimal.NewFromInt(int64(endOfMonth.Day()))
}
func formatStats(stats [][]string) string {
var text string
for _, cols := range stats {
text += fmt.Sprintf("%s\t%s\n", cols[0], cols[1])
}
return text
}
type Due struct {
Amount decimal.Decimal
Description string
}
// DisplayDues
//
// Input example:
//
// -120 y foo
// -34.5 m bar
// -6 M baz
// 789 Y xyzzy
// 1200 M incoming
//
// Output example:
//
// Bar -34,50
// Foo -10,00
// Baz -6,00
func DisplayDues(dueData string) string {
var dues []Due
for _, line := range lines(dueData) {
fields := strings.Fields(line)
val, err := extractDueValue(fields)
if err != nil {
return "invalid dues"
}
if val.LessThan(decimal.Zero) {
dues = append(dues, Due{decimal.Decimal.Abs(val), fields[2]})
}
}
sort.SliceStable(dues, func(a, b int) bool {
return dues[a].Amount.GreaterThan(dues[b].Amount)
})
var lines []string
for _, due := range dues {
lines = append(lines, fmt.Sprintf("%s\t%s\n", strings.Title(due.Description), money(due.Amount)))
}
text := strings.Join(lines, "")
return replaceDotsWithCommas(text)
}
//
// Common stuff
//
func lines(data string) []string {
return strings.Split(strings.Trim(data, "\n"), "\n")
}
func money(amount decimal.Decimal) string {
return amount.StringFixed(2)
}
func percentage(amount decimal.Decimal) string {
return amount.String() + "%"
}
func sumValues(data string, extractor func([]string) (decimal.Decimal, error)) (decimal.Decimal, error) {
var tot decimal.Decimal
for _, line := range lines(data) {
val, err := extractor(strings.Fields(line))
if err != nil {
return decimal.Decimal{}, err
}
tot = tot.Add(val)
}
return tot, nil
}
func replaceDotsWithCommas(text string) string {
return strings.ReplaceAll(text, ".", ",")
}