[FIX] product: remove the package when we unlink it from product

Steps to reproduce the bug:
- Enable “Product packing” in the inventory settings
- Create a storable product “P1”
    - Add a package with barcode “123”
    - save the changes
    - Delete the package
    - Try to set the same barcode “123” for the product

Problem:
A validation error is triggered: "A packaging already uses the barcode"

Solution:
When we delete the package from the product, we have to delete
completely the “product.packaging" record.

opw-3378288

closes odoo/odoo#131825

X-original-commit: d01d8a9cedec2d1c17469538103340fc934470f1
Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com>
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
Signed-off-by: Tiffany Chang (tic) <tic@odoo.com>
This commit is contained in:
Touati Djamel (otd)
2023-08-14 13:42:13 +02:00
parent 276dbdad5a
commit 4be094ae9a
2 changed files with 17 additions and 1 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ class ProductPackaging(models.Model):
name = fields.Char('Product Packaging', required=True)
sequence = fields.Integer('Sequence', default=1, help="The first in the sequence is the default one.")
product_id = fields.Many2one('product.product', string='Product', check_company=True, required=True)
product_id = fields.Many2one('product.product', string='Product', check_company=True, required=True, ondelete="cascade")
qty = fields.Float('Contained Quantity', default=1, digits='Product Unit of Measure', help="Quantity of products contained in the packaging.")
barcode = fields.Char('Barcode', copy=False, help="Barcode used for packaging identification. Scan this packaging barcode from a transfer in the Barcode app to move all the contained units")
product_uom_id = fields.Many2one('uom.uom', related='product_id.uom_id', readonly=True)
+16
View File
@@ -57,3 +57,19 @@ class TestProductBarcode(TransactionCase):
assert 'Barcode "3" already assigned to product(s): BC3, BC4' in exc.args[0]
assert 'Barcode "4" already assigned to product(s): BC5, BC6' in exc.args[0]
assert 'Barcode "1" already assigned to product(s): BC1' in exc.args[0]
def test_delete_package_and_use_its_barcode_in_product(self):
""" Test that the barcode of the package can be used when the package is removed from the product."""
product = self.env['product.product'].create({
'name': 'product',
'packaging_ids': [(0, 0, {
'name': 'packing',
'barcode': '1234',
})]
})
package = product.packaging_ids
self.assertTrue(package.exists())
self.assertEqual(package.barcode, '1234')
product.packaging_ids = False
self.assertFalse(package.exists())
product.barcode = '1234'