From d25f5b7188ce28796d76761df4cabd8d026ce138 Mon Sep 17 00:00:00 2001 From: duongtq Date: Thu, 1 Feb 2024 11:33:27 +0700 Subject: [PATCH] [IMP] base_wamas_ubl: rebuild mapping dynamically and override default mapping --- base_wamas_ubl/__manifest__.py | 4 +- base_wamas_ubl/lib/wamas/ubl2wamas.py | 7 ++-- base_wamas_ubl/lib/wamas/utils.py | 39 +++++++++++++----- base_wamas_ubl/models/__init__.py | 1 + base_wamas_ubl/models/base_wamas_ubl.py | 41 +++++++++++++++++-- base_wamas_ubl/models/uom_uom.py | 13 ++++++ ...LE_AUSKQ_WATEKQ_WATEPQ-DESPATCH_ADVICE.xml | 4 +- base_wamas_ubl/tests/test_base_wamas_ubl.py | 27 ++++++++++++ base_wamas_ubl/views/uom_category.xml | 12 ++++++ base_wamas_ubl/views/uom_uom.xml | 21 ++++++++++ 10 files changed, 150 insertions(+), 19 deletions(-) create mode 100644 base_wamas_ubl/models/uom_uom.py create mode 100644 base_wamas_ubl/views/uom_category.xml create mode 100644 base_wamas_ubl/views/uom_uom.xml diff --git a/base_wamas_ubl/__manifest__.py b/base_wamas_ubl/__manifest__.py index 4b42b83bcf..82842b7555 100644 --- a/base_wamas_ubl/__manifest__.py +++ b/base_wamas_ubl/__manifest__.py @@ -11,7 +11,7 @@ "website": "https://github.com/OCA/edi", "license": "AGPL-3", "author": "Camptocamp,Odoo Community Association (OCA)", - "depends": ["base_edi", "base_ubl"], + "depends": ["base_edi", "base_ubl", "uom_unece"], "external_dependencies": { "python": ["xmltodict", "dotty-dict", "pytz"], }, @@ -19,6 +19,8 @@ "security/ir.model.access.csv", "wizard/wamas_ubl_wiz_check.xml", "wizard/wamas_ubl_wiz_simulate.xml", + "views/uom_uom.xml", + "views/uom_category.xml", "views/wamas_menu.xml", ], } diff --git a/base_wamas_ubl/lib/wamas/ubl2wamas.py b/base_wamas_ubl/lib/wamas/ubl2wamas.py index b43d6d57d7..366a820fef 100755 --- a/base_wamas_ubl/lib/wamas/ubl2wamas.py +++ b/base_wamas_ubl/lib/wamas/ubl2wamas.py @@ -18,7 +18,7 @@ from utils import file_open, generate_wamas_line -def ubl2list(infile, telegram_type): # noqa: C901 +def ubl2list(infile, telegram_type, extra_data=False): # noqa: C901 res = [] my_dict = Dotty(xmltodict.parse(infile)) @@ -54,6 +54,7 @@ def ubl2list(infile, telegram_type): # noqa: C901 line_idx=line_idx, len_loop=len_loop, idx_loop=idx_loop, + extra_data=extra_data, ) if line: res.append(line) @@ -61,8 +62,8 @@ def ubl2list(infile, telegram_type): # noqa: C901 return res -def ubl2wamas(infile, telegram_type, verbose=False): - lst_of_str_wamas = ubl2list(infile, telegram_type) +def ubl2wamas(infile, telegram_type, extra_data=False, verbose=False): + lst_of_str_wamas = ubl2list(infile, telegram_type, extra_data=extra_data) wamas = "\n".join(lst_of_str_wamas) if verbose: _logger.debug(wamas) diff --git a/base_wamas_ubl/lib/wamas/utils.py b/base_wamas_ubl/lib/wamas/utils.py index 932ff3ce8d..3d2f54bc80 100644 --- a/base_wamas_ubl/lib/wamas/utils.py +++ b/base_wamas_ubl/lib/wamas/utils.py @@ -211,9 +211,16 @@ def get_date_from_field(*args): return res -def convert_unit_code(key, val): +def convert_unit_code(key, val, unit_mapping=None): if key in LST_FIELD_UNIT_CODE: - return MAPPING_UNITCODE_UBL_TO_WAMAS["unitCode"].get(val, val) + _mapping = ( + unit_mapping + and unit_mapping.get( + "MAPPING_UNITCODE_UBL_TO_WAMAS", MAPPING_UNITCODE_UBL_TO_WAMAS + ) + or MAPPING_UNITCODE_UBL_TO_WAMAS + ) + return _mapping["unitCode"].get(val, val) return val @@ -327,7 +334,8 @@ def generate_wamas_line(dict_item, grammar, **kwargs): # noqa: C901 val = globals()[df_func](*args) - val = convert_unit_code(_key, val) + extra_data = kwargs.get("extra_data", None) + val = convert_unit_code(_key, val, unit_mapping=extra_data) if kwargs.get("check_to_set_value_to_string", False): # Ignore convert string of float/int/date/datetime type # to move entire value when convert wamas2wamas @@ -352,16 +360,20 @@ def generate_wamas_line(dict_item, grammar, **kwargs): # noqa: C901 return res -def generate_wamas_lines(dict_input, telegram_type, line_idx, wamas_lines): +def generate_wamas_lines( + dict_input, telegram_type, line_idx, wamas_lines, extra_data=False +): line_idx += 1 grammar = DICT_WAMAS_GRAMMAR[telegram_type.lower()] - line = generate_wamas_line(dict_input, grammar, line_idx=line_idx) + line = generate_wamas_line( + dict_input, grammar, line_idx=line_idx, extra_data=extra_data + ) if line: wamas_lines.append(line) return line_idx, wamas_lines -def dict2wamas(dict_input, telegram_type): +def dict2wamas(dict_input, telegram_type, extra_data=False): wamas_lines = [] lst_telegram_type = telegram_type.split(",") @@ -375,16 +387,16 @@ def dict2wamas(dict_input, telegram_type): # 1 line for `KstAus_LagIdKom = kMEZ` dict_input["picking_zone"] = "kMEZ" line_idx, wamas_lines = generate_wamas_lines( - dict_input, telegram_type, line_idx, wamas_lines + dict_input, telegram_type, line_idx, wamas_lines, extra_data=extra_data ) # 1 line for `KstAus_LagIdKom = kPAR` dict_input["picking_zone"] = "kPAR" line_idx, wamas_lines = generate_wamas_lines( - dict_input, telegram_type, line_idx, wamas_lines + dict_input, telegram_type, line_idx, wamas_lines, extra_data=extra_data ) else: line_idx, wamas_lines = generate_wamas_lines( - dict_input, telegram_type, line_idx, wamas_lines + dict_input, telegram_type, line_idx, wamas_lines, extra_data=extra_data ) return "\n".join(wamas_lines).encode("iso-8859-1") @@ -510,13 +522,20 @@ def wamas2dict( def dict2ubl(template, data, verbose=False, extra_data=False): t = miniqweb.QWebXml(template) + _mapping = ( + extra_data + and extra_data.get( + "MAPPING_UNITCODE_WAMAS_TO_UBL", MAPPING_UNITCODE_WAMAS_TO_UBL + ) + or MAPPING_UNITCODE_WAMAS_TO_UBL + ) # Convert dict to object to use dotted notation in template globals_dict = { "record": obj(data), "get_date": get_date, "get_time": get_time, "get_current_date": get_current_date, - "MAPPING": MAPPING_UNITCODE_WAMAS_TO_UBL, + "MAPPING": _mapping, "extra_data": extra_data, } xml = t.render(globals_dict) diff --git a/base_wamas_ubl/models/__init__.py b/base_wamas_ubl/models/__init__.py index e56afa0e44..97c44c6bda 100644 --- a/base_wamas_ubl/models/__init__.py +++ b/base_wamas_ubl/models/__init__.py @@ -1 +1,2 @@ from . import base_wamas_ubl +from . import uom_uom diff --git a/base_wamas_ubl/models/base_wamas_ubl.py b/base_wamas_ubl/models/base_wamas_ubl.py index 93b8f44fe0..86b1fe0ee0 100644 --- a/base_wamas_ubl/models/base_wamas_ubl.py +++ b/base_wamas_ubl/models/base_wamas_ubl.py @@ -4,6 +4,7 @@ from odoo import _, api, models +from ..lib.wamas.structure import MappingDict from ..lib.wamas.ubl2wamas import ubl2wamas from ..lib.wamas.utils import ( detect_wamas_type, @@ -25,19 +26,24 @@ def wamas2dict(self, str_file): @api.model def dict2ubl(self, template, data): - return dict2ubl(template, data) + extra_data = self._build_unitcode_mapping() + return dict2ubl(template, data, extra_data=extra_data) @api.model def wamas2ubl(self, str_file, extra_data=False): + extra_data = extra_data if extra_data else {} + extra_data.update(self._build_unitcode_mapping()) return wamas2ubl(str_file, extra_data=extra_data) @api.model def ubl2wamas(self, str_file, telegram_type): - return ubl2wamas(str_file, telegram_type) + extra_data = self._build_unitcode_mapping() + return ubl2wamas(str_file, telegram_type, extra_data=extra_data) @api.model def dict2wamas(self, dict_input, telegram_type): - return dict2wamas(dict_input, telegram_type) + extra_data = self._build_unitcode_mapping() + return dict2wamas(dict_input, telegram_type, extra_data=extra_data) @api.model def get_wamas_type(self, str_file): @@ -69,3 +75,32 @@ def get_supported_telegram(self): @api.model def get_supported_telegram_w2w(self): return get_supported_telegram_w2w() + + @api.model + def _build_unitcode_mapping(self): + uom_records = self.env["uom.uom"].search([]) + uom_records = uom_records.filtered( + lambda rec: rec.wamas_code and rec.unece_code + ) + if not uom_records: + return {} + + MAPPING_UNITCODE_WAMAS_TO_UBL = { + "unitCode": MappingDict( + { + "": False, # undefined, + } + ) + } + MAPPING_UNITCODE_UBL_TO_WAMAS = {"unitCode": MappingDict()} + for uom_record in uom_records: + MAPPING_UNITCODE_WAMAS_TO_UBL["unitCode"][ + uom_record.wamas_code + ] = uom_record.unece_code + MAPPING_UNITCODE_UBL_TO_WAMAS["unitCode"][ + uom_record.unece_code + ] = uom_record.wamas_code + return { + "MAPPING_UNITCODE_WAMAS_TO_UBL": MAPPING_UNITCODE_WAMAS_TO_UBL, + "MAPPING_UNITCODE_UBL_TO_WAMAS": MAPPING_UNITCODE_UBL_TO_WAMAS, + } diff --git a/base_wamas_ubl/models/uom_uom.py b/base_wamas_ubl/models/uom_uom.py new file mode 100644 index 0000000000..7720dabcab --- /dev/null +++ b/base_wamas_ubl/models/uom_uom.py @@ -0,0 +1,13 @@ +# Copyright 2024 Jacques-Etienne Baudoux (BCIM) +# Copyright 2024 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class UomUom(models.Model): + _inherit = "uom.uom" + + wamas_code = fields.Char( + string="WAMAS Code", + ) diff --git a/base_wamas_ubl/tests/samples/WAMAS2UBL-SAMPLE_AUSKQ_WATEKQ_WATEPQ-DESPATCH_ADVICE.xml b/base_wamas_ubl/tests/samples/WAMAS2UBL-SAMPLE_AUSKQ_WATEKQ_WATEPQ-DESPATCH_ADVICE.xml index a1123bdc30..9dee72a5b7 100644 --- a/base_wamas_ubl/tests/samples/WAMAS2UBL-SAMPLE_AUSKQ_WATEKQ_WATEPQ-DESPATCH_ADVICE.xml +++ b/base_wamas_ubl/tests/samples/WAMAS2UBL-SAMPLE_AUSKQ_WATEKQ_WATEPQ-DESPATCH_ADVICE.xml @@ -81,7 +81,7 @@ 1 - 7.0 + 7.0 000101 @@ -106,7 +106,7 @@ 2 - 8.0 + 8.0 000101 diff --git a/base_wamas_ubl/tests/test_base_wamas_ubl.py b/base_wamas_ubl/tests/test_base_wamas_ubl.py index 4b58bcd200..b4ce1b4633 100644 --- a/base_wamas_ubl/tests/test_base_wamas_ubl.py +++ b/base_wamas_ubl/tests/test_base_wamas_ubl.py @@ -12,6 +12,8 @@ from odoo.addons.account.tests.common import AccountTestInvoicingCommon +from ..lib.wamas.structure import MappingDict + class TestBaseWamas(TransactionCase): @classmethod @@ -75,6 +77,31 @@ def setUpClass(cls): and cls.partner_2.child_ids[0].email or "", }, + "MAPPING_UNITCODE_WAMAS_TO_UBL": { + "unitCode": MappingDict( + { + "BOT": "XBQ", # plastic bottle + "BOUT": "C62", # Unit + "BOITE": "XBX", # box + "LITRE": "LTR", # litre + "PET": "XBO", # glass bottle + "TETRA": "X4A", # tetra pack, changed 'X4B' to 'X4A'for testing + "": False, # undefined, + } + ) + }, + "MAPPING_UNITCODE_UBL_TO_WAMAS": { + "unitCode": MappingDict( + { + "XBQ": "BOT", # plastic bottle + "C62": "BOUT", # Unit + "XBX": "BOITE", # box + "LTR": "LITRE", # litre + "XBO": "PET", # glass bottle + "X4A": "TETRA", # tetra pack, changed 'X4B' to 'X4A'for testing + } + ) + }, } @freeze_time("2023-05-01") diff --git a/base_wamas_ubl/views/uom_category.xml b/base_wamas_ubl/views/uom_category.xml new file mode 100644 index 0000000000..bd9f901f55 --- /dev/null +++ b/base_wamas_ubl/views/uom_category.xml @@ -0,0 +1,12 @@ + + + + uom.category + + + + + + + + diff --git a/base_wamas_ubl/views/uom_uom.xml b/base_wamas_ubl/views/uom_uom.xml new file mode 100644 index 0000000000..1bb790f6df --- /dev/null +++ b/base_wamas_ubl/views/uom_uom.xml @@ -0,0 +1,21 @@ + + + + uom.uom + + + + + + + + + uom.uom + + + + + + + +