diff --git a/app/Http/Controllers/Finance/AccountLedgerController.php b/app/Http/Controllers/Finance/AccountLedgerController.php index a3e9513b..32ec071f 100644 --- a/app/Http/Controllers/Finance/AccountLedgerController.php +++ b/app/Http/Controllers/Finance/AccountLedgerController.php @@ -76,21 +76,44 @@ public function list(Request $request): JsonResponse ->orderBy('je.entry_date') ->get(); - // 분리 전표가 있는 원본 카드 전표 제외 (중복 방지) - $splitBaseKeys = $allLines - ->filter(fn ($l) => $l->source_type === 'ecard_transaction' && $l->source_key && str_contains($l->source_key, '|split:')) - ->map(fn ($l) => explode('|split:', $l->source_key)[0]) - ->unique() - ->all(); + // 유효하지 않은 분리 전표 + 원본 전표 중복 제거 + $ecardSplitLines = $allLines->filter( + fn ($l) => $l->source_type === 'ecard_transaction' && $l->source_key && str_contains($l->source_key, '|split:') + ); - if (! empty($splitBaseKeys)) { - $allLines = $allLines->filter(function ($l) use ($splitBaseKeys) { + if ($ecardSplitLines->isNotEmpty()) { + // 현재 유효한 split ID 조회 + $splitBaseKeys = $ecardSplitLines + ->map(fn ($l) => explode('|split:', $l->source_key)[0]) + ->unique() + ->all(); + + $validSplitIds = DB::table('barobill_card_transaction_splits') + ->where('tenant_id', $tenantId) + ->whereIn('original_unique_key', $splitBaseKeys) + ->pluck('id') + ->all(); + + $validSplitSuffixes = array_map(fn ($id) => '|split:'.$id, $validSplitIds); + + $allLines = $allLines->filter(function ($l) use ($splitBaseKeys, $validSplitSuffixes) { if ($l->source_type !== 'ecard_transaction' || ! $l->source_key) { return true; } - // 분리 전표가 존재하면 원본(non-split) 전표는 제외 - return str_contains($l->source_key, '|split:') || ! in_array($l->source_key, $splitBaseKeys); + if (str_contains($l->source_key, '|split:')) { + // 분리 전표: 현재 유효한 split ID만 유지 + foreach ($validSplitSuffixes as $suffix) { + if (str_ends_with($l->source_key, $suffix)) { + return true; + } + } + + return false; + } + + // 원본(non-split) 전표: 분리 전표가 존재하면 제외 + return ! in_array($l->source_key, $splitBaseKeys); })->values(); } diff --git a/app/Models/Barobill/CardTransactionSplit.php b/app/Models/Barobill/CardTransactionSplit.php index e7e731cc..46863c26 100644 --- a/app/Models/Barobill/CardTransactionSplit.php +++ b/app/Models/Barobill/CardTransactionSplit.php @@ -92,7 +92,20 @@ public static function getByUniqueKey(int $tenantId, string $uniqueKey): \Illumi */ public static function saveSplits(int $tenantId, string $uniqueKey, array $originalData, array $splits): void { - // 기존 분개 삭제 + // 기존 split의 분개 전표도 삭제 (재분리 시 이전 분개 잔존 방지) + $oldSplitIds = self::where('tenant_id', $tenantId) + ->where('original_unique_key', $uniqueKey) + ->pluck('id'); + + if ($oldSplitIds->isNotEmpty()) { + $oldSourceKeys = $oldSplitIds->map(fn ($id) => $uniqueKey.'|split:'.$id)->all(); + \App\Models\Finance\JournalEntry::where('tenant_id', $tenantId) + ->where('source_type', 'ecard_transaction') + ->whereIn('source_key', $oldSourceKeys) + ->delete(); + } + + // 기존 분리 삭제 self::where('tenant_id', $tenantId) ->where('original_unique_key', $uniqueKey) ->delete();