-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeancount_importer_volksbank.py
318 lines (278 loc) · 13 KB
/
beancount_importer_volksbank.py
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
# -*- coding: utf-8 -*-
#
# Beancount importer for csv exports from Volksbank or GLS Bank.
# Author: Frank Stollmeier
# License: GNU GPLv3
#
import datetime
from beancount.core.number import D
from beancount.core import data
from beancount.core import amount
from beancount.core import position
from beancount.ingest import importer
from beancount import loader
class VolksbankImporter(importer.ImporterProtocol):
'''An importer for CSV export from a Volksbank online banking.'''
def __init__(self, importing_account, default_adjacent_account = "Unknown:account", target_journal = None, currency = 'EUR', flag = '!'):
'''
Parameters
----------
importing_account: string, name of account belonging to the csv export (one leg of the transaction)
default_adjacent_account: string, default account to collect the expenses (other leg of the transaction)
target_journal: string, optional. Filename of the target journal to guess the corresponding account names instead of using the default_adjacent_account (the other leg of the transaction)
currency: string, optional. Default is 'EUR'
flag: char, optional. Default is '!'
'''
self.account = importing_account
self.default_adjacent_account = default_adjacent_account
self.target_journal = target_journal
self.currency = currency
self.flag = flag
self.initialize_guessing()
self.file_format_version = None
def initialize_guessing(self):
'''Create a dictionary of the previous transactions in 'target_journal', which can be used to guess the right postings for the new transactions.'''
self.posting_dict = dict()
if not (self.target_journal is None):
entries,errors,options = loader.load_file(self.target_journal)
entries = [e for e in entries if isinstance(e, data.Transaction)]
entries = sorted(entries, key = lambda e: e.date)
for entry in entries:
if not (self.account in [p.account for p in entry.postings]):
continue
if entry.payee is None:
continue
if len(entry.payee) == 0:
continue
if not entry.payee in self.posting_dict:
self.posting_dict[entry.payee] = []
self.posting_dict[entry.payee].append(entry.postings)
def guess_postings(self, payee, total_transaction_value):
'''Guess postings based on the previous transactions with the same payee.
The guess is simply the most recent transaction with the same payee. If the transaction consists of multiple postings, the total_transaction_value is distributed to the postings in the same ratios as in the previous posting.
If there is no previous transaction with the same payee, the target account is the default_adjacent_account.
Parameters
----------
payee: string
total_transaction_value: float
'''
new_postings = []
if payee in self.posting_dict:
previous_postings = self.posting_dict[payee][-1]
accounts = []
units = []
for prev_posting in previous_postings:
accounts.append(prev_posting.account)
units.append(prev_posting.units)
if prev_posting.account == self.account:
prev_posting_had_reversed_signs = 0 > float(prev_posting.units.number) * total_transaction_value
s = sum([float(u.number) for u in units if u.number > 0])
for account,unit in zip(accounts,units):
share = float(unit.number) / s
if prev_posting_had_reversed_signs:
share = -share
p = data.Posting(account, amount.Amount(D(str(round(share*abs(total_transaction_value),2))), self.currency), None, None, None, None)
new_postings.append(p)
#move importing_account to the end of the list
i = 0
for j,posting in enumerate(new_postings):
if posting.account == self.account:
i = j
new_postings.append(new_postings.pop(i))
else:
new_postings.append(data.Posting(self.default_adjacent_account, amount.Amount(D(str(-total_transaction_value)), self.currency), None, None, None, None))
new_postings.append(data.Posting(self.account, amount.Amount(D(str(total_transaction_value)), self.currency), None, None, None, None))
return new_postings
def identify(self, file):
header_version1 = '"Buchungstag";"Valuta";"Auftraggeber/Zahlungsempfänger";"Empfänger/Zahlungspflichtiger";"Konto-Nr.";"IBAN";"BLZ";"BIC";"Vorgang/Verwendungszweck";"Kundenreferenz";"Währung";"Umsatz";" "'
header_version2 = "Buchungstag;Valuta;Textschlüssel;Primanota;Zahlungsempfänger;ZahlungsempfängerKto;ZahlungsempfängerIBAN;ZahlungsempfängerBLZ;ZahlungsempfängerBIC;Vorgang/Verwendungszweck;Kundenreferenz;Währung;Umsatz;Soll/Haben"
header_version3 = "Bezeichnung Auftragskonto;IBAN Auftragskonto;BIC Auftragskonto;Bankname Auftragskonto;Buchungstag;Valutadatum;Name Zahlungsbeteiligter;IBAN Zahlungsbeteiligter;BIC (SWIFT-Code) Zahlungsbeteiligter;Buchungstext;Verwendungszweck;Betrag;Waehrung;Saldo nach Buchung;Bemerkung;Kategorie;Steuerrelevant;Glaeubiger ID;Mandatsreferenz"
with open(file.name, "r", encoding = "ISO-8859-1") as f:
for line in f:
if header_version1 in line:
self.file_format_version = 1
return True
elif header_version2 in line:
self.file_format_version = 2
return True
elif header_version3 in line:
self.file_format_version = 3
return True
print('Unable to identify file format.')
return False
def file_account(self, file):
return self.account
def extract(self, file):
#parse csv file
if self.file_format_version == 1:
buchungstag, auftraggeber_empfaenger, buchungstext, verwendungszweck, betrag, kontostand, indices, endsaldo = parse_csv_file_v1(file.name)
elif self.file_format_version == 2:
buchungstag, auftraggeber_empfaenger, buchungstext, verwendungszweck, betrag, kontostand, indices, endsaldo = parse_csv_file_v2(file.name)
elif self.file_format_version == 3:
buchungstag, auftraggeber_empfaenger, buchungstext, verwendungszweck, betrag, kontostand, indices, endsaldo = parse_csv_file_v3(file.name)
else:
raise IOError("Unknown file format.")
#create transactions
entries = []
for i in range(len(buchungstag)):
postings = self.guess_postings(auftraggeber_empfaenger[i], float(betrag[i]) )
meta = data.new_metadata(file.name, indices[i])
txn = data.Transaction(meta, buchungstag[i], self.flag, auftraggeber_empfaenger[i], verwendungszweck[i], data.EMPTY_SET, data.EMPTY_SET, postings)
entries.append(txn)
#create balance
meta = data.new_metadata(file.name, endsaldo[2])
entries.append( data.Balance(meta, endsaldo[0] + datetime.timedelta(days=1), self.account, amount.Amount(D(endsaldo[1]), self.currency), None, None) )
return entries
def convert_value(value, soll_haben):
'''Convert number format from the CSV file and add sign +/- depending on Soll/Haben (Credit/Debit).
Example: convert_value('1.200,30', 'S') returns '-1200.30'.
Parameters
----------
value: string, number with ',' as decimal separator and '.' as thousand-separator
soll_haben: string, 'S' for 'Soll' and 'H' for 'Haben'
'''
return ('-' if ('S' in soll_haben) else '') + value.replace('.','').replace(',','.').replace('"','')
def convert_value2(value):
'''Convert signed numbers from the CSV file. For unsigned number with Soll/Haben see convert_value().
Parameters
----------
value: string, like '-4,7' or '5,21'
'''
return value.replace('.','').replace(',','.')
def convert_date(date):
'''Convert date from the CSV file (format '04.10.2020') to datetime.
Parameters
----------
date: string
'''
day,month,year = date.replace('"','').split('.')
return datetime.date(int(year),int(month),int(day))
def parse_csv_file_v1(filename):
'''Parse CSV file.
Parameters
----------
filename: string
'''
buchungstag = []
auftraggeber_empfaenger = []
buchungstext = []
verwendungszweck = []
betrag = []
kontostand = []
endsaldo = None
linenumbers = []
header = True
collector = ""
linenumber = -1
f = open(filename, 'r', encoding = 'iso-8859-1')
for line in f:
linenumber += 1
#skip header
if header:
if "Valuta" in line:
header = False
continue
#skip empty lines
if len(line) == 0:
continue
#Each transaction in the csv file is splitted over several lines, so we have to collect the content of several lines until the transaction is complete:
collector = collector + line.replace('\n',' ')
if '"S"' in collector or '"H"' in collector:
if "Anfangssaldo" in collector:
pass
elif "Endsaldo" in collector:
result = collector.split(';')
date = result[0]
amount = convert_value(result[11], result[12].replace('"',''))
endsaldo = (convert_date(date),amount, linenumber)
else:
values = collector.split(';')
buchungstag.append(convert_date(values[0]))
auftraggeber_empfaenger.append(values[3].replace('"',''))
buchungstext.append('')
verwendungszweck.append(values[8].replace('"',''))
betrag.append(convert_value(values[11], collector[-3:]))
kontostand.append(None)
linenumbers.append(linenumber)
collector = ""
return buchungstag, auftraggeber_empfaenger, buchungstext, verwendungszweck, betrag, kontostand, linenumbers, endsaldo
def parse_csv_file_v2(filename):
'''Parse CSV file with the new file format they started to use at the beginning of 2022.
Parameters
----------
filename: string
'''
buchungstag = []
auftraggeber_empfaenger = []
buchungstext = []
verwendungszweck = []
betrag = []
kontostand = []
endsaldo = None
linenumbers = []
header = True
collector = ""
linenumber = -1
f = open(filename, 'r', encoding = 'iso-8859-1')
for line in f:
linenumber += 1
#skip header
if header:
if "Valuta" in line:
header = False
continue
values = line.split(';')
if len(values[0]) == 0:
continue
if values[10] == 'Endsaldo':
date = convert_date(values[0])
amount = convert_value(values[12], values[13])
endsaldo = (date, amount, linenumber)
elif values[10] == 'Anfangssaldo':
continue
else:
buchungstag.append(convert_date(values[0]))
auftraggeber_empfaenger.append(values[4])
buchungstext.append('')
verwendungszweck.append(values[9])
betrag.append(convert_value(values[12], values[13]))
kontostand.append(None)
linenumbers.append(linenumber)
return buchungstag, auftraggeber_empfaenger, buchungstext, verwendungszweck, betrag, kontostand, linenumbers, endsaldo
def parse_csv_file_v3(filename):
'''Parse CSV file with the new file format they started to use at the beginning of 2022.
Parameters
----------
filename: string
'''
buchungstag = []
auftraggeber_empfaenger = []
buchungstext = []
verwendungszweck = []
betrag = []
kontostand = []
endsaldo = None
linenumbers = []
header = True
collector = ""
linenumber = -1
f = open(filename, 'r', encoding = 'iso-8859-1')
for line in f:
linenumber += 1
#skip header
if header:
if "Mandatsreferenz" in line:
header = False
continue
values = line.split(';')
if len(values[0]) == 0:
continue
buchungstag.append(convert_date(values[4]))
auftraggeber_empfaenger.append(values[6])
buchungstext.append('')
verwendungszweck.append(values[10])
betrag.append(convert_value2(values[11]))
kontostand.append(convert_value2(values[13]))
linenumbers.append(linenumber)
endsaldo = (buchungstag[0], kontostand[0], linenumbers[0])
return buchungstag, auftraggeber_empfaenger, buchungstext, verwendungszweck, betrag, kontostand, linenumbers, endsaldo