-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathynabOutput.go
63 lines (53 loc) · 1.23 KB
/
ynabOutput.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
package csvrewrite
import (
"bufio"
"fmt"
"strconv"
"strings"
)
const YNABDateFormat string = "02.01.2006"
type YNABOutput struct {
}
func (*YNABOutput) WriteHeader(w *bufio.Writer) {
fmt.Fprintln(w, "Date,Payee,Category,Memo,Outflow,Inflow")
}
func (*YNABOutput) Process(w *bufio.Writer, t *Transaction) {
date := t.Date.Format(YNABDateFormat)
outflow := ""
inflow := ""
if t.ValueCent < 0 {
outflow = formatValue(-t.ValueCent)
} else {
inflow = formatValue(t.ValueCent)
}
removeInvalidChars(t)
output := strings.Join([]string{date, t.Payee, t.Category, t.Comment, outflow, inflow}, ",")
fmt.Fprintln(w, output)
}
func (y *YNABOutput) BatchProcess(w *bufio.Writer, t []*Transaction) {
for _, trans := range t {
y.Process(w, trans)
}
}
func formatValue(v int) string {
neg := v < 0
if neg {
v = -v
}
str := strconv.FormatInt(int64(v), 10)
for len(str) < 3 {
str = "0" + str
}
eur := str[:len(str)-2]
cent := str[len(str)-2:]
result := eur + "." + cent
if neg {
result = "-" + result
}
return result
}
func removeInvalidChars(t *Transaction) {
t.Category = strings.Replace(t.Category, ",", ";", -1)
t.Comment = strings.Replace(t.Comment, ",", ";", -1)
t.Payee = strings.Replace(t.Payee, ",", ";", -1)
}