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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
# vim: fileencoding=utf-8:
'''
The MT940 format is a standard for bank account statements. It is used by
many banks in Europe and is based on the SWIFT MT940 format.
The MT940 tags are:
+---------+-----------------------------------------------------------------+
| Tag | Description |
+=========+=================================================================+
| `:13:` | Date/Time indication at which the report was created |
+---------+-----------------------------------------------------------------+
| `:20:` | Transaction Reference Number |
+---------+-----------------------------------------------------------------+
| `:21:` | Related Reference Number |
+---------+-----------------------------------------------------------------+
| `:25:` | Account Identification |
+---------+-----------------------------------------------------------------+
| `:28:` | Statement Number |
+---------+-----------------------------------------------------------------+
| `:34:` | The floor limit for debit and credit |
+---------+-----------------------------------------------------------------+
| `:60F:` | Opening Balance |
+---------+-----------------------------------------------------------------+
| `:60M:` | Intermediate Balance |
+---------+-----------------------------------------------------------------+
| `:60E:` | Closing Balance |
+---------+-----------------------------------------------------------------+
| `:61:` | Statement Line |
+---------+-----------------------------------------------------------------+
| `:62:` | Closing Balance |
+---------+-----------------------------------------------------------------+
| `:62M:` | Intermediate Closing Balance |
+---------+-----------------------------------------------------------------+
| `:62F:` | Final Closing Balance |
+---------+-----------------------------------------------------------------+
| `:64:` | Available Balance |
+---------+-----------------------------------------------------------------+
| `:65:` | Forward Available Balance |
+---------+-----------------------------------------------------------------+
| `:86:` | Transaction Information |
+---------+-----------------------------------------------------------------+
| `:90:` | Total number and amount of debit entries |
+---------+-----------------------------------------------------------------+
| `:NS:` | Bank specific Non-swift extensions containing extra information |
+---------+-----------------------------------------------------------------+
Format
---------------------
Sources:
.. _Swift for corporates: http://www.sepaforcorporates.com/\
swift-for-corporates/account-statement-mt940-file-format-overview/
.. _Rabobank MT940: https://www.rabobank.nl/images/\
formaatbeschrijving_swift_bt940s_1_0_nl_rib_29539296.pdf
- `Swift for corporates`_
- `Rabobank MT940`_
The pattern for the tags use the following syntax:
::
[] = optional
! = fixed length
a = Text
x = Alphanumeric, seems more like text actually. Can include special
characters (slashes) and whitespace as well as letters and numbers
d = Numeric separated by decimal (usually comma)
c = Code list value
n = Numeric
'''
from __future__ import print_function
import logging
import re
try:
import enum
except ImportError: # pragma: no cover
import sys
print('MT940 requires the `enum34` package', file=sys.stderr)
class enum(object):
@staticmethod
def unique(*args, **kwargs):
return []
Enum = object
from . import models
logger = logging.getLogger(__name__)
class Tag(object):
id = 0
RE_FLAGS = re.IGNORECASE | re.VERBOSE | re.UNICODE
scope = models.Transactions
def __init__(self):
self.re = re.compile(self.pattern, self.RE_FLAGS)
def parse(self, transactions, value):
match = self.re.match(value)
if match: # pragma: no branch
self.logger.debug(
'matched (%d) %r against "%s", got: %s',
len(value), value, self.pattern,
match.groupdict()
)
else: # pragma: no cover
self.logger.error(
'matching id=%s (len=%d) "%s" against\n %s',
self.id,
len(value),
value,
self.pattern
)
part_value = value
for pattern in self.pattern.split('\n'):
match = re.match(pattern, part_value, self.RE_FLAGS)
if match:
self.logger.info(
'matched %r against %r, got: %s',
pattern, match.group(0),
match.groupdict()
)
part_value = part_value[len(match.group(0)):]
else:
self.logger.error(
'no match for %r against %r',
pattern, part_value
)
raise RuntimeError(
'Unable to parse %r from %r' % (self, value),
self, value
)
return match.groupdict()
def __call__(self, transactions, value):
return value
def __new__(cls, *args, **kwargs):
cls.name = cls.__name__
words = re.findall('([A-Z][a-z]+)', cls.__name__)
cls.slug = '_'.join(w.lower() for w in words)
cls.logger = logger.getChild(cls.name)
return object.__new__(cls, *args, **kwargs)
def __hash__(self):
return self.id
class DateTimeIndication(Tag):
'''Date/Time indication at which the report was created
Pattern: 6!n4!n1! x4!n
'''
id = 13
pattern = r'''^
(?P<year>\d{2})
(?P<month>\d{2})
(?P<day>\d{2})
(?P<hour>\d{2})
(?P<minute>\d{2})
(\+(?P<offset>\d{4})|)
'''
def __call__(self, transactions, value):
data = super(DateTimeIndication, self).__call__(transactions, value)
return {
'date': models.DateTime(**data)
}
class TransactionReferenceNumber(Tag):
'''Transaction reference number
Pattern: 16x
'''
id = 20
pattern = r'(?P<transaction_reference>.{0,16})'
class RelatedReference(Tag):
'''Related reference
Pattern: 16x
'''
id = 21
pattern = r'(?P<related_reference>.{0,16})'
class AccountIdentification(Tag):
'''Account identification
Pattern: 35x
'''
id = 25
pattern = r'(?P<account_identification>.{0,35})'
class StatementNumber(Tag):
'''Statement number / sequence number
Pattern: 5n[/5n]
'''
id = 28
pattern = r'''
(?P<statement_number>\d{1,5}) # 5n
(?:/?(?P<sequence_number>\d{1,5}))? # [/5n]
$'''
class FloorLimitIndicator(Tag):
'''Floor limit indicator
indicates the minimum value reported for debit and credit amounts
Pattern: :34F:GHSC0,00
'''
id = 34
pattern = r'''^
(?P<currency>[A-Z]{3}) # 3!a Currency
(?P<status>[DC ]?) # 2a Debit/Credit Mark
(?P<amount>[0-9,]{0,16}) # 15d Amount (includes decimal sign, so 16)
$'''
def __call__(self, transactions, value):
data = super(FloorLimitIndicator, self).__call__(transactions, value)
if data['status']:
return {
data['status'].lower() + '_floor_limit': models.Amount(**data)
}
data_d = data.copy()
data_c = data.copy()
data_d.update({'status': 'D'})
data_c.update({'status': 'C'})
return {
'd_floor_limit': models.Amount(**data_d),
'c_floor_limit': models.Amount(**data_c)
}
class NonSwift(Tag):
'''Non-swift extension for MT940 containing extra information. The
actual definition is not consistent between banks so the current
implementation is a tad limited. Feel free to extend the implementation
and create a pull request with a better version :)
It seems this could be anything so we'll have to be flexible about it.
Pattern: `2!n35x | *x`
'''
class scope(models.Transaction, models.Transactions):
pass
id = 'NS'
pattern = r'''
(?P<non_swift>
(
(\d{2}.{0,})
(\n\d{2}.{0,})*
)|(
[^\n]*
)
)
$'''
sub_pattern = r'''
(?P<ns_id>\d{2})(?P<ns_data>.{0,})
'''
sub_pattern_m = re.compile(
sub_pattern,
re.IGNORECASE | re.VERBOSE | re.UNICODE
)
def __call__(self, transactions, value):
text = []
data = value['non_swift']
for line in data.split('\n'):
frag = self.sub_pattern_m.match(line)
if frag and frag.group(2):
ns = frag.groupdict()
value['non_swift_' + ns['ns_id']] = ns['ns_data']
text.append(ns['ns_data'])
elif len(text) and text[-1]:
text.append('')
elif line.strip():
text.append(line.strip())
value['non_swift_text'] = '\n'.join(text)
value['non_swift'] = data
return value
class BalanceBase(Tag):
'''Balance base
Pattern: 1!a6!n3!a15d
'''
pattern = r'''^
(?P<status>[DC]) # 1!a Debit/Credit
(?P<year>\d{2}) # 6!n Value Date (YYMMDD)
(?P<month>\d{2})
(?P<day>\d{2})
(?P<currency>.{3}) # 3!a Currency
(?P<amount>[0-9,]{0,16}) # 15d Amount (includes decimal sign, so 16)
'''
def __call__(self, transactions, value):
data = super(BalanceBase, self).__call__(transactions, value)
data['amount'] = models.Amount(**data)
data['date'] = models.Date(**data)
return {
self.slug: models.Balance(**data)
}
class OpeningBalance(BalanceBase):
id = 60
class FinalOpeningBalance(BalanceBase):
id = '60F'
class IntermediateOpeningBalance(BalanceBase):
id = '60M'
class Statement(Tag):
'''
The MT940 Tag 61 provides information about a single transaction that
has taken place on the account. Each transaction is identified by a
unique transaction reference number (Tag 20) and is described in the
Statement Line (Tag 61).
Pattern: 6!n[4!n]2a[1!a]15d1!a3!c23x[//16x]
The fields are:
- `value_date`: transaction date (YYMMDD)
- `entry_date`: Optional 4-digit month value and 2-digit day value of
the entry date (MMDD)
- `funds_code`: Optional 1-character code indicating the funds type (
the third character of the currency code if needed)
- `amount`: 15-digit value of the transaction amount, including commas
for decimal separation
- `transaction_type`: Optional 4-character transaction type
identification code starting with a letter followed by alphanumeric
characters and spaces
- `customer_reference`: Optional 16-character customer reference,
excluding any bank reference
- `bank_reference`: Optional 23-character bank reference starting with
"//"
- `supplementary_details`: Optional 34-character supplementary details
about the transaction.
The Tag 61 can occur multiple times within an MT940 file, with each
occurrence representing a different transaction.
'''
id = 61
scope = models.Transaction
pattern = r'''^
(?P<year>\d{2}) # 6!n Value Date (YYMMDD)
(?P<month>\d{2})
(?P<day>\d{2})
(?P<entry_month>\d{2})? # [4!n] Entry Date (MMDD)
(?P<entry_day>\d{2})?
(?P<status>R?[DC]) # 2a Debit/Credit Mark
(?P<funds_code>[A-Z])? # [1!a] Funds Code (3rd character of the currency
# code, if needed)
[\n ]? # apparently some banks (sparkassen) incorporate newlines here
# cuscal can also send a space here as well
(?P<amount>[\d,]{1,15}) # 15d Amount
(?P<id>[A-Z][A-Z0-9 ]{3})? # 1!a3!c Transaction Type Identification Code
# We need the (slow) repeating negative lookahead to search for // so we
# don't acciddntly include the bank reference in the customer reference.
(?P<customer_reference>((?!//)[^\n]){0,16}) # 16x Customer Reference
(//(?P<bank_reference>.{0,23}))? # [//23x] Bank Reference
(\n?(?P<extra_details>.{0,34}))? # [34x] Supplementary Details
$'''
def __call__(self, transactions, value):
data = super(Statement, self).__call__(transactions, value)
data.setdefault('currency', transactions.currency)
data['amount'] = models.Amount(**data)
date = data['date'] = models.Date(**data)
if data.get('entry_day') and data.get('entry_month'):
entry_date = data['entry_date'] = models.Date(
day=data.get('entry_day'),
month=data.get('entry_month'),
year=str(data['date'].year),
)
if date > entry_date and (date - entry_date).days >= 330:
year = 1
elif entry_date > date and (entry_date - date).days >= 330:
year = -1
else:
year = 0
data['guessed_entry_date'] = models.Date(
day=entry_date.day,
month=entry_date.month,
year=entry_date.year + year,
)
return data
class StatementASNB(Statement):
'''StatementASNB
From: https://www.sepaforcorporates.com/swift-for-corporates
Pattern: 6!n[4!n]2a[1!a]15d1!a3!c16x[//16x]
[34x]
But ASN bank puts the IBAN in the customer reference, which is acording to
Wikipedia at most 34 characters.
So this is the new pattern:
Pattern: 6!n[4!n]2a[1!a]15d1!a3!c34x[//16x]
[34x]
'''
pattern = r'''^
(?P<year>\d{2}) # 6!n Value Date (YYMMDD)
(?P<month>\d{2})
(?P<day>\d{2})
(?P<entry_month>\d{2})? # [4!n] Entry Date (MMDD)
(?P<entry_day>\d{2})?
(?P<status>[A-Z]?[DC]) # 2a Debit/Credit Mark
(?P<funds_code>[A-Z])? # [1!a] Funds Code (3rd character of the currency
# code, if needed)
\n? # apparently some banks (sparkassen) incorporate newlines here
(?P<amount>[\d,]{1,15}) # 15d Amount
(?P<id>[A-Z][A-Z0-9 ]{3})? # 1!a3!c Transaction Type Identification Code
(?P<customer_reference>.{0,34}) # 34x Customer Reference
(//(?P<bank_reference>.{0,16}))? # [//16x] Bank Reference
(\n?(?P<extra_details>.{0,34}))? # [34x] Supplementary Details
$'''
def __call__(self, transactions, value):
return super(StatementASNB, self).__call__(transactions, value)
class ClosingBalance(BalanceBase):
id = 62
class IntermediateClosingBalance(ClosingBalance):
id = '62M'
class FinalClosingBalance(ClosingBalance):
id = '62F'
class AvailableBalance(BalanceBase):
id = 64
class ForwardAvailableBalance(BalanceBase):
id = 65
class TransactionDetails(Tag):
'''Transaction details
Pattern: 6x65x
'''
id = 86
scope = models.Transaction
pattern = r'''
(?P<transaction_details>(([\s\S]{0,65}\r?\n?){0,8}[\s\S]{0,65}))
'''
class SumEntries(Tag):
'''Number and Sum of debit Entries
'''
id = 90
pattern = r'''^
(?P<number>\d*)
(?P<currency>.{3}) # 3!a Currency
(?P<amount>[\d,]{1,15}) # 15d Amount
'''
def __call__(self, transactions, value):
data = super(SumEntries, self).__call__(transactions, value)
data['status'] = self.status
return {
self.slug: models.SumAmount(**data)
}
class SumDebitEntries(SumEntries):
status = 'D'
id = '90D'
class SumCreditEntries(SumEntries):
status = 'C'
id = '90C'
@enum.unique
class Tags(enum.Enum):
DATE_TIME_INDICATION = DateTimeIndication()
TRANSACTION_REFERENCE_NUMBER = TransactionReferenceNumber()
RELATED_REFERENCE = RelatedReference()
ACCOUNT_IDENTIFICATION = AccountIdentification()
STATEMENT_NUMBER = StatementNumber()
OPENING_BALANCE = OpeningBalance()
INTERMEDIATE_OPENING_BALANCE = IntermediateOpeningBalance()
FINAL_OPENING_BALANCE = FinalOpeningBalance()
STATEMENT = Statement()
CLOSING_BALANCE = ClosingBalance()
INTERMEDIATE_CLOSING_BALANCE = IntermediateClosingBalance()
FINAL_CLOSING_BALANCE = FinalClosingBalance()
AVAILABLE_BALANCE = AvailableBalance()
FORWARD_AVAILABLE_BALANCE = ForwardAvailableBalance()
TRANSACTION_DETAILS = TransactionDetails()
FLOOR_LIMIT_INDICATOR = FloorLimitIndicator()
NON_SWIFT = NonSwift()
SUM_ENTRIES = SumEntries()
SUM_DEBIT_ENTRIES = SumDebitEntries()
SUM_CREDIT_ENTRIES = SumCreditEntries()
TAG_BY_ID = {t.value.id: t.value for t in Tags}
|