2020-01-16 19:02:17 +01:00
|
|
|
import csv
|
2020-02-08 19:07:05 +01:00
|
|
|
from collections import defaultdict
|
2020-01-16 19:02:17 +01:00
|
|
|
|
|
|
|
import click
|
|
|
|
import dateparser
|
|
|
|
|
2020-02-12 00:00:05 +01:00
|
|
|
from ofx_processor.utils import ynab
|
|
|
|
|
2020-01-16 19:02:17 +01:00
|
|
|
|
2020-02-22 14:53:52 +01:00
|
|
|
@click.command(help="Process Revolut bank statement (CSV)")
|
|
|
|
@click.argument("csv_filename")
|
|
|
|
def cli(csv_filename):
|
|
|
|
ynab_transactions = []
|
|
|
|
transaction_ids = defaultdict(int)
|
|
|
|
|
|
|
|
with open(csv_filename) as f:
|
|
|
|
reader = csv.DictReader(f, delimiter=";")
|
|
|
|
for line in reader:
|
|
|
|
ynab_transaction = line_to_ynab_transaction(line, transaction_ids)
|
|
|
|
ynab_transactions.append(ynab_transaction)
|
|
|
|
click.secho(f"Processed {len(ynab_transactions)} transactions total.", fg="blue")
|
|
|
|
|
|
|
|
ynab.push_transactions(ynab_transactions, "revolut")
|
|
|
|
|
|
|
|
|
|
|
|
def _amount_str_to_float(amount):
|
2020-01-16 19:02:17 +01:00
|
|
|
if amount:
|
|
|
|
return float(amount.replace(",", "."))
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
def process_memo(line):
|
|
|
|
return " - ".join(
|
2020-01-17 16:18:47 +01:00
|
|
|
filter(
|
|
|
|
None,
|
|
|
|
map(str.strip, [line.get("Category", ""), line.get("Exchange Rate", "")]),
|
|
|
|
)
|
2020-01-16 19:02:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-17 16:18:47 +01:00
|
|
|
def process_date(line):
|
|
|
|
return dateparser.parse(line.get("Completed Date")).strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
|
2020-02-22 14:53:52 +01:00
|
|
|
def _process_inflow(line):
|
|
|
|
return _amount_str_to_float(line.get("Paid In (EUR)"))
|
2020-01-17 16:18:47 +01:00
|
|
|
|
|
|
|
|
2020-02-22 14:53:52 +01:00
|
|
|
def _process_outflow(line):
|
|
|
|
return _amount_str_to_float(line.get("Paid Out (EUR)"))
|
2020-01-16 19:02:17 +01:00
|
|
|
|
|
|
|
|
2020-02-22 14:53:52 +01:00
|
|
|
def process_amount(line):
|
|
|
|
outflow = _process_outflow(line)
|
|
|
|
inflow = _process_inflow(line)
|
|
|
|
amount = -outflow if outflow else inflow
|
|
|
|
amount = int(amount * 1000)
|
|
|
|
return amount
|
2020-01-16 19:02:17 +01:00
|
|
|
|
|
|
|
|
2020-02-22 14:53:52 +01:00
|
|
|
def line_to_ynab_transaction(line, transaction_ids):
|
|
|
|
date = process_date(line)
|
|
|
|
payee = process_payee(line)
|
|
|
|
memo = process_memo(line)
|
|
|
|
amount = process_amount(line)
|
|
|
|
import_id = f"YNAB:{amount}:{date}"
|
|
|
|
transaction_ids[import_id] += 1
|
|
|
|
occurrence = transaction_ids[import_id]
|
|
|
|
import_id = f"{import_id}:{occurrence}"
|
|
|
|
ynab_transaction = {
|
|
|
|
"date": date,
|
|
|
|
"amount": amount,
|
|
|
|
"payee_name": payee,
|
|
|
|
"memo": memo,
|
|
|
|
"import_id": import_id,
|
|
|
|
}
|
|
|
|
return ynab_transaction
|
2020-01-16 19:02:17 +01:00
|
|
|
|
|
|
|
|
2020-02-22 14:53:52 +01:00
|
|
|
def process_payee(line):
|
|
|
|
payee = line["Reference"]
|
|
|
|
return payee
|