Skip to content

Commit

Permalink
[FIX] product: improve variant-template consistency in supplierinfo
Browse files Browse the repository at this point in the history
Versions
--------
15.0+

Steps
-----
1. Go to Purchase;
2. create a Vendor Pricelist;
3. select a Product, a Product Variant & a Unit Price;
4. empty the Product field & save;
5. create a RFQ;
6. add the Product Variant from the Vendor Pricelist.

Issue
-----
The product's price remains 0.

Cause
-----
The `seller_ids` field in `product.template` creates one-to-many
relation with `product.supplierinfo`'s `product_tmpl_id` field. While it
is possible to select a specific product variant and have the product
field empty, this renders the Vendor Pricelist unavailable for Purchase
Orders.

Solution
--------
Add a `_sanitize_vals` method to `product.supplierinfo` to be used on
create/write, which ensures that if there's a `product_id`, the record's
`product_tmpl_id` is consistent with it. This allows for record imports
to be usable & consistent when only a variant is specified.

Also backport a modified  `onchange` method added in
5537090. Originally it only reset
`product_id` if `product_tmpl_id` was changed to a different non-falsy
value. Modified, it also resets `product_id` if `product_tmpl_id` is
changed to a falsy value, as it would otherwise just re-add the removed
value on create/write after this commit.

opw-3664524

closes odoo#151210

X-original-commit: 5721223
Signed-off-by: Levi Siuzdak <sile@odoo.com>
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
  • Loading branch information
lvsz committed Jan 26, 2024
1 parent 4c4e26b commit 6c736a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
19 changes: 18 additions & 1 deletion addons/product/models/product_supplierinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def _compute_price_discounted(self):

@api.onchange('product_tmpl_id')
def _onchange_product_tmpl_id(self):
if self.product_id and self.product_tmpl_id and self.product_id not in self.product_tmpl_id.product_variant_ids:
"""Clear product variant if it no longer matches the product template."""
if self.product_id and self.product_id not in self.product_tmpl_id.product_variant_ids:
self.product_id = False

@api.model
Expand All @@ -82,3 +83,19 @@ def get_import_templates(self):
'label': _('Import Template for Vendor Pricelists'),
'template': '/product/static/xls/product_supplierinfo.xls'
}]

def _sanitize_vals(self, vals):
"""Sanitize vals to sync product variant & template on read/write."""
if 'product_id' in vals and 'product_tmpl_id' not in vals:
product = self.env['product.product'].browse(vals['product_id'])
vals['product_tmpl_id'] = product.product_tmpl_id.id

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
self._sanitize_vals(vals)
return super().create(vals_list)

def write(self, vals):
self._sanitize_vals(vals)
return super().write(vals)
11 changes: 11 additions & 0 deletions addons/product/tests/test_seller.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,14 @@ def test_40_seller_min_qty_precision(self):

# Assert: The set value is kept
self.assertEqual(supplier_info.min_qty, precise_value)

def test_50_seller_ids(self):
vendors = self.env['product.supplierinfo'].create([{
'partner_id': self.asustec.id,
'product_tmpl_id': self.product_consu.product_tmpl_id.id,
}, {
'partner_id': self.camptocamp.id,
'product_id': self.product_consu.id,
}])
self.assertEqual(vendors, self.product_consu.seller_ids,
"Sellers of a product should be listed in the product's seller_ids")

0 comments on commit 6c736a7

Please sign in to comment.