1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
# ledger-autosync plugin for CSV files from N26, a Berlin-based online bank.
import datetime
from decimal import Decimal
import re
from ledgerautosync.converter import (
Amount,
Converter,
CsvConverter,
Posting,
Transaction,
)
class N26Converter(CsvConverter):
FIELDSET = set(
[
"Date",
"Payee",
"Account number",
"Transaction type",
"Payment reference",
"Amount (EUR)",
"Amount (Foreign Currency)",
"Type Foreign Currency",
"Exchange Rate",
]
)
def __init__(self, *args, **kwargs):
super(N26Converter, self).__init__(*args, **kwargs)
def mk_currency(self, currency):
if currency == "USD":
currency = "$"
elif currency == "GBP":
currency = "£"
elif currency == "EUR":
currency = "€"
return currency
def convert(self, row):
amt = Decimal(row["Amount (EUR)"])
curr_foreign = self.mk_currency(row["Type Foreign Currency"] or "EUR")
amt_foreign = (
Decimal(row["Amount (Foreign Currency)"])
if row["Amount (Foreign Currency)"]
else amt
)
if amt < 0:
reverse = False
acct_from = self.name
curr_from = self.mk_currency("EUR")
amt_from = Amount(amt, curr_from, reverse=reverse)
acct_to = "Expenses:Misc"
curr_to = curr_foreign
amt_to = Amount(amt_foreign, curr_to, reverse=not reverse)
else:
reverse = True
acct_from = "Assets:Other"
curr_from = curr_foreign
amt_from = Amount(amt_foreign, curr_from, reverse=reverse)
acct_to = self.name
curr_to = self.mk_currency("EUR")
amt_to = Amount(amt, curr_to, reverse=not reverse)
payee = re.sub(
r"[A-Za-z]+('[A-Za-z]+)?",
lambda word: word.group(0).capitalize(),
row["Payee"],
)
meta = {"csvid": self.get_csv_id(row)}
posting_from = Posting(
acct_from, amt_from, metadata=meta if acct_from == self.name else {}
)
posting_to = Posting(
acct_to, amt_to, metadata=meta if acct_to == self.name else {}
)
return Transaction(
date=datetime.datetime.strptime(row["Date"], "%Y-%m-%d"),
cleared=True,
date_format="%Y-%m-%d",
payee=payee,
postings=[posting_to, posting_from],
)
|