Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions fints/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,52 @@ def get_transactions(self, account: SEPAAccount, start_date: datetime.date = Non

return statement

def get_filtered_transactions(self, account: SEPAAccount, start_date: datetime.date = None,
end_date: datetime.date = None):
"""
Fetches the list of transactions of a bank account in a certain timeframe.
MT940-Errors are catched and the statements containing them returned as a seperate list.
:param account: SEPA
:param start_date: First day to fetch
:param end_date: Last day to fetch
:return: A tuple with a list of mt940.models.Transaction objects and another list with tuples of mt940-data
and error messages.
"""

with self._get_dialog() as dialog:
hkkaz = self._find_highest_supported_command(HKKAZ5, HKKAZ6, HKKAZ7)

logger.info('Start fetching from {} to {}'.format(start_date, end_date))
responses = self._fetch_with_touchdowns(
dialog,
lambda touchdown: hkkaz(
account=hkkaz._fields['account'].type.from_sepa_account(
account),
all_accounts=False,
date_start=start_date,
date_end=end_date,
touchdown_point=touchdown,
),
'HIKAZ'
)
logger.info('Fetching done.')

statement = []
with_error = []
for seg in responses:
# Note: MT940 messages are encoded in the S.W.I.F.T character set,
# which is a subset of ISO 8859. There are no character in it that
# differ between ISO 8859 variants, so we'll arbitrarily chose 8859-1.
try:
statement += mt940_to_array(seg.statement_booked.decode('iso-8859-1'))
except Exception as e:
with_error.append((seg.statement_booked.decode('iso-8859-1'), str(e)))

logger.debug('Statement: {}'.format(statement))
logger.debug('With error: {}'.format(with_error))

return statement, with_error

def get_credit_card_transactions(self, account: SEPAAccount, credit_card_number: str, start_date: datetime.date = None, end_date: datetime.date = None):
# FIXME Reverse engineered, probably wrong
with self._get_dialog() as dialog:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def test_get_sepa_accounts(fints_client):

assert accounts


def test_get_information(fints_client):
with fints_client:
information = fints_client.get_information()
Expand Down Expand Up @@ -202,3 +201,13 @@ def test_get_transactions(fints_client):

assert len(transactions) == 3
assert transactions[0].data['amount'].amount == Decimal('182.34')

def test_get_filtered_transactions(fints_client):
with fints_client:
accounts = fints_client.get_sepa_accounts()

transactions, errors = fints_client.get_filtered_transactions(accounts[0])

assert len(transactions) == 3
assert len(errors) == 0
assert transactions[0].data['amount'].amount == Decimal('182.34')