Skip to content

Commit ba56743

Browse files
migration-bot-adhocjcadhoc
authored andcommitted
[MIG] product_uoms: Migration to 19.0
Part-of: #804 Signed-off-by: Juan Carreras <jc@adhoc.com.ar>
1 parent 41d1b8c commit ba56743

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

product_uoms/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
##############################################################################
2020
{
2121
"name": "Product UOMS",
22-
"version": "18.0.1.0.0",
22+
"version": "19.0.1.0.0",
2323
"category": "base.module_category_knowledge_management",
2424
"author": "ADHOC SA",
2525
"website": "www.adhoc.com.ar",
@@ -36,7 +36,7 @@
3636
"views/product_template_views.xml",
3737
"security/ir.model.access.csv",
3838
],
39-
"installable": False,
39+
"installable": True,
4040
"auto_install": False,
4141
"application": False,
4242
}

product_uoms/demo/product_product_demo.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
<odoo noupdate="1">
33
<record id="product_on_cm_saleable_on_m_and_km" model="product.product">
44
<field name="name">Product On Centimeter Saleable on Meter and Km</field>
5-
<field name="categ_id" ref="product.product_category_4"/>
65
<field name="uom_id" ref="uom.product_uom_cm"/>
7-
<field name="uom_po_id" ref="uom.product_uom_cm"/>
86
<field name="standard_price">1</field>
97
<field name="list_price">2</field>
108
<field name="type">consu</field>

product_uoms/models/product_product.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def get_product_uoms(self, product_uom, use=False):
1515
We send product uom so it can be send from sale or purchase
1616
"""
1717
self.ensure_one()
18-
return product_uom + self.env["uom.uom"].search(
19-
[("category_id", "=", product_uom.category_id.id), ("id", "!=", product_uom.id)]
20-
)
18+
# Get the root reference UoM by traversing up the parent chain
19+
root_uom = product_uom
20+
while root_uom.relative_uom_id:
21+
root_uom = root_uom.relative_uom_id
22+
23+
# Find all UoMs that have the same root reference
24+
all_uoms = self.env["uom.uom"].search([])
25+
compatible_uoms = all_uoms.filtered(lambda u: u._has_common_reference(product_uom) and u.id != product_uom.id)
26+
27+
return product_uom + compatible_uoms

product_uoms/models/product_template.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
class ProductTemplate(models.Model):
1010
_inherit = "product.template"
1111

12-
uom_category_id = fields.Many2one(
13-
related="uom_id.category_id",
14-
)
1512
uom_ids = fields.One2many(
1613
"product.uoms",
1714
"product_tmpl_id",
@@ -25,6 +22,13 @@ class ProductTemplate(models.Model):
2522
@api.constrains("uom_ids", "uom_id")
2623
def _check_uoms(self):
2724
for rec in self:
28-
uom_categories = rec.uom_ids.mapped("uom_id.category_id")
29-
if len(uom_categories) > 1 or (uom_categories and uom_categories != rec.uom_id.category_id):
30-
raise ValidationError(_("UOMs Category must be of the same " "UOM Category as Product Unit of Measure"))
25+
# Use _has_common_reference() to check if UoMs are compatible
26+
product_uom = rec.uom_id
27+
for uom_line in rec.uom_ids:
28+
if not product_uom._has_common_reference(uom_line.uom_id):
29+
raise ValidationError(
30+
_(
31+
"UOMs must have the same reference unit as the Product Unit of Measure (%s)",
32+
product_uom.name,
33+
)
34+
)

product_uoms/models/product_uoms.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ProductUoms(models.Model):
2020
required=True,
2121
)
2222

23-
_sql_constraints = [
24-
("uom_uniq", "unique(product_tmpl_id, uom_id)", ("UOM must be unique per Product Template!")),
25-
]
23+
_uom_uniq = models.Constraint(
24+
"unique(product_tmpl_id, uom_id)",
25+
"UOM must be unique per Product Template!",
26+
)

product_uoms/views/product_template_views.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
<field name="inherit_id" ref="product.product_template_form_view"/>
88
<field name="arch" type="xml">
99
<field name="product_tooltip" position="after">
10-
<field name="uom_category_id" invisible="1"/>
1110
<field name="uom_ids">
1211
<list editable="bottom">
1312
<field name="sequence" widget="handle"/>
14-
<field name="uom_id" domain="[('category_id', '=', parent.uom_category_id)]" options="{'no_create': True, 'no_open': True}"/>
13+
<field name="uom_id" options="{'no_create': True, 'no_open': True}"/>
1514
</list>
1615
</field>
1716
</field>

0 commit comments

Comments
 (0)