[IMP] stock_cycle_count: Refactor responsible_id sync using compute/inverse fields

This commit is contained in:
Joan Sisquella
2024-10-22 16:07:58 +02:00
parent cddec1e480
commit b8fd2aad63
2 changed files with 18 additions and 12 deletions

View File

@@ -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:

View File

@@ -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: