refactor: Remove extensive debug logging and update move line quantity using the write method for persistence.

This commit is contained in:
Suherdy Yacob 2026-02-11 10:32:06 +07:00
parent 4a44ffa1fe
commit 78e6f6f078

View File

@ -132,22 +132,15 @@ class QualityCheck(models.Model):
check.failure_location_id = dest_location
return
# Partial Failure Case (Split)
_logger.info(f"DEBUG: Partial failure split for check {check.id}")
# Determine Lot ID with fallback to MO
lot_id = move_line.lot_id.id
if not lot_id and move.production_id:
# Check for lot_producing_ids (M2M) or lot_producing_id (M2O)
if hasattr(move.production_id, 'lot_producing_ids') and move.production_id.lot_producing_ids:
lot_id = move.production_id.lot_producing_ids[0].id
_logger.info(f"DEBUG: Found lot {lot_id} from MO lot_producing_ids")
elif hasattr(move.production_id, 'lot_producing_id') and move.production_id.lot_producing_id:
lot_id = move.production_id.lot_producing_id.id
_logger.info(f"DEBUG: Found lot {lot_id} from MO lot_producing_id")
if not lot_id:
_logger.warning(f"DEBUG: No Lot ID found for Move {move.id} / Line {move_line.id}")
# Common line values preparation (template)
line_vals_template = {
'product_id': move.product_id.id,
@ -159,7 +152,6 @@ class QualityCheck(models.Model):
}
if move.state == 'done':
_logger.info(f"DEBUG: Move {move.id} is DONE. Creating new move from Stock -> Reject")
source_location_id = move.location_dest_id.id
new_failed_move = move.copy({
@ -179,35 +171,27 @@ class QualityCheck(models.Model):
'location_id': source_location_id,
})
failed_move_line = self.env['stock.move.line'].create(line_vals)
_logger.info(f"DEBUG: Manually created line {failed_move_line.id} with lot {lot_id}")
new_failed_move.picked = True
new_failed_move._action_done()
else:
_logger.info(f"DEBUG: Move {move.id} is ASSIGNED/CONFIRMED. Using _split")
# _split returns a list of values (dicts)
vals_list = move._split(failed_qty)
_logger.info(f"DEBUG: _split returned vals count: {len(vals_list)}")
if vals_list:
new_failed_move = self.env['stock.move'].create(vals_list)
_logger.info(f"DEBUG: Created split move {new_failed_move.id}. State: {new_failed_move.state}")
else:
_logger.warning("DEBUG: _split returned empty! Fallback to copy.")
new_failed_move = move.copy({
'location_dest_id': dest_location,
'product_uom_qty': failed_qty,
'state': 'draft',
'move_line_ids': [],
})
_logger.info(f"DEBUG: Fallback created move {new_failed_move.id}")
# Configure and Confirm
new_failed_move.location_dest_id = dest_location
new_failed_move._action_confirm()
_logger.info(f"DEBUG: Confirmed new move {new_failed_move.id}. State: {new_failed_move.state}")
# Manual Line Creation/Update (Force Lot)
line_vals = line_vals_template.copy()
@ -216,37 +200,22 @@ class QualityCheck(models.Model):
'location_id': new_failed_move.location_id.id,
})
_logger.info(f"DEBUG: Line Vals: {line_vals}")
if new_failed_move.move_line_ids:
# Update existing auto-created line
target_line = new_failed_move.move_line_ids[0]
target_line.write(line_vals)
failed_move_line = target_line
_logger.info(f"DEBUG: Updated existing line {target_line.id} with Lot {lot_id}")
else:
# Create new line
failed_move_line = self.env['stock.move.line'].create(line_vals)
_logger.info(f"DEBUG: Manually created line {failed_move_line.id} with Lot {lot_id}")
# Reduce original reservation to free up Lot
# Important: Do this AFTER Confirming new move but BEFORE Done?
# Or maybe before creating new line?
# Actually, if we reduce quantity on original line, it releases reservation.
# Use write to ensure persistence
if move_line.quantity >= failed_qty:
_logger.info(f"DEBUG: Reducing original line {move_line.id} qty from {move_line.quantity} by {failed_qty}")
move_line.quantity -= failed_qty
move_line.write({'quantity': move_line.quantity - failed_qty})
new_failed_move.picked = True
try:
new_failed_move._action_done()
_logger.info(f"DEBUG: Forced new move {new_failed_move.id} to DONE state.")
except Exception as e:
_logger.error(f"DEBUG: Failed to force Done on {new_failed_move.id}. Error: {e}")
# Log line details for debugging
for ml in new_failed_move.move_line_ids:
_logger.info(f"DEBUG: Line {ml.id}: Lot={ml.lot_id.name}, Qty={ml.quantity}, Loc={ml.location_id.name}->{ml.location_dest_id.name}")
raise e
new_failed_move._action_done()
_logger.info(f"DEBUG: Forced new move {new_failed_move.id} to DONE state.")