From af1788024685ab31ac0885f8bb3746820d7e3a45 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:42:58 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[payroll]=20tenant=5Fid=20null=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - session('selected_tenant_id')에 기본값 1 추가 - PayrollSetting::getOrCreate, scopeForTenant 수정 - PayrollService 전체 tenant_id 조회에 기본값 적용 - Payroll 모델 scopeForTenant 동일 패턴 적용 --- app/Models/HR/Payroll.php | 7 ++----- app/Models/HR/PayrollSetting.php | 9 +++------ app/Services/HR/PayrollService.php | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/Models/HR/Payroll.php b/app/Models/HR/Payroll.php index 13865469..8351587b 100644 --- a/app/Models/HR/Payroll.php +++ b/app/Models/HR/Payroll.php @@ -155,12 +155,9 @@ public function isDeletable(): bool public function scopeForTenant($query, ?int $tenantId = null) { - $tenantId = $tenantId ?? session('selected_tenant_id'); - if ($tenantId) { - return $query->where($this->table.'.tenant_id', $tenantId); - } + $tenantId = $tenantId ?? session('selected_tenant_id', 1); - return $query; + return $query->where($this->table.'.tenant_id', $tenantId); } public function scopeForPeriod($query, int $year, int $month) diff --git a/app/Models/HR/PayrollSetting.php b/app/Models/HR/PayrollSetting.php index 6e0f4a06..30dba1cc 100644 --- a/app/Models/HR/PayrollSetting.php +++ b/app/Models/HR/PayrollSetting.php @@ -62,12 +62,9 @@ class PayrollSetting extends Model public function scopeForTenant($query, ?int $tenantId = null) { - $tenantId = $tenantId ?? session('selected_tenant_id'); - if ($tenantId) { - return $query->where('tenant_id', $tenantId); - } + $tenantId = $tenantId ?? session('selected_tenant_id', 1); - return $query; + return $query->where('tenant_id', $tenantId); } // ========================================================================= @@ -76,7 +73,7 @@ public function scopeForTenant($query, ?int $tenantId = null) public static function getOrCreate(?int $tenantId = null): self { - $tenantId = $tenantId ?? session('selected_tenant_id'); + $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 a6be8a8c..dda7f4ac 100644 --- a/app/Services/HR/PayrollService.php +++ b/app/Services/HR/PayrollService.php @@ -46,7 +46,7 @@ class PayrollService */ private function buildFilteredQuery(array $filters = []) { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $query = Payroll::query() ->with(['user', 'user.tenantProfiles' => fn ($q) => $q->where('tenant_id', $tenantId), 'user.tenantProfiles.department']) @@ -98,7 +98,7 @@ public function getExportData(array $filters = []): Collection */ public function getMonthlyStats(?int $year = null, ?int $month = null): array { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $year = $year ?? now()->year; $month = $month ?? now()->month; @@ -134,7 +134,7 @@ public function getMonthlyStats(?int $year = null, ?int $month = null): array */ public function storePayroll(array $data): Payroll { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); return DB::transaction(function () use ($data, $tenantId) { $calculated = $this->calculateAmounts($data); @@ -172,7 +172,7 @@ public function storePayroll(array $data): Payroll */ public function updatePayroll(int $id, array $data): ?Payroll { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $payroll = Payroll::query() ->forTenant($tenantId) @@ -211,7 +211,7 @@ public function updatePayroll(int $id, array $data): ?Payroll */ public function deletePayroll(int $id): bool { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $payroll = Payroll::query() ->forTenant($tenantId) @@ -232,7 +232,7 @@ public function deletePayroll(int $id): bool */ public function confirmPayroll(int $id): ?Payroll { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $payroll = Payroll::query() ->forTenant($tenantId) @@ -257,7 +257,7 @@ public function confirmPayroll(int $id): ?Payroll */ public function payPayroll(int $id): ?Payroll { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $payroll = Payroll::query() ->forTenant($tenantId) @@ -281,7 +281,7 @@ public function payPayroll(int $id): ?Payroll */ public function bulkGenerate(int $year, int $month): array { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); $settings = PayrollSetting::getOrCreate($tenantId); $created = 0; $skipped = 0; @@ -468,7 +468,7 @@ private function calculateEmploymentInsurance(float $grossSalary, PayrollSetting */ public function getDepartments(): Collection { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); return Department::query() ->where('is_active', true) @@ -483,7 +483,7 @@ public function getDepartments(): Collection */ public function getActiveEmployees(): Collection { - $tenantId = session('selected_tenant_id'); + $tenantId = session('selected_tenant_id', 1); return Employee::query() ->with('user:id,name')