json(['success' => false, 'data' => []]); } $codeGroup = $request->input('code_group', 'product'); $categories = Category::query() ->where('tenant_id', $tenantId) ->where('code_group', $codeGroup) ->whereNull('deleted_at') ->orderBy('parent_id') ->orderBy('sort_order') ->get(['id', 'parent_id', 'code', 'name']); return response()->json([ 'success' => true, 'data' => $categories, ]); } /** * 트리 데이터 조회 (HTMX) */ public function tree(Request $request): View { $tenantId = session('selected_tenant_id'); $codeGroup = $request->input('code_group', 'product'); $categories = collect(); if ($tenantId) { $categories = Category::query() ->where('tenant_id', $tenantId) ->where('code_group', $codeGroup) ->whereNull('parent_id') ->with('childrenRecursive') ->orderBy('sort_order') ->get(); } return view('categories.partials.tree', [ 'categories' => $categories, 'codeGroup' => $codeGroup, ]); } /** * 단일 조회 */ public function show(int $id): JsonResponse { $category = Category::with('children')->findOrFail($id); return response()->json([ 'success' => true, 'data' => $category, ]); } /** * 생성 */ public function store(Request $request): JsonResponse { $tenantId = session('selected_tenant_id'); if (! $tenantId) { return response()->json(['success' => false, 'message' => '테넌트를 선택해주세요.'], 400); } $validated = $request->validate([ 'code_group' => 'required|string|max:50', 'parent_id' => 'nullable|integer|exists:categories,id', 'code' => 'required|string|max:50', 'name' => 'required|string|max:100', 'profile_code' => 'nullable|string|max:50', 'description' => 'nullable|string|max:500', 'is_active' => 'boolean', ]); // 코드 중복 체크 $exists = Category::query() ->where('tenant_id', $tenantId) ->where('code_group', $validated['code_group']) ->where('code', $validated['code']) ->exists(); if ($exists) { return response()->json(['success' => false, 'message' => '이미 존재하는 코드입니다.'], 400); } // 최대 sort_order 조회 $maxSortOrder = Category::query() ->where('tenant_id', $tenantId) ->where('code_group', $validated['code_group']) ->where('parent_id', $validated['parent_id'] ?? null) ->max('sort_order') ?? 0; $category = Category::create([ 'tenant_id' => $tenantId, 'parent_id' => $validated['parent_id'] ?? null, 'code_group' => $validated['code_group'], 'code' => $validated['code'], 'name' => $validated['name'], 'profile_code' => $validated['profile_code'] ?? null, 'description' => $validated['description'] ?? null, 'is_active' => $validated['is_active'] ?? true, 'sort_order' => $maxSortOrder + 1, 'created_by' => Auth::id(), ]); return response()->json([ 'success' => true, 'message' => '카테고리가 생성되었습니다.', 'data' => $category, ]); } /** * 수정 */ public function update(Request $request, int $id): JsonResponse { $category = Category::findOrFail($id); $validated = $request->validate([ 'code' => 'sometimes|required|string|max:50', 'name' => 'sometimes|required|string|max:100', 'profile_code' => 'nullable|string|max:50', 'description' => 'nullable|string|max:500', 'is_active' => 'sometimes|boolean', ]); // 코드 변경 시 중복 체크 if (isset($validated['code']) && $validated['code'] !== $category->code) { $exists = Category::query() ->where('tenant_id', $category->tenant_id) ->where('code_group', $category->code_group) ->where('code', $validated['code']) ->where('id', '!=', $id) ->exists(); if ($exists) { return response()->json(['success' => false, 'message' => '이미 존재하는 코드입니다.'], 400); } } $category->update(array_merge($validated, ['updated_by' => Auth::id()])); return response()->json([ 'success' => true, 'message' => '카테고리가 수정되었습니다.', 'data' => $category->fresh(), ]); } /** * 삭제 */ public function destroy(int $id): JsonResponse { $category = Category::findOrFail($id); // 하위 카테고리 존재 여부 확인 if ($category->children()->count() > 0) { return response()->json([ 'success' => false, 'message' => '하위 카테고리가 있어 삭제할 수 없습니다.', ], 400); } $category->update(['deleted_by' => Auth::id()]); $category->delete(); return response()->json([ 'success' => true, 'message' => '카테고리가 삭제되었습니다.', ]); } /** * 활성 상태 토글 */ public function toggle(int $id): JsonResponse { $category = Category::findOrFail($id); $category->update([ 'is_active' => ! $category->is_active, 'updated_by' => Auth::id(), ]); return response()->json([ 'success' => true, 'message' => $category->is_active ? '활성화되었습니다.' : '비활성화되었습니다.', 'is_active' => $category->is_active, ]); } /** * 테넌트 카테고리를 글로벌로 복사 (HQ 또는 슈퍼관리자) */ public function promoteToGlobal(int $id): JsonResponse { $user = Auth::user(); $tenantId = session('selected_tenant_id'); $tenant = $tenantId ? \App\Models\Tenants\Tenant::find($tenantId) : null; $isHQ = $tenant?->tenant_type === 'HQ'; $isSuperAdmin = $user?->isSuperAdmin() ?? false; if (! $isHQ && ! $isSuperAdmin) { return response()->json(['success' => false, 'message' => '본사 또는 슈퍼관리자만 글로벌로 복사할 수 있습니다.'], 403); } $category = Category::findOrFail($id); // 이미 글로벌에 동일 코드가 있는지 확인 $exists = GlobalCategory::query() ->where('code_group', $category->code_group) ->where('code', $category->code) ->whereNull('deleted_at') ->exists(); if ($exists) { return response()->json(['success' => false, 'message' => '이미 동일한 글로벌 카테고리가 존재합니다.'], 400); } GlobalCategory::create([ 'parent_id' => null, 'code_group' => $category->code_group, 'code' => $category->code, 'name' => $category->name, 'profile_code' => $category->profile_code, 'description' => $category->description, 'is_active' => true, 'sort_order' => $category->sort_order, 'created_by' => Auth::id(), ]); return response()->json([ 'success' => true, 'message' => '글로벌 카테고리로 복사되었습니다.', ]); } /** * 이동 (부모 변경) */ public function move(Request $request, int $id): JsonResponse { $category = Category::findOrFail($id); $validated = $request->validate([ 'parent_id' => 'nullable|integer|exists:categories,id', ]); $newParentId = $validated['parent_id'] ?? null; // 자기 자신이나 자식을 부모로 설정 불가 if ($newParentId === $id) { return response()->json(['success' => false, 'message' => '자기 자신을 부모로 설정할 수 없습니다.'], 400); } // 자식 카테고리를 부모로 설정하는 것 방지 (순환 참조) if ($newParentId) { $parent = Category::find($newParentId); while ($parent) { if ($parent->id === $id) { return response()->json(['success' => false, 'message' => '하위 카테고리를 부모로 설정할 수 없습니다.'], 400); } $parent = $parent->parent; } } $category->update([ 'parent_id' => $newParentId, 'updated_by' => Auth::id(), ]); return response()->json([ 'success' => true, 'message' => '카테고리가 이동되었습니다.', ]); } /** * 순서 변경 */ public function reorder(Request $request): JsonResponse { $validated = $request->validate([ 'items' => 'required|array', 'items.*.id' => 'required|integer|exists:categories,id', 'items.*.sort_order' => 'required|integer|min:0', ]); DB::beginTransaction(); try { foreach ($validated['items'] as $item) { Category::where('id', $item['id'])->update([ 'sort_order' => $item['sort_order'], 'updated_by' => Auth::id(), ]); } DB::commit(); return response()->json([ 'success' => true, 'message' => '순서가 변경되었습니다.', ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'success' => false, 'message' => '순서 변경 중 오류가 발생했습니다.', ], 500); } } }