diff --git a/app/Http/Controllers/Barobill/EaccountController.php b/app/Http/Controllers/Barobill/EaccountController.php index 76acee2e..fdd83eb7 100644 --- a/app/Http/Controllers/Barobill/EaccountController.php +++ b/app/Http/Controllers/Barobill/EaccountController.php @@ -397,7 +397,8 @@ public function transactions(Request $request): JsonResponse // 수동 입력 건 병합 (중복 제거: 수동 거래와 동일한 API 거래는 제외) $manualLogs = $this->convertManualToLogs($manualTransactions); - $mergedLogs = $this->mergeWithDedup($logs['logs'], $manualLogs['logs']); + $mergeResult = $this->mergeWithDedup($logs['logs'], $manualLogs['logs']); + $mergedLogs = $mergeResult['logs']; // 날짜/시간 기준 정렬 (최신순) usort($mergedLogs, function ($a, $b) { @@ -410,10 +411,10 @@ public function transactions(Request $request): JsonResponse $baseBalance = $this->findBaseBalance($tenantId, $startDate, $bankAccountNum); $mergedLogs = $this->recalcManualBalances($mergedLogs, $baseBalance); - // summary 합산 (중복 제거 후 count 재계산) + // summary 합산 (중복 제거된 API 거래 금액 차감) $mergedSummary = [ - 'totalDeposit' => $logs['summary']['totalDeposit'] + $manualLogs['summary']['totalDeposit'], - 'totalWithdraw' => $logs['summary']['totalWithdraw'] + $manualLogs['summary']['totalWithdraw'], + 'totalDeposit' => $logs['summary']['totalDeposit'] + $manualLogs['summary']['totalDeposit'] - $mergeResult['removedDeposit'], + 'totalWithdraw' => $logs['summary']['totalWithdraw'] + $manualLogs['summary']['totalWithdraw'] - $mergeResult['removedWithdraw'], 'count' => count($mergedLogs), ]; @@ -502,9 +503,10 @@ private function getAllAccountsTransactions(string $userId, string $startDate, s // 수동 입력 건 병합 (중복 제거: 수동 거래와 동일한 API 거래는 제외) if ($manualTransactions && $manualTransactions->isNotEmpty()) { $manualLogs = $this->convertManualToLogs($manualTransactions); - $allLogs = $this->mergeWithDedup($allLogs, $manualLogs['logs']); - $totalDeposit += $manualLogs['summary']['totalDeposit']; - $totalWithdraw += $manualLogs['summary']['totalWithdraw']; + $mergeResult = $this->mergeWithDedup($allLogs, $manualLogs['logs']); + $allLogs = $mergeResult['logs']; + $totalDeposit += $manualLogs['summary']['totalDeposit'] - $mergeResult['removedDeposit']; + $totalWithdraw += $manualLogs['summary']['totalWithdraw'] - $mergeResult['removedWithdraw']; } // 날짜/시간 기준 정렬 (최신순) @@ -1518,11 +1520,13 @@ private function findBaseBalance(int $tenantId, string $startDate, ?string $bank * API 로그와 수동 로그 병합 (중복 제거) * 수동 거래와 동일한 API 거래가 있으면 API 거래를 제외하고 수동 거래를 유지 * 매칭 기준: 계좌번호 + 거래일시 + 입금액 + 출금액 (잔액 제외 - 수동 거래는 재계산됨) + * + * @return array ['logs' => array, 'removedDeposit' => float, 'removedWithdraw' => float] */ private function mergeWithDedup(array $apiLogs, array $manualLogs): array { if (empty($manualLogs)) { - return $apiLogs; + return ['logs' => $apiLogs, 'removedDeposit' => 0, 'removedWithdraw' => 0]; } // 수동 거래의 매칭 키 생성 (잔액 제외) @@ -1539,7 +1543,8 @@ private function mergeWithDedup(array $apiLogs, array $manualLogs): array // API 로그에서 수동 거래와 중복되는 것 제외 $dedupedApiLogs = []; - $removedCount = 0; + $removedDeposit = 0; + $removedWithdraw = 0; foreach ($apiLogs as $aLog) { $key = implode('|', [ $aLog['bankAccountNum'] ?? '', @@ -1548,17 +1553,26 @@ private function mergeWithDedup(array $apiLogs, array $manualLogs): array (int) ($aLog['withdraw'] ?? 0), ]); if (isset($manualKeys[$key])) { - $removedCount++; + $removedDeposit += (float) ($aLog['deposit'] ?? 0); + $removedWithdraw += (float) ($aLog['withdraw'] ?? 0); continue; // 수동 거래가 우선, API 거래 스킵 } $dedupedApiLogs[] = $aLog; } - if ($removedCount > 0) { - Log::info('[Eaccount] 중복 거래 제거', ['removed' => $removedCount]); + if ($removedDeposit > 0 || $removedWithdraw > 0) { + Log::info('[Eaccount] 중복 거래 제거', [ + 'count' => count($manualLogs) - count($dedupedApiLogs) + count($apiLogs) - count($manualLogs), + 'removedDeposit' => $removedDeposit, + 'removedWithdraw' => $removedWithdraw, + ]); } - return array_merge($dedupedApiLogs, $manualLogs); + return [ + 'logs' => array_merge($dedupedApiLogs, $manualLogs), + 'removedDeposit' => $removedDeposit, + 'removedWithdraw' => $removedWithdraw, + ]; } private function recalcManualBalances(array $logs, ?float $baseBalance = null): array