Simplify _should_bypass_set_qty_producing to check only manual_consumption and add a guard in write to prevent zeroing out consumed quantities for production components.

This commit is contained in:
Suherdy Yacob 2026-02-11 00:16:57 +07:00
parent 1ff922db7b
commit 71db3eed46

View File

@ -5,17 +5,27 @@ class StockMove(models.Model):
def _should_bypass_set_qty_producing(self): def _should_bypass_set_qty_producing(self):
""" """
Prevent auto-update of consumed quantity if: Only bypass if explicitly flagged as manual consumption.
- The move already has a quantity set (partial consumption). We rely on the write() method to set this flag and to block resets.
- The move is already picked.
""" """
if self.sudo().quantity > 0 or self.sudo().picked or self.sudo().manual_consumption: if self.sudo().manual_consumption:
return True return True
return super()._should_bypass_set_qty_producing() return super()._should_bypass_set_qty_producing()
def write(self, vals): def write(self, vals):
# Enforce manual usage flags if quantity is being set # 1. Universal Zero-Guard: Block reset to 0 for MO components that have content
if 'quantity' in vals and vals['quantity'] == 0:
# We filter for moves that are components and have 'something' (qty or lines)
# and are NOT being cancelled/scrapped (check context or state if needed, but simple is better for now)
protected = self.filtered(lambda m: m.raw_material_production_id and (m.quantity > 0 or m.move_line_ids))
if protected:
# Remove quantity from vals to prevent reset
vals = dict(vals)
del vals['quantity']
# 2. Enforce flags for positive updates
if 'quantity' in vals and vals['quantity'] > 0: if 'quantity' in vals and vals['quantity'] > 0:
vals['manual_consumption'] = True vals['manual_consumption'] = True
vals['picked'] = True vals['picked'] = True
return super().write(vals) return super().write(vals)