mo_lock_consumed/models/stock_move.py

32 lines
1.3 KiB
Python

from odoo import models
class StockMove(models.Model):
_inherit = 'stock.move'
def _should_bypass_set_qty_producing(self):
"""
Only bypass if explicitly flagged as manual consumption.
We rely on the write() method to set this flag and to block resets.
"""
if self.sudo().manual_consumption:
return True
return super()._should_bypass_set_qty_producing()
def write(self, vals):
# 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:
vals['manual_consumption'] = True
vals['picked'] = True
return super().write(vals)