From d9972ddec442d40a239d0e973d3fa29daf29de4d Mon Sep 17 00:00:00 2001 From: William Henrotin Date: Thu, 12 Jan 2023 15:42:40 +0000 Subject: [PATCH] [FIX] mrp: clear finished moves The finished move of a production order should be deleted if we change the product to avoid having draft stock move detached from any business documents closes odoo/odoo#118957 Task: 3172098 X-original-commit: 4478560d7769a8a795a94aead875e02d1394dd71 Signed-off-by: Adrien Widart --- addons/mrp/models/mrp_production.py | 2 ++ addons/mrp/tests/test_order.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 967df5bbf40..fa053312995 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -739,6 +739,8 @@ class MrpProduction(models.Model): Command.update(m.id, updated_values) for m in production.move_finished_ids ] continue + # delete to remove existing moves from database and clear to remove new records + production.move_finished_ids = [Command.delete(m) for m in production.move_finished_ids.ids] production.move_finished_ids = [Command.clear()] if production.product_id: production._create_update_move_finished() diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index 3bb2b8c4e6c..28bdd0b2d7c 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -3135,6 +3135,20 @@ class TestMrpOrder(TestMrpCommon): self.assertEqual(mo.move_raw_ids.quantity_done, 1.25) + def test_clear_finished_move(self): + """ Test that the finished moves created by the compute are correctly + erased after changing the finished product""" + mo_form = Form(self.env['mrp.production']) + mo_form.product_id = self.product_1 + mo = mo_form.save() + self.assertEqual(len(mo.move_finished_ids), 1) + mo.product_id = self.product_2 + self.assertEqual(len(mo.move_finished_ids), 1) + self.assertFalse(self.env['stock.move'].search([ + ('product_id', '=', self.product_1.id), + ('state', '=', 'draft'), + ])) + def test_compute_picking_type_id(self): """ Test that the operation type set on the bom is set in the manufacturing order