Skip to content

Commit eb38fc6

Browse files
author
duongtq
committed
[IMP] base_wamas_ubl: rebuild mapping dynamically and override default mapping
1 parent 644353c commit eb38fc6

10 files changed

+150
-19
lines changed

base_wamas_ubl/__manifest__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
"website": "https://github.com/OCA/edi",
1212
"license": "AGPL-3",
1313
"author": "Camptocamp,Odoo Community Association (OCA)",
14-
"depends": ["base_edi", "base_ubl"],
14+
"depends": ["base_edi", "base_ubl", "uom_unece"],
1515
"external_dependencies": {
1616
"python": ["xmltodict", "dotty-dict", "pytz"],
1717
},
1818
"data": [
1919
"security/ir.model.access.csv",
2020
"wizard/wamas_ubl_wiz_check.xml",
2121
"wizard/wamas_ubl_wiz_simulate.xml",
22+
"views/uom_uom.xml",
23+
"views/uom_category.xml",
2224
"views/wamas_menu.xml",
2325
],
2426
}

base_wamas_ubl/lib/wamas/ubl2wamas.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from utils import file_open, generate_wamas_line
1919

2020

21-
def ubl2list(infile, telegram_type): # noqa: C901
21+
def ubl2list(infile, telegram_type, extra_data=False): # noqa: C901
2222
res = []
2323

2424
my_dict = Dotty(xmltodict.parse(infile))
@@ -54,15 +54,16 @@ def ubl2list(infile, telegram_type): # noqa: C901
5454
line_idx=line_idx,
5555
len_loop=len_loop,
5656
idx_loop=idx_loop,
57+
extra_data=extra_data,
5758
)
5859
if line:
5960
res.append(line)
6061

6162
return res
6263

6364

64-
def ubl2wamas(infile, telegram_type, verbose=False):
65-
lst_of_str_wamas = ubl2list(infile, telegram_type)
65+
def ubl2wamas(infile, telegram_type, extra_data=False, verbose=False):
66+
lst_of_str_wamas = ubl2list(infile, telegram_type, extra_data=extra_data)
6667
wamas = "\n".join(lst_of_str_wamas)
6768
if verbose:
6869
_logger.debug(wamas)

base_wamas_ubl/lib/wamas/utils.py

+29-10
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,16 @@ def get_date_from_field(*args):
211211
return res
212212

213213

214-
def convert_unit_code(key, val):
214+
def convert_unit_code(key, val, unit_mapping=None):
215215
if key in LST_FIELD_UNIT_CODE:
216-
return MAPPING_UNITCODE_UBL_TO_WAMAS["unitCode"].get(val, val)
216+
MAPPING = (
217+
unit_mapping
218+
and unit_mapping.get(
219+
"MAPPING_UNITCODE_UBL_TO_WAMAS", MAPPING_UNITCODE_UBL_TO_WAMAS
220+
)
221+
or MAPPING_UNITCODE_UBL_TO_WAMAS
222+
)
223+
return MAPPING["unitCode"].get(val, val)
217224
return val
218225

219226

@@ -327,7 +334,8 @@ def generate_wamas_line(dict_item, grammar, **kwargs): # noqa: C901
327334

328335
val = globals()[df_func](*args)
329336

330-
val = convert_unit_code(_key, val)
337+
extra_data = kwargs.get("extra_data", None)
338+
val = convert_unit_code(_key, val, unit_mapping=extra_data)
331339
if kwargs.get("check_to_set_value_to_string", False):
332340
# Ignore convert string of float/int/date/datetime type
333341
# to move entire value when convert wamas2wamas
@@ -352,16 +360,20 @@ def generate_wamas_line(dict_item, grammar, **kwargs): # noqa: C901
352360
return res
353361

354362

355-
def generate_wamas_lines(dict_input, telegram_type, line_idx, wamas_lines):
363+
def generate_wamas_lines(
364+
dict_input, telegram_type, line_idx, wamas_lines, extra_data=False
365+
):
356366
line_idx += 1
357367
grammar = DICT_WAMAS_GRAMMAR[telegram_type.lower()]
358-
line = generate_wamas_line(dict_input, grammar, line_idx=line_idx)
368+
line = generate_wamas_line(
369+
dict_input, grammar, line_idx=line_idx, extra_data=extra_data
370+
)
359371
if line:
360372
wamas_lines.append(line)
361373
return line_idx, wamas_lines
362374

363375

364-
def dict2wamas(dict_input, telegram_type):
376+
def dict2wamas(dict_input, telegram_type, extra_data=False):
365377
wamas_lines = []
366378
lst_telegram_type = telegram_type.split(",")
367379

@@ -375,16 +387,16 @@ def dict2wamas(dict_input, telegram_type):
375387
# 1 line for `KstAus_LagIdKom = kMEZ`
376388
dict_input["picking_zone"] = "kMEZ"
377389
line_idx, wamas_lines = generate_wamas_lines(
378-
dict_input, telegram_type, line_idx, wamas_lines
390+
dict_input, telegram_type, line_idx, wamas_lines, extra_data=extra_data
379391
)
380392
# 1 line for `KstAus_LagIdKom = kPAR`
381393
dict_input["picking_zone"] = "kPAR"
382394
line_idx, wamas_lines = generate_wamas_lines(
383-
dict_input, telegram_type, line_idx, wamas_lines
395+
dict_input, telegram_type, line_idx, wamas_lines, extra_data=extra_data
384396
)
385397
else:
386398
line_idx, wamas_lines = generate_wamas_lines(
387-
dict_input, telegram_type, line_idx, wamas_lines
399+
dict_input, telegram_type, line_idx, wamas_lines, extra_data=extra_data
388400
)
389401
return "\n".join(wamas_lines).encode("iso-8859-1")
390402

@@ -510,13 +522,20 @@ def wamas2dict(
510522

511523
def dict2ubl(template, data, verbose=False, extra_data=False):
512524
t = miniqweb.QWebXml(template)
525+
MAPPING = (
526+
extra_data
527+
and extra_data.get(
528+
"MAPPING_UNITCODE_WAMAS_TO_UBL", MAPPING_UNITCODE_WAMAS_TO_UBL
529+
)
530+
or MAPPING_UNITCODE_WAMAS_TO_UBL
531+
)
513532
# Convert dict to object to use dotted notation in template
514533
globals_dict = {
515534
"record": obj(data),
516535
"get_date": get_date,
517536
"get_time": get_time,
518537
"get_current_date": get_current_date,
519-
"MAPPING": MAPPING_UNITCODE_WAMAS_TO_UBL,
538+
"MAPPING": MAPPING,
520539
"extra_data": extra_data,
521540
}
522541
xml = t.render(globals_dict)

base_wamas_ubl/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import base_wamas_ubl
2+
from . import uom_uom

base_wamas_ubl/models/base_wamas_ubl.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from odoo import _, api, models
66

7+
from ..lib.wamas.structure import MappingDict
78
from ..lib.wamas.ubl2wamas import ubl2wamas
89
from ..lib.wamas.utils import (
910
detect_wamas_type,
@@ -25,19 +26,24 @@ def wamas2dict(self, str_file):
2526

2627
@api.model
2728
def dict2ubl(self, template, data):
28-
return dict2ubl(template, data)
29+
extra_data = self._build_unitcode_mapping()
30+
return dict2ubl(template, data, extra_data=extra_data)
2931

3032
@api.model
3133
def wamas2ubl(self, str_file, extra_data=False):
34+
extra_data = extra_data if extra_data else {}
35+
extra_data.update(self._build_unitcode_mapping())
3236
return wamas2ubl(str_file, extra_data=extra_data)
3337

3438
@api.model
3539
def ubl2wamas(self, str_file, telegram_type):
36-
return ubl2wamas(str_file, telegram_type)
40+
extra_data = self._build_unitcode_mapping()
41+
return ubl2wamas(str_file, telegram_type, extra_data=extra_data)
3742

3843
@api.model
3944
def dict2wamas(self, dict_input, telegram_type):
40-
return dict2wamas(dict_input, telegram_type)
45+
extra_data = self._build_unitcode_mapping()
46+
return dict2wamas(dict_input, telegram_type, extra_data=extra_data)
4147

4248
@api.model
4349
def get_wamas_type(self, str_file):
@@ -69,3 +75,32 @@ def get_supported_telegram(self):
6975
@api.model
7076
def get_supported_telegram_w2w(self):
7177
return get_supported_telegram_w2w()
78+
79+
@api.model
80+
def _build_unitcode_mapping(self):
81+
uom_records = self.env["uom.uom"].search([])
82+
uom_records = uom_records.filtered(
83+
lambda rec: rec.wamas_code and rec.unece_code
84+
)
85+
if not uom_records:
86+
return {}
87+
88+
MAPPING_UNITCODE_WAMAS_TO_UBL = {
89+
"unitCode": MappingDict(
90+
{
91+
"": False, # undefined,
92+
}
93+
)
94+
}
95+
MAPPING_UNITCODE_UBL_TO_WAMAS = {"unitCode": MappingDict()}
96+
for uom_record in uom_records:
97+
MAPPING_UNITCODE_WAMAS_TO_UBL["unitCode"][
98+
uom_record.wamas_code
99+
] = uom_record.unece_code
100+
MAPPING_UNITCODE_UBL_TO_WAMAS["unitCode"][
101+
uom_record.unece_code
102+
] = uom_record.wamas_code
103+
return {
104+
"MAPPING_UNITCODE_WAMAS_TO_UBL": MAPPING_UNITCODE_WAMAS_TO_UBL,
105+
"MAPPING_UNITCODE_UBL_TO_WAMAS": MAPPING_UNITCODE_UBL_TO_WAMAS,
106+
}

base_wamas_ubl/models/uom_uom.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
2+
# Copyright 2024 Camptocamp SA
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4+
5+
from odoo import fields, models
6+
7+
8+
class UomUom(models.Model):
9+
_inherit = "uom.uom"
10+
11+
wamas_code = fields.Char(
12+
string="WAMAS Code",
13+
)

base_wamas_ubl/tests/samples/WAMAS2UBL-SAMPLE_AUSKQ_WATEKQ_WATEPQ-DESPATCH_ADVICE.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
</cac:GrossWeightMeasure>
8282
</cac:Shipment>
8383
<cbc:ID>1</cbc:ID>
84-
<cbc:DeliveredQuantity unitCode="X4B">7.0</cbc:DeliveredQuantity>
84+
<cbc:DeliveredQuantity unitCode="X4A">7.0</cbc:DeliveredQuantity>
8585
<cac:OrderLineReference>
8686
<cbc:LineID>000101</cbc:LineID>
8787
</cac:OrderLineReference>
@@ -106,7 +106,7 @@
106106
</cac:GrossWeightMeasure>
107107
</cac:Shipment>
108108
<cbc:ID>2</cbc:ID>
109-
<cbc:DeliveredQuantity unitCode="X4B">8.0</cbc:DeliveredQuantity>
109+
<cbc:DeliveredQuantity unitCode="X4A">8.0</cbc:DeliveredQuantity>
110110
<cac:OrderLineReference>
111111
<cbc:LineID>000101</cbc:LineID>
112112
</cac:OrderLineReference>

base_wamas_ubl/tests/test_base_wamas_ubl.py

+27
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

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

15+
from ..lib.wamas.structure import MappingDict
16+
1517

1618
class TestBaseWamas(TransactionCase):
1719
@classmethod
@@ -75,6 +77,31 @@ def setUpClass(cls):
7577
and cls.partner_2.child_ids[0].email
7678
or "",
7779
},
80+
"MAPPING_UNITCODE_WAMAS_TO_UBL": {
81+
"unitCode": MappingDict(
82+
{
83+
"BOT": "XBQ", # plastic bottle
84+
"BOUT": "C62", # Unit
85+
"BOITE": "XBX", # box
86+
"LITRE": "LTR", # litre
87+
"PET": "XBO", # glass bottle
88+
"TETRA": "X4A", # tetra pack, changed 'X4B' to 'X4A'for testing
89+
"": False, # undefined,
90+
}
91+
)
92+
},
93+
"MAPPING_UNITCODE_UBL_TO_WAMAS": {
94+
"unitCode": MappingDict(
95+
{
96+
"XBQ": "BOT", # plastic bottle
97+
"C62": "BOUT", # Unit
98+
"XBX": "BOITE", # box
99+
"LTR": "LITRE", # litre
100+
"XBO": "PET", # glass bottle
101+
"X4A": "TETRA", # tetra pack, changed 'X4B' to 'X4A'for testing
102+
}
103+
)
104+
},
78105
}
79106

80107
@freeze_time("2023-05-01")

base_wamas_ubl/views/uom_category.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="product_uom_categ_form_view" model="ir.ui.view">
4+
<field name="model">uom.category</field>
5+
<field name="inherit_id" ref="uom_unece.product_uom_categ_form_view" />
6+
<field name="arch" type="xml">
7+
<field name="unece_code" position="after">
8+
<field name="wamas_code" optional="show" />
9+
</field>
10+
</field>
11+
</record>
12+
</odoo>

base_wamas_ubl/views/uom_uom.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="product_uom_form_view" model="ir.ui.view">
4+
<field name="model">uom.uom</field>
5+
<field name="inherit_id" ref="uom_unece.product_uom_form_view" />
6+
<field name="arch" type="xml">
7+
<field name="unece_code" position="after">
8+
<field name="wamas_code" />
9+
</field>
10+
</field>
11+
</record>
12+
<record id="product_uom_tree_view" model="ir.ui.view">
13+
<field name="model">uom.uom</field>
14+
<field name="inherit_id" ref="uom_unece.product_uom_tree_view" />
15+
<field name="arch" type="xml">
16+
<field name="unece_code" position="after">
17+
<field name="wamas_code" optional="show" />
18+
</field>
19+
</field>
20+
</record>
21+
</odoo>

0 commit comments

Comments
 (0)