changeset 11:66abac370964

Renamed properties of Transaction to follow the norm issue3
author Nicolas Évrard <nicoe@b2ck.com>
date Wed, 04 Feb 2015 11:38:34 +0100
parents e30b12fab046
children 992bfc9c7841
files CHANGELOG mt940/MT940.txt mt940/__init__.py mt940/test.py
diffstat 4 files changed, 39 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGELOG	Tue Jan 06 16:55:28 2015 +0100
+++ b/CHANGELOG	Wed Feb 04 11:38:34 2015 +0100
@@ -1,3 +1,4 @@
+* Renamed properties of Transaction to follow the norm
 * Add support for ING specific description format
 
 Version 0.1 - 2014-06-25
--- a/mt940/MT940.txt	Tue Jan 06 16:55:28 2015 +0100
+++ b/mt940/MT940.txt	Wed Feb 04 11:38:34 2015 +0100
@@ -10,4 +10,7 @@
 :86:/TRTP/SEPA OVERBOEKING/IBAN/FR12345678901234/BIC/GEFRADAM
 /NAME/QASD JGRED/REMI/Dit zijn de omschrijvingsregels/EREF/NOTPRO
 VIDED
+:61:1412051205RC15,67IDXXREF
+DATA
+:61:171214C15,67TIDXTEST//REFERENCE
 :62F:C120514EUR5638,62
--- a/mt940/__init__.py	Tue Jan 06 16:55:28 2015 +0100
+++ b/mt940/__init__.py	Wed Feb 04 11:38:34 2015 +0100
@@ -57,18 +57,18 @@
 
 def _parse_amount(amount, sign='C'):
     amount = Decimal(amount.replace(',', '.'))
-    if sign == 'D':
+    if sign in ('D', 'RC'):
         return -amount
     return amount
 
 TRANSACTION_RE = re.compile(r"""
     (?P<date>\d{6})
-    (?P<booking>\d{0,4})
-    (?P<sign>(D|C))
-    (?P<code>\D?)
+    (?P<booking>\d{4})?
+    (?P<sign>D|C|RC|RD)
+    (?P<code>\w)??  # ING skips this mandatory field
     (?P<amount>(\d|,){1,15})
-    (?P<id>\D.{3})
-    (?P<reference>.{0,16})""", re.VERBOSE)
+    (?P<id>\w{4})
+    (?P<reference>.{0,34})""", re.VERBOSE)
 
 
 class MT940(object):
@@ -126,9 +126,9 @@
         lines = transaction.splitlines()
         if len(lines) == 1:
             transaction, = lines
-            account = None
+            additional_data = None
         else:
-            transaction, account = lines
+            transaction, additional_data = lines
         transaction = TRANSACTION_RE.match(transaction)
         date = _parse_date(transaction.group('date'))
         if transaction.group('booking'):
@@ -141,7 +141,9 @@
             transaction.group('sign'))
         id_ = transaction.group('id')
         reference = transaction.group('reference')
-        return (date, booking, amount, id_, reference, account, '')
+        reference, _, institution_reference = reference.partition('//')
+        return (date, booking, amount, id_, reference,
+            institution_reference, additional_data, '')
 
     def _set_statement(self, values, transactions):
         self.statements.append(
@@ -155,7 +157,8 @@
         'start_balance', 'transactions', 'end_balance'])
 Balance = namedtuple('Balance', ['date', 'amount', 'currency'])
 Transaction = namedtuple('Transaction', ['date', 'booking', 'amount', 'id',
-        'reference', 'account', 'description'])
+        'reference', 'institution_reference', 'additional_data',
+        'description'])
 
 
 def _find_swift_tags(tags, description):
--- a/mt940/test.py	Tue Jan 06 16:55:28 2015 +0100
+++ b/mt940/test.py	Wed Feb 04 11:38:34 2015 +0100
@@ -72,18 +72,38 @@
 
     def test_transaction(self):
         "Test transaction"
-        transaction, = self.mt940.statements[0].transactions
+        transaction = self.mt940.statements[0].transactions[0]
         self.assertEqual(transaction.date, datetime.date(2012, 5, 12))
         self.assertEqual(transaction.booking, datetime.date(2012, 5, 14))
         self.assertEqual(transaction.amount, Decimal('500.01'))
         self.assertEqual(transaction.id, 'N654')
         self.assertEqual(transaction.reference, 'NONREF')
-        self.assertEqual(transaction.account, '987654321')
+        self.assertEqual(transaction.additional_data, '987654321')
         self.assertEqual(transaction.description,
             '''/TRTP/SEPA OVERBOEKING/IBAN/FR12345678901234/BIC/GEFRADAM
 /NAME/QASD JGRED/REMI/Dit zijn de omschrijvingsregels/EREF/NOTPRO
 VIDED''')
 
+        transaction = self.mt940.statements[0].transactions[1]
+        self.assertEqual(transaction.date, datetime.date(2014, 12, 5))
+        self.assertEqual(transaction.booking, datetime.date(2014, 12, 5))
+        self.assertEqual(transaction.amount, Decimal('-15.67'))
+        self.assertEqual(transaction.id, 'IDXX')
+        self.assertEqual(transaction.reference, 'REF'),
+        self.assertEqual(transaction.institution_reference, '')
+        self.assertEqual(transaction.additional_data, 'DATA')
+        self.assertEqual(transaction.description, '')
+
+        transaction = self.mt940.statements[0].transactions[2]
+        self.assertEqual(transaction.date, datetime.date(2017, 12, 14))
+        self.assertEqual(transaction.booking, None)
+        self.assertEqual(transaction.amount, Decimal('15.67'))
+        self.assertEqual(transaction.id, 'TIDX')
+        self.assertEqual(transaction.reference, 'TEST'),
+        self.assertEqual(transaction.institution_reference, 'REFERENCE')
+        self.assertEqual(transaction.additional_data, None)
+        self.assertEqual(transaction.description, '')
+
 
 class TestRaboDescription(unittest.TestCase):