diff options
| author | William Henrotin <whe@odoo.com> | 2020-04-01 11:38:17 +0000 |
|---|---|---|
| committer | Arnold Moyaux <arm@odoo.com> | 2020-06-18 11:35:38 +0000 |
| commit | f979e3e093409fb043ed83b5d6764c272096c923 (patch) | |
| tree | 36e2d83c9a929cad83e729179c27b118d4de3770 /addons | |
| parent | e617aa91c536093f481da28b0cf061a6abe55b2a (diff) | |
[FIX] product: editing uom on product.product
Updating the uom_id on a product_product will eventually change also the
uom_po_id to have them belonging to the same category. Saving the update
call write() on product with a dict of two values (uom_id and uom_po_id)
As product.product is inherited from product.template, the write() on
the corresponding product_template is called but in this case 2 times
for each values. The first time, only uom_id is updated. The _check_uom
constraint is triggered and obviously failed. The uom_po_id on the
template is not yet updated.
This commit makes the sync in the beginning of write() of product.template.
Which means before calling the constraint.
This commit also makes the onchanges on uom_id acts the same between
product.product and product.template.
Task : 2197818
closes odoo/odoo#48714
Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/product/models/product.py | 7 | ||||
| -rw-r--r-- | addons/product/models/product_template.py | 10 | ||||
| -rw-r--r-- | addons/product/tests/test_variants.py | 23 |
3 files changed, 38 insertions, 2 deletions
diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 8677dd17914..adc4a69e4f9 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -313,7 +313,12 @@ class ProductProduct(models.Model): '&', ('product_id', '=', product.id), ('applied_on', '=', '0_product_variant')] product.pricelist_item_count = self.env['product.pricelist.item'].search_count(domain) - @api.onchange('uom_id', 'uom_po_id') + @api.onchange('uom_id') + def _onchange_uom_id(self): + if self.uom_id: + self.uom_po_id = self.uom_id.id + + @api.onchange('uom_po_id') def _onchange_uom(self): if self.uom_id and self.uom_po_id and self.uom_id.category_id != self.uom_po_id.category_id: self.uom_po_id = self.uom_id diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 8da67700c14..bf28f6e5acb 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -379,6 +379,11 @@ class ProductTemplate(models.Model): if self.uom_id: self.uom_po_id = self.uom_id.id + @api.onchange('uom_po_id') + def _onchange_uom(self): + if self.uom_id and self.uom_po_id and self.uom_id.category_id != self.uom_po_id.category_id: + self.uom_po_id = self.uom_id + @api.onchange('type') def _onchange_type(self): # Do nothing but needed for inheritance @@ -413,6 +418,11 @@ class ProductTemplate(models.Model): return templates def write(self, vals): + if 'uom_id' in vals or 'uom_po_id' in vals: + uom_id = self.env['uom.uom'].browse(vals.get('uom_id')) or self.uom_id + uom_po_id = self.env['uom.uom'].browse(vals.get('uom_po_id')) or self.uom_po_id + if uom_id and uom_po_id and uom_id.category_id != uom_po_id.category_id: + vals['uom_po_id'] = uom_id.id res = super(ProductTemplate, self).write(vals) if 'attribute_line_ids' in vals or vals.get('active'): self._create_variant_ids() diff --git a/addons/product/tests/test_variants.py b/addons/product/tests/test_variants.py index d1ee1107a91..b5a09d9f736 100644 --- a/addons/product/tests/test_variants.py +++ b/addons/product/tests/test_variants.py @@ -8,7 +8,7 @@ from PIL import Image from . import common from odoo.exceptions import UserError -from odoo.tests.common import TransactionCase +from odoo.tests.common import TransactionCase, Form class TestVariantsSearch(TransactionCase): @@ -922,6 +922,27 @@ class TestVariantsArchive(common.TestProductCommon): name_searched = self.env['product.template'].name_search(name='cima') self.assertIn(template.id, [ng[0] for ng in name_searched]) + def test_uom_update_variant(self): + """ Changing the uom on the template do not behave the same + as changing on the product product.""" + units = self.env.ref('uom.product_uom_unit') + cm = self.env.ref('uom.product_uom_cm') + template = self.env['product.template'].create({ + 'name': 'kardon' + }) + + template_form = Form(template) + template_form.uom_id = cm + self.assertEqual(template_form.uom_po_id, cm) + template = template_form.save() + + variant_form = Form(template.product_variant_ids) + variant_form.uom_id = units + self.assertEqual(variant_form.uom_po_id, units) + variant = variant_form.save() + self.assertEqual(variant.uom_po_id, units) + self.assertEqual(template.uom_po_id, units) + def _update_color_vars(self, ptal): self.ptal_color = ptal self.assertEqual(self.ptal_color.attribute_id, self.pa_color) |
