|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Finance\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Finance\Http\Requests\StoreQuoteRequest; |
| 7 | +use App\Modules\Finance\Http\Resources\QuoteResource; |
| 8 | +use App\Modules\Finance\Models\Contact; |
| 9 | +use App\Modules\Finance\Models\Invoice; |
| 10 | +use App\Modules\Finance\Models\InvoiceItem; |
| 11 | +use App\Modules\Finance\Models\Quote; |
| 12 | +use App\Modules\Finance\Models\QuoteItem; |
| 13 | +use Illuminate\Http\RedirectResponse; |
| 14 | +use Illuminate\Http\Request; |
| 15 | +use Illuminate\Support\Facades\DB; |
| 16 | +use Inertia\Inertia; |
| 17 | +use Inertia\Response; |
| 18 | + |
| 19 | +class QuoteController extends Controller |
| 20 | +{ |
| 21 | + public function index(Request $request): Response |
| 22 | + { |
| 23 | + $this->authorize('viewAny', Quote::class); |
| 24 | + |
| 25 | + $quotes = Quote::with('contact') |
| 26 | + ->when($request->status, fn ($q) => $q->where('status', $request->status)) |
| 27 | + ->when($request->contact_id, fn ($q) => $q->where('contact_id', $request->contact_id)) |
| 28 | + ->when($request->search, fn ($q) => $q->where('number', 'like', "%{$request->search}%")) |
| 29 | + ->latest('issue_date') |
| 30 | + ->paginate(25) |
| 31 | + ->withQueryString(); |
| 32 | + |
| 33 | + return Inertia::render('Finance/Quotes/Index', [ |
| 34 | + 'quotes' => QuoteResource::collection($quotes), |
| 35 | + 'contacts' => Contact::customers()->active()->orderBy('name')->get(['id', 'name']), |
| 36 | + 'filters' => $request->only(['status', 'contact_id', 'search']), |
| 37 | + 'breadcrumbs' => [ |
| 38 | + ['label' => 'Finance'], |
| 39 | + ['label' => 'Quotes', 'href' => route('finance.quotes.index')], |
| 40 | + ], |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + public function create(): Response |
| 45 | + { |
| 46 | + $this->authorize('create', Quote::class); |
| 47 | + |
| 48 | + return Inertia::render('Finance/Quotes/Create', [ |
| 49 | + 'contacts' => Contact::customers()->active()->orderBy('name')->get(['id', 'name']), |
| 50 | + 'breadcrumbs' => [ |
| 51 | + ['label' => 'Finance'], |
| 52 | + ['label' => 'Quotes', 'href' => route('finance.quotes.index')], |
| 53 | + ['label' => 'New Quote'], |
| 54 | + ], |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + public function store(StoreQuoteRequest $request): RedirectResponse |
| 59 | + { |
| 60 | + $this->authorize('create', Quote::class); |
| 61 | + |
| 62 | + $data = $request->validated(); |
| 63 | + |
| 64 | + $quote = DB::transaction(function () use ($data) { |
| 65 | + $quote = Quote::create([ |
| 66 | + 'tenant_id' => auth()->user()->tenant_id, |
| 67 | + 'contact_id' => $data['contact_id'] ?? null, |
| 68 | + 'issue_date' => $data['issue_date'], |
| 69 | + 'expiry_date' => $data['expiry_date'] ?? null, |
| 70 | + 'notes' => $data['notes'] ?? null, |
| 71 | + 'created_by' => auth()->id(), |
| 72 | + ]); |
| 73 | + |
| 74 | + $quote->update([ |
| 75 | + 'number' => 'QUOTE-' . now()->format('Y') . '-' . str_pad((string) $quote->id, 5, '0', STR_PAD_LEFT), |
| 76 | + ]); |
| 77 | + |
| 78 | + foreach ($data['items'] as $item) { |
| 79 | + QuoteItem::create([ |
| 80 | + 'quote_id' => $quote->id, |
| 81 | + 'description' => $item['description'], |
| 82 | + 'quantity' => $item['quantity'], |
| 83 | + 'unit_price' => $item['unit_price'], |
| 84 | + 'tax_rate' => $item['tax_rate'], |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + return $quote; |
| 89 | + }); |
| 90 | + |
| 91 | + return redirect()->route('finance.quotes.show', $quote) |
| 92 | + ->with('success', 'Quote created.'); |
| 93 | + } |
| 94 | + |
| 95 | + public function show(Quote $quote): Response |
| 96 | + { |
| 97 | + $this->authorize('view', $quote); |
| 98 | + |
| 99 | + $quote->load(['contact', 'items', 'creator']); |
| 100 | + |
| 101 | + return Inertia::render('Finance/Quotes/Show', [ |
| 102 | + 'quote' => new QuoteResource($quote), |
| 103 | + 'breadcrumbs' => [ |
| 104 | + ['label' => 'Finance'], |
| 105 | + ['label' => 'Quotes', 'href' => route('finance.quotes.index')], |
| 106 | + ['label' => $quote->number ?? "Quote #{$quote->id}"], |
| 107 | + ], |
| 108 | + ]); |
| 109 | + } |
| 110 | + |
| 111 | + public function send(Quote $quote): RedirectResponse |
| 112 | + { |
| 113 | + $this->authorize('update', $quote); |
| 114 | + |
| 115 | + try { |
| 116 | + $quote->transitionTo('sent'); |
| 117 | + } catch (\DomainException $e) { |
| 118 | + return back()->withErrors(['status' => $e->getMessage()]); |
| 119 | + } |
| 120 | + |
| 121 | + return back()->with('success', 'Quote marked as sent.'); |
| 122 | + } |
| 123 | + |
| 124 | + public function accept(Quote $quote): RedirectResponse |
| 125 | + { |
| 126 | + $this->authorize('update', $quote); |
| 127 | + |
| 128 | + try { |
| 129 | + $quote->transitionTo('accepted'); |
| 130 | + } catch (\DomainException $e) { |
| 131 | + return back()->withErrors(['status' => $e->getMessage()]); |
| 132 | + } |
| 133 | + |
| 134 | + return back()->with('success', 'Quote accepted.'); |
| 135 | + } |
| 136 | + |
| 137 | + public function decline(Quote $quote): RedirectResponse |
| 138 | + { |
| 139 | + $this->authorize('update', $quote); |
| 140 | + |
| 141 | + try { |
| 142 | + $quote->transitionTo('declined'); |
| 143 | + } catch (\DomainException $e) { |
| 144 | + return back()->withErrors(['status' => $e->getMessage()]); |
| 145 | + } |
| 146 | + |
| 147 | + return back()->with('success', 'Quote declined.'); |
| 148 | + } |
| 149 | + |
| 150 | + public function convertToInvoice(Quote $quote): RedirectResponse |
| 151 | + { |
| 152 | + $this->authorize('update', $quote); |
| 153 | + |
| 154 | + if ($quote->status !== 'accepted') { |
| 155 | + return back()->withErrors(['status' => 'Only accepted quotes can be converted to invoices.']); |
| 156 | + } |
| 157 | + |
| 158 | + $invoice = DB::transaction(function () use ($quote) { |
| 159 | + $quote->load('items'); |
| 160 | + |
| 161 | + $invoice = Invoice::create([ |
| 162 | + 'tenant_id' => $quote->tenant_id, |
| 163 | + 'contact_id' => $quote->contact_id, |
| 164 | + 'issue_date' => now()->toDateString(), |
| 165 | + 'status' => 'draft', |
| 166 | + 'created_by' => auth()->id(), |
| 167 | + ]); |
| 168 | + |
| 169 | + $invoice->update([ |
| 170 | + 'number' => 'INV-' . now()->format('Y') . '-' . str_pad((string) $invoice->id, 5, '0', STR_PAD_LEFT), |
| 171 | + ]); |
| 172 | + |
| 173 | + foreach ($quote->items as $item) { |
| 174 | + InvoiceItem::create([ |
| 175 | + 'invoice_id' => $invoice->id, |
| 176 | + 'description' => $item->description, |
| 177 | + 'quantity' => $item->quantity, |
| 178 | + 'unit_price' => $item->unit_price, |
| 179 | + 'tax_rate' => $item->tax_rate, |
| 180 | + ]); |
| 181 | + } |
| 182 | + |
| 183 | + return $invoice; |
| 184 | + }); |
| 185 | + |
| 186 | + return redirect()->route('finance.invoices.show', $invoice) |
| 187 | + ->with('success', 'Quote converted to invoice.'); |
| 188 | + } |
| 189 | + |
| 190 | + public function destroy(Quote $quote): RedirectResponse |
| 191 | + { |
| 192 | + $this->authorize('delete', $quote); |
| 193 | + |
| 194 | + $quote->delete(); |
| 195 | + |
| 196 | + return redirect()->route('finance.quotes.index') |
| 197 | + ->with('success', 'Quote deleted.'); |
| 198 | + } |
| 199 | +} |
0 commit comments