input('start_date', now()->startOfYear()->format('Y-m-d')); $endDate = $request->input('end_date', now()->format('Y-m-d')); // Get all active accounts with their balances $accounts = ChartOfAccount::where('is_active', true) ->orderBy('account_code') ->get() ->map(function ($account) use ($startDate, $endDate) { // Get debit and credit totals for this account $debitTotal = AccountingEntry::where('chart_of_account_id', $account->id) ->whereBetween('entry_date', [$startDate, $endDate]) ->where('entry_type', AccountingEntry::ENTRY_TYPE_DEBIT) ->sum('amount'); $creditTotal = AccountingEntry::where('chart_of_account_id', $account->id) ->whereBetween('entry_date', [$startDate, $endDate]) ->where('entry_type', AccountingEntry::ENTRY_TYPE_CREDIT) ->sum('amount'); // Only include accounts with activity if ($debitTotal == 0 && $creditTotal == 0) { return null; } return [ 'account' => $account, 'debit_total' => $debitTotal, 'credit_total' => $creditTotal, ]; }) ->filter() // Remove null entries ->values(); // Calculate grand totals $grandDebitTotal = $accounts->sum('debit_total'); $grandCreditTotal = $accounts->sum('credit_total'); // Check if balanced $isBalanced = bccomp((string)$grandDebitTotal, (string)$grandCreditTotal, 2) === 0; $difference = $grandDebitTotal - $grandCreditTotal; // Group accounts by type $accountsByType = $accounts->groupBy(function ($item) { return $item['account']->account_type; }); return view('admin.trial-balance.index', compact( 'accounts', 'accountsByType', 'startDate', 'endDate', 'grandDebitTotal', 'grandCreditTotal', 'isBalanced', 'difference' )); } }