[FIX] point_of_sale: fix combo choice reference on variants creation

-When product in combo choices are being archived because of variants creation, replace the archived reference with all newly created variants.
-Add test to reproduce this use case

task id: 3713861

closes odoo/odoo#156301

Signed-off-by: Vlad Stroia (vlst) <vlst@odoo.com>
This commit is contained in:
Manu Vaillant (manv)
2024-03-04 17:02:44 +00:00
parent a73f06af8a
commit aacd4bca5a
2 changed files with 64 additions and 0 deletions
+24
View File
@@ -45,6 +45,30 @@ class ProductTemplate(models.Model):
if combo_name:
raise UserError(_('You must first remove this product from the %s combo', combo_name))
def _create_variant_ids(self):
res = super()._create_variant_ids()
for template in self:
archived_product = self.env['product.product'].search([('product_tmpl_id', '=', template.id), ('active', '=', False)], limit=1)
if archived_product:
combo_choices_to_delete = self.env['pos.combo.line'].search([
('product_id', '=', archived_product.id)
])
if combo_choices_to_delete:
# Delete old combo line
combo_ids = combo_choices_to_delete.mapped('combo_id')
combo_choices_to_delete.unlink()
# Create new combo line (one for each new variant) in each combo
new_variants = template.product_variant_ids.filtered(lambda v: v.active)
self.env['pos.combo.line'].create([
{
'product_id': variant.id,
'combo_id': combo_id.id,
}
for variant in new_variants for combo_id in combo_ids
])
return res
class ProductProduct(models.Model):
_inherit = 'product.product'
@@ -45,3 +45,43 @@ class TestPointOfSale(TransactionCase):
self.assertEqual(new_config.pricelist_id, self.env['product.pricelist'],
"POS config incorrectly has pricelist %s" % new_config.pricelist_id.display_name)
def test_product_combo_variants(self):
# Create product and combo
product = self.env['product.product'].create({
'name': 'Test Product 1',
'list_price': 100,
'taxes_id': False,
'available_in_pos': True,
})
product_1_combo_line = self.env["pos.combo.line"].create({
"product_id": product.id,
})
pos_combo = self.env["pos.combo"].create(
{
"name": "Pos combo",
"combo_line_ids": [
(6, 0, [product_1_combo_line.id])
],
}
)
# Add attribute and values, simulating variant creation
size_attribute = self.env['product.attribute'].create({'name': 'Size'})
attribute_value_1 = self.env['product.attribute.value'].create({'name': 'Large', 'attribute_id': size_attribute.id})
attribute_value_2 = self.env['product.attribute.value'].create({'name': 'Small', 'attribute_id': size_attribute.id})
original_product_id = product.id
product_template = product.product_tmpl_id
product.product_tmpl_id.with_context(create_product_product=True).write({
'attribute_line_ids': [(0, 0, {
'attribute_id': size_attribute.id,
'value_ids': [(6, 0, [attribute_value_1.id, attribute_value_2.id])],
})],
})
# Check that original product should not be in combo anymore (replace by variants)
self.assertTrue(original_product_id not in pos_combo.combo_line_ids.mapped('product_id').ids, "Original product should not be in combo")
# Check that variants are in combo
variant_ids = product_template.product_variant_ids.ids
for variant_id in variant_ids:
self.assertIn(variant_id, pos_combo.combo_line_ids.mapped('product_id').ids, "Variant should be in combo")