fix:계좌관리 페이지 HX-Redirect 추가 및 잔액 계산 개선
- BankAccountController에 HX-Redirect 추가 (HTMX 네비게이션 시 스크립트 실행 보장) - latestBalances() 수동입력 거래 포함 정확한 잔액 계산으로 개선 - N+1 쿼리 제거: 전체 거래를 한번에 조회 후 계좌별 그룹화 처리 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -249,28 +249,42 @@ public function latestBalances(Request $request): JsonResponse
|
||||
try {
|
||||
$tenantId = session('selected_tenant_id', self::HEADQUARTERS_TENANT_ID);
|
||||
|
||||
// 각 계좌의 가장 최근 거래에서 잔액 조회
|
||||
$balances = BankTransaction::where('tenant_id', $tenantId)
|
||||
->select('bank_account_num', 'bank_name', 'balance', 'trans_date', 'trans_time')
|
||||
->whereIn('id', function ($query) use ($tenantId) {
|
||||
$query->select(DB::raw('MAX(id)'))
|
||||
->from('barobill_bank_transactions')
|
||||
->where('tenant_id', $tenantId)
|
||||
->groupBy('bank_account_num');
|
||||
})
|
||||
// 전체 거래를 한번에 조회 (계좌별 → 시간순)
|
||||
$allTransactions = BankTransaction::where('tenant_id', $tenantId)
|
||||
->select('bank_account_num', 'bank_name', 'balance', 'deposit', 'withdraw', 'trans_date', 'trans_time', 'is_manual')
|
||||
->orderBy('bank_account_num')
|
||||
->orderBy('trans_date', 'asc')
|
||||
->orderBy('trans_time', 'asc')
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
$result = [];
|
||||
foreach ($balances as $b) {
|
||||
$result[] = [
|
||||
'bankAccountNum' => $b->bank_account_num,
|
||||
'bankName' => $b->bank_name,
|
||||
'balance' => (float) $b->balance,
|
||||
'lastTransDate' => $b->trans_date,
|
||||
'lastTransTime' => $b->trans_time,
|
||||
];
|
||||
// 계좌별로 그룹화하여 최종 잔액 계산
|
||||
$accountBalances = [];
|
||||
foreach ($allTransactions as $tx) {
|
||||
$accNum = $tx->bank_account_num;
|
||||
|
||||
if (!isset($accountBalances[$accNum])) {
|
||||
$accountBalances[$accNum] = [
|
||||
'bankAccountNum' => $accNum,
|
||||
'bankName' => $tx->bank_name,
|
||||
'balance' => 0,
|
||||
'lastTransDate' => '',
|
||||
'lastTransTime' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$prev = $accountBalances[$accNum]['balance'];
|
||||
if (!$tx->is_manual && (float) $tx->balance != 0) {
|
||||
$accountBalances[$accNum]['balance'] = (float) $tx->balance;
|
||||
} else {
|
||||
$accountBalances[$accNum]['balance'] = $prev + (float) $tx->deposit - (float) $tx->withdraw;
|
||||
}
|
||||
$accountBalances[$accNum]['lastTransDate'] = $tx->trans_date;
|
||||
$accountBalances[$accNum]['lastTransTime'] = $tx->trans_time;
|
||||
}
|
||||
|
||||
$result = array_values($accountBalances);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'balances' => $result,
|
||||
|
||||
Reference in New Issue
Block a user