diff --git a/models/stock_move.py b/models/stock_move.py index 87fef4f..34dd6d1 100644 --- a/models/stock_move.py +++ b/models/stock_move.py @@ -5,17 +5,27 @@ class StockMove(models.Model): def _should_bypass_set_qty_producing(self): """ - Prevent auto-update of consumed quantity if: - - The move already has a quantity set (partial consumption). - - The move is already picked. + 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().quantity > 0 or self.sudo().picked or self.sudo().manual_consumption: + if self.sudo().manual_consumption: return True return super()._should_bypass_set_qty_producing() 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: vals['manual_consumption'] = True vals['picked'] = True + return super().write(vals)