Skip to content

Commit

Permalink
Merge pull request #115 from alvarogarcia7/patch-1
Browse files Browse the repository at this point in the history
[Revolut] Support importing the fees
  • Loading branch information
tarioch committed Aug 5, 2024
2 parents 6d8f1a6 + 76e9737 commit 4b3e52c
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions src/tariochbctools/importers/revolut/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from io import StringIO

from beancount.core import amount, data
from beancount.core.number import D
from beancount.core.number import ZERO, D
from beancount.ingest import importer
from beancount.ingest.importers.mixins import identifier
from dateutil.parser import parse
Expand All @@ -13,10 +13,11 @@
class Importer(identifier.IdentifyMixin, importer.ImporterProtocol):
"""An importer for Revolut CSV files."""

def __init__(self, regexps, account, currency):
def __init__(self, regexps, account, currency, fee=None):
identifier.IdentifyMixin.__init__(self, matchers=[("filename", regexps)])
self.account = account
self.currency = currency
self._fee = fee

def name(self):
return super().name() + self.account
Expand Down Expand Up @@ -46,44 +47,66 @@ def extract(self, file, existing_entries):
skipinitialspace=True,
)
next(reader)
is_fee_mode = self._fee is not None
for row in reader:
try:
bal = D(row["Balance"].replace("'", "").strip())
amount_raw = D(row["Amount"].replace("'", "").strip())
amt = amount.Amount(amount_raw, row["Currency"])
balance = amount.Amount(bal, self.currency)
book_date = parse(row["Completed Date"].strip()).date()
fee_amt_raw = D(row["Fee"].replace("'", "").strip())
fee = amount.Amount(-fee_amt_raw, row["Currency"])
except Exception as e:
logging.warning(e)
continue

if is_fee_mode and fee_amt_raw == ZERO:
continue

postings = [
data.Posting(self.account, amt, None, None, None, None),
]
description = row["Description"].strip()
if is_fee_mode:
postings = [
data.Posting(self.account, fee, None, None, None, None),
data.Posting(
self._fee["account"], -fee, None, None, None, None
),
]
description = f"Fees for {description}"

assert isinstance(
description, str
), "Actual type of description is " + str(type(description))

entry = data.Transaction(
data.new_metadata(file.name, 0, {}),
book_date,
"*",
"",
row["Description"].strip(),
description,
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(self.account, amt, None, None, None, None),
],
postings,
)
entries.append(entry)

# only add balance after the last (newest) transaction
try:
book_date = book_date + timedelta(days=1)
entry = data.Balance(
data.new_metadata(file.name, 0, {}),
book_date,
self.account,
balance,
None,
None,
)
entries.append(entry)
except NameError:
pass
if not is_fee_mode:
# only add balance after the last (newest) transaction
try:
book_date = book_date + timedelta(days=1)
entry = data.Balance(
data.new_metadata(file.name, 0, {}),
book_date,
self.account,
balance,
None,
None,
)
entries.append(entry)
except NameError:
pass

return entries

0 comments on commit 4b3e52c

Please sign in to comment.