From a2627a24e98d710ad041eaf5f38321e2c962c294 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 1 Sep 2020 19:18:55 +0200 Subject: [PATCH] Cleanup payee name and date for LCL --- ofx_processor/processors/lcl.py | 33 ++++++++++++++++++++++++++- ofx_processor/utils/base_processor.py | 7 +++--- tests/samples/lcl.ofx | 7 ++++++ tests/samples/lcl_as_downloaded.ofx | 7 ++++++ tests/samples/lcl_expected.json | 9 +++++++- tests/samples/lcl_transactions.json | 10 +++++++- tests/test_lcl_processor.py | 18 +++++++++++++++ 7 files changed, 84 insertions(+), 7 deletions(-) diff --git a/ofx_processor/processors/lcl.py b/ofx_processor/processors/lcl.py index e3be24f..daaad56 100644 --- a/ofx_processor/processors/lcl.py +++ b/ofx_processor/processors/lcl.py @@ -1,12 +1,43 @@ import sys +from datetime import datetime import click +import dateparser from ofx_processor.utils.base_ofx import OfxBaseLine, OfxBaseProcessor class LclLine(OfxBaseLine): - pass + def _extract_payee_and_date(self): + split = self.data.name.split() + default_date = ( + self.data.dtposted.isoformat().split("T")[0] if self.data.dtposted else None + ) + + if not split: + return "", default_date + + date_spot = split[-1] + if "/" not in date_spot: + return " ".join(split), default_date + + date = dateparser.parse( + split[-1], date_formats=["%d/%m/%Y"], languages=["fr"] + ) # type: datetime + if date: + name = " ".join(split[:-1]) + date = date.strftime("%Y-%m-%d") # type: str + else: + name = " ".join(split) + date = default_date + + return name, date + + def get_payee(self): + return self._extract_payee_and_date()[0] + + def get_date(self): + return self._extract_payee_and_date()[1] class LclProcessor(OfxBaseProcessor): diff --git a/ofx_processor/utils/base_processor.py b/ofx_processor/utils/base_processor.py index d543076..942cdbb 100644 --- a/ofx_processor/utils/base_processor.py +++ b/ofx_processor/utils/base_processor.py @@ -2,15 +2,14 @@ import os from collections import defaultdict import click +from ofxtools.models import STMTTRN from ofx_processor.utils import ynab class BaseLine: - data = None - - def __init__(self, data=None): - self.data = data + def __init__(self, data: STMTTRN = None): + self.data = data # type: STMTTRN def get_date(self): raise NotImplementedError # pragma: nocover diff --git a/tests/samples/lcl.ofx b/tests/samples/lcl.ofx index 723cf6a..918ca5b 100644 --- a/tests/samples/lcl.ofx +++ b/tests/samples/lcl.ofx @@ -45,6 +45,13 @@ NEWFILEUID:NONE 348 120320 100000 VIR INST M PAYEE 1 + +DEBIT +20200720 +-400.45 +348 200720 -40045 + CB IKEA 17/07/20 + +1000.00 diff --git a/tests/samples/lcl_as_downloaded.ofx b/tests/samples/lcl_as_downloaded.ofx index f6f3627..891104c 100644 --- a/tests/samples/lcl_as_downloaded.ofx +++ b/tests/samples/lcl_as_downloaded.ofx @@ -46,6 +46,13 @@ NEWFILEUID:NONE 348 120320 100000 VIR INST M PAYEE 1 + +DEBIT +20200720 +-400.45 +348 200720 -40045 + CB IKEA 17/07/20 + +1000.00 diff --git a/tests/samples/lcl_expected.json b/tests/samples/lcl_expected.json index 466af23..bec5da4 100644 --- a/tests/samples/lcl_expected.json +++ b/tests/samples/lcl_expected.json @@ -2,8 +2,15 @@ { "date": "2020-03-12", "amount": 1000000, - "payee_name": "VIR INST M PAYEE 1", + "payee_name": "VIR INST M PAYEE 1", "memo": null, "import_id": "YNAB:1000000:2020-03-12:1" + }, + { + "date": "2020-07-17", + "amount": -400450, + "payee_name": "CB IKEA", + "memo": null, + "import_id": "YNAB:-400450:2020-07-17:1" } ] \ No newline at end of file diff --git a/tests/samples/lcl_transactions.json b/tests/samples/lcl_transactions.json index 041fcc8..4b0edbd 100644 --- a/tests/samples/lcl_transactions.json +++ b/tests/samples/lcl_transactions.json @@ -3,10 +3,18 @@ { "date": "2020-03-12", "amount": 1000000, - "payee_name": "VIR INST M PAYEE 1", + "payee_name": "VIR INST M PAYEE 1", "memo": null, "import_id": "YNAB:1000000:2020-03-12:1", "account_id": "" + }, + { + "date": "2020-07-17", + "amount": -400450, + "payee_name": "CB IKEA", + "memo": null, + "import_id": "YNAB:-400450:2020-07-17:1", + "account_id": "" } ] } \ No newline at end of file diff --git a/tests/test_lcl_processor.py b/tests/test_lcl_processor.py index 86bad73..ceb2962 100644 --- a/tests/test_lcl_processor.py +++ b/tests/test_lcl_processor.py @@ -14,6 +14,14 @@ class LclLineTestCase(unittest.TestCase): result_name = LclLine(transaction).get_payee() self.assertEqual(result_name, name) + def test_get_name_with_date(self): + name = " CB IKEA 17/07/20" + transaction = OfxTransaction(name=name) + + expected_name = "CB IKEA" + result_name = LclLine(transaction).get_payee() + self.assertEqual(result_name, expected_name) + def test_get_memo(self): memo = "VIR INST" transaction = OfxTransaction(memo=memo) @@ -28,6 +36,16 @@ class LclLineTestCase(unittest.TestCase): result_date = LclLine(transaction).get_date() self.assertEqual(result_date, expected_date) + def test_get_date_with_transaction_name(self): + transaction = OfxTransaction( + dtposted=datetime.datetime(2020, 1, 23, 1, 2, 3), + name=" CB IKEA 17/07/20", + ) + expected_date = "2020-07-17" + + result_date = LclLine(transaction).get_date() + self.assertEqual(result_date, expected_date) + def test_get_amount_positive(self): transaction = OfxTransaction(trnamt=52.2) expected_amount = 52200