From 1e2c03a47dfa33afedecfd803058ad838b943fb1 Mon Sep 17 00:00:00 2001 From: "Tiffany Chang (tic)" Date: Tue, 25 May 2021 15:12:32 +0000 Subject: [PATCH] [FIX] mrp: lock MOs by default Previous specification of making MOs unlocked by default was confusing for users and made it too easy to accidentally change the qty to consume rather than the consumed amount. This fix makes it so MOs are now locked by default and will be unlocked/locked (including existing draft / confirmed / in progress MOs) automatically when setting is changed. Previous setting (Lock Quantities To Consume) has been repurposed for this. When setting is active, no lock/unlock button will be visible to match the updated setting description (i.e. non-done MOs can never be locked). Task: 2518523 ENT PR (only fixes test): odoo/enterprise#19732 Upgrade PR: odoo/upgrade#2655 --- addons/mrp/models/mrp_production.py | 8 ++++++-- addons/mrp/models/res_config_settings.py | 10 +++++++++- addons/mrp/security/mrp_security.xml | 4 ++-- addons/mrp/tests/test_order.py | 2 ++ addons/mrp/tests/test_procurement.py | 1 + .../tests/test_warehouse_multistep_manufacturing.py | 2 +- addons/mrp/views/res_config_settings_views.xml | 6 +++--- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index c85f09a550c..294db037e37 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -69,7 +69,7 @@ class MrpProduction(models.Model): @api.model def _get_default_is_locked(self): - return self.user_has_groups('mrp.group_locked_by_default') + return not self.user_has_groups('mrp.group_unlocked_by_default') name = fields.Char( 'Reference', copy=False, readonly=True, default=lambda x: _('New')) @@ -501,7 +501,11 @@ class MrpProduction(models.Model): @api.depends('state') def _compute_show_lock(self): for order in self: - order.show_lock = self.env.user.has_group('mrp.group_locked_by_default') and order.id is not False and order.state not in {'cancel', 'draft'} + order.show_lock = order.state == 'done' or ( + not self.env.user.has_group('mrp.group_unlocked_by_default') + and order.id is not False + and order.state not in {'cancel', 'draft'} + ) @api.depends('state','move_raw_ids') def _compute_show_lot_ids(self): diff --git a/addons/mrp/models/res_config_settings.py b/addons/mrp/models/res_config_settings.py index ca11ff63370..a103fd3ac04 100644 --- a/addons/mrp/models/res_config_settings.py +++ b/addons/mrp/models/res_config_settings.py @@ -19,7 +19,7 @@ class ResConfigSettings(models.TransientModel): module_mrp_subcontracting = fields.Boolean("Subcontracting") group_mrp_routings = fields.Boolean("MRP Work Orders", implied_group='mrp.group_mrp_routings') - group_locked_by_default = fields.Boolean("Lock Quantities To Consume", implied_group='mrp.group_locked_by_default') + group_unlocked_by_default = fields.Boolean("Unlock Manufacturing Orders", implied_group='mrp.group_unlocked_by_default') @api.onchange('use_manufacturing_lead') def _onchange_use_manufacturing_lead(self): @@ -38,3 +38,11 @@ class ResConfigSettings(models.TransientModel): self.module_mrp_workorder = True else: self.module_mrp_workorder = False + + @api.onchange('group_unlocked_by_default') + def _onchange_group_unlocked_by_default(self): + """ When changing this setting, we want existing MOs to automatically update to match setting. """ + if self.group_unlocked_by_default: + self.env['mrp.production'].search([('state', 'not in', ('cancel', 'done')), ('is_locked', '=', True)]).is_locked = False + else: + self.env['mrp.production'].search([('state', 'not in', ('cancel', 'done')), ('is_locked', '=', False)]).is_locked = True diff --git a/addons/mrp/security/mrp_security.xml b/addons/mrp/security/mrp_security.xml index 795e6c7813b..e350b76bfdf 100644 --- a/addons/mrp/security/mrp_security.xml +++ b/addons/mrp/security/mrp_security.xml @@ -30,8 +30,8 @@ - - Locked by default + + Unlocked by default diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index c8af55626e3..959adaae46c 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -396,6 +396,7 @@ class TestMrpOrder(TestMrpCommon): production = production_form.save() production.action_confirm() production.action_assign() + production.is_locked = False production_form = Form(production) # change the quantity producing and the initial demand # in the same transaction @@ -1027,6 +1028,7 @@ class TestMrpOrder(TestMrpCommon): mo.bom_id.consumption = 'flexible' # Because we'll over-consume with a product not defined in the BOM mo.action_assign() + mo.is_locked = False mo_form = Form(mo) mo_form.qty_producing = 3 diff --git a/addons/mrp/tests/test_procurement.py b/addons/mrp/tests/test_procurement.py index 6ec586d8fcd..15a0d4ce3b9 100644 --- a/addons/mrp/tests/test_procurement.py +++ b/addons/mrp/tests/test_procurement.py @@ -467,6 +467,7 @@ class TestProcurement(TestMrpCommon): self.assertEqual(len(mo), 1, "Manufacture order was not automatically created") mo.action_assign() + mo.is_locked = False self.assertEqual(mo.move_raw_ids.reserved_availability, 0, "No components should be reserved yet") self.assertEqual(mo.product_qty, 15, "Quantity to produce should be picking demand + reordering rule max qty") diff --git a/addons/mrp/tests/test_warehouse_multistep_manufacturing.py b/addons/mrp/tests/test_warehouse_multistep_manufacturing.py index ba48660e707..6f3397a987a 100644 --- a/addons/mrp/tests/test_warehouse_multistep_manufacturing.py +++ b/addons/mrp/tests/test_warehouse_multistep_manufacturing.py @@ -297,7 +297,7 @@ class TestMultistepManufacturingWarehouse(TestMrpCommon): production = production_form.save() production.action_confirm() - + production.is_locked = False production_form = Form(production) with production_form.move_raw_ids.new() as move: move.product_id = new_product diff --git a/addons/mrp/views/res_config_settings_views.xml b/addons/mrp/views/res_config_settings_views.xml index 58e10021f6d..d3dd90cb263 100644 --- a/addons/mrp/views/res_config_settings_views.xml +++ b/addons/mrp/views/res_config_settings_views.xml @@ -65,12 +65,12 @@
- +
-