140 lines
4.1 KiB
PHP
140 lines
4.1 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
|
|
{
|
|
$isAllowedUser = in_array(auth()->user()->name, self::ALLOWED_SALARY_USERS);
|
|
$isDevSuperAdmin = ! app()->environment('production') && auth()->user()->isSuperAdmin();
|
|
|
|
if (! $isAllowedUser && ! $isDevSuperAdmin) {
|
|
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',
|
|
'meal_allowance' => 'nullable|integer|min:0|max:1000000',
|
|
'fixed_overtime_hours' => 'nullable|integer|min:0|max:52',
|
|
'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(),
|
|
]);
|
|
}
|
|
}
|