Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] base_wamas_ubl: rebuild mapping dynamically and override default mapping #917

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion base_wamas_ubl/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
"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"],
},
"data": [
"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",
],
}
7 changes: 4 additions & 3 deletions base_wamas_ubl/lib/wamas/ubl2wamas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -54,15 +54,16 @@ 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)

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)
Expand Down
39 changes: 29 additions & 10 deletions base_wamas_ubl/lib/wamas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,16 @@
return res


def convert_unit_code(key, val):
def convert_unit_code(key, val, unit_mapping=None):

Check warning on line 214 in base_wamas_ubl/lib/wamas/utils.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/lib/wamas/utils.py#L214

Added line #L214 was not covered by tests
if key in LST_FIELD_UNIT_CODE:
return MAPPING_UNITCODE_UBL_TO_WAMAS["unitCode"].get(val, val)
_mapping = (

Check warning on line 216 in base_wamas_ubl/lib/wamas/utils.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/lib/wamas/utils.py#L216

Added line #L216 was not covered by tests
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


Expand Down Expand Up @@ -327,7 +334,8 @@

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
Expand All @@ -352,16 +360,20 @@
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

Check warning on line 364 in base_wamas_ubl/lib/wamas/utils.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/lib/wamas/utils.py#L364

Added line #L364 was not covered by tests
):
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(",")

Expand All @@ -375,16 +387,16 @@
# 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")

Expand Down Expand Up @@ -510,13 +522,20 @@

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)
Expand Down
1 change: 1 addition & 0 deletions base_wamas_ubl/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import base_wamas_ubl
from . import uom_uom
41 changes: 38 additions & 3 deletions base_wamas_ubl/models/base_wamas_ubl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,19 +26,24 @@

@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)

Check warning on line 30 in base_wamas_ubl/models/base_wamas_ubl.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/models/base_wamas_ubl.py#L29-L30

Added lines #L29 - L30 were not covered by tests

@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):
Expand Down Expand Up @@ -69,3 +75,32 @@
@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 = {

Check warning on line 88 in base_wamas_ubl/models/base_wamas_ubl.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/models/base_wamas_ubl.py#L88

Added line #L88 was not covered by tests
"unitCode": MappingDict(
{
"": False, # undefined,
}
)
}
MAPPING_UNITCODE_UBL_TO_WAMAS = {"unitCode": MappingDict()}

Check warning on line 95 in base_wamas_ubl/models/base_wamas_ubl.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/models/base_wamas_ubl.py#L95

Added line #L95 was not covered by tests
for uom_record in uom_records:
MAPPING_UNITCODE_WAMAS_TO_UBL["unitCode"][

Check warning on line 97 in base_wamas_ubl/models/base_wamas_ubl.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/models/base_wamas_ubl.py#L97

Added line #L97 was not covered by tests
uom_record.wamas_code
] = uom_record.unece_code
MAPPING_UNITCODE_UBL_TO_WAMAS["unitCode"][

Check warning on line 100 in base_wamas_ubl/models/base_wamas_ubl.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/models/base_wamas_ubl.py#L100

Added line #L100 was not covered by tests
uom_record.unece_code
] = uom_record.wamas_code
return {

Check warning on line 103 in base_wamas_ubl/models/base_wamas_ubl.py

View check run for this annotation

Codecov / codecov/patch

base_wamas_ubl/models/base_wamas_ubl.py#L103

Added line #L103 was not covered by tests
"MAPPING_UNITCODE_WAMAS_TO_UBL": MAPPING_UNITCODE_WAMAS_TO_UBL,
"MAPPING_UNITCODE_UBL_TO_WAMAS": MAPPING_UNITCODE_UBL_TO_WAMAS,
}
13 changes: 13 additions & 0 deletions base_wamas_ubl/models/uom_uom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# 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",
)
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</cac:GrossWeightMeasure>
</cac:Shipment>
<cbc:ID>1</cbc:ID>
<cbc:DeliveredQuantity unitCode="X4B">7.0</cbc:DeliveredQuantity>
<cbc:DeliveredQuantity unitCode="X4A">7.0</cbc:DeliveredQuantity>
<cac:OrderLineReference>
<cbc:LineID>000101</cbc:LineID>
</cac:OrderLineReference>
Expand All @@ -106,7 +106,7 @@
</cac:GrossWeightMeasure>
</cac:Shipment>
<cbc:ID>2</cbc:ID>
<cbc:DeliveredQuantity unitCode="X4B">8.0</cbc:DeliveredQuantity>
<cbc:DeliveredQuantity unitCode="X4A">8.0</cbc:DeliveredQuantity>
<cac:OrderLineReference>
<cbc:LineID>000101</cbc:LineID>
</cac:OrderLineReference>
Expand Down
27 changes: 27 additions & 0 deletions base_wamas_ubl/tests/test_base_wamas_ubl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from odoo.addons.account.tests.common import AccountTestInvoicingCommon

from ..lib.wamas.structure import MappingDict


class TestBaseWamas(TransactionCase):
@classmethod
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi reviewer, the default of 'TETRA' is X4B, so i just want to change to X4A to test

To make sure that it override the default no

"": 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")
Expand Down
12 changes: 12 additions & 0 deletions base_wamas_ubl/views/uom_category.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="product_uom_categ_form_view" model="ir.ui.view">
<field name="model">uom.category</field>
<field name="inherit_id" ref="uom_unece.product_uom_categ_form_view" />
<field name="arch" type="xml">
<field name="unece_code" position="after">
<field name="wamas_code" optional="show" />
</field>
</field>
</record>
</odoo>
21 changes: 21 additions & 0 deletions base_wamas_ubl/views/uom_uom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="product_uom_form_view" model="ir.ui.view">
<field name="model">uom.uom</field>
<field name="inherit_id" ref="uom_unece.product_uom_form_view" />
<field name="arch" type="xml">
<field name="unece_code" position="after">
<field name="wamas_code" />
</field>
</field>
</record>
<record id="product_uom_tree_view" model="ir.ui.view">
<field name="model">uom.uom</field>
<field name="inherit_id" ref="uom_unece.product_uom_tree_view" />
<field name="arch" type="xml">
<field name="unece_code" position="after">
<field name="wamas_code" optional="show" />
</field>
</field>
</record>
</odoo>
Loading