From b8fd2aad63600d9d32bf72fce42937d11eb00e95 Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Tue, 22 Oct 2024 16:07:58 +0200 Subject: [PATCH] [IMP] stock_cycle_count: Refactor responsible_id sync using compute/inverse fields --- stock_cycle_count/models/stock_cycle_count.py | 11 ----------- stock_cycle_count/models/stock_inventory.py | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/stock_cycle_count/models/stock_cycle_count.py b/stock_cycle_count/models/stock_cycle_count.py index d1b4b243ebb2..d39eb72e74d6 100644 --- a/stock_cycle_count/models/stock_cycle_count.py +++ b/stock_cycle_count/models/stock_cycle_count.py @@ -84,17 +84,6 @@ class StockCycleCount(models.Model): readonly=True, ) - def write(self, vals): - result = super().write(vals) - if "responsible_id" in vals and not self.env.context.get("no_propagate"): - stock_inventory_records = self.mapped("stock_adjustment_ids") - for record in stock_inventory_records: - if record.responsible_id.id != vals["responsible_id"]: - record.with_context(no_propagate=True).write( - {"responsible_id": vals["responsible_id"]} - ) - return result - @api.depends("stock_adjustment_ids") def _compute_inventory_adj_count(self): for rec in self: diff --git a/stock_cycle_count/models/stock_inventory.py b/stock_cycle_count/models/stock_inventory.py index 74fabd2d0dc3..c7304ed56f10 100644 --- a/stock_cycle_count/models/stock_inventory.py +++ b/stock_cycle_count/models/stock_inventory.py @@ -35,10 +35,27 @@ class StockInventory(models.Model): default=False, ) responsible_id = fields.Many2one( - states={"draft": [("readonly", False)], "in_progress": [("readonly", False)]}, tracking=True, + compute="_compute_responsible_id", + inverse="_inverse_responsible_id", + store=True, + readonly=False, ) + @api.depends("cycle_count_id.responsible_id") + def _compute_responsible_id(self): + for inv in self: + if inv.cycle_count_id: + inv.responsible_id = inv.cycle_count_id.responsible_id + inv.stock_quant_ids.write( + {"user_id": inv.cycle_count_id.responsible_id} + ) + + def _inverse_responsible_id(self): + for inv in self: + if inv.cycle_count_id and inv.responsible_id: + inv.cycle_count_id.responsible_id = inv.responsible_id + def write(self, vals): result = super().write(vals) if "responsible_id" in vals: