|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Models\Account; |
| 7 | +use App\Modules\Finance\Models\Budget; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Inertia\Inertia; |
| 12 | +use Inertia\Response; |
| 13 | + |
| 14 | +class BudgetController extends Controller |
| 15 | +{ |
| 16 | + public function index(Request $request): Response |
| 17 | + { |
| 18 | + $this->authorize('viewAny', Budget::class); |
| 19 | + |
| 20 | + $budgets = Budget::withCount('lines') |
| 21 | + ->orderByDesc('year') |
| 22 | + ->orderByDesc('id') |
| 23 | + ->get() |
| 24 | + ->map(fn ($b) => [ |
| 25 | + 'id' => $b->id, |
| 26 | + 'name' => $b->name, |
| 27 | + 'year' => $b->year, |
| 28 | + 'period_type' => $b->period_type, |
| 29 | + 'status' => $b->status, |
| 30 | + 'lines_count' => $b->lines_count, |
| 31 | + ]); |
| 32 | + |
| 33 | + return Inertia::render('Finance/Budgets/Index', [ |
| 34 | + 'budgets' => $budgets, |
| 35 | + 'breadcrumbs' => [ |
| 36 | + ['label' => 'Finance'], |
| 37 | + ['label' => 'Budgets'], |
| 38 | + ], |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + public function create(): Response |
| 43 | + { |
| 44 | + $this->authorize('create', Budget::class); |
| 45 | + |
| 46 | + $accounts = Account::whereIn('type', ['income', 'expense']) |
| 47 | + ->where('is_active', true) |
| 48 | + ->orderBy('code') |
| 49 | + ->get(['id', 'code', 'name', 'type']); |
| 50 | + |
| 51 | + return Inertia::render('Finance/Budgets/Create', [ |
| 52 | + 'accounts' => $accounts, |
| 53 | + 'breadcrumbs' => [ |
| 54 | + ['label' => 'Finance'], |
| 55 | + ['label' => 'Budgets', 'href' => '/finance/budgets'], |
| 56 | + ['label' => 'New Budget'], |
| 57 | + ], |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + public function store(Request $request): RedirectResponse |
| 62 | + { |
| 63 | + $this->authorize('create', Budget::class); |
| 64 | + |
| 65 | + $validated = $request->validate([ |
| 66 | + 'name' => 'required|string|max:191', |
| 67 | + 'year' => 'required|integer|min:2000|max:2100', |
| 68 | + 'period_type' => 'required|in:annual,monthly,quarterly', |
| 69 | + 'notes' => 'nullable|string', |
| 70 | + 'lines' => 'required|array|min:1', |
| 71 | + 'lines.*.account_id' => 'required|exists:accounts,id', |
| 72 | + 'lines.*.period' => 'required|integer|min:0|max:12', |
| 73 | + 'lines.*.amount' => 'required|numeric|min:0', |
| 74 | + ]); |
| 75 | + |
| 76 | + DB::transaction(function () use ($validated, $request) { |
| 77 | + $budget = Budget::create([ |
| 78 | + 'tenant_id' => $request->user()->tenant_id, |
| 79 | + 'name' => $validated['name'], |
| 80 | + 'year' => $validated['year'], |
| 81 | + 'period_type' => $validated['period_type'], |
| 82 | + 'notes' => $validated['notes'] ?? null, |
| 83 | + 'status' => 'draft', |
| 84 | + 'created_by' => $request->user()->id, |
| 85 | + ]); |
| 86 | + |
| 87 | + foreach ($validated['lines'] as $line) { |
| 88 | + $budget->lines()->create([ |
| 89 | + 'account_id' => $line['account_id'], |
| 90 | + 'period' => $line['period'], |
| 91 | + 'amount' => $line['amount'], |
| 92 | + 'notes' => $line['notes'] ?? null, |
| 93 | + ]); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + return redirect()->route('finance.budgets.index') |
| 98 | + ->with('success', 'Budget created successfully.'); |
| 99 | + } |
| 100 | + |
| 101 | + public function show(Budget $budget): Response |
| 102 | + { |
| 103 | + $this->authorize('view', $budget); |
| 104 | + $budget->load(['lines.account']); |
| 105 | + |
| 106 | + $tenantId = request()->user()->tenant_id; |
| 107 | + $year = $budget->year; |
| 108 | + |
| 109 | + // Compute actuals from posted journal entries for this year |
| 110 | + $actuals = DB::table('journal_lines') |
| 111 | + ->join('journal_entries', 'journal_lines.journal_entry_id', '=', 'journal_entries.id') |
| 112 | + ->join('accounts', 'journal_lines.account_id', '=', 'accounts.id') |
| 113 | + ->where('journal_entries.tenant_id', $tenantId) |
| 114 | + ->where('journal_entries.status', 'posted') |
| 115 | + ->whereYear('journal_entries.date', $year) |
| 116 | + ->whereIn('accounts.type', ['income', 'expense']) |
| 117 | + ->select( |
| 118 | + 'journal_lines.account_id', |
| 119 | + 'accounts.type', |
| 120 | + DB::raw('SUM(journal_lines.debit) as total_debit'), |
| 121 | + DB::raw('SUM(journal_lines.credit) as total_credit'), |
| 122 | + ) |
| 123 | + ->groupBy('journal_lines.account_id', 'accounts.type') |
| 124 | + ->get() |
| 125 | + ->keyBy('account_id'); |
| 126 | + |
| 127 | + $lines = $budget->lines->map(function ($line) use ($actuals) { |
| 128 | + $actual = $actuals->get($line->account_id); |
| 129 | + $actualAmount = 0; |
| 130 | + if ($actual) { |
| 131 | + $actualAmount = $actual->type === 'income' |
| 132 | + ? (float)$actual->total_credit - (float)$actual->total_debit |
| 133 | + : (float)$actual->total_debit - (float)$actual->total_credit; |
| 134 | + } |
| 135 | + $variance = $actualAmount - (float)$line->amount; |
| 136 | + $variancePct = $line->amount != 0 ? round($variance / $line->amount * 100, 1) : null; |
| 137 | + |
| 138 | + return [ |
| 139 | + 'id' => $line->id, |
| 140 | + 'account_id' => $line->account_id, |
| 141 | + 'account_code' => $line->account->code, |
| 142 | + 'account_name' => $line->account->name, |
| 143 | + 'account_type' => $line->account->type, |
| 144 | + 'period' => $line->period, |
| 145 | + 'budget' => round((float)$line->amount, 2), |
| 146 | + 'actual' => round($actualAmount, 2), |
| 147 | + 'variance' => round($variance, 2), |
| 148 | + 'variance_pct' => $variancePct, |
| 149 | + ]; |
| 150 | + }); |
| 151 | + |
| 152 | + return Inertia::render('Finance/Budgets/Show', [ |
| 153 | + 'budget' => [ |
| 154 | + 'id' => $budget->id, |
| 155 | + 'name' => $budget->name, |
| 156 | + 'year' => $budget->year, |
| 157 | + 'period_type' => $budget->period_type, |
| 158 | + 'status' => $budget->status, |
| 159 | + 'notes' => $budget->notes, |
| 160 | + ], |
| 161 | + 'lines' => $lines->values(), |
| 162 | + 'total_budget' => $lines->sum('budget'), |
| 163 | + 'total_actual' => $lines->sum('actual'), |
| 164 | + 'total_variance' => round($lines->sum('variance'), 2), |
| 165 | + 'breadcrumbs' => [ |
| 166 | + ['label' => 'Finance'], |
| 167 | + ['label' => 'Budgets', 'href' => '/finance/budgets'], |
| 168 | + ['label' => $budget->name], |
| 169 | + ], |
| 170 | + ]); |
| 171 | + } |
| 172 | + |
| 173 | + public function destroy(Budget $budget): RedirectResponse |
| 174 | + { |
| 175 | + $this->authorize('delete', $budget); |
| 176 | + |
| 177 | + $budget->delete(); |
| 178 | + |
| 179 | + return redirect()->route('finance.budgets.index') |
| 180 | + ->with('success', 'Budget deleted.'); |
| 181 | + } |
| 182 | +} |
0 commit comments