feat: Add safety refresh to recalculate current value, quantity, and extra cost during inventory revaluation validation to handle race conditions.

This commit is contained in:
Suherdy Yacob 2026-03-03 10:07:21 +07:00
parent baa34ff7ba
commit 4874173749
2 changed files with 18 additions and 0 deletions

View File

@ -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.