-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomdirectInput.go
208 lines (179 loc) · 4.57 KB
/
comdirectInput.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
package csvrewrite
import (
"fmt"
"strconv"
"strings"
"time"
)
const comdirectDateFormat string = "02.01.2006"
type ComdirectInput struct {
headerFound bool
sub PayeeSubstitution
}
type handler func(*Transaction) bool
func NewComdirectInput(sub PayeeSubstitution) *ComdirectInput {
com := ComdirectInput{}
com.headerFound = false
com.sub = sub
return &com
}
func (c *ComdirectInput) ProcessLine(line string) (*Transaction, error) {
if !c.headerFound {
if strings.EqualFold(line, `"Buchungstag";"Wertstellung (Valuta)";"Vorgang";"Buchungstext";"Umsatz in EUR";`) ||
strings.EqualFold(line, `"Buchungstag";"Umsatztag";"Vorgang";"Referenz";"Buchungstext";"Umsatz in EUR";`) {
c.headerFound = true
return nil, fmt.Errorf("")
}
if len(line) > 0 {
return nil, fmt.Errorf("Ignored content before header:\n%s", line)
} else {
}
}
handlers := []handler{handleLastschrift, handleWertpapiere, handleVisa, handleVisaMonthlyPayment,
handleAuszahlung, handleBarEinzahlung, handleKupon}
tokens := splitLine(line, ';')
length := len(tokens)
if length < 5 {
if len(line) > 0 {
return nil, fmt.Errorf("Ignored content:\n%s", line)
} else {
return nil, fmt.Errorf("")
}
}
buchungsTag, _ := time.Parse(comdirectDateFormat, tokens[0])
vorgang := tokens[2]
buchungsText := tokens[3]
umsatz := tokens[4]
if length == 7 {
buchungsText = tokens[4]
umsatz = tokens[5]
}
var err error = nil
t := Transaction{}
t.Date = buchungsTag
t.ValueCent, err = parseValue(umsatz)
t.Comment = buchungsText
t.Category = vorgang
if err != nil {
fmt.Println(err)
return nil, fmt.Errorf("error in parsing value from line '%s':\n%s", line, err.Error())
}
processed := false
for _, h := range handlers {
if h(&t) {
processed = true
break
}
}
if !processed {
return nil, fmt.Errorf("No handler applicable for line '%s'\n", line)
}
t.Category = ""
c.sub.substitute(&t)
filterRef(&t)
return &t, nil
}
func (*ComdirectInput) PreFilter(input string) string {
str := strings.Replace(input, "\r\n", "\n", -1)
str = strings.Replace(str, "\r", "\n", -1)
return strings.Replace(str, "\n\"neu\";", "", -1)
}
func parseValue(str string) (int, error) {
rawValue := strings.Replace(str, ".", "", -1)
rawValue = strings.Replace(rawValue, ",", "", -1)
value, err := strconv.ParseInt(rawValue, 10, 32)
return int(value), err
}
func handleLastschrift(t *Transaction) bool {
if strings.Contains(t.Category, "Lastschrift") || strings.Contains(t.Category, "Überweisung") {
s := strings.Split(t.Comment, "Buchungstext: ")
if len(s) >= 2 {
t.Comment = s[1]
}
t.Payee = strings.Replace(s[0], "Auftraggeber: ", "", 1)
t.Payee = strings.Replace(t.Payee, "Empfänger: ", "", 1)
t.Category = ""
return true
}
return false
}
func handleWertpapiere(t *Transaction) bool {
if strings.Contains(t.Category, "Wertpapiere") {
t.Comment = strings.Replace(t.Comment, "Buchungstext: ", "", 1)
t.Payee = "Transfer: .comdirect Depot"
t.Category = ""
return true
}
return false
}
func handleKupon(t *Transaction) bool {
if strings.Contains(t.Category, "Kupon") {
t.Comment = strings.Replace(t.Comment, "Buchungstext: ", "", 1)
t.Payee = "Transfer: .comdirect Depot"
t.Category = ""
return true
}
return false
}
func handleVisa(t *Transaction) bool {
if strings.Contains(t.Category, "Visa-Umsatz") {
t.Payee = t.Comment
t.Comment = ""
t.Category = ""
return true
}
return false
}
func handleVisaMonthlyPayment(t *Transaction) bool {
if strings.Contains(t.Category, "Visa-Kartenabrechnung") {
t.Payee = "Transfer: .comdirect"
t.Comment = ""
t.Category = ""
return true
}
return false
}
func handleAuszahlung(t *Transaction) bool {
if strings.Contains(t.Category, "Auszahlung GAA") {
t.Payee = "Transfer : Cash"
t.Comment = ""
t.Category = ""
return true
}
return false
}
func handleBarEinzahlung(t *Transaction) bool {
if strings.Contains(t.Category, "Bar") && strings.Contains(t.Comment, "EINZAHLUNG") {
t.Payee = "Transfer : Cash"
t.Comment = ""
t.Category = ""
return true
}
return false
}
func filterRef(t *Transaction) {
t.Comment = strings.Split(t.Comment, " End-to-End-Ref.:")[0]
}
func splitLine(s string, separator rune) []string {
inQuotes := false
var result = make([]string, 0)
curStr := ""
for _, runeValue := range s {
if runeValue == '"' {
inQuotes = !inQuotes
continue
}
if inQuotes {
curStr += string(runeValue)
continue
}
if runeValue == separator {
result = append(result, curStr)
curStr = ""
continue
}
curStr += string(runeValue)
}
result = append(result, curStr)
return result
}