From 90c9173370cb0fbb3cb752ab3de0c4c38607138c Mon Sep 17 00:00:00 2001 From: paso-odoo Date: Tue, 23 May 2023 09:18:48 +0000 Subject: [PATCH] [FIX] mrp: handle keyError when workorder(s) deleted If applied, this commit will solve the issue of keyError when we confirm the manufacturing order and work order (s) are deleted. Steps to produce: - Configure BOM with 'Operations'. - Set operations in the 'Consumed in Operation' field which is in 'Components'. - Create MO using that BOM. - Delete the work order(s) from MO. - Click on the 'Confirm' button. Fix this issue when the work order is not available for that operation, setting False in stock.move record(s). sentry - 4171798106 closes odoo/odoo#123033 X-original-commit: 36e02b91e88c5e014d132643b6ffb24a71342d6c Signed-off-by: Parth Solanki (paso) Signed-off-by: William Henrotin (whe) --- addons/mrp/models/mrp_production.py | 2 +- addons/mrp/tests/test_order.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 829198e5ab5..70d59ca46c1 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -1282,7 +1282,7 @@ class MrpProduction(models.Model): for move in (self.move_raw_ids | self.move_finished_ids): if move.operation_id: move.write({ - 'workorder_id': workorder_per_operation[move.operation_id].id + 'workorder_id': workorder_per_operation[move.operation_id].id if move.operation_id in workorder_per_operation else False }) else: bom = move.bom_line_id.bom_id if (move.bom_line_id and move.bom_line_id.bom_id in workorder_boms) else self.bom_id diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index bc869b614a0..e95d1f85e09 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -3274,3 +3274,14 @@ class TestMrpOrder(TestMrpCommon): # Only duration from the workcenter specific capacity setup/cleanup times since there is one defined for this product. self.assertEqual(production.workorder_ids[0].duration_expected, 15.0, "Capacity setup time (5) + capacity cleanup time (10)") + + def test_unlink_workorder_with_consumed_operations(self): + self.bom_3.bom_line_ids[0].operation_id = self.bom_3.operation_ids[0].id + self.bom_3.bom_line_ids[1].operation_id = self.bom_3.operation_ids[1].id + mo_form = Form(self.env['mrp.production']) + mo_form.bom_id = self.bom_3 + mo = mo_form.save() + mo.workorder_ids[1].unlink() + mo.action_confirm() + self.assertEqual(mo.state, 'confirmed') + self.assertEqual(len(mo.workorder_ids), 2)