fix: Ensure precise MPS quantity propagation by rounding intermediate calculations to 8 decimal places and applying UoM rounding to final quantities.

This commit is contained in:
Suherdy Yacob 2026-03-16 09:44:05 +07:00
parent 179b7b53b7
commit d0b1363c44

View File

@ -60,15 +60,15 @@ class MrpProductionSchedule(models.Model):
ratio = ratios.get(self.product_id, 0.0) ratio = ratios.get(self.product_id, 0.0)
if ratio > 0: if ratio > 0:
parent_diff = diff_qty / ratio parent_diff = round(diff_qty / ratio, 8)
p_date_start, p_date_stop = parent_schedule.company_id._get_date_range(force_period=period_scale)[date_index] p_date_start, p_date_stop = parent_schedule.company_id._get_date_range(force_period=period_scale)[date_index]
p_exist = parent_schedule.forecast_ids.filtered(lambda f: f.date >= p_date_start and f.date <= p_date_stop) p_exist = parent_schedule.forecast_ids.filtered(lambda f: f.date >= p_date_start and f.date <= p_date_stop)
p_old_qty_base = sum(p_exist.mapped('replenish_qty')) if p_exist else 0.0 p_old_qty_base = sum(p_exist.mapped('replenish_qty')) if p_exist else 0.0
if hasattr(parent_schedule, 'packaging_id') and parent_schedule.packaging_id and parent_schedule.packaging_id.qty: if hasattr(parent_schedule, 'packaging_id') and parent_schedule.packaging_id and parent_schedule.packaging_id.qty:
p_old_qty_pack = p_old_qty_base / parent_schedule.packaging_id.qty p_old_qty_pack = round(p_old_qty_base / parent_schedule.packaging_id.qty, 8)
parent_diff = parent_diff / parent_schedule.packaging_id.qty parent_diff = round(parent_diff / parent_schedule.packaging_id.qty, 8)
else: else:
p_old_qty_pack = p_old_qty_base p_old_qty_pack = p_old_qty_base
@ -76,6 +76,9 @@ class MrpProductionSchedule(models.Model):
if new_parent_qty < 0: if new_parent_qty < 0:
new_parent_qty = 0.0 new_parent_qty = 0.0
# Round to the UoM rounding of the product before saving
new_parent_qty = float_round(new_parent_qty, precision_rounding=parent_schedule.product_uom_id.rounding)
# Update parent with 'up' context. This propagates it all the way to the top recursively! # Update parent with 'up' context. This propagates it all the way to the top recursively!
parent_schedule.with_context(propagate_direction='up').set_replenish_qty(date_index, new_parent_qty, period_scale) parent_schedule.with_context(propagate_direction='up').set_replenish_qty(date_index, new_parent_qty, period_scale)
@ -91,18 +94,18 @@ class MrpProductionSchedule(models.Model):
ratio = ratios.get(child_schedule.product_id, 0.0) ratio = ratios.get(child_schedule.product_id, 0.0)
if ratio > 0: if ratio > 0:
child_diff = diff_qty * ratio child_diff = round(diff_qty * ratio, 8)
if hasattr(child_schedule, 'packaging_id') and child_schedule.packaging_id and child_schedule.packaging_id.qty: if hasattr(child_schedule, 'packaging_id') and child_schedule.packaging_id and child_schedule.packaging_id.qty:
packaging_qty = child_schedule.packaging_id.qty packaging_qty = child_schedule.packaging_id.qty
child_diff = child_diff / packaging_qty child_diff = round(child_diff / packaging_qty, 8)
c_date_start, c_date_stop = child_schedule.company_id._get_date_range(force_period=period_scale)[date_index] c_date_start, c_date_stop = child_schedule.company_id._get_date_range(force_period=period_scale)[date_index]
c_exist = child_schedule.forecast_ids.filtered(lambda f: f.date >= c_date_start and f.date <= c_date_stop) c_exist = child_schedule.forecast_ids.filtered(lambda f: f.date >= c_date_start and f.date <= c_date_stop)
c_old_qty_base = sum(c_exist.mapped('replenish_qty')) if c_exist else 0.0 c_old_qty_base = sum(c_exist.mapped('replenish_qty')) if c_exist else 0.0
if hasattr(child_schedule, 'packaging_id') and child_schedule.packaging_id and child_schedule.packaging_id.qty: if hasattr(child_schedule, 'packaging_id') and child_schedule.packaging_id and child_schedule.packaging_id.qty:
c_old_qty_pack = c_old_qty_base / child_schedule.packaging_id.qty c_old_qty_pack = round(c_old_qty_base / child_schedule.packaging_id.qty, 8)
else: else:
c_old_qty_pack = c_old_qty_base c_old_qty_pack = c_old_qty_base
@ -110,6 +113,9 @@ class MrpProductionSchedule(models.Model):
if new_child_qty_pack < 0: if new_child_qty_pack < 0:
new_child_qty_pack = 0.0 new_child_qty_pack = 0.0
# Round to the UoM rounding of the product before saving
new_child_qty_pack = float_round(new_child_qty_pack, precision_rounding=child_schedule.product_uom_id.rounding)
# Update child with 'down' context. This propagates it all the way to the bottom! # Update child with 'down' context. This propagates it all the way to the bottom!
child_schedule.with_context(propagate_direction='down').set_replenish_qty(date_index, new_child_qty_pack, period_scale) child_schedule.with_context(propagate_direction='down').set_replenish_qty(date_index, new_child_qty_pack, period_scale)