forked from mfiedler/CsvToSepaDD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvToSepaDD.py
executable file
·233 lines (185 loc) · 7.18 KB
/
CsvToSepaDD.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
#!/usr/bin/python3
import argparse
import csv
import datetime
import pprint
import string
import sys
import pathlib
# automate loading of used modules
Script = pathlib.Path(sys.argv[0])
sys.path.append(f'{Script.parent}/PySepaDD')
sys.path.append(f'{Script.parent}/PyIbanCheck')
import PySepaDD
from ibancheck import IBANcheck
# if true, all debits will occur as a single item at the creditor's bank
# account. Otherwise, for each debtor there will be a single item.
DEFAULT_BATCH = True
DEFAULT_CURRENCY = 'EUR'
class spgVereinExcelDialect(csv.Dialect):
'''
Describes the default properties of a CSV file generated by SPG Verein
using the "Excel" preset.
'''
delimiter = ';'
quotechar = '"'
lineterminator = '\r\n'
quoting = csv.QUOTE_MINIMAL
strict = True
csv.register_dialect('spg-verein-excel', spgVereinExcelDialect)
class calcDialect(csv.Dialect):
'''
Describes the default properties of a CSV file generated by
Libreoffice/OpenOffice Calc.
'''
delimiter = ','
quotechar = '"'
doublequote = True
skipinitialspace = False
lineterminator = '\n'
quoting = csv.QUOTE_MINIMAL
strict = True
csv.register_dialect('calc-default', calcDialect)
def parseDate(dateString):
'''
Returns the given string converted to a date object
'''
allowedFormats = [
'%Y-%m-%d',
'%d.%m.%Y',
'%d.%m.%y',
]
for form in allowedFormats:
try:
return datetime.datetime.strptime(dateString, form)
except ValueError:
pass
raise ValueError('%s: date format not recognized' % dateString)
def euroToCents(amountString):
'''
Converts a string representing an amount of euros into an amount of cents
represented as int
'''
delim = None
if ',' in amountString:
delim = ','
elif '.' in amountString:
delim = '.'
if not delim:
cents = amountString + '00'
return int(cents)
else:
delimIndex = amountString.find(delim)
cents = amountString[:delimIndex]
amountString = amountString[delimIndex + 1:]
for iteration in ['ten cents', 'one cents']:
if amountString:
cents += amountString[0]
amountString = amountString[1:]
else:
cents += '0'
if amountString:
print >> sys.stderr, 'Warning: amount had more than two decimal places, ignoring the remainder'
return int(cents)
def csvToSepa(args):
'''
Converts the SEPA direct debit data from a given CSV file to SEPA XML
'''
ibc = IBANcheck()
config = None
with open(args.configfile, 'r') as f:
try:
config = eval(f.read())
except SyntaxError:
print(f'\nDie Datei "{args.configfile}" ist vermutlich keine gültige Konfigurationsdatei!')
print('Bitte die Reihenfolge der Parameter beachten!')
print('weitere Informationen: CsvToSepaDD.py convert --help')
exit()
with open(args.inputfile, 'r') as inFile, open(args.outputfile, 'w') as outFile:
csvReader = csv.DictReader(inFile, dialect=config['csv_dialect'])
sepaWriter = PySepaDD.PySepaDD(config)
# check for all required fields:
#
# amount is as full currency, e. g. 42, 42.0, 42.00, 42,0, 42,00
# type may be any of:
# FRST - first of a sequence of debits
# RCUR - recurring debit of a sequence. Must not be used if there
# was no debit of type FRST yet in the past!
# OOFF - non-recurring debit, mandate is valid only for one
# transaction
# FNAL final debit in a sequence
# collection_date is the date when the debit will be executed
# mandate_id is a unique value representing the debtor's mandate
# mandate_date is the date when the mandate was created/granted/signed
# by the debtor
# FIXME: separaten kontoinhaber unterstuetzen?
requiredFields = ['first_name', 'last_name', 'IBAN', 'BIC', 'amount',
'type', 'collection_date', 'mandate_id', 'mandate_date',
'description']
error = False
for field in requiredFields:
if not field in csvReader.fieldnames:
print >> sys.stderr, 'missing field in CSV header: %s' % field
error = True
if error:
raise KeyError
for row in csvReader:
row['IBAN'] = row['IBAN'].replace(' ','').upper()
if not ibc.check(row['IBAN']):
print(f'IBAN ({row["IBAN"]}) is not valid. Name: {row["first_name"]} {row["last_name"]}',file=sys.stderr)
row['IBAN'] = row['IBAN'].replace(' ','').upper()
payment = {
'name': f'{row["first_name"]} {row["last_name"]}',
'IBAN': row['IBAN'].replace(' ','').upper(),
'BIC': row['BIC'],
'amount': euroToCents(row['amount']),
'type': row['type'],
'collection_date': parseDate(row['collection_date']),
'mandate_id': row['mandate_id'],
'mandate_date': parseDate(row['mandate_date']),
'description': row['description'],
}
sepaWriter.add_payment(payment)
sepaXml = sepaWriter.export()
outFile.write(sepaXml)
def createConfig(args):
'''Interactively creates a configuation file'''
name = input('your name: ')
iban = input('your IBAN: ').replace(' ','')
bic = input('your BIC: ')
creditorId = input('your creditor id: ')
csvDialect = input('CSV dialect [%s]: ' % ' '.join(sorted(csv.list_dialects())))
# we use a PySepaDD-compatible configuration dict for simplicity
config = {
'name': name,
'IBAN': iban,
'BIC': bic,
'creditor_id': creditorId,
'currency': DEFAULT_CURRENCY,
'batch': DEFAULT_BATCH,
'csv_dialect': csvDialect,
}
with open(args.configfile, 'w') as f:
pprint.pprint(config, stream=f, indent=4)
print (f'''Configuration written to file {args.configfile}.
You can edit this file with a text
editor if you need to change something later.''')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create SEPA XML direct debit files from CSV')
subparsers = parser.add_subparsers()
genConfigParser = subparsers.add_parser('genconfig', help='generate a configuration file')
genConfigParser.set_defaults(func=createConfig)
genConfigParser.add_argument('configfile', help='name of the configuration file')
convertParser = subparsers.add_parser('convert', help='convert a CSV file to a SEPA XML file')
convertParser.set_defaults(func=csvToSepa)
convertParser.add_argument('configfile', help='configuration file to use')
convertParser.add_argument('inputfile', help='input file')
convertParser.add_argument('outputfile', help='output file')
try:
args = parser.parse_args()
except SystemExit:
exit()
try:
args.func(args)
except AttributeError:
parser.parse_args(['-h'])