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 89 90 91 92 93 94 95 96 97 98 99 100 101
|
# ledger-autosync plugin for CSV files from Revolut, a Lithuania-based online bank.
import datetime
from decimal import Decimal
import re
from ledgerautosync.converter import (
Amount,
Converter,
CsvConverter,
Posting,
Transaction,
)
class RevolutConverter(CsvConverter):
FIELDSET = set(
[
"Type",
"Product",
"Started Date",
"Completed Date",
"Description",
"Amount",
"Fee",
"Currency",
"State",
"Balance",
]
)
def __init__(self, *args, **kwargs):
super(RevolutConverter, 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"])
if not amt:
return ""
currency = self.mk_currency(row["Currency"])
cleared = row["State"] == "COMPLETED"
if row["Type"] == "TOPUP":
reverse = True
acct_from = "Assets:Other"
amt_from = Amount(amt, currency, reverse=reverse)
acct_to = self.name
amt_to = Amount(amt, currency, reverse=not reverse)
else:
reverse = False
acct_from = self.name
amt_from = Amount(amt, currency, reverse=reverse)
acct_to = "Expenses:Misc"
amt_to = Amount(amt, currency, reverse=not reverse)
payee = row["Description"]
meta = {"csvid": self.get_csv_id(row)}
posting_from = Posting(
acct_from, amt_from, metadata=meta if acct_from == self.name else {}
)
posting_fee = (
Posting(
"Expenses:Bank Charges",
Amount(Decimal(row["Fee"]), currency, reverse=True),
)
if row["Fee"] != "0.00"
else None
)
posting_to = Posting(
acct_to, amt_to, metadata=meta if acct_to == self.name else {}
)
date = datetime.datetime.strptime(row["Started Date"], "%Y-%m-%d %H:%M:%S")
aux_date = (
datetime.datetime.strptime(row["Completed Date"], "%Y-%m-%d %H:%M:%S")
if row["Completed Date"]
else None
)
if aux_date and (date.date() == aux_date.date()):
aux_date = None
postings = [posting_to, posting_from]
if posting_fee:
postings.append(posting_fee)
postings.append(posting_fee.clone_inverted(self.name))
return Transaction(
date=date,
cleared=cleared,
aux_date=aux_date,
date_format="%Y-%m-%d",
payee=payee,
postings=postings,
)
|