Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Revolut] Support importing the fees #115

Merged
merged 5 commits into from
Aug 5, 2024
Merged
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
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
Loading