diff --git a/app/Http/Controllers/Barobill/EaccountController.php b/app/Http/Controllers/Barobill/EaccountController.php index 7b99059a..2585eb88 100644 --- a/app/Http/Controllers/Barobill/EaccountController.php +++ b/app/Http/Controllers/Barobill/EaccountController.php @@ -365,10 +365,11 @@ public function transactions(Request $request): JsonResponse if ($errorCode && in_array($errorCode, [-25005, -25001])) { // API 데이터 없어도 수동 건은 표시 $manualLogs = $this->convertManualToLogs($manualTransactions); + $recalcLogs = $this->recalcManualBalances($manualLogs['logs']); return response()->json([ 'success' => true, 'data' => [ - 'logs' => $manualLogs['logs'], + 'logs' => $recalcLogs, 'summary' => $manualLogs['summary'], 'pagination' => ['currentPage' => 1, 'maxPageNum' => 1] ] @@ -389,6 +390,9 @@ public function transactions(Request $request): JsonResponse return strcmp($dtB, $dtA); }); + // 수동입력 건 잔액 재계산 (직전 거래 기준) + $mergedLogs = $this->recalcManualBalances($mergedLogs); + // summary 합산 $mergedSummary = [ 'totalDeposit' => $logs['summary']['totalDeposit'] + $manualLogs['summary']['totalDeposit'], @@ -495,6 +499,9 @@ private function getAllAccountsTransactions(string $userId, string $startDate, s return strcmp($dateB, $dateA); }); + // 수동입력 건 잔액 재계산 (직전 거래 기준) + $allLogs = $this->recalcManualBalances($allLogs); + // 페이지네이션 $totalCount = count($allLogs); $maxPageNum = (int)ceil($totalCount / $limit); @@ -1262,6 +1269,34 @@ public function destroyManual(int $id): JsonResponse } } + /** + * 병합된 로그에서 수동입력 건의 잔액을 직전 거래 기준으로 재계산 + * 로그는 날짜 내림차순(DESC) 정렬 상태로 전달됨 + */ + private function recalcManualBalances(array $logs): array + { + if (empty($logs)) return $logs; + + // 시간순(ASC)으로 뒤집어서 순차 처리 + $logs = array_reverse($logs); + + $prevBalance = null; + foreach ($logs as &$log) { + if (!empty($log['isManual'])) { + $deposit = (float) ($log['deposit'] ?? 0); + $withdraw = (float) ($log['withdraw'] ?? 0); + $newBalance = ($prevBalance !== null ? $prevBalance : 0) + $deposit - $withdraw; + $log['balance'] = $newBalance; + $log['balanceFormatted'] = number_format($newBalance); + } + $prevBalance = (float) ($log['balance'] ?? 0); + } + unset($log); + + // 다시 내림차순(DESC)으로 복원 + return array_reverse($logs); + } + /** * 수동 입력 건을 API 로그 형식으로 변환 */