fix: [payroll] 전월 복사/일괄 생성 시 SoftDeletes 유니크 제약 충돌 수정
- exists() 대신 withTrashed()->first()로 soft-deleted 레코드 포함 체크 - soft-deleted 레코드 존재 시 forceDelete() 후 재생성 - copyFromPreviousMonth(), bulkGenerate() 양쪽 동일 수정
This commit is contained in:
@@ -327,18 +327,24 @@ public function copyFromPreviousMonth(int $year, int $month): array
|
||||
|
||||
DB::transaction(function () use ($previousPayrolls, $tenantId, $year, $month, &$created, &$skipped) {
|
||||
foreach ($previousPayrolls as $prev) {
|
||||
$exists = Payroll::query()
|
||||
// SoftDeletes 포함하여 유니크 제약 충돌 방지
|
||||
$existing = Payroll::withTrashed()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('user_id', $prev->user_id)
|
||||
->forPeriod($year, $month)
|
||||
->exists();
|
||||
->first();
|
||||
|
||||
if ($exists) {
|
||||
if ($existing && ! $existing->trashed()) {
|
||||
$skipped++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// soft-deleted 레코드가 있으면 삭제 후 재생성
|
||||
if ($existing && $existing->trashed()) {
|
||||
$existing->forceDelete();
|
||||
}
|
||||
|
||||
Payroll::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'user_id' => $prev->user_id,
|
||||
@@ -388,18 +394,24 @@ public function bulkGenerate(int $year, int $month): array
|
||||
|
||||
DB::transaction(function () use ($employees, $tenantId, $year, $month, $settings, &$created, &$skipped) {
|
||||
foreach ($employees as $employee) {
|
||||
$exists = Payroll::query()
|
||||
// SoftDeletes 포함하여 유니크 제약 충돌 방지
|
||||
$existing = Payroll::withTrashed()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('user_id', $employee->user_id)
|
||||
->forPeriod($year, $month)
|
||||
->exists();
|
||||
->first();
|
||||
|
||||
if ($exists) {
|
||||
if ($existing && ! $existing->trashed()) {
|
||||
$skipped++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// soft-deleted 레코드가 있으면 삭제 후 재생성
|
||||
if ($existing && $existing->trashed()) {
|
||||
$existing->forceDelete();
|
||||
}
|
||||
|
||||
$annualSalary = $employee->getJsonExtraValue('salary', 0);
|
||||
$baseSalary = $annualSalary > 0 ? round($annualSalary / 12) : 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user