feat: Refine component availability check to use forecast_availability and prevent hiding produce buttons for zero-consumed partial productions.

This commit is contained in:
Suherdy Yacob 2026-02-12 16:44:05 +07:00
parent 0285ce9ebe
commit 2f32964266

View File

@ -49,11 +49,13 @@ class MrpProduction(models.Model):
# Check availability based on the ACTUAL 'Consumed' quantity # Check availability based on the ACTUAL 'Consumed' quantity
qty_to_consume = move.quantity qty_to_consume = move.quantity
# Check availability # Skip check if product is not storable (Service, etc.)
product_in_location = move.product_id.with_context(location=location.id) if not move.product_id.is_storable:
available_qty = product_in_location.qty_available continue
if available_qty - qty_to_consume < 0: # Check availability
# We use forecast_availability as it includes reservations
if move.forecast_availability < qty_to_consume:
potential_negative = True potential_negative = True
break break
@ -61,13 +63,7 @@ class MrpProduction(models.Model):
production.show_produce = False production.show_produce = False
production.show_produce_all = False production.show_produce_all = False
# User request: "hide the produce if all components consumed is 0 (does not make sense right to produce without components)" # Relaxed guard: Do not hide if qty_producing > 0 even if consumption is 0.
# This should only apply if we are in "Partial Produce" mode (qty_producing > 0), where consumption should have been calculated/entered. # This allows users to trigger consumption by clicking the Produce button.
# If qty_producing is 0 (Fresh MO), we usually show "Produce All", which will auto-calculate consumption, so we shouldn't hide it. # If they really want to block 0-consumption production, it should be a validation error on click,
if production.qty_producing > 0: # not a hidden button that prevents them from even trying.
# Check if ALL moves have 0 quantity
# We filter out cancelled moves, and maybe we should focus on raw materials.
relevant_moves = production.move_raw_ids.filtered(lambda m: m.state not in ('done', 'cancel'))
if relevant_moves and all(m.quantity == 0 for m in relevant_moves):
production.show_produce = False
production.show_produce_all = False