From 74b810e894c8b612bd708464ebcba89c30e42cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Fri, 6 Feb 2026 12:54:15 +0900 Subject: [PATCH] =?UTF-8?q?fix:=EA=B3=84=EC=A2=8C=20=EC=9E=94=EC=95=A1=20?= =?UTF-8?q?=EA=B8=B0=EC=A4=80=EA=B0=92=20=EA=B3=84=EC=82=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0=20-=20=EC=88=98=EB=8F=99=EC=9E=85=EB=A0=A5=20?= =?UTF-8?q?=EA=B1=B4=EB=8F=84=20=EB=88=84=EC=A0=81=20=EC=B6=94=EC=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - findBaseBalance를 이전 전체 거래 순회 방식으로 변경 - API 데이터는 바로빌 잔액을 그대로 사용 - 수동입력 건(잔액 0 저장)은 입출금 누적으로 잔액 계산 - 9월 수동입력 → 10월 조회 시 정확한 이전 잔액 적용 Co-Authored-By: Claude Opus 4.6 --- .../Barobill/EaccountController.php | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Barobill/EaccountController.php b/app/Http/Controllers/Barobill/EaccountController.php index e41612aa..7075fc72 100644 --- a/app/Http/Controllers/Barobill/EaccountController.php +++ b/app/Http/Controllers/Barobill/EaccountController.php @@ -1277,23 +1277,36 @@ public function destroyManual(int $id): JsonResponse * 로그는 날짜 내림차순(DESC) 정렬 상태로 전달됨 */ /** - * 조회기간 직전의 마지막 잔액 조회 (DB 저장 데이터 기준) + * 조회기간 직전의 마지막 잔액 조회 + * API 데이터는 잔액이 정확하므로 그대로 사용, + * 수동입력은 잔액이 0일 수 있으므로 입출금 누적으로 계산 */ private function findBaseBalance(int $tenantId, string $startDate, ?string $bankAccountNum = null): ?float { - $query = BankTransaction::where('tenant_id', $tenantId) + // 조회기간 이전의 모든 거래를 시간순(ASC)으로 조회 + $prevTransactions = BankTransaction::where('tenant_id', $tenantId) ->where('trans_date', '<', $startDate) - ->where('balance', '!=', 0); + ->when($bankAccountNum, fn($q) => $q->where('bank_account_num', $bankAccountNum)) + ->orderBy('trans_date', 'asc') + ->orderBy('trans_time', 'asc') + ->get(); - if ($bankAccountNum) { - $query->where('bank_account_num', $bankAccountNum); + if ($prevTransactions->isEmpty()) return null; + + // 시간순으로 순회하며 잔액 추적 + $balance = null; + foreach ($prevTransactions as $tx) { + if (!$tx->is_manual && (float) $tx->balance != 0) { + // API 데이터: 바로빌이 제공한 정확한 잔액 사용 + $balance = (float) $tx->balance; + } else { + // 수동입력 또는 잔액 0인 건: 이전 잔액에서 입출금 계산 + $prev = $balance ?? 0; + $balance = $prev + (float) $tx->deposit - (float) $tx->withdraw; + } } - $lastTx = $query->orderBy('trans_date', 'desc') - ->orderBy('trans_time', 'desc') - ->first(); - - return $lastTx ? (float) $lastTx->balance : null; + return $balance; } private function recalcManualBalances(array $logs, ?float $baseBalance = null): array