Skip to content

Commit

Permalink
Merge pull request #15 from tarioch/feature/tlimp_balance
Browse files Browse the repository at this point in the history
Generate also balance statements for truelayer importer
  • Loading branch information
tarioch authored Apr 26, 2020
2 parents b2d5ebd + 4d6fe09 commit 8c68d88
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/tariochbctools/importers/truelayer/importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yaml
import dateutil.parser
from datetime import timedelta
from os import path
import requests

Expand Down Expand Up @@ -42,25 +43,46 @@ def extract(self, file, existing_entries):
accountId = account['account_id']
accountCcy = account['currency']
r = requests.get(f'https://api.truelayer.com/data/v1/accounts/{accountId}/transactions', headers=headers)
for trx in r.json()['results']:
transactions = sorted(r.json()['results'], key=lambda trx: trx['timestamp'])
for trx in transactions:
metakv = {
'tlref': trx['meta']['provider_id'],
}
if trx['transaction_classification']:
metakv['category'] = trx['transaction_classification'][0]
meta = data.new_metadata('', 0, metakv)
trxDate = dateutil.parser.parse(trx['timestamp']).date()
account = baseAccount + accountCcy
entry = data.Transaction(
meta,
dateutil.parser.parse(trx['timestamp']).date(),
trxDate,
'*',
'',
trx['description'],
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(baseAccount + accountCcy, amount.Amount(D(str(trx['amount'])), trx['currency']), None, None, None, None),
data.Posting(account, amount.Amount(D(str(trx['amount'])), trx['currency']), None, None, None, None),
]
)
entries.append(entry)

if trx['transaction_id'] == transactions[-1]['transaction_id']:
balDate = trxDate + timedelta(days=1)
metakv = {}
if existing_entries is not None:
for exEntry in existing_entries:
if isinstance(exEntry, data.Balance) and exEntry.date == balDate and exEntry.account == account:
metakv['__duplicate__'] = True

meta = data.new_metadata('', 0, metakv)
entries.append(data.Balance(
meta,
balDate,
account,
amount.Amount(D(str(trx['running_balance']['amount'])), trx['running_balance']['currency']),
None,
None,
))

return entries

0 comments on commit 8c68d88

Please sign in to comment.