Skip to content

Commit

Permalink
[IMP] stock_cycle_count: Refactor responsible_id sync using compute/i…
Browse files Browse the repository at this point in the history
…nverse fields
  • Loading branch information
JoanSForgeFlow committed Oct 22, 2024
1 parent cddec1e commit b8fd2aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
11 changes: 0 additions & 11 deletions stock_cycle_count/models/stock_cycle_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 18 additions & 1 deletion stock_cycle_count/models/stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b8fd2aa

Please sign in to comment.