From 4be094ae9a997d9e27299d76d53fde67b48b51a1 Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Mon, 24 Jul 2023 13:19:46 +0000 Subject: [PATCH] [FIX] product: remove the package when we unlink it from product MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) Signed-off-by: William Henrotin (whe) Signed-off-by: Tiffany Chang (tic) --- addons/product/models/product_packaging.py | 2 +- addons/product/tests/test_barcode.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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'