diff --git a/addons/product/models/product_packaging.py b/addons/product/models/product_packaging.py index a0f07c194bc..d62f1bd7f5b 100644 --- a/addons/product/models/product_packaging.py +++ b/addons/product/models/product_packaging.py @@ -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) diff --git a/addons/product/tests/test_barcode.py b/addons/product/tests/test_barcode.py index 74b5a41220b..866b4e51359 100644 --- a/addons/product/tests/test_barcode.py +++ b/addons/product/tests/test_barcode.py @@ -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'