From 8309d457b5ce280166c6f32d5e11d0fccc105c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Fri, 27 Feb 2026 09:48:16 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[payroll]=20=EA=B8=89=EC=97=AC=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20500=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 중복 급여 등록 시 유니크 제약 위반 대신 422 응답 반환 - tenant_id null 방어 처리 (세션 값이 null인 경우 기본값 적용) --- .../Controllers/Api/Admin/HR/PayrollController.php | 5 +++++ app/Models/HR/PayrollSetting.php | 4 ++-- app/Services/HR/PayrollService.php | 13 +++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/Admin/HR/PayrollController.php b/app/Http/Controllers/Api/Admin/HR/PayrollController.php index c4aad48b..1d9dd834 100644 --- a/app/Http/Controllers/Api/Admin/HR/PayrollController.php +++ b/app/Http/Controllers/Api/Admin/HR/PayrollController.php @@ -91,6 +91,11 @@ public function store(Request $request): JsonResponse 'message' => '급여가 등록되었습니다.', 'data' => $payroll, ], 201); + } catch (\InvalidArgumentException $e) { + return response()->json([ + 'success' => false, + 'message' => $e->getMessage(), + ], 422); } catch (\Throwable $e) { report($e); diff --git a/app/Models/HR/PayrollSetting.php b/app/Models/HR/PayrollSetting.php index 30dba1cc..f2871aa8 100644 --- a/app/Models/HR/PayrollSetting.php +++ b/app/Models/HR/PayrollSetting.php @@ -62,7 +62,7 @@ class PayrollSetting extends Model public function scopeForTenant($query, ?int $tenantId = null) { - $tenantId = $tenantId ?? session('selected_tenant_id', 1); + $tenantId = $tenantId ?? session('selected_tenant_id') ?? 1; return $query->where('tenant_id', $tenantId); } @@ -73,7 +73,7 @@ public function scopeForTenant($query, ?int $tenantId = null) public static function getOrCreate(?int $tenantId = null): self { - $tenantId = $tenantId ?? session('selected_tenant_id', 1); + $tenantId = $tenantId ?? session('selected_tenant_id') ?? 1; return self::firstOrCreate( ['tenant_id' => $tenantId], diff --git a/app/Services/HR/PayrollService.php b/app/Services/HR/PayrollService.php index dda7f4ac..c145badd 100644 --- a/app/Services/HR/PayrollService.php +++ b/app/Services/HR/PayrollService.php @@ -136,6 +136,19 @@ public function storePayroll(array $data): Payroll { $tenantId = session('selected_tenant_id', 1); + // 동일 대상/기간 중복 체크 + $exists = Payroll::where('tenant_id', $tenantId) + ->where('user_id', $data['user_id']) + ->where('pay_year', $data['pay_year']) + ->where('pay_month', $data['pay_month']) + ->exists(); + + if ($exists) { + throw new \InvalidArgumentException( + "해당 직원의 {$data['pay_year']}년 {$data['pay_month']}월 급여가 이미 등록되어 있습니다." + ); + } + return DB::transaction(function () use ($data, $tenantId) { $calculated = $this->calculateAmounts($data);