From a053365ed5a74996df3f266d054fc80dbae08bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Fri, 13 Mar 2026 17:11:10 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[finance]=20=EB=AF=B8=EC=A7=80=EA=B8=89?= =?UTF-8?q?=EA=B8=88=20=EA=B4=80=EB=A6=AC=20=ED=94=84=EB=A6=AC=EB=9E=9C?= =?UTF-8?q?=EC=84=9C=20=EC=9E=94=EC=95=A1=20=EB=A7=88=EC=9D=B4=EB=84=88?= =?UTF-8?q?=EC=8A=A4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 중복 journal_entry_lines 탐지 및 삭제 마이그레이션 - 동일 전표 내 같은 계정/거래처/금액의 중복 라인 정리 - 삭제 후 전표 차대 합계 재계산 --- ...0000_fix_duplicate_journal_entry_lines.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 database/migrations/2026_03_13_100000_fix_duplicate_journal_entry_lines.php diff --git a/database/migrations/2026_03_13_100000_fix_duplicate_journal_entry_lines.php b/database/migrations/2026_03_13_100000_fix_duplicate_journal_entry_lines.php new file mode 100644 index 00000000..ef327c6b --- /dev/null +++ b/database/migrations/2026_03_13_100000_fix_duplicate_journal_entry_lines.php @@ -0,0 +1,92 @@ + 1 + "); + + if (empty($duplicates)) { + Log::info('[Migration] 중복 journal_entry_lines 없음 - 정리 불필요'); + + return; + } + + $totalDeleted = 0; + + foreach ($duplicates as $dup) { + $allIds = explode(',', $dup->all_ids); + $keepId = $dup->keep_id; + $deleteIds = array_filter($allIds, fn ($id) => (int) $id !== (int) $keepId); + + if (! empty($deleteIds)) { + Log::info("[Migration] 중복 라인 삭제: journal_entry_id={$dup->journal_entry_id}, " + ."account_code={$dup->account_code}, dc_type={$dup->dc_type}, " + ."partner={$dup->trading_partner_name}, " + ."keep_id={$keepId}, delete_ids=".implode(',', $deleteIds)); + + $deleted = DB::table('journal_entry_lines') + ->whereIn('id', $deleteIds) + ->delete(); + + $totalDeleted += $deleted; + } + } + + Log::info("[Migration] 중복 journal_entry_lines 정리 완료: {$totalDeleted}건 삭제"); + + // 2. 삭제 후 전표 합계 재계산 + if ($totalDeleted > 0) { + $affectedEntryIds = collect($duplicates)->pluck('journal_entry_id')->unique(); + + foreach ($affectedEntryIds as $entryId) { + $sums = DB::selectOne(" + SELECT COALESCE(SUM(debit_amount), 0) as total_debit, + COALESCE(SUM(credit_amount), 0) as total_credit + FROM journal_entry_lines + WHERE journal_entry_id = ? + ", [$entryId]); + + DB::table('journal_entries') + ->where('id', $entryId) + ->update([ + 'total_debit' => $sums->total_debit, + 'total_credit' => $sums->total_credit, + ]); + + Log::info("[Migration] 전표 합계 재계산: id={$entryId}, " + ."debit={$sums->total_debit}, credit={$sums->total_credit}"); + } + } + } + + /** + * 데이터 정리 마이그레이션이므로 rollback 불가 + */ + public function down(): void + { + Log::warning('[Migration] 중복 라인 삭제는 롤백할 수 없습니다.'); + } +};