feat:코드그룹 DB기반 관리, 스코프 필터, 동기화 테넌트명 표시

- 공통코드/카테고리: 하드코딩 그룹 라벨 제거, DB description 기반으로 전환
- 코드그룹 신규 생성 기능 추가 (사이드바 + 모달, TenantSetting 저장)
- 글로벌/테넌트 스코프 분류 및 필터 버튼 (전체/글로벌/테넌트)
- 사이드바 컴팩트 레이아웃 (100+ 그룹 대응)
- 동기화 페이지 3종(메뉴/공통코드/카테고리) 테넌트 회사명 표시

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-30 22:25:05 +09:00
parent f0ea6c71de
commit a0ec103614
10 changed files with 559 additions and 75 deletions

View File

@@ -5,12 +5,87 @@
use App\Models\Category;
use App\Models\GlobalCategory;
use App\Models\Tenants\Tenant;
use App\Models\Tenants\TenantSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
class CategoryController extends Controller
{
/**
* 커스텀 그룹 라벨 조회
*/
private function getCustomGroupLabels(): array
{
$tenantId = session('selected_tenant_id');
if (! $tenantId) {
return [];
}
$setting = TenantSetting::withoutGlobalScopes()
->where('tenant_id', $tenantId)
->where('setting_group', 'category')
->where('setting_key', 'custom_group_labels')
->first();
return $setting?->setting_value ?? [];
}
/**
* 카테고리 그룹 추가
*/
public function storeGroup(Request $request): RedirectResponse
{
$tenantId = session('selected_tenant_id');
if (! $tenantId) {
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
}
$validated = $request->validate([
'group_code' => ['required', 'string', 'max:50', 'regex:/^[a-z][a-z0-9_]*$/'],
'group_label' => 'required|string|max:50',
], [
'group_code.regex' => '그룹 코드는 영문 소문자, 숫자, 언더스코어만 사용 가능합니다.',
]);
$groupCode = $validated['group_code'];
$groupLabel = $validated['group_label'];
// DB에 이미 존재하는 그룹인지 체크
$existsInGlobal = GlobalCategory::whereNull('deleted_at')
->where('code_group', $groupCode)
->exists();
$existsInTenant = Category::where('code_group', $groupCode)->exists();
if ($existsInGlobal || $existsInTenant) {
return redirect()->back()->with('error', '이미 존재하는 그룹 코드입니다.');
}
// 커스텀 라벨 중복 체크
$customLabels = $this->getCustomGroupLabels();
if (isset($customLabels[$groupCode])) {
return redirect()->back()->with('error', '이미 존재하는 커스텀 그룹 코드입니다.');
}
$customLabels[$groupCode] = $groupLabel;
TenantSetting::withoutGlobalScopes()->updateOrCreate(
[
'tenant_id' => $tenantId,
'setting_group' => 'category',
'setting_key' => 'custom_group_labels',
],
[
'setting_value' => $customLabels,
'description' => '카테고리 커스텀 그룹 라벨',
]
);
return redirect()
->route('categories.index', ['group' => $groupCode])
->with('success', "'{$groupLabel}' 그룹이 추가되었습니다.");
}
/**
* 카테고리 관리 페이지
@@ -26,35 +101,64 @@ public function index(Request $request): View|Response
$tenant = $tenantId ? Tenant::find($tenantId) : null;
$isHQ = $tenantId == 1;
// 모든 code_group 조회 (글로벌 + 테넌트)
$globalGroups = GlobalCategory::whereNull('deleted_at')
->select('code_group')
->distinct()
->pluck('code_group')
// 글로벌 그룹 + description
$globalGroupDescs = GlobalCategory::whereNull('deleted_at')
->selectRaw('code_group, MIN(description) as description')
->groupBy('code_group')
->pluck('description', 'code_group')
->toArray();
$tenantGroups = [];
// 테넌트 그룹 + description
$tenantGroupDescs = [];
if ($tenantId) {
$tenantGroups = Category::where('tenant_id', $tenantId)
$tenantGroupDescs = Category::where('tenant_id', $tenantId)
->whereNull('deleted_at')
->select('code_group')
->distinct()
->pluck('code_group')
->selectRaw('code_group, MIN(description) as description')
->groupBy('code_group')
->pluck('description', 'code_group')
->toArray();
}
$allGroups = array_unique(array_merge($globalGroups, $tenantGroups));
sort($allGroups);
// 커스텀 그룹 라벨 (빈 그룹용 — TenantSetting)
$customLabels = $this->getCustomGroupLabels();
$codeGroupLabels = config('categories.code_group_labels', []);
// config 라벨 (하위호환 폴백)
$configLabels = config('categories.code_group_labels', []);
// 그룹별 스코프 분류
$allGroupKeys = array_unique(array_merge(
array_keys($globalGroupDescs),
array_keys($tenantGroupDescs),
array_keys($customLabels)
));
sort($allGroupKeys);
$codeGroups = [];
foreach ($allGroups as $group) {
$codeGroups[$group] = $codeGroupLabels[$group] ?? $group;
$groupScopes = [];
foreach ($allGroupKeys as $group) {
$inGlobal = isset($globalGroupDescs[$group]);
$inTenant = isset($tenantGroupDescs[$group]);
$codeGroups[$group] = ! empty($globalGroupDescs[$group])
? $globalGroupDescs[$group]
: (! empty($tenantGroupDescs[$group])
? $tenantGroupDescs[$group]
: ($customLabels[$group] ?? $configLabels[$group] ?? $group));
if ($inGlobal && $inTenant) {
$groupScopes[$group] = 'both';
} elseif ($inGlobal) {
$groupScopes[$group] = 'global';
} elseif ($inTenant) {
$groupScopes[$group] = 'tenant';
} else {
$groupScopes[$group] = 'custom';
}
}
if (empty($codeGroups)) {
$codeGroups['product'] = $codeGroupLabels['product'] ?? 'product';
$codeGroups['product'] = $configLabels['product'] ?? 'product';
$groupScopes['product'] = 'custom';
}
$selectedGroup = $request->get('group', array_key_first($codeGroups));
@@ -86,7 +190,8 @@ public function index(Request $request): View|Response
return view('categories.index', [
'codeGroups' => $codeGroups,
'codeGroupLabels' => $codeGroupLabels,
'groupScopes' => $groupScopes,
'codeGroupLabels' => $codeGroups,
'selectedGroup' => $selectedGroup,
'globalCategories' => $globalCategories,
'tenantCategories' => $tenantCategories,
@@ -123,4 +228,4 @@ private function addChildrenRecursive(&$result, $byParent, $parentId, $depth): v
$this->addChildrenRecursive($result, $byParent, $child->id, $depth + 1);
}
}
}
}