diff --git a/models/__pycache__/stock_inventory_revaluation.cpython-312.pyc b/models/__pycache__/stock_inventory_revaluation.cpython-312.pyc index 09c9c82..61c0ce1 100644 Binary files a/models/__pycache__/stock_inventory_revaluation.cpython-312.pyc and b/models/__pycache__/stock_inventory_revaluation.cpython-312.pyc differ diff --git a/models/stock_inventory_revaluation.py b/models/stock_inventory_revaluation.py index 25431bf..d0144f6 100755 --- a/models/stock_inventory_revaluation.py +++ b/models/stock_inventory_revaluation.py @@ -99,6 +99,24 @@ class StockInventoryRevaluation(models.Model): def action_validate(self): self.ensure_one() + # 0. Safety Refresh: Re-calculate current value/quantity from DB to handle race conditions + # Identify layers created UP TO the revaluation date + layers = self.env['stock.valuation.layer'].search([ + ('product_id', '=', self.product_id.id), + ('create_date', '<=', self.date), + ('company_id', '=', self.company_id.id) + ]) + real_qty = sum(layers.mapped('quantity')) + real_value = sum(layers.mapped('value')) + + # Update if stale + # We assume 'new_value' is the User's Truth (Target). + if self.normalization_adjustment: + self.current_value = real_value + self.quantity = real_qty + # Recalculate Extra Cost to ensure: Real + Extra = Target + self.extra_cost = self.new_value - self.current_value + # If normalizing, we actually expect/allow Extra Cost to be anything, # as long as New Value (Current + Extra) is valid. # But legacy check says extra_cost != 0.