-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmt940.py
190 lines (138 loc) · 4.48 KB
/
mt940.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
from data import Transaction
BANK_NAME = 'Revolut'
BANK_BIC = 'REVOLT21'
DEFAULT_SEQUENCE_NO = 1
class Mt940Writer:
def __init__(self, filename, account_iban, month):
self.file = open(filename, 'w')
self.account_iban = account_iban
self._write_header(month)
self._written_starting_balance = False
self._written_ending_balance = False
self._balance = None
self._date = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.release()
def write_transaction(self, transaction : Transaction):
if not self._written_starting_balance:
self._write_starting_balance(transaction.datetime,
transaction.before_balance)
self.file.writelines([
Mt940.make_61(
transaction.datetime,
transaction.amount),
Mt940.make_86(
transaction.iban,
transaction.name,
transaction.description)
])
self._balance = transaction.after_balance
self._date = transaction.datetime
def release(self):
if not self.file.closed \
and self._written_starting_balance \
and not self._written_ending_balance:
self._write_ending_balance()
if not self.file.closed:
self.file.close()
def _write_header(self, month):
self.file.write(
Mt940.make_header(BANK_BIC))
self.file.writelines([
Mt940.make_20(BANK_NAME, month),
Mt940.make_25(self.account_iban, CURRENCY),
Mt940.make_28c(DEFAULT_SEQUENCE_NO)
])
def _write_starting_balance(self, date, balance):
self.file.write(
Mt940.make_60f(date, balance, CURRENCY))
self._written_starting_balance = True
def _write_ending_balance(self):
self.file.write(
Mt940.make_62f(self._date, self._balance, CURRENCY))
self._written_ending_balance = True
CURRENCY = 'EUR'
# format identifier
TAG_940 = '940'
# header
FORMAT_HEADER = ':' + TAG_940 + ':\n'
# transaction ref
FORMAT_20 = ':20:' + TAG_940 + '{bank}-{month}\n'
# account id
FORMAT_25 = ':25:{iban} {currency}\n'
# sequence no
FORMAT_28C = ':28C:{seqno}\n'
# opening balance
FORMAT_60F = ':60F:{sign}{date}{currency}{amount}\n'
# closing balance
FORMAT_62F = ':62F:{sign}{date}{currency}{amount}\n'
# transaction
FORMAT_61 = ':61:{date}{amount}{magic}\n'
# transaction 2
FORMAT_86 = ':86:/IBAN/{iban}/NAME/{name}/REMI/{description}\n'
MAGIC = 'NTRFNONREF'
class Mt940:
@staticmethod
def make_header(bic):
return FORMAT_HEADER.format(
bic=bic)
@staticmethod
def make_20(bank, month):
return FORMAT_20.format(
bank=bank,
month=month)
@staticmethod
def make_25(iban, currency):
return FORMAT_25.format(
iban=iban,
currency=currency)
@staticmethod
def make_28c(seqno):
return FORMAT_28C.format(
seqno=Mt940.pad_5(seqno))
@staticmethod
def make_60f(datetime, balance, currency):
return FORMAT_60F.format(
sign=Mt940.amount_sign(balance),
date=Mt940.date(datetime),
currency=currency,
amount= Mt940.amount_val(balance))
@staticmethod
def make_62f(datetime, balance, currency):
return FORMAT_62F.format(
sign=Mt940.amount_sign(balance),
date=Mt940.date(datetime),
currency=currency,
amount= Mt940.amount_val(balance))
@staticmethod
def make_61(datetime, amount):
return FORMAT_61.format(
date=Mt940.date(datetime),
amount=Mt940.amount(amount),
magic=MAGIC)
@staticmethod
def make_86(iban, name, description):
return FORMAT_86.format(
iban=iban,
name=name,
description=description)
@staticmethod
def pad_5(val):
return str(val).zfill(5)
@staticmethod
def amount_sign(val):
return 'C' if val > 0 else 'D'
@staticmethod
def amount_val(val):
return '{0:.2f}'.format(abs(val)).replace('.', ',')
@staticmethod
def amount(val):
return Mt940.amount_sign(val) + Mt940.amount_val(val)
@staticmethod
def date(val, with_year=True):
if with_year:
return val.strftime('%y%m%d')
else:
return val.strftime('%m%d')