From e43c424fd910fc4bab4452827d643366b5244c9d Mon Sep 17 00:00:00 2001 From: William Henrotin Date: Fri, 16 Apr 2021 09:30:23 +0000 Subject: [PATCH] [FIX] mrp: split call to write on stock move You can, in a production order, change the quantity done of a stock move raw and change its initial demand ('To Consume' field) in the same transaction. This can lead to some issue as changing the quantity done will update the stock move line and changing the initial demand will unreserve the stock move thus impacting the stock move lines too. This commit will split the values to update of a stock in move in case the two fields have to be updated. First the stock move lines, then the initial demand. This commit also remove the default_product_uom_qty in the move_raw_ids fields. This ensure the onchanges do not create/edit any stock move lines with some reserved quantity. opw : 2451298 closes odoo/odoo#70975 X-original-commit: e8c1f6b1a3f58b68181bf4d2599bd37efb83b5c7 Signed-off-by: agr-odoo Signed-off-by: William Henrotin --- addons/mrp/models/stock_move.py | 8 +++++++ addons/mrp/tests/test_order.py | 28 +++++++++++++++++++++++ addons/mrp/views/mrp_production_views.xml | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/addons/mrp/models/stock_move.py b/addons/mrp/models/stock_move.py index c49ce26d253..f9ac90dda8f 100644 --- a/addons/mrp/models/stock_move.py +++ b/addons/mrp/models/stock_move.py @@ -194,6 +194,14 @@ class StockMove(models.Model): defaults['additional'] = True return defaults + def write(self, vals): + if 'product_uom_qty' in vals and 'move_line_ids' in vals: + # first update lines then product_uom_qty as the later will unreserve + # so possibly unlink lines + move_line_vals = vals.pop('move_line_ids') + super().write({'move_line_ids': move_line_vals}) + return super().write(vals) + def _action_assign(self): res = super(StockMove, self)._action_assign() for move in self.filtered(lambda x: x.production_id or x.raw_material_production_id): diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index a96ccf8eed8..735608d91c6 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -377,6 +377,34 @@ class TestMrpOrder(TestMrpCommon): production.workorder_ids[0].button_start() self.assertEqual(production.workorder_ids.qty_producing, 5, "Wrong quantity is suggested to produce.") + def test_update_quantity_5(self): + bom = self.env['mrp.bom'].create({ + 'product_id': self.product_6.id, + 'product_tmpl_id': self.product_6.product_tmpl_id.id, + 'product_qty': 1, + 'product_uom_id': self.product_6.uom_id.id, + 'type': 'normal', + 'bom_line_ids': [ + (0, 0, {'product_id': self.product_2.id, 'product_qty': 3}), + ], + }) + production_form = Form(self.env['mrp.production']) + production_form.product_id = self.product_6 + production_form.bom_id = bom + production_form.product_qty = 1 + production_form.product_uom_id = self.product_6.uom_id + production = production_form.save() + production.action_confirm() + production.action_assign() + production_form = Form(production) + # change the quantity producing and the initial demand + # in the same transaction + production_form.qty_producing = 10 + with production_form.move_raw_ids.edit(0) as move: + move.product_uom_qty = 2 + production = production_form.save() + production.button_mark_done() + def test_update_plan_date(self): """Editing the scheduled date after planning the MO should unplan the MO, and adjust the date on the stock moves""" planned_date = datetime(2023, 5, 15, 9, 0) diff --git a/addons/mrp/views/mrp_production_views.xml b/addons/mrp/views/mrp_production_views.xml index c375e274274..f3dd2c06e47 100644 --- a/addons/mrp/views/mrp_production_views.xml +++ b/addons/mrp/views/mrp_production_views.xml @@ -215,7 +215,7 @@