fix:수동입력 건 잔액을 직전 거래 기준으로 자동 재계산

- recalcManualBalances() 메서드 추가: 병합된 로그를 시간순으로 순회하며
  수동입력 건의 잔액을 직전 거래 잔액 + 입금 - 출금으로 재계산
- 단일 계좌/전체 계좌/수동건만 있는 경우 모두 적용
- API 거래의 잔액은 그대로 유지

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
김보곤
2026-02-06 11:20:12 +09:00
parent e8f3396e61
commit 0f40ca673a

View File

@@ -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 로그 형식으로 변환
*/