|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Models\Contact; |
| 7 | +use App\Modules\Finance\Models\Contract; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Validation\Rule; |
| 11 | +use Inertia\Inertia; |
| 12 | +use Inertia\Response; |
| 13 | + |
| 14 | +class ContractController extends Controller |
| 15 | +{ |
| 16 | + public function index(Request $request): Response |
| 17 | + { |
| 18 | + $this->authorize('viewAny', Contract::class); |
| 19 | + |
| 20 | + $contracts = Contract::with('contact') |
| 21 | + ->when($request->status, fn ($q) => $q->where('status', $request->status)) |
| 22 | + ->when($request->type, fn ($q) => $q->where('type', $request->type)) |
| 23 | + ->latest() |
| 24 | + ->paginate(15) |
| 25 | + ->withQueryString(); |
| 26 | + |
| 27 | + return Inertia::render('Finance/Contracts/Index', [ |
| 28 | + 'contracts' => $contracts, |
| 29 | + 'filters' => $request->only(['status', 'type']), |
| 30 | + 'breadcrumbs' => [ |
| 31 | + ['label' => 'Finance'], |
| 32 | + ['label' => 'Contracts', 'href' => route('finance.contracts.index')], |
| 33 | + ], |
| 34 | + ]); |
| 35 | + } |
| 36 | + |
| 37 | + public function create(): Response |
| 38 | + { |
| 39 | + $this->authorize('create', Contract::class); |
| 40 | + |
| 41 | + return Inertia::render('Finance/Contracts/Create', [ |
| 42 | + 'contacts' => Contact::orderBy('name')->get(['id', 'name']), |
| 43 | + 'breadcrumbs' => [ |
| 44 | + ['label' => 'Finance'], |
| 45 | + ['label' => 'Contracts', 'href' => route('finance.contracts.index')], |
| 46 | + ['label' => 'New Contract'], |
| 47 | + ], |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + public function store(Request $request): RedirectResponse |
| 52 | + { |
| 53 | + $this->authorize('create', Contract::class); |
| 54 | + |
| 55 | + $data = $request->validate([ |
| 56 | + 'title' => ['required', 'string', 'max:255'], |
| 57 | + 'reference' => ['nullable', 'string', 'max:100'], |
| 58 | + 'contact_id' => ['nullable', Rule::exists('contacts', 'id')], |
| 59 | + 'type' => ['required', Rule::in(['client', 'vendor', 'employment', 'nda', 'other'])], |
| 60 | + 'status' => ['nullable', Rule::in(['draft', 'active', 'expired', 'terminated'])], |
| 61 | + 'value' => ['nullable', 'numeric', 'min:0'], |
| 62 | + 'currency_code' => ['nullable', 'string', 'size:3'], |
| 63 | + 'start_date' => ['nullable', 'date'], |
| 64 | + 'end_date' => ['nullable', 'date', 'after_or_equal:start_date'], |
| 65 | + 'auto_renew' => ['boolean'], |
| 66 | + 'renewal_notice_days' => ['nullable', 'integer', 'min:0'], |
| 67 | + 'description' => ['nullable', 'string'], |
| 68 | + 'terms' => ['nullable', 'string'], |
| 69 | + ]); |
| 70 | + |
| 71 | + $contract = Contract::create([ |
| 72 | + 'tenant_id' => auth()->user()->tenant_id, |
| 73 | + ...$data, |
| 74 | + ]); |
| 75 | + |
| 76 | + return redirect()->route('finance.contracts.show', $contract) |
| 77 | + ->with('success', 'Contract created.'); |
| 78 | + } |
| 79 | + |
| 80 | + public function show(Contract $contract): Response |
| 81 | + { |
| 82 | + $this->authorize('view', $contract); |
| 83 | + |
| 84 | + $contract->load('contact'); |
| 85 | + |
| 86 | + return Inertia::render('Finance/Contracts/Show', [ |
| 87 | + 'contract' => $contract, |
| 88 | + 'breadcrumbs' => [ |
| 89 | + ['label' => 'Finance'], |
| 90 | + ['label' => 'Contracts', 'href' => route('finance.contracts.index')], |
| 91 | + ['label' => $contract->title], |
| 92 | + ], |
| 93 | + ]); |
| 94 | + } |
| 95 | + |
| 96 | + public function edit(Contract $contract): Response |
| 97 | + { |
| 98 | + $this->authorize('create', Contract::class); |
| 99 | + |
| 100 | + return Inertia::render('Finance/Contracts/Edit', [ |
| 101 | + 'contract' => $contract, |
| 102 | + 'contacts' => Contact::orderBy('name')->get(['id', 'name']), |
| 103 | + 'breadcrumbs' => [ |
| 104 | + ['label' => 'Finance'], |
| 105 | + ['label' => 'Contracts', 'href' => route('finance.contracts.index')], |
| 106 | + ['label' => $contract->title, 'href' => route('finance.contracts.show', $contract)], |
| 107 | + ['label' => 'Edit'], |
| 108 | + ], |
| 109 | + ]); |
| 110 | + } |
| 111 | + |
| 112 | + public function update(Request $request, Contract $contract): RedirectResponse |
| 113 | + { |
| 114 | + $this->authorize('create', Contract::class); |
| 115 | + |
| 116 | + $data = $request->validate([ |
| 117 | + 'title' => ['required', 'string', 'max:255'], |
| 118 | + 'reference' => ['nullable', 'string', 'max:100'], |
| 119 | + 'contact_id' => ['nullable', Rule::exists('contacts', 'id')], |
| 120 | + 'type' => ['required', Rule::in(['client', 'vendor', 'employment', 'nda', 'other'])], |
| 121 | + 'status' => ['nullable', Rule::in(['draft', 'active', 'expired', 'terminated'])], |
| 122 | + 'value' => ['nullable', 'numeric', 'min:0'], |
| 123 | + 'currency_code' => ['nullable', 'string', 'size:3'], |
| 124 | + 'start_date' => ['nullable', 'date'], |
| 125 | + 'end_date' => ['nullable', 'date', 'after_or_equal:start_date'], |
| 126 | + 'auto_renew' => ['boolean'], |
| 127 | + 'renewal_notice_days' => ['nullable', 'integer', 'min:0'], |
| 128 | + 'description' => ['nullable', 'string'], |
| 129 | + 'terms' => ['nullable', 'string'], |
| 130 | + ]); |
| 131 | + |
| 132 | + $contract->update($data); |
| 133 | + |
| 134 | + return redirect()->route('finance.contracts.show', $contract) |
| 135 | + ->with('success', 'Contract updated.'); |
| 136 | + } |
| 137 | + |
| 138 | + public function destroy(Contract $contract): RedirectResponse |
| 139 | + { |
| 140 | + $this->authorize('delete', $contract); |
| 141 | + |
| 142 | + $contract->delete(); |
| 143 | + |
| 144 | + return redirect()->route('finance.contracts.index') |
| 145 | + ->with('success', 'Contract deleted.'); |
| 146 | + } |
| 147 | + |
| 148 | + public function activate(Contract $contract): RedirectResponse |
| 149 | + { |
| 150 | + $this->authorize('create', Contract::class); |
| 151 | + |
| 152 | + $contract->activate(); |
| 153 | + |
| 154 | + return back()->with('success', 'Contract activated.'); |
| 155 | + } |
| 156 | + |
| 157 | + public function terminate(Contract $contract): RedirectResponse |
| 158 | + { |
| 159 | + $this->authorize('create', Contract::class); |
| 160 | + |
| 161 | + $contract->terminate(); |
| 162 | + |
| 163 | + return back()->with('success', 'Contract terminated.'); |
| 164 | + } |
| 165 | +} |
0 commit comments