diff --git a/app/Services/WorkOrderService.php b/app/Services/WorkOrderService.php index 3da8978..366e419 100644 --- a/app/Services/WorkOrderService.php +++ b/app/Services/WorkOrderService.php @@ -51,6 +51,8 @@ public function index(array $params) $query = WorkOrder::query() ->where('tenant_id', $tenantId) + ->whereNotNull('process_id') + ->where(fn ($q) => $q->whereNull('options->is_auxiliary')->orWhere('options->is_auxiliary', false)) ->with([ 'assignee:id,name', 'assignees.user:id,name', @@ -80,14 +82,9 @@ public function index(array $params) } // 공정 필터 (process_id) - // - 'none' 또는 '0': 공정 미지정 (process_id IS NULL) - // - 숫자: 해당 공정 ID로 필터 - if ($processId !== null) { - if ($processId === 'none' || $processId === '0' || $processId === 0) { - $query->whereNull('process_id'); - } else { - $query->where('process_id', $processId); - } + // 기본 조건으로 process_id IS NOT NULL이므로 'none'은 무의미 + if ($processId !== null && $processId !== 'none' && $processId !== '0' && $processId !== 0) { + $query->where('process_id', $processId); } // 공정 코드 필터 (process_code) - 대시보드용 @@ -163,29 +160,29 @@ public function stats(): array { $tenantId = $this->tenantId(); - $counts = WorkOrder::where('tenant_id', $tenantId) + // 실제 작업건만 카운트 (공정 배정 + 보조공정 제외) + $baseQuery = WorkOrder::where('tenant_id', $tenantId) + ->whereNotNull('process_id') + ->where(fn ($q) => $q->whereNull('options->is_auxiliary')->orWhere('options->is_auxiliary', false)); + + $counts = (clone $baseQuery) ->select('status', DB::raw('count(*) as count')) ->groupBy('status') ->pluck('count', 'status') ->toArray(); // 공정별 카운트 (탭 숫자 표시용) - $byProcess = WorkOrder::where('tenant_id', $tenantId) + $byProcess = (clone $baseQuery) ->select('process_id', DB::raw('count(*) as count')) ->groupBy('process_id') ->pluck('count', 'process_id') ->toArray(); $total = array_sum($counts); - $noneCount = $byProcess[''] ?? $byProcess[0] ?? 0; - // null 키는 빈 문자열로 변환되므로 별도 처리 + // process_id IS NOT NULL 기본 필터 적용으로 null 키 처리 불필요 $processedByProcess = []; foreach ($byProcess as $key => $count) { - if ($key === '' || $key === 0 || $key === null) { - $processedByProcess['none'] = $count; - } else { - $processedByProcess[(string) $key] = $count; - } + $processedByProcess[(string) $key] = $count; } return [