|
| 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\DeliveryNote; |
| 8 | +use App\Modules\Finance\Models\Invoice; |
| 9 | +use App\Modules\Finance\Models\SalesOrder; |
| 10 | +use App\Modules\Inventory\Models\Product; |
| 11 | +use Illuminate\Http\RedirectResponse; |
| 12 | +use Illuminate\Http\Request; |
| 13 | +use Inertia\Inertia; |
| 14 | +use Inertia\Response; |
| 15 | + |
| 16 | +class DeliveryNoteController extends Controller |
| 17 | +{ |
| 18 | + public function index(): Response |
| 19 | + { |
| 20 | + $this->authorize('viewAny', DeliveryNote::class); |
| 21 | + $deliveryNotes = DeliveryNote::with(['contact', 'salesOrder', 'invoice']) |
| 22 | + ->orderByDesc('created_at') |
| 23 | + ->paginate(25); |
| 24 | + return Inertia::render('Finance/DeliveryNotes/Index', compact('deliveryNotes')); |
| 25 | + } |
| 26 | + |
| 27 | + public function create(Request $request): Response |
| 28 | + { |
| 29 | + $this->authorize('create', DeliveryNote::class); |
| 30 | + $contacts = Contact::where('type', 'customer')->orWhere('type', 'both')->orderBy('name')->get(['id', 'name']); |
| 31 | + $salesOrders = SalesOrder::whereIn('status', ['confirmed', 'invoiced'])->orderByDesc('order_date')->get(['id', 'reference', 'number']); |
| 32 | + $invoices = Invoice::whereIn('status', ['sent', 'partial'])->orderByDesc('issue_date')->get(['id', 'reference']); |
| 33 | + $products = Product::orderBy('name')->get(['id', 'name', 'sku']); |
| 34 | + $salesOrderId = $request->get('sales_order_id'); |
| 35 | + $invoiceId = $request->get('invoice_id'); |
| 36 | + return Inertia::render('Finance/DeliveryNotes/Create', compact('contacts', 'salesOrders', 'invoices', 'products', 'salesOrderId', 'invoiceId')); |
| 37 | + } |
| 38 | + |
| 39 | + public function store(Request $request): RedirectResponse |
| 40 | + { |
| 41 | + $this->authorize('create', DeliveryNote::class); |
| 42 | + $data = $request->validate([ |
| 43 | + 'reference' => 'required|string|max:100|unique:delivery_notes,reference', |
| 44 | + 'sales_order_id' => 'nullable|exists:sales_orders,id', |
| 45 | + 'invoice_id' => 'nullable|exists:invoices,id', |
| 46 | + 'contact_id' => 'nullable|exists:contacts,id', |
| 47 | + 'carrier' => 'nullable|string|max:100', |
| 48 | + 'tracking_number' => 'nullable|string|max:100', |
| 49 | + 'dispatch_date' => 'nullable|date', |
| 50 | + 'notes' => 'nullable|string', |
| 51 | + 'items' => 'required|array|min:1', |
| 52 | + 'items.*.description' => 'required|string', |
| 53 | + 'items.*.product_id' => 'nullable|exists:products,id', |
| 54 | + 'items.*.quantity' => 'required|numeric|min:0.01', |
| 55 | + ]); |
| 56 | + |
| 57 | + $dn = DeliveryNote::create([ |
| 58 | + 'tenant_id' => auth()->user()->tenant_id, |
| 59 | + 'reference' => $data['reference'], |
| 60 | + 'sales_order_id' => $data['sales_order_id'] ?? null, |
| 61 | + 'invoice_id' => $data['invoice_id'] ?? null, |
| 62 | + 'contact_id' => $data['contact_id'] ?? null, |
| 63 | + 'carrier' => $data['carrier'] ?? null, |
| 64 | + 'tracking_number' => $data['tracking_number'] ?? null, |
| 65 | + 'dispatch_date' => $data['dispatch_date'] ?? null, |
| 66 | + 'status' => 'draft', |
| 67 | + 'notes' => $data['notes'] ?? null, |
| 68 | + ]); |
| 69 | + |
| 70 | + foreach ($data['items'] as $item) { |
| 71 | + $dn->items()->create([ |
| 72 | + 'product_id' => $item['product_id'] ?? null, |
| 73 | + 'description' => $item['description'], |
| 74 | + 'quantity' => $item['quantity'], |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + return redirect()->route('finance.delivery-notes.show', $dn)->with('success', 'Delivery note created.'); |
| 79 | + } |
| 80 | + |
| 81 | + public function show(DeliveryNote $deliveryNote): Response |
| 82 | + { |
| 83 | + $this->authorize('view', $deliveryNote); |
| 84 | + $deliveryNote->load(['contact', 'salesOrder', 'invoice', 'items.product']); |
| 85 | + return Inertia::render('Finance/DeliveryNotes/Show', compact('deliveryNote')); |
| 86 | + } |
| 87 | + |
| 88 | + public function dispatch(DeliveryNote $deliveryNote, Request $request): RedirectResponse |
| 89 | + { |
| 90 | + $this->authorize('update', $deliveryNote); |
| 91 | + abort_unless($deliveryNote->status === 'draft', 422, 'Only draft notes can be dispatched.'); |
| 92 | + $request->validate(['dispatch_date' => 'nullable|date']); |
| 93 | + $deliveryNote->update([ |
| 94 | + 'status' => 'dispatched', |
| 95 | + 'dispatch_date' => $request->dispatch_date ?? now()->toDateString(), |
| 96 | + ]); |
| 97 | + return back()->with('success', 'Delivery note dispatched.'); |
| 98 | + } |
| 99 | + |
| 100 | + public function deliver(DeliveryNote $deliveryNote, Request $request): RedirectResponse |
| 101 | + { |
| 102 | + $this->authorize('update', $deliveryNote); |
| 103 | + abort_unless($deliveryNote->status === 'dispatched', 422, 'Only dispatched notes can be marked delivered.'); |
| 104 | + $request->validate(['delivery_date' => 'nullable|date']); |
| 105 | + $deliveryNote->update([ |
| 106 | + 'status' => 'delivered', |
| 107 | + 'delivery_date' => $request->delivery_date ?? now()->toDateString(), |
| 108 | + ]); |
| 109 | + return back()->with('success', 'Delivery confirmed.'); |
| 110 | + } |
| 111 | + |
| 112 | + public function destroy(DeliveryNote $deliveryNote): RedirectResponse |
| 113 | + { |
| 114 | + $this->authorize('delete', $deliveryNote); |
| 115 | + abort_unless($deliveryNote->status === 'draft', 422, 'Only draft delivery notes can be deleted.'); |
| 116 | + $deliveryNote->delete(); |
| 117 | + return redirect()->route('finance.delivery-notes.index')->with('success', 'Delivery note deleted.'); |
| 118 | + } |
| 119 | +} |
0 commit comments