749 lines
27 KiB
PHP
749 lines
27 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Products\CommonCode;
|
|
use App\Models\Tenants\Tenant;
|
|
use App\Models\Tenants\TenantSetting;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CommonCodeController extends Controller
|
|
{
|
|
/**
|
|
* 공통코드 관리 페이지
|
|
*/
|
|
public function index(Request $request): View|Response
|
|
{
|
|
// HTMX 요청 시 전체 페이지 리로드
|
|
if ($request->header('HX-Request')) {
|
|
return response('', 200)->header('HX-Redirect', route('common-codes.index'));
|
|
}
|
|
|
|
$tenantId = session('selected_tenant_id');
|
|
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
|
|
// 선택된 코드 그룹 (기본: item_type)
|
|
$selectedGroup = $request->get('group', 'item_type');
|
|
|
|
// 글로벌 그룹 (tenant_id IS NULL)
|
|
$globalGroupDescs = CommonCode::query()
|
|
->whereNull('tenant_id')
|
|
->selectRaw('code_group, MIN(description) as description')
|
|
->groupBy('code_group')
|
|
->pluck('description', 'code_group')
|
|
->toArray();
|
|
|
|
// 테넌트 그룹
|
|
$tenantGroupDescs = [];
|
|
if ($tenantId) {
|
|
$tenantGroupDescs = CommonCode::query()
|
|
->where('tenant_id', $tenantId)
|
|
->selectRaw('code_group, MIN(description) as description')
|
|
->groupBy('code_group')
|
|
->pluck('description', 'code_group')
|
|
->toArray();
|
|
}
|
|
|
|
// 커스텀 그룹 라벨 (빈 그룹용 — TenantSetting)
|
|
$customLabels = $this->getCustomGroupLabels();
|
|
|
|
// 그룹별 스코프 분류
|
|
$allGroupKeys = array_unique(array_merge(
|
|
array_keys($globalGroupDescs),
|
|
array_keys($tenantGroupDescs),
|
|
array_keys($customLabels)
|
|
));
|
|
sort($allGroupKeys);
|
|
|
|
$codeGroups = [];
|
|
$groupScopes = [];
|
|
foreach ($allGroupKeys as $group) {
|
|
$inGlobal = array_key_exists($group, $globalGroupDescs);
|
|
$inTenant = array_key_exists($group, $tenantGroupDescs);
|
|
|
|
// 라벨: 글로벌 description 우선 → 테넌트 → 커스텀 → 키 자체
|
|
$codeGroups[$group] = ! empty($globalGroupDescs[$group])
|
|
? $globalGroupDescs[$group]
|
|
: (! empty($tenantGroupDescs[$group])
|
|
? $tenantGroupDescs[$group]
|
|
: ($customLabels[$group] ?? $group));
|
|
|
|
// 스코프: global, both, tenant, custom
|
|
if ($inGlobal && $inTenant) {
|
|
$groupScopes[$group] = 'both';
|
|
} elseif ($inGlobal) {
|
|
$groupScopes[$group] = 'global';
|
|
} elseif ($inTenant) {
|
|
$groupScopes[$group] = 'tenant';
|
|
} else {
|
|
$groupScopes[$group] = 'custom';
|
|
}
|
|
}
|
|
|
|
// 선택된 그룹이 목록에 없으면 첫 번째 그룹으로 대체
|
|
if (! isset($codeGroups[$selectedGroup]) && ! empty($codeGroups)) {
|
|
$selectedGroup = array_key_first($codeGroups);
|
|
}
|
|
|
|
// 선택된 그룹의 코드 목록
|
|
$globalCodes = collect();
|
|
$tenantCodes = collect();
|
|
|
|
if ($tenantId && isset($codeGroups[$selectedGroup])) {
|
|
// 글로벌 코드 (tenant_id IS NULL)
|
|
$globalCodes = CommonCode::query()
|
|
->whereNull('tenant_id')
|
|
->where('code_group', $selectedGroup)
|
|
->orderBy('sort_order')
|
|
->get();
|
|
|
|
// 테넌트 코드
|
|
$tenantCodes = CommonCode::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('code_group', $selectedGroup)
|
|
->orderBy('sort_order')
|
|
->get();
|
|
}
|
|
|
|
$globalCodeKeys = $globalCodes->pluck('code')->toArray();
|
|
$tenantCodeKeys = $tenantCodes->pluck('code')->toArray();
|
|
|
|
return view('common-codes.index', [
|
|
'tenant' => $tenant,
|
|
'isHQ' => $isHQ,
|
|
'codeGroups' => $codeGroups,
|
|
'groupScopes' => $groupScopes,
|
|
'selectedGroup' => $selectedGroup,
|
|
'globalCodes' => $globalCodes,
|
|
'tenantCodes' => $tenantCodes,
|
|
'globalCodeKeys' => $globalCodeKeys,
|
|
'tenantCodeKeys' => $tenantCodeKeys,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 커스텀 그룹 라벨 조회
|
|
*/
|
|
private function getCustomGroupLabels(): array
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
if (! $tenantId) {
|
|
return [];
|
|
}
|
|
|
|
$setting = TenantSetting::withoutGlobalScopes()
|
|
->where('tenant_id', $tenantId)
|
|
->where('setting_group', 'common_code')
|
|
->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에 이미 존재하는 그룹인지 체크
|
|
$existsInDb = CommonCode::query()
|
|
->where('code_group', $groupCode)
|
|
->exists();
|
|
|
|
if ($existsInDb) {
|
|
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' => 'common_code',
|
|
'setting_key' => 'custom_group_labels',
|
|
],
|
|
[
|
|
'setting_value' => $customLabels,
|
|
'description' => '공통코드 커스텀 그룹 라벨',
|
|
]
|
|
);
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $groupCode])
|
|
->with('success', "'{$groupLabel}' 그룹이 추가되었습니다.");
|
|
}
|
|
|
|
/**
|
|
* 코드 저장 (신규/수정)
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
|
|
|
if (! $tenantId) {
|
|
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
|
|
}
|
|
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
$isGlobal = $request->boolean('is_global');
|
|
|
|
// 글로벌 코드는 HQ만 생성 가능
|
|
if ($isGlobal && ! $isHQ) {
|
|
return redirect()->back()->with('error', '글로벌 코드는 본사만 생성할 수 있습니다.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'code_group' => 'required|string|max:50',
|
|
'code' => 'required|string|max:50',
|
|
'name' => 'required|string|max:100',
|
|
'sort_order' => 'nullable|integer|min:0|max:9999',
|
|
'attributes' => 'nullable|json',
|
|
'is_global' => 'nullable|boolean',
|
|
]);
|
|
|
|
// 중복 체크
|
|
$targetTenantId = $isGlobal ? null : $tenantId;
|
|
$exists = CommonCode::query()
|
|
->where('tenant_id', $targetTenantId)
|
|
->where('code_group', $validated['code_group'])
|
|
->where('code', $validated['code'])
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
return redirect()->back()
|
|
->with('error', '이미 존재하는 코드입니다.')
|
|
->withInput();
|
|
}
|
|
|
|
CommonCode::create([
|
|
'tenant_id' => $targetTenantId,
|
|
'code_group' => $validated['code_group'],
|
|
'code' => $validated['code'],
|
|
'name' => $validated['name'],
|
|
'sort_order' => $validated['sort_order'] ?? 0,
|
|
'attributes' => $validated['attributes'] ? json_decode($validated['attributes'], true) : null,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $validated['code_group']])
|
|
->with('success', '코드가 추가되었습니다.');
|
|
}
|
|
|
|
/**
|
|
* 코드 수정
|
|
*/
|
|
public function update(Request $request, int $id): RedirectResponse|JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
|
|
|
if (! $tenantId) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '테넌트를 먼저 선택해주세요.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
|
|
}
|
|
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
|
|
$code = CommonCode::find($id);
|
|
if (! $code) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '코드를 찾을 수 없습니다.'], 404);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '코드를 찾을 수 없습니다.');
|
|
}
|
|
|
|
// 권한 체크: 슈퍼관리자는 모든 코드 수정 가능
|
|
$isSuperAdmin = auth()->user()?->isSuperAdmin() ?? false;
|
|
|
|
if (! $isSuperAdmin) {
|
|
// 글로벌 코드는 HQ만
|
|
if ($code->tenant_id === null && ! $isHQ) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '글로벌 코드는 본사만 수정할 수 있습니다.'], 403);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '글로벌 코드는 본사만 수정할 수 있습니다.');
|
|
}
|
|
|
|
// 테넌트 코드는 해당 테넌트만
|
|
if ($code->tenant_id !== null && $code->tenant_id !== $tenantId) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '다른 테넌트의 코드는 수정할 수 없습니다.'], 403);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '다른 테넌트의 코드는 수정할 수 없습니다.');
|
|
}
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'sometimes|required|string|max:100',
|
|
'sort_order' => 'sometimes|nullable|integer|min:0|max:9999',
|
|
'attributes' => 'sometimes|nullable|json',
|
|
'is_active' => 'sometimes|boolean',
|
|
]);
|
|
|
|
// 필드별 업데이트
|
|
if (isset($validated['name'])) {
|
|
$code->name = $validated['name'];
|
|
}
|
|
if (array_key_exists('sort_order', $validated)) {
|
|
$code->sort_order = $validated['sort_order'] ?? 0;
|
|
}
|
|
if (array_key_exists('attributes', $validated)) {
|
|
$code->attributes = $validated['attributes'] ? json_decode($validated['attributes'], true) : null;
|
|
}
|
|
if (isset($validated['is_active'])) {
|
|
$code->is_active = $validated['is_active'];
|
|
}
|
|
|
|
$code->save();
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => '수정되었습니다.']);
|
|
}
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $code->code_group])
|
|
->with('success', '코드가 수정되었습니다.');
|
|
}
|
|
|
|
/**
|
|
* 활성화 토글 (AJAX)
|
|
*/
|
|
public function toggle(Request $request, int $id): JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
|
|
|
if (! $tenantId) {
|
|
return response()->json(['error' => '테넌트를 먼저 선택해주세요.'], 400);
|
|
}
|
|
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
|
|
$code = CommonCode::find($id);
|
|
if (! $code) {
|
|
return response()->json(['error' => '코드를 찾을 수 없습니다.'], 404);
|
|
}
|
|
|
|
// 권한 체크: 슈퍼관리자는 모든 코드 수정 가능
|
|
$isSuperAdmin = auth()->user()?->isSuperAdmin() ?? false;
|
|
|
|
if (! $isSuperAdmin) {
|
|
if ($code->tenant_id === null && ! $isHQ) {
|
|
return response()->json(['error' => '글로벌 코드는 본사만 수정할 수 있습니다.'], 403);
|
|
}
|
|
|
|
if ($code->tenant_id !== null && $code->tenant_id !== $tenantId) {
|
|
return response()->json(['error' => '다른 테넌트의 코드는 수정할 수 없습니다.'], 403);
|
|
}
|
|
}
|
|
|
|
$code->is_active = ! $code->is_active;
|
|
$code->save();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'is_active' => $code->is_active,
|
|
'message' => $code->is_active ? '활성화되었습니다.' : '비활성화되었습니다.',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 테넌트 코드를 글로벌로 일괄 복사 (HQ 또는 슈퍼관리자)
|
|
*/
|
|
public function bulkPromoteToGlobal(Request $request): RedirectResponse|JsonResponse
|
|
{
|
|
$tenant = session('selected_tenant_id') ? Tenant::find(session('selected_tenant_id')) : null;
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
$isSuperAdmin = auth()->user()?->isSuperAdmin() ?? false;
|
|
|
|
if (! $isHQ && ! $isSuperAdmin) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '본사 또는 슈퍼관리자만 글로벌로 복사할 수 있습니다.'], 403);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '본사 또는 슈퍼관리자만 글로벌로 복사할 수 있습니다.');
|
|
}
|
|
|
|
$idsJson = $request->input('ids_json');
|
|
if ($idsJson) {
|
|
$ids = json_decode($idsJson, true);
|
|
if (! is_array($ids) || empty($ids)) {
|
|
return redirect()->back()->with('error', '복사할 코드를 선택해주세요.');
|
|
}
|
|
} else {
|
|
$validated = $request->validate(['ids' => 'required|array|min:1', 'ids.*' => 'integer']);
|
|
$ids = $validated['ids'];
|
|
}
|
|
|
|
$codeGroup = null;
|
|
$copiedCount = 0;
|
|
$skippedCount = 0;
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach ($ids as $id) {
|
|
$tenantCode = CommonCode::whereNotNull('tenant_id')->find($id);
|
|
if (! $tenantCode) {
|
|
continue;
|
|
}
|
|
|
|
$codeGroup = $tenantCode->code_group;
|
|
|
|
$exists = CommonCode::query()
|
|
->whereNull('tenant_id')
|
|
->where('code_group', $tenantCode->code_group)
|
|
->where('code', $tenantCode->code)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
$skippedCount++;
|
|
|
|
continue;
|
|
}
|
|
|
|
CommonCode::create([
|
|
'tenant_id' => null,
|
|
'code_group' => $tenantCode->code_group,
|
|
'code' => $tenantCode->code,
|
|
'name' => $tenantCode->name,
|
|
'sort_order' => $tenantCode->sort_order,
|
|
'attributes' => $tenantCode->attributes,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$copiedCount++;
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
return redirect()->back()->with('error', '복사 중 오류가 발생했습니다.');
|
|
}
|
|
|
|
$message = "{$copiedCount}개 코드가 글로벌로 복사되었습니다.";
|
|
if ($skippedCount > 0) {
|
|
$message .= " ({$skippedCount}개는 이미 존재하여 건너뜀)";
|
|
}
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $codeGroup ?? 'item_type'])
|
|
->with('success', $message);
|
|
}
|
|
|
|
/**
|
|
* 글로벌 코드를 테넌트용으로 복사
|
|
*/
|
|
public function copy(Request $request, int $id): RedirectResponse|JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
|
|
if (! $tenantId) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '테넌트를 먼저 선택해주세요.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
|
|
}
|
|
|
|
$globalCode = CommonCode::whereNull('tenant_id')->find($id);
|
|
if (! $globalCode) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '글로벌 코드를 찾을 수 없습니다.'], 404);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '글로벌 코드를 찾을 수 없습니다.');
|
|
}
|
|
|
|
// 이미 복사된 코드가 있는지 확인
|
|
$exists = CommonCode::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('code_group', $globalCode->code_group)
|
|
->where('code', $globalCode->code)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '이미 복사된 코드가 있습니다.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '이미 복사된 코드가 있습니다.');
|
|
}
|
|
|
|
// 복사
|
|
CommonCode::create([
|
|
'tenant_id' => $tenantId,
|
|
'code_group' => $globalCode->code_group,
|
|
'code' => $globalCode->code,
|
|
'name' => $globalCode->name,
|
|
'sort_order' => $globalCode->sort_order,
|
|
'attributes' => $globalCode->attributes,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => '코드가 복사되었습니다.']);
|
|
}
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $globalCode->code_group])
|
|
->with('success', '글로벌 코드가 테넌트용으로 복사되었습니다.');
|
|
}
|
|
|
|
/**
|
|
* 글로벌 코드를 테넌트용으로 일괄 복사
|
|
*/
|
|
public function bulkCopy(Request $request): RedirectResponse|JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
|
|
if (! $tenantId) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '테넌트를 먼저 선택해주세요.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
|
|
}
|
|
|
|
// JSON 문자열로 받은 경우 처리
|
|
$idsJson = $request->input('ids_json');
|
|
if ($idsJson) {
|
|
$ids = json_decode($idsJson, true);
|
|
if (! is_array($ids) || empty($ids)) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '복사할 코드를 선택해주세요.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '복사할 코드를 선택해주세요.');
|
|
}
|
|
} else {
|
|
$validated = $request->validate([
|
|
'ids' => 'required|array|min:1',
|
|
'ids.*' => 'integer',
|
|
]);
|
|
$ids = $validated['ids'];
|
|
}
|
|
$codeGroup = null;
|
|
$copiedCount = 0;
|
|
$skippedCount = 0;
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
foreach ($ids as $id) {
|
|
$globalCode = CommonCode::whereNull('tenant_id')->find($id);
|
|
if (! $globalCode) {
|
|
continue;
|
|
}
|
|
|
|
$codeGroup = $globalCode->code_group;
|
|
|
|
// 이미 복사된 코드가 있는지 확인
|
|
$exists = CommonCode::query()
|
|
->where('tenant_id', $tenantId)
|
|
->where('code_group', $globalCode->code_group)
|
|
->where('code', $globalCode->code)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
$skippedCount++;
|
|
|
|
continue;
|
|
}
|
|
|
|
// 복사
|
|
CommonCode::create([
|
|
'tenant_id' => $tenantId,
|
|
'code_group' => $globalCode->code_group,
|
|
'code' => $globalCode->code,
|
|
'name' => $globalCode->name,
|
|
'sort_order' => $globalCode->sort_order,
|
|
'attributes' => $globalCode->attributes,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$copiedCount++;
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '복사 중 오류가 발생했습니다.'], 500);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '복사 중 오류가 발생했습니다.');
|
|
}
|
|
|
|
$message = "{$copiedCount}개 코드가 복사되었습니다.";
|
|
if ($skippedCount > 0) {
|
|
$message .= " ({$skippedCount}개는 이미 존재하여 건너뜀)";
|
|
}
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => $message, 'copied' => $copiedCount, 'skipped' => $skippedCount]);
|
|
}
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $codeGroup ?? 'item_type'])
|
|
->with('success', $message);
|
|
}
|
|
|
|
/**
|
|
* 테넌트 코드를 글로벌로 복사 (HQ 또는 슈퍼관리자)
|
|
*/
|
|
public function promoteToGlobal(Request $request, int $id): RedirectResponse|JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
$isSuperAdmin = auth()->user()?->isSuperAdmin() ?? false;
|
|
|
|
if (! $isHQ && ! $isSuperAdmin) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '본사 또는 슈퍼관리자만 글로벌로 복사할 수 있습니다.'], 403);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '본사 또는 슈퍼관리자만 글로벌로 복사할 수 있습니다.');
|
|
}
|
|
|
|
$tenantCode = CommonCode::whereNotNull('tenant_id')->find($id);
|
|
if (! $tenantCode) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '테넌트 코드를 찾을 수 없습니다.'], 404);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '테넌트 코드를 찾을 수 없습니다.');
|
|
}
|
|
|
|
// 이미 글로벌에 동일 코드가 있는지 확인
|
|
$exists = CommonCode::query()
|
|
->whereNull('tenant_id')
|
|
->where('code_group', $tenantCode->code_group)
|
|
->where('code', $tenantCode->code)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '이미 동일한 글로벌 코드가 존재합니다.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '이미 동일한 글로벌 코드가 존재합니다.');
|
|
}
|
|
|
|
CommonCode::create([
|
|
'tenant_id' => null,
|
|
'code_group' => $tenantCode->code_group,
|
|
'code' => $tenantCode->code,
|
|
'name' => $tenantCode->name,
|
|
'sort_order' => $tenantCode->sort_order,
|
|
'attributes' => $tenantCode->attributes,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => '글로벌 코드로 복사되었습니다.']);
|
|
}
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $tenantCode->code_group])
|
|
->with('success', '테넌트 코드가 글로벌로 복사되었습니다.');
|
|
}
|
|
|
|
/**
|
|
* 코드 삭제 (테넌트 코드만)
|
|
*/
|
|
public function destroy(Request $request, int $id): RedirectResponse|JsonResponse
|
|
{
|
|
$tenantId = session('selected_tenant_id');
|
|
$tenant = $tenantId ? Tenant::find($tenantId) : null;
|
|
|
|
if (! $tenantId) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '테넌트를 먼저 선택해주세요.'], 400);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '테넌트를 먼저 선택해주세요.');
|
|
}
|
|
|
|
$isHQ = $tenant?->tenant_type === 'HQ';
|
|
|
|
$code = CommonCode::find($id);
|
|
if (! $code) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '코드를 찾을 수 없습니다.'], 404);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '코드를 찾을 수 없습니다.');
|
|
}
|
|
|
|
// 권한 체크: 슈퍼관리자는 모든 코드 삭제 가능
|
|
$isSuperAdmin = auth()->user()?->isSuperAdmin() ?? false;
|
|
|
|
if (! $isSuperAdmin) {
|
|
// 글로벌 코드 삭제는 HQ만
|
|
if ($code->tenant_id === null && ! $isHQ) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '글로벌 코드는 본사만 삭제할 수 있습니다.'], 403);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '글로벌 코드는 본사만 삭제할 수 있습니다.');
|
|
}
|
|
|
|
// 다른 테넌트 코드 삭제 불가
|
|
if ($code->tenant_id !== null && $code->tenant_id !== $tenantId) {
|
|
if ($request->ajax()) {
|
|
return response()->json(['error' => '다른 테넌트의 코드는 삭제할 수 없습니다.'], 403);
|
|
}
|
|
|
|
return redirect()->back()->with('error', '다른 테넌트의 코드는 삭제할 수 없습니다.');
|
|
}
|
|
}
|
|
|
|
$codeGroup = $code->code_group;
|
|
$code->delete();
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => '코드가 삭제되었습니다.']);
|
|
}
|
|
|
|
return redirect()
|
|
->route('common-codes.index', ['group' => $codeGroup])
|
|
->with('success', '코드가 삭제되었습니다.');
|
|
}
|
|
}
|