From 95df2e9dc7e27ac4217f392e0591fed0ec397884 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 13 Aug 2026 21:20:53 +0700 Subject: [PATCH] feat: optimize cash overview calculations for total balance, deposits, and withdrawals --- app/Services/AnalysisService.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Services/AnalysisService.php b/app/Services/AnalysisService.php index be71919..39e9580 100644 --- a/app/Services/AnalysisService.php +++ b/app/Services/AnalysisService.php @@ -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, ]; }