feat: optimize cash overview calculations for total balance, deposits, and withdrawals

This commit is contained in:
Yoga Pangestu 2026-08-13 21:20:53 +07:00
parent 083cde60f9
commit 95df2e9dc7

View File

@ -184,11 +184,14 @@ public function getCashOverview(?string $startDate, ?string $endDate): array
$transactions = $cashAccount->cashTransactions();
$this->applyDateFilter($transactions, $startDate, $endDate, 'cash_transactions.created_at');
$totalDeposit = (clone $transactions)->where('type', CashTransactionType::DEPOSIT)->sum('amount');
$totalWithdrawal = (clone $transactions)->where('type', CashTransactionType::WITHDRAWAL)->sum('amount');
return [
'total_balance' => $cashAccount->balance,
'total_balance' => (int) $totalDeposit - (int) $totalWithdrawal,
'total_transactions' => (clone $transactions)->count(),
'total_deposit' => (int) (clone $transactions)->where('type', CashTransactionType::DEPOSIT)->sum('amount'),
'total_withdrawal' => (int) (clone $transactions)->where('type', CashTransactionType::WITHDRAWAL)->sum('amount'),
'total_deposit' => (int) $totalDeposit,
'total_withdrawal' => (int) $totalWithdrawal,
];
}