From bd2bbb27fe83ab082fe04374de0fb45c5750488f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Wed, 22 Jan 2020 08:17:09 +0000 Subject: [PATCH] [IMP] mrp: add unbuild button on MO In this commit: - On done MO's form, add an Unbuild button, it would open a wizard with the form view of an unbuild, and some pre-filled fields such as MO, product, BOM, quantity and total quantity finished which can be edited. - In the unbuild order, improve the on_change of mo_id: if the selected product is tracked by serial number, set quantity to 1 by default and readonly - Also clean the testcase of unbuild order. task-2144636 closes odoo/odoo#43719 Signed-off-by: Simon Lejeune (sle) Co-authored-by: Ankita Raval --- addons/mrp/models/mrp_production.py | 12 ++++++++++++ addons/mrp/models/mrp_unbuild.py | 6 +++++- addons/mrp/tests/test_unbuild.py | 18 ------------------ addons/mrp/views/mrp_production_views.xml | 1 + addons/mrp/views/mrp_unbuild_views.xml | 4 ++-- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index df6f3272b8d..70250d72e63 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -1143,3 +1143,15 @@ class MrpProduction(models.Model): return self.env.ref('mrp.exception_on_mo').render(values=values) self.env['stock.picking']._log_activity(_render_note_exception_quantity_mo, documents) + + def button_unbuild(self): + self.ensure_one() + return { + 'name': _('Unbuild'), + 'view_mode': 'form', + 'res_model': 'mrp.unbuild', + 'view_id': self.env.ref('mrp.mrp_unbuild_form_view').id, + 'type': 'ir.actions.act_window', + 'context': {'default_mo_id': self.id, 'create': False, 'edit': False}, + 'target': 'new', + } diff --git a/addons/mrp/models/mrp_unbuild.py b/addons/mrp/models/mrp_unbuild.py index dbeeeb84fe7..9d57f1b71b9 100644 --- a/addons/mrp/models/mrp_unbuild.py +++ b/addons/mrp/models/mrp_unbuild.py @@ -106,8 +106,12 @@ class MrpUnbuild(models.Model): def _onchange_mo_id(self): if self.mo_id: self.product_id = self.mo_id.product_id.id - self.product_qty = self.mo_id.product_qty + self.bom_id = self.mo_id.bom_id self.product_uom_id = self.mo_id.product_uom_id + if self.has_tracking == 'serial': + self.product_qty = 1 + else: + self.product_qty = self.mo_id.product_qty @api.onchange('product_id') def _onchange_product_id(self): diff --git a/addons/mrp/tests/test_unbuild.py b/addons/mrp/tests/test_unbuild.py index bf5c6e25f47..79aabcd0978 100644 --- a/addons/mrp/tests/test_unbuild.py +++ b/addons/mrp/tests/test_unbuild.py @@ -50,7 +50,6 @@ class TestUnbuild(TestMrpCommon): x.product_id = p_final x.bom_id = bom x.product_qty = 3 - x.product_uom_id = self.uom_unit x.save().action_unbuild() @@ -62,7 +61,6 @@ class TestUnbuild(TestMrpCommon): x.product_id = p_final x.bom_id = bom x.product_qty = 2 - x.product_uom_id = self.uom_unit x.save().action_unbuild() self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location), 0, 'You should have 0 finalproduct in stock') @@ -73,7 +71,6 @@ class TestUnbuild(TestMrpCommon): x.product_id = p_final x.bom_id = bom x.product_qty = 5 - x.product_uom_id = self.uom_unit x.save().action_unbuild() # Check quantity in stock after last unbuild. @@ -128,14 +125,12 @@ class TestUnbuild(TestMrpCommon): x.product_id = p_final x.bom_id = bom x.product_qty = 3 - x.product_uom_id = self.uom_unit unbuild_order = x.save() x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom x.product_qty = 3 - x.product_uom_id = self.uom_unit x.lot_id = lot x.save().action_unbuild() @@ -148,7 +143,6 @@ class TestUnbuild(TestMrpCommon): x.bom_id = bom x.product_qty = 2 x.lot_id = lot - x.product_uom_id = self.uom_unit x.save().action_unbuild() self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location, lot_id=lot), 0, 'You should have 0 finalproduct in stock') @@ -160,7 +154,6 @@ class TestUnbuild(TestMrpCommon): x.bom_id = bom x.product_qty = 5 x.lot_id = lot - x.product_uom_id = self.uom_unit x.save().action_unbuild() self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location, lot_id=lot, allow_negative=True), -5, 'You should have negative quantity for final product in stock') @@ -213,7 +206,6 @@ class TestUnbuild(TestMrpCommon): x.product_id = p_final x.bom_id = bom x.product_qty = 3 - x.product_uom_id = self.uom_unit unbuild_order = x.save() # This should fail since we do not provide the MO that we wanted to unbuild. (without MO we do not know which consumed lot we have to restore) @@ -232,7 +224,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 2 x.save().action_unbuild() @@ -244,7 +235,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 5 x.save().action_unbuild() @@ -307,14 +297,12 @@ class TestUnbuild(TestMrpCommon): with self.assertRaises(AssertionError): x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.product_qty = 3 x.save() with self.assertRaises(AssertionError): x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.product_qty = 3 x.save() @@ -323,7 +311,6 @@ class TestUnbuild(TestMrpCommon): with self.assertRaises(AssertionError): x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 3 x.save() @@ -333,7 +320,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 3 x.lot_id = lot_final @@ -346,7 +332,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 2 x.lot_id = lot_final @@ -359,7 +344,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 5 x.lot_id = lot_final @@ -417,7 +401,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = p_final x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 5 x.save().action_unbuild() @@ -580,7 +563,6 @@ class TestUnbuild(TestMrpCommon): x = Form(self.env['mrp.unbuild']) x.product_id = finshed_product x.bom_id = bom - x.product_uom_id = self.uom_unit x.mo_id = mo x.product_qty = 1 x.location_id = self.stock_location diff --git a/addons/mrp/views/mrp_production_views.xml b/addons/mrp/views/mrp_production_views.xml index 3c1766a5dd7..2aa12daf471 100644 --- a/addons/mrp/views/mrp_production_views.xml +++ b/addons/mrp/views/mrp_production_views.xml @@ -66,6 +66,7 @@