feat:테넌트설정, 공통코드관리, 메뉴동기화 기능 추가
- 테넌트 설정 페이지 (재고관리 품목유형 등) - 공통코드 관리 페이지 (글로벌/테넌트별 코드 관리) - 메뉴 동기화 기능 (로컬↔개발↔운영 환경간 메뉴 Push/Pull)
This commit is contained in:
373
app/Http/Controllers/CommonCodeController.php
Normal file
373
app/Http/Controllers/CommonCodeController.php
Normal file
@@ -0,0 +1,373 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Products\CommonCode;
|
||||
use App\Models\Tenants\Tenant;
|
||||
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\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CommonCodeController extends Controller
|
||||
{
|
||||
/**
|
||||
* 코드 그룹 라벨
|
||||
*/
|
||||
private const CODE_GROUP_LABELS = [
|
||||
'item_type' => '품목유형',
|
||||
'material_type' => '자재유형',
|
||||
'client_type' => '거래처유형',
|
||||
'order_status' => '주문상태',
|
||||
'order_type' => '주문유형',
|
||||
'delivery_method' => '배송방법',
|
||||
'tenant_type' => '테넌트유형',
|
||||
'product_category' => '제품분류',
|
||||
'motor_type' => '모터유형',
|
||||
'controller_type' => '컨트롤러유형',
|
||||
'painting_type' => '도장유형',
|
||||
'position_type' => '위치유형',
|
||||
'capability_profile' => '생산능력',
|
||||
'bad_debt_progress' => '대손진행',
|
||||
'height_construction_cost' => '높이시공비',
|
||||
'width_construction_cost' => '폭시공비',
|
||||
];
|
||||
|
||||
/**
|
||||
* 공통코드 관리 페이지
|
||||
*/
|
||||
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');
|
||||
|
||||
// 코드 그룹 목록 (실제 존재하는 그룹만)
|
||||
$existingGroups = CommonCode::query()
|
||||
->select('code_group')
|
||||
->distinct()
|
||||
->pluck('code_group')
|
||||
->toArray();
|
||||
|
||||
$codeGroups = collect(self::CODE_GROUP_LABELS)
|
||||
->filter(fn($label, $group) => in_array($group, $existingGroups))
|
||||
->toArray();
|
||||
|
||||
// 선택된 그룹의 코드 목록
|
||||
$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();
|
||||
}
|
||||
|
||||
return view('common-codes.index', [
|
||||
'tenant' => $tenant,
|
||||
'isHQ' => $isHQ,
|
||||
'codeGroups' => $codeGroups,
|
||||
'selectedGroup' => $selectedGroup,
|
||||
'globalCodes' => $globalCodes,
|
||||
'tenantCodes' => $tenantCodes,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 코드 저장 (신규/수정)
|
||||
*/
|
||||
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', '코드를 찾을 수 없습니다.');
|
||||
}
|
||||
|
||||
// 권한 체크: 글로벌 코드는 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);
|
||||
}
|
||||
|
||||
// 권한 체크
|
||||
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 ? '활성화되었습니다.' : '비활성화되었습니다.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 글로벌 코드를 테넌트용으로 복사
|
||||
*/
|
||||
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 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', '코드를 찾을 수 없습니다.');
|
||||
}
|
||||
|
||||
// 글로벌 코드 삭제는 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', '코드가 삭제되었습니다.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user