Skip to content

Commit

Permalink
[IMP] l10n_eu_product_adr: distinct uom for limited quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Nov 16, 2021
1 parent 59433d8 commit 60a4ffb
Show file tree
Hide file tree
Showing 9 changed files with 4,108 additions and 2,944 deletions.
3 changes: 2 additions & 1 deletion l10n_eu_product_adr/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["product", "stock"],
"depends": ["stock"],
"development_status": "Beta",
"data": [
"data/uom_uom.xml",
"data/adr_class.xml",
"data/adr_label.xml",
"data/adr_packing_instruction.xml",
Expand Down
6,952 changes: 4,024 additions & 2,928 deletions l10n_eu_product_adr/data/adr_goods.xml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions l10n_eu_product_adr/data/uom_uom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="product_uom_mililiter" model="uom.uom">
<field name="name">ml</field>
<field name="category_id" ref="uom.product_uom_categ_vol" />
<field name="factor">1000</field>
<field name="uom_type">smaller</field>
</record>
</odoo>
8 changes: 6 additions & 2 deletions l10n_eu_product_adr/models/adr_goods.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class AdrGoods(models.Model):
"vehicles.)"
),
)
limited_quantity = fields.Char()
limited_quantity = fields.Float(digits="Product Unit of Measure")
limited_quantity_uom_id = fields.Many2one("uom.uom", string="Limited Quantity UoM")
packing_instruction_ids = fields.Many2many(
comodel_name="adr.packing.instruction",
string="Packing Instructions",
Expand Down Expand Up @@ -95,7 +96,10 @@ def name_get(self):
if rec.transport_category != "-":
affixes.append(_("cat:%s") % rec.transport_category)
if rec.limited_quantity:
affixes.append(_("qty:%s") % rec.limited_quantity)
affixes.append(
_("qty:%s %s")
% (rec.limited_quantity, rec.limited_quantity_uom_id.name)
)
if affixes:
name += " (%s)" % (", ".join(affixes))
res.append((rec.id, name))
Expand Down
7 changes: 6 additions & 1 deletion l10n_eu_product_adr/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class ProductTemplate(models.Model):
adr_label_ids = fields.Many2many(
"adr.label", related="adr_goods_id.label_ids", readonly=True
)
adr_limited_quantity = fields.Char(
adr_limited_quantity = fields.Float(
related="adr_goods_id.limited_quantity",
readonly=True,
)
adr_limited_quantity_uom_id = fields.Many2one(
related="adr_goods_id.limited_quantity_uom_id",
readonly=True,
)
adr_packing_instruction_ids = fields.Many2many(
"adr.packing.instruction",
Expand Down
55 changes: 45 additions & 10 deletions l10n_eu_product_adr/scripts/import_adr_multilang_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@
"9A",
]

uom_map = {
"g": "uom.product_uom_gram",
"kg": "uom.product_uom_kgm",
"l": "uom.product_uom_litre",
"ml": "l10n_eu_product_adr.product_uom_mililiter",
}

# Keytypes
single = []
by_qty = []
Expand Down Expand Up @@ -153,9 +160,29 @@ def parse_packing_instructions(row):
return res


def parse_limited_quantity(row):
def parse_limited_quantity(row, split=True):
"""
:param split: when set, parse into float and Odoo uom. Otherwise, return
text representation as is.
"""
value = row[index["limited_quantity"]] or ""
return (value if value != "0" else "").strip()
if not value or value == "0":
return "" if not split else (False, False)
value = value.strip()
if not split:
return value
match = re.match(r"([0-9]+\.?[0-9]?)\s+([a-zA-Z]+)", value)
if not match:
if "BP 251" in value: # known case
return False, False
else:
raise ValueError("Cannot parse limited quantity: %s (%s)" % (value, row))
quantity, uom_name = match.groups()
if uom_name.lower() not in uom_map:
raise ValueError(
"Unknown uom %s in limited quantity %s (%s)" % (uom_name, value, row)
)
return quantity, uom_map[uom_name.lower()]


def apply_description_quirk(row):
Expand All @@ -172,7 +199,7 @@ def get_xml_id(row):
if un_number in single:
parts = [un_number]
else:
qty = parse_limited_quantity(row).replace(" ", "")
qty = parse_limited_quantity(row, split=False).replace(" ", "")
if un_number in by_qty:
parts = [un_number, qty]
else:
Expand Down Expand Up @@ -226,10 +253,19 @@ def packing_instruction_ids(record, value, row):

@transformer
def limited_quantity(record, value, row):
value = parse_limited_quantity(row)
el = etree.SubElement(record, "field", attrib={"name": "limited_quantity"})
if value:
el.text = value
quantity, uom = parse_limited_quantity(row)
if quantity:
etree.SubElement(
record, "field", attrib={"name": "limited_quantity"}
).text = quantity
etree.SubElement(
record,
"field",
attrib={
"name": "limited_quantity_uom_id",
"ref": uom,
},
)


@transformer
Expand Down Expand Up @@ -285,8 +321,7 @@ def parse_transport_category(row):
if not match:
raise ValueError(
"Unknown value for transport code/tunnel restriction code: "
"%s" % value,
row,
"%s (%s)" % (value, row)
)
category = match.groups()[0].strip()
tunnel_restriction_code = match.groups()[1].strip()
Expand Down Expand Up @@ -410,7 +445,7 @@ def populate_key_types(sheet):
if len(rows) == 1:
single.append(key)
else:
qtys = [parse_limited_quantity(row) for row in rows]
qtys = [parse_limited_quantity(row, split=False) for row in rows]
if len(set(qtys)) == len(qtys):
by_qty.append(key)
else:
Expand Down
12 changes: 10 additions & 2 deletions l10n_eu_product_adr/tests/test_adr_goods.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ def test_02_adr_goods(self):
self.assertEqual(
adr_goods.name_get(), [(adr_goods.id, "9999 test goods (cat:4)")]
)
adr_goods.limited_quantity = "5 ml"
adr_goods.write(
{
"limited_quantity": 5,
"limited_quantity_uom_id": self.env.ref(
"l10n_eu_product_adr.product_uom_mililiter"
).id,
}
)
self.assertEqual(
adr_goods.name_get(), [(adr_goods.id, "9999 test goods (cat:4, qty:5 ml)")]
adr_goods.name_get(),
[(adr_goods.id, "9999 test goods (cat:4, qty:5.0 ml)")],
)

def test_03_adr_label(self):
Expand Down
2 changes: 2 additions & 0 deletions l10n_eu_product_adr/views/adr_goods_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</group>
<group string="Transport">
<field name="limited_quantity" />
<field name="limited_quantity_uom_id" class="oe_inline" />
<field
name="packing_instruction_ids"
options="{'no_create': True}"
Expand Down Expand Up @@ -61,6 +62,7 @@
<field name="class_id" />
<field name="classification_code" />
<field name="limited_quantity" />
<field name="limited_quantity_uom_id" />
<field name="transport_category" />
<field name="tunnel_restriction_code" />
</tree>
Expand Down
4 changes: 4 additions & 0 deletions l10n_eu_product_adr/views/product_template_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
</group>
<group string="Transport">
<field name="adr_limited_quantity" />
<field
name="adr_limited_quantity_uom_id"
class="oe_inline"
/>
<field
name="adr_packing_instruction_ids"
widget="many2many_tags"
Expand Down

0 comments on commit 60a4ffb

Please sign in to comment.