]> git.ipfire.org Git - thirdparty/python-fints.git/commitdiff
Add basic account balance request
authorChristian Meißner <christian.meissner@posteo.de>
Mon, 9 Jan 2017 22:51:08 +0000 (23:51 +0100)
committerChristian Meißner <christian.meissner@posteo.de>
Mon, 9 Jan 2017 23:05:44 +0000 (00:05 +0100)
fints/client.py
fints/models.py
fints/segments/saldo.py [new file with mode: 0644]

index 4070e5c8f766904d58ae35c78ee6f2498cafc1d5..671af70d0c9dcd592c37a0ffe2163d38cd8288ee 100644 (file)
@@ -1,13 +1,17 @@
 import logging
 import re
+import datetime
 
 from .connection import FinTSHTTPSConnection
 from .dialog import FinTSDialog
 from .message import FinTSMessage
 from .models import SEPAAccount
+from .models import Saldo
 from .segments.accounts import HKSPA
 from .segments.statement import HKKAZ
+from .segments.saldo import HKSAL
 from .utils import mt940_to_array
+from mt940.models import Balance
 
 logger = logging.getLogger(__name__)
 
@@ -113,7 +117,52 @@ class FinTS3Client:
             )
         ])
 
+    def get_balance(self, account):
+        # init dialog
+        dialog = self._new_dialog()
+        dialog.sync()
+        dialog.init()
+
+        # execute job
+        msg = self._create_balance_message(dialog, account)
+        logger.debug('Sending HKSAL: {}'.format(msg))
+        resp = dialog.send(msg)
+        logger.debug('Got HKSAL response: {}'.format(resp))
+        
+        # end dialog
+        dialog.end()
+
+        # find segment and split up to balance part
+        seg = resp._find_segment('HISAL')
+        arr = seg.split('+')[4].split(':')
+   
+        # get balance date
+        date = datetime.datetime.strptime(arr[3], "%Y%m%d").date()
+    
+        # return balance
+        return Balance(arr[0], arr[1], date, currency=arr[2])
+
+    def _create_balance_message(self, dialog: FinTSDialog, account: SEPAAccount):
+        hversion = dialog.hksalversion
+
+        if hversion in (1, 2, 3, 4, 5, 6):
+            acc = ':'.join([
+                account.accountnumber, account.subaccount, str(280), account.blz
+            ])
+        elif hversion == 7:
+            acc = ':'.join([
+                account.iban, account.bic, account.accountnumber, account.subaccount, str(280), account.blz
+            ])
+        else:
+            raise ValueError('Unsupported HKSAL version {}'.format(hversion))
 
+        return self._new_message(dialog, [
+            HKSAL(
+                3,
+                hversion,
+                acc
+            )
+        ])
 
 class FinTS3PinTanClient(FinTS3Client):
     def __init__(self, blz, username, pin, server):
index 2c21878ab8288b72650769a432726f7c6ab1d5dc..f2ebc6933007aa1f1f40697d75898284613ce52e 100644 (file)
@@ -1,3 +1,5 @@
 from collections import namedtuple
 
 SEPAAccount = namedtuple('SEPAAccount', 'iban bic accountnumber subaccount blz')
+
+Saldo = namedtuple('Saldo', 'account date value currency')
diff --git a/fints/segments/saldo.py b/fints/segments/saldo.py
new file mode 100644 (file)
index 0000000..76ef8e1
--- /dev/null
@@ -0,0 +1,16 @@
+from . import FinTS3Segment
+
+class HKSAL(FinTS3Segment):
+    """
+    HKSAL (Konto Saldo anfordern)
+    Section C.2.1.2
+    """
+    type = 'HKSAL'
+
+    def __init__(self, segno, version, account):
+        self.version = version
+        data = [
+            account,
+            'N'
+        ]
+        super().__init__(segno, data)