From 5b1bc2e1da165dd29b97656fa9effd998e9d5a40 Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Mon, 3 Jul 2023 12:43:41 +0000 Subject: [PATCH] [FIX] mrp: raise validation error only if total cost share exceeds 100 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce the bug: - Enable the "by-product" option in the mrp settings. - Create a product A with the following Bill of Materials: - Component: 1 unit of Product B. - By-product: 2 units of Product C. - Create a Manufacturing Order to produce one unit of Product A. - Confirm the MO. - Set the quantity of Product A to 1. - Set the quantity of the by-product "C" to 1 instead of 2. - Attempt to validate the MO. Problem: When validating the MO, we check if the sum of the cost share in the “move_byproduct” does not exceed 100. However, we do not exclude the cancelled move, leading to incorrect calculations. opw-3375388 closes odoo/odoo#127178 X-original-commit: 7fb1c9ff07f5ec05fc313283a497137327d9ce3a Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- addons/mrp/models/mrp_production.py | 2 +- addons/mrp/tests/test_byproduct.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 99b25f8f341..db64af44d15 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -794,7 +794,7 @@ class MrpProduction(models.Model): for order in self: if any(move.cost_share < 0 for move in order.move_byproduct_ids): raise ValidationError(_("By-products cost shares must be positive.")) - if sum(order.move_byproduct_ids.mapped('cost_share')) > 100: + if sum(order.move_byproduct_ids.filtered(lambda m: m.state != 'cancel').mapped('cost_share')) > 100: raise ValidationError(_("The total cost share for a manufacturing order's by-products cannot exceed 100.")) def write(self, vals): diff --git a/addons/mrp/tests/test_byproduct.py b/addons/mrp/tests/test_byproduct.py index c4a2902ec7f..38856ca0ac1 100644 --- a/addons/mrp/tests/test_byproduct.py +++ b/addons/mrp/tests/test_byproduct.py @@ -386,3 +386,24 @@ class TestMrpByProduct(common.TransactionCase): byproduct_1.cost_share = 60 byproduct_2.cost_share = 70 mo.write({'move_byproduct_ids': [(6, 0, [byproduct_1.id, byproduct_2.id])]}) + + def test_check_byproducts_cost_share_02(self): + """ + Test that byproducts with total cost_share < 100% with a cancelled moves will don't throw a ValidationError + """ + self.bom_byproduct.byproduct_ids[0].cost_share = 70 + self.bom_byproduct.byproduct_ids[0].product_qty = 2 + mo = self.env["mrp.production"].create({ + 'product_id': self.product_a.id, + 'product_qty': 1.0, + 'bom_id': self.bom_byproduct.id, + }) + mo.action_confirm() + self.assertEqual(mo.state, 'confirmed') + mo_form = Form(mo) + mo_form.qty_producing = 1 + mo = mo_form.save() + self.assertEqual(mo.state, 'to_close') + mo.move_byproduct_ids[0].quantity_done = 1 + mo.button_mark_done() + self.assertEqual(mo.state, 'done')