diff --git a/app/Http/Controllers/DocumentTemplateController.php b/app/Http/Controllers/DocumentTemplateController.php index 7f1a67f6..4c755f0c 100644 --- a/app/Http/Controllers/DocumentTemplateController.php +++ b/app/Http/Controllers/DocumentTemplateController.php @@ -75,13 +75,39 @@ private function getCurrentTenant(): ?Tenant } /** - * 문서분류 목록 조회 (글로벌 + 테넌트, 기존 데이터에서 group by) + * 문서분류 목록 조회 (common_codes 기반 + 기존 데이터 폴백) */ private function getCategories(): array { $tenantId = session('selected_tenant_id'); - return DocumentTemplate::query() + // 1. common_codes에서 document_category 조회 (테넌트 우선, 없으면 글로벌) + $commonCategories = DB::table('common_codes') + ->where('code_group', 'document_category') + ->where('is_active', true) + ->where(function ($q) use ($tenantId) { + if ($tenantId) { + $q->where('tenant_id', $tenantId) + ->orWhereNull('tenant_id'); + } else { + $q->whereNull('tenant_id'); + } + }) + ->orderBy('sort_order') + ->get(['code', 'name', 'tenant_id']) + ->unique('code') // code 기준 중복 제거 (tenant_id 있는 것이 우선) + ->map(fn ($c) => [ + 'code' => $c->code, + 'name' => $c->name, + ]) + ->values() + ->toArray(); + + // 2. 기존 템플릿에서 사용 중인 카테고리 (common_codes에 없는 것) + $usedCodes = array_column($commonCategories, 'code'); + $usedNames = array_column($commonCategories, 'name'); + + $existingCategories = DocumentTemplate::query() ->where(function ($query) use ($tenantId) { $query->whereNull('tenant_id'); if ($tenantId) { @@ -90,10 +116,15 @@ private function getCategories(): array }) ->whereNotNull('category') ->where('category', '!=', '') + ->whereNotIn('category', $usedCodes) // code로 사용된 것 제외 + ->whereNotIn('category', $usedNames) // name으로 사용된 것 제외 ->distinct() ->orderBy('category') ->pluck('category') + ->map(fn ($c) => ['code' => $c, 'name' => $c]) ->toArray(); + + return array_merge($commonCategories, $existingCategories); } /** diff --git a/resources/views/document-templates/edit.blade.php b/resources/views/document-templates/edit.blade.php index 0a87a9e0..e3702c18 100644 --- a/resources/views/document-templates/edit.blade.php +++ b/resources/views/document-templates/edit.blade.php @@ -84,13 +84,8 @@ class="w-full px-3 py-1.5 border border-gray-300 rounded-lg text-sm focus:outlin