diff --git a/app/Services/WorkOrderService.php b/app/Services/WorkOrderService.php index fc295c01..98b19908 100644 --- a/app/Services/WorkOrderService.php +++ b/app/Services/WorkOrderService.php @@ -2480,7 +2480,6 @@ public function storeItemInspection(int $workOrderId, int $itemId, array $data): ); // 검사 완료 시 WO 상태 자동 전이 - // 1. waiting/pending → in_progress (아직 진행중 아니면) $workOrder->refresh(); if (in_array($workOrder->status, [WorkOrder::STATUS_PENDING, WorkOrder::STATUS_WAITING])) { $workOrder->status = WorkOrder::STATUS_IN_PROGRESS; @@ -2490,12 +2489,35 @@ public function storeItemInspection(int $workOrderId, int $itemId, array $data): $this->syncOrderStatus($workOrder, $tenantId); } - // 2. 모든 공정 단계 완료 확인 → WO 자동 완료 - $this->autoCompleteWorkOrderIfAllStepsDone($workOrder, $tenantId, $userId); + // 재공품(STOCK): 검사 완료 = 전체 완료 (단일 부품이므로) + $order = $workOrder->sales_order_id + ? Order::withoutGlobalScopes()->find($workOrder->sales_order_id) + : null; + $isStock = $order && $order->order_type_code === 'STOCK'; + + if ($isStock) { + // 미완료 단계 일괄 완료 + WorkOrderStepProgress::where('work_order_id', $workOrder->id) + ->where('status', '!=', 'completed') + ->update([ + 'status' => 'completed', + 'completed_at' => now(), + 'completed_by' => $userId, + ]); + + // WO → completed + if ($workOrder->status !== WorkOrder::STATUS_COMPLETED) { + $this->updateStatus($workOrder->id, WorkOrder::STATUS_COMPLETED); + } + } else { + // 일반 수주: 모든 공정 단계 완료 확인 → WO 자동 완료 + $this->autoCompleteWorkOrderIfAllStepsDone($workOrder, $tenantId, $userId); + } return [ 'item_id' => $item->id, 'inspection_data' => $item->getInspectionData(), + 'work_order_completed' => $isStock && $workOrder->fresh()->status === WorkOrder::STATUS_COMPLETED, ]; }