From bf7e47056ad737bb2245ecb550ffa0ec9ff7b715 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Tue, 10 Jan 2023 18:14:32 +0100 Subject: [PATCH] mrp_p_auto_validate: fix auto_validate flag Relying on onchange makes this value set only if the BOM is set manually on the Form view, what isn't the case when MO are created by running procurements. --- .../models/mrp_production.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mrp_production_auto_validate/models/mrp_production.py b/mrp_production_auto_validate/models/mrp_production.py index 6252b9b17..51c0c843a 100644 --- a/mrp_production_auto_validate/models/mrp_production.py +++ b/mrp_production_auto_validate/models/mrp_production.py @@ -13,7 +13,9 @@ class MrpProduction(models.Model): auto_validate = fields.Boolean( string="Auto Validate", - default=False, + compute="_compute_auto_validate", + store=True, + states={"draft": [("readonly", False)]}, ) @api.constrains("bom_id", "auto_validate", "product_qty") @@ -39,11 +41,16 @@ class MrpProduction(models.Model): ).format(qty=mo.bom_id.product_qty) ) - @api.onchange("bom_id") - def _onchange_bom_id(self): - res = super()._onchange_bom_id() - self.auto_validate = self.bom_id.mo_auto_validation - return res + @api.depends("bom_id.mo_auto_validation", "state") + def _compute_auto_validate(self): + for prod in self: + if prod.state != "draft": + # Avoid recomputing the value once the MO is confirmed. + # e.g. if the value changes on the BOM but the MO was already confirmed, + # or if the user forces another value while the MO is in draft, + # we don't want to change the value after confirmation. + continue + prod.auto_validate = prod.bom_id.mo_auto_validation def _auto_validate_after_picking(self): self.ensure_one()