forked from gaugendre/ofx-processor
Cleanup payee name and date for LCL
This commit is contained in:
parent
d7329514f8
commit
a2627a24e9
7 changed files with 84 additions and 7 deletions
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -45,6 +45,13 @@ NEWFILEUID:NONE
|
|||
<FITID>348 120320 100000
|
||||
<NAME>VIR INST M PAYEE 1
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT
|
||||
<DTPOSTED>20200720
|
||||
<TRNAMT>-400.45
|
||||
<FITID>348 200720 -40045
|
||||
<NAME> CB IKEA 17/07/20
|
||||
</STMTTRN>
|
||||
</BANKTRANLIST>
|
||||
<LEDGERBAL>
|
||||
<BALAMT>+1000.00
|
||||
|
|
|
@ -46,6 +46,13 @@ NEWFILEUID:NONE
|
|||
<FITID>348 120320 100000
|
||||
<NAME>VIR INST M PAYEE 1
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT
|
||||
<DTPOSTED>20200720
|
||||
<TRNAMT>-400.45
|
||||
<FITID>348 200720 -40045
|
||||
<NAME> CB IKEA 17/07/20
|
||||
</STMTTRN>
|
||||
</BANKTRANLIST>
|
||||
<LEDGERBAL>
|
||||
<BALAMT>+1000.00
|
||||
|
|
|
@ -5,5 +5,12 @@
|
|||
"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"
|
||||
}
|
||||
]
|
|
@ -7,6 +7,14 @@
|
|||
"memo": null,
|
||||
"import_id": "YNAB:1000000:2020-03-12:1",
|
||||
"account_id": "<YOUR CE ACCOUNT ID>"
|
||||
},
|
||||
{
|
||||
"date": "2020-07-17",
|
||||
"amount": -400450,
|
||||
"payee_name": "CB IKEA",
|
||||
"memo": null,
|
||||
"import_id": "YNAB:-400450:2020-07-17:1",
|
||||
"account_id": "<YOUR CE ACCOUNT ID>"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue