- 사원 상세/수정 페이지에 연봉정보 입력 섹션 추가 - 특수 권한 사용자만 열람/수정 가능한 접근 제어 적용 - 연봉 변경 시 자동 이력 기록 - 일반 API 응답에서 연봉 데이터 노출 방지 (toArray 오버라이드)
135 lines
3.8 KiB
PHP
135 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\HR;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\HR\Employee;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class EmployeeSalaryController extends Controller
|
|
{
|
|
private const ALLOWED_SALARY_USERS = ['이의찬', '전진선', '김보곤'];
|
|
|
|
private function checkSalaryAccess(): ?JsonResponse
|
|
{
|
|
if (! in_array(auth()->user()->name, self::ALLOWED_SALARY_USERS)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '연봉 정보는 권한이 있는 관계자만 열람할 수 있습니다.',
|
|
], 403);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 사원 연봉 정보 조회
|
|
*/
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
if ($denied = $this->checkSalaryAccess()) {
|
|
return $denied;
|
|
}
|
|
|
|
$employee = Employee::forTenant()->find($id);
|
|
|
|
if (! $employee) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '사원 정보를 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $employee->getSalaryInfo(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 사원 연봉 정보 저장/수정
|
|
*/
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
if ($denied = $this->checkSalaryAccess()) {
|
|
return $denied;
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'annual_salary' => 'nullable|integer|min:0',
|
|
'effective_date' => 'nullable|date',
|
|
'notes' => 'nullable|string|max:500',
|
|
]);
|
|
|
|
$employee = Employee::forTenant()->find($id);
|
|
|
|
if (! $employee) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '사원 정보를 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
try {
|
|
$employee->setSalaryInfo($validated);
|
|
$employee->save();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '연봉 정보가 저장되었습니다.',
|
|
'data' => $employee->getSalaryInfo(),
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '연봉 정보 저장 중 오류가 발생했습니다.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 연봉 이력 삭제 (특정 인덱스)
|
|
*/
|
|
public function deleteHistory(Request $request, int $id, int $historyIndex): JsonResponse
|
|
{
|
|
if ($denied = $this->checkSalaryAccess()) {
|
|
return $denied;
|
|
}
|
|
|
|
$employee = Employee::forTenant()->find($id);
|
|
|
|
if (! $employee) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '사원 정보를 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
$salaryInfo = $employee->getSalaryInfo();
|
|
$history = $salaryInfo['history'] ?? [];
|
|
|
|
if (! isset($history[$historyIndex])) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '해당 이력을 찾을 수 없습니다.',
|
|
], 404);
|
|
}
|
|
|
|
array_splice($history, $historyIndex, 1);
|
|
$salaryInfo['history'] = $history;
|
|
|
|
$employee->setJsonExtraValue('salary_info', $salaryInfo);
|
|
$employee->save();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '이력이 삭제되었습니다.',
|
|
'data' => $employee->getSalaryInfo(),
|
|
]);
|
|
}
|
|
}
|