diff --git a/account_bank_statement_import_camt_oca/models/parser.py b/account_bank_statement_import_camt_oca/models/parser.py index a149b917..68779782 100644 --- a/account_bank_statement_import_camt_oca/models/parser.py +++ b/account_bank_statement_import_camt_oca/models/parser.py @@ -4,9 +4,11 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). import re +import pytz +from dateutil.parser import isoparse from lxml import etree -from odoo import models +from odoo import fields, models class CamtParser(models.AbstractModel): @@ -130,6 +132,19 @@ class CamtParser(models.AbstractModel): """Parse an Ntry node and yield transactions""" transaction = {"name": "/", "amount": 0} # fallback defaults self.add_value_from_node(ns, node, "./ns:BookgDt/ns:Dt", transaction, "date") + # Check `DtTm` if date was not found in `Dt`. + if "date" not in transaction: + self.add_value_from_node( + ns, node, "./ns:BookgDt/ns:DtTm", transaction, "date" + ) + date_val = transaction.get("date") + if date_val: + # Get the date respectful of the timezone. Credit to @StefanRijnhart. + dt_no_tz = isoparse(date_val).astimezone(pytz.utc).replace(tzinfo=None) + transaction["date"] = fields.Date.to_string( + fields.Date.context_today(self, timestamp=dt_no_tz) + ) + amount = self.parse_amount(ns, node) if amount != 0.0: transaction["amount"] = amount diff --git a/account_bank_statement_import_camt_oca/test_files/test-camt053-bookgdt-dttm b/account_bank_statement_import_camt_oca/test_files/test-camt053-bookgdt-dttm new file mode 100644 index 00000000..e617dbdb --- /dev/null +++ b/account_bank_statement_import_camt_oca/test_files/test-camt053-bookgdt-dttm @@ -0,0 +1,115 @@ + + + + TESTBANK/NL/1420561226673 + 2014-01-06T16:20:26.673Z + + + 1234Test/1 + 2 + 2014-01-06T16:20:26.673Z + + 2014-01-05T00:00:00.000Z + 2014-01-05T23:59:59.999Z + + + + NL77ABNA0574908765 + + Example company + + + ABNANL2A + + + + + + + OPBD + + + 15568.27 + CRDT +
+
2014-01-05
+ +
+ + + + CLBD + + + 15121.12 + CRDT +
+
2014-01-05
+ +
+ + 754.25 + DBIT + BOOK + + 2014-01-04T23:00:00.000Z + + +
2014-01-05
+
+ + + PMNT + + RDDT + ESDD + + + + EI + + + + + + INNDNL2U20141231000142300002844 + 435005714488-ABNO33052620 + 1880000341866 + + + + 754.25 + + + + + INSURANCE COMPANY TESTX + + TEST STREET 20 + 1234 AB TESTCITY + NL + + + + + NL46ABNA0499998748 + + + + + + + ABNANL2A + + + + + Insurance policy 857239PERIOD 01.01.2014 - 31.12.2014 + + MKB Insurance 859239PERIOD 01.01.2014 - 31.12.2014 + + +
+
+
+
diff --git a/account_bank_statement_import_camt_oca/tests/test_import_bank_statement.py b/account_bank_statement_import_camt_oca/tests/test_import_bank_statement.py index ac0d0688..9f009da5 100644 --- a/account_bank_statement_import_camt_oca/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_camt_oca/tests/test_import_bank_statement.py @@ -152,3 +152,23 @@ class TestImport(TransactionCase): ) self.assertTrue(all([st.line_ids for st in bank_st_record])) + + def test_import_transaction_bookgdt_datetime(self): + """Test correct date is set when `BookgDt` is a datetime (`DtTm`).""" + testfile = get_module_resource( + "account_bank_statement_import_camt_oca", + "test_files", + "test-camt053-bookgdt-dttm", + ) + with open(testfile, "rb") as datafile: + camt_file = base64.b64encode(datafile.read()) + + self.env["account.bank.statement.import"].create( + {"attachment_ids": [(0, 0, {"name": "test file", "datas": camt_file})]} + ).import_file() + + bank_st_record = self.env["account.bank.statement"].search( + [("name", "=", "1234Test/1")], limit=1 + ) + statement_lines = bank_st_record.line_ids + self.assertEqual(statement_lines.mapped("date"), [date(2014, 1, 5)])