|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Accounting\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Accounting\Models\Account; |
| 7 | +use App\Modules\Accounting\Models\AccountingPeriod; |
| 8 | +use App\Modules\Accounting\Models\JournalEntryLine; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Inertia\Inertia; |
| 12 | +use Inertia\Response; |
| 13 | + |
| 14 | +class AccountingReportController extends Controller |
| 15 | +{ |
| 16 | + public function trialBalance(Request $request): Response |
| 17 | + { |
| 18 | + $tenantId = auth()->user()->tenant_id; |
| 19 | + $asOf = $request->as_of ?? now()->toDateString(); |
| 20 | + |
| 21 | + $accounts = Account::withoutGlobalScopes() |
| 22 | + ->where('chart_of_accounts.tenant_id', $tenantId) |
| 23 | + ->select('chart_of_accounts.*') |
| 24 | + ->selectRaw('COALESCE(SUM(accounting_journal_entry_lines.debit), 0) as total_debit') |
| 25 | + ->selectRaw('COALESCE(SUM(accounting_journal_entry_lines.credit), 0) as total_credit') |
| 26 | + ->leftJoin('accounting_journal_entry_lines', 'accounting_journal_entry_lines.account_id', '=', 'chart_of_accounts.id') |
| 27 | + ->leftJoin('accounting_journal_entries', function ($join) use ($asOf) { |
| 28 | + $join->on('accounting_journal_entries.id', '=', 'accounting_journal_entry_lines.journal_entry_id') |
| 29 | + ->where('accounting_journal_entries.status', 'posted') |
| 30 | + ->where('accounting_journal_entries.entry_date', '<=', $asOf); |
| 31 | + }) |
| 32 | + ->groupBy('chart_of_accounts.id') |
| 33 | + ->havingRaw('total_debit != 0 OR total_credit != 0') |
| 34 | + ->orderBy('code') |
| 35 | + ->get(); |
| 36 | + |
| 37 | + $totalDebits = $accounts->sum('total_debit'); |
| 38 | + $totalCredits = $accounts->sum('total_credit'); |
| 39 | + $isBalanced = abs($totalDebits - $totalCredits) < 0.01; |
| 40 | + |
| 41 | + return Inertia::render('Accounting/Reports/TrialBalance', [ |
| 42 | + 'accounts' => $accounts, |
| 43 | + 'totalDebits' => $totalDebits, |
| 44 | + 'totalCredits' => $totalCredits, |
| 45 | + 'isBalanced' => $isBalanced, |
| 46 | + 'asOf' => $asOf, |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + public function balanceSheet(Request $request): Response |
| 51 | + { |
| 52 | + $tenantId = auth()->user()->tenant_id; |
| 53 | + $asOf = $request->as_of ?? now()->toDateString(); |
| 54 | + |
| 55 | + $accounts = $this->getAccountBalances($tenantId, $asOf); |
| 56 | + |
| 57 | + $assets = $accounts->where('type', 'asset'); |
| 58 | + $liabilities = $accounts->where('type', 'liability'); |
| 59 | + $equity = $accounts->where('type', 'equity'); |
| 60 | + |
| 61 | + $totalAssets = $assets->sum(fn ($a) => $a->total_debit - $a->total_credit); |
| 62 | + $totalLiabilities = $liabilities->sum(fn ($a) => $a->total_credit - $a->total_debit); |
| 63 | + $totalEquity = $equity->sum(fn ($a) => $a->total_credit - $a->total_debit); |
| 64 | + |
| 65 | + return Inertia::render('Accounting/Reports/BalanceSheet', [ |
| 66 | + 'assets' => $assets->values(), |
| 67 | + 'liabilities' => $liabilities->values(), |
| 68 | + 'equity' => $equity->values(), |
| 69 | + 'totalAssets' => $totalAssets, |
| 70 | + 'totalLiabilities' => $totalLiabilities, |
| 71 | + 'totalEquity' => $totalEquity, |
| 72 | + 'asOf' => $asOf, |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + public function incomeStatement(Request $request): Response |
| 77 | + { |
| 78 | + $tenantId = auth()->user()->tenant_id; |
| 79 | + $startDate = $request->start_date ?? now()->startOfYear()->toDateString(); |
| 80 | + $endDate = $request->end_date ?? now()->toDateString(); |
| 81 | + |
| 82 | + $accounts = Account::withoutGlobalScopes() |
| 83 | + ->where('chart_of_accounts.tenant_id', $tenantId) |
| 84 | + ->whereIn('chart_of_accounts.type', ['revenue', 'expense']) |
| 85 | + ->select('chart_of_accounts.*') |
| 86 | + ->selectRaw('COALESCE(SUM(accounting_journal_entry_lines.debit), 0) as total_debit') |
| 87 | + ->selectRaw('COALESCE(SUM(accounting_journal_entry_lines.credit), 0) as total_credit') |
| 88 | + ->leftJoin('accounting_journal_entry_lines', 'accounting_journal_entry_lines.account_id', '=', 'chart_of_accounts.id') |
| 89 | + ->leftJoin('accounting_journal_entries', function ($join) use ($startDate, $endDate) { |
| 90 | + $join->on('accounting_journal_entries.id', '=', 'accounting_journal_entry_lines.journal_entry_id') |
| 91 | + ->where('accounting_journal_entries.status', 'posted') |
| 92 | + ->whereBetween('accounting_journal_entries.entry_date', [$startDate, $endDate]); |
| 93 | + }) |
| 94 | + ->groupBy('chart_of_accounts.id') |
| 95 | + ->orderBy('code') |
| 96 | + ->get(); |
| 97 | + |
| 98 | + $revenue = $accounts->where('type', 'revenue'); |
| 99 | + $expenses = $accounts->where('type', 'expense'); |
| 100 | + |
| 101 | + $totalRevenue = $revenue->sum(fn ($a) => $a->total_credit - $a->total_debit); |
| 102 | + $totalExpenses = $expenses->sum(fn ($a) => $a->total_debit - $a->total_credit); |
| 103 | + $netIncome = $totalRevenue - $totalExpenses; |
| 104 | + |
| 105 | + return Inertia::render('Accounting/Reports/IncomeStatement', [ |
| 106 | + 'revenue' => $revenue->values(), |
| 107 | + 'expenses' => $expenses->values(), |
| 108 | + 'totalRevenue' => $totalRevenue, |
| 109 | + 'totalExpenses' => $totalExpenses, |
| 110 | + 'netIncome' => $netIncome, |
| 111 | + 'startDate' => $startDate, |
| 112 | + 'endDate' => $endDate, |
| 113 | + ]); |
| 114 | + } |
| 115 | + |
| 116 | + public function generalLedger(Request $request, Account $account): Response |
| 117 | + { |
| 118 | + $tenantId = auth()->user()->tenant_id; |
| 119 | + |
| 120 | + $lines = JournalEntryLine::with('journalEntry') |
| 121 | + ->where('accounting_journal_entry_lines.account_id', $account->id) |
| 122 | + ->whereHas('journalEntry', fn ($q) => $q->where('status', 'posted') |
| 123 | + ->where('tenant_id', $tenantId)) |
| 124 | + ->join('accounting_journal_entries', 'accounting_journal_entries.id', '=', 'accounting_journal_entry_lines.journal_entry_id') |
| 125 | + ->orderBy('accounting_journal_entries.entry_date') |
| 126 | + ->orderBy('accounting_journal_entries.id') |
| 127 | + ->select('accounting_journal_entry_lines.*') |
| 128 | + ->get(); |
| 129 | + |
| 130 | + // Compute running balance |
| 131 | + $runningBalance = 0.0; |
| 132 | + $linesWithBalance = $lines->map(function ($line) use (&$runningBalance, $account) { |
| 133 | + if ($account->isDebitNormal()) { |
| 134 | + $runningBalance += $line->debit - $line->credit; |
| 135 | + } else { |
| 136 | + $runningBalance += $line->credit - $line->debit; |
| 137 | + } |
| 138 | + return array_merge($line->toArray(), ['running_balance' => $runningBalance]); |
| 139 | + }); |
| 140 | + |
| 141 | + $allAccounts = Account::withoutGlobalScopes() |
| 142 | + ->where('tenant_id', $tenantId) |
| 143 | + ->where('is_active', true) |
| 144 | + ->orderBy('code') |
| 145 | + ->get(['id', 'code', 'name']); |
| 146 | + |
| 147 | + return Inertia::render('Accounting/Reports/GeneralLedger', [ |
| 148 | + 'account' => $account, |
| 149 | + 'lines' => $linesWithBalance, |
| 150 | + 'allAccounts' => $allAccounts, |
| 151 | + ]); |
| 152 | + } |
| 153 | + |
| 154 | + private function getAccountBalances(int $tenantId, string $asOf) |
| 155 | + { |
| 156 | + return Account::withoutGlobalScopes() |
| 157 | + ->where('chart_of_accounts.tenant_id', $tenantId) |
| 158 | + ->select('chart_of_accounts.*') |
| 159 | + ->selectRaw('COALESCE(SUM(accounting_journal_entry_lines.debit), 0) as total_debit') |
| 160 | + ->selectRaw('COALESCE(SUM(accounting_journal_entry_lines.credit), 0) as total_credit') |
| 161 | + ->leftJoin('accounting_journal_entry_lines', 'accounting_journal_entry_lines.account_id', '=', 'chart_of_accounts.id') |
| 162 | + ->leftJoin('accounting_journal_entries', function ($join) use ($asOf) { |
| 163 | + $join->on('accounting_journal_entries.id', '=', 'accounting_journal_entry_lines.journal_entry_id') |
| 164 | + ->where('accounting_journal_entries.status', 'posted') |
| 165 | + ->where('accounting_journal_entries.entry_date', '<=', $asOf); |
| 166 | + }) |
| 167 | + ->groupBy('chart_of_accounts.id') |
| 168 | + ->orderBy('code') |
| 169 | + ->get(); |
| 170 | + } |
| 171 | +} |
0 commit comments