|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\POS\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\POS\Models\PosOrder; |
| 7 | +use App\Modules\POS\Models\PosOrderItem; |
| 8 | +use App\Modules\POS\Models\PosSession; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Inertia\Inertia; |
| 11 | +use Inertia\Response; |
| 12 | + |
| 13 | +class PosOrderController extends Controller |
| 14 | +{ |
| 15 | + public function index(Request $request): Response |
| 16 | + { |
| 17 | + $orders = PosOrder::with(['session']) |
| 18 | + ->when($request->status, fn ($q) => $q->where('status', $request->status)) |
| 19 | + ->when($request->session_id, fn ($q) => $q->where('session_id', $request->session_id)) |
| 20 | + ->latest() |
| 21 | + ->paginate(25) |
| 22 | + ->through(fn ($o) => [ |
| 23 | + 'id' => $o->id, |
| 24 | + 'receipt_number' => $o->receipt_number, |
| 25 | + 'customer_name' => $o->customer_name, |
| 26 | + 'session' => $o->session ? ['id' => $o->session->id, 'name' => $o->session->name] : null, |
| 27 | + 'total' => $o->total, |
| 28 | + 'payment_method' => $o->payment_method, |
| 29 | + 'status' => $o->status, |
| 30 | + 'created_at' => $o->created_at, |
| 31 | + ]); |
| 32 | + |
| 33 | + return Inertia::render('POS/Orders/Index', [ |
| 34 | + 'orders' => $orders, |
| 35 | + 'filters' => $request->only(['status', 'session_id']), |
| 36 | + ]); |
| 37 | + } |
| 38 | + |
| 39 | + public function store(Request $request): Response |
| 40 | + { |
| 41 | + $data = $request->validate([ |
| 42 | + 'session_id' => 'required|exists:pos_sessions,id', |
| 43 | + 'customer_name' => 'nullable|string|max:255', |
| 44 | + 'customer_email' => 'nullable|email|max:255', |
| 45 | + 'discount_amount' => 'nullable|numeric|min:0', |
| 46 | + 'tax_amount' => 'nullable|numeric|min:0', |
| 47 | + 'amount_paid' => 'required|numeric|min:0', |
| 48 | + 'payment_method' => 'required|in:cash,card,digital_wallet,split', |
| 49 | + 'notes' => 'nullable|string', |
| 50 | + 'items' => 'required|array|min:1', |
| 51 | + 'items.*.product_id' => 'nullable|exists:products,id', |
| 52 | + 'items.*.product_name' => 'required|string', |
| 53 | + 'items.*.product_sku' => 'nullable|string', |
| 54 | + 'items.*.quantity' => 'required|numeric|min:0.001', |
| 55 | + 'items.*.unit_price' => 'required|numeric|min:0', |
| 56 | + 'items.*.discount_percent' => 'nullable|numeric|min:0|max:100', |
| 57 | + 'items.*.line_total' => 'required|numeric|min:0', |
| 58 | + ]); |
| 59 | + |
| 60 | + $subtotal = collect($data['items'])->sum('line_total'); |
| 61 | + $discountAmount = $data['discount_amount'] ?? 0; |
| 62 | + $taxAmount = $data['tax_amount'] ?? 0; |
| 63 | + $total = $subtotal - $discountAmount + $taxAmount; |
| 64 | + $amountPaid = $data['amount_paid']; |
| 65 | + $changeGiven = max(0, $amountPaid - $total); |
| 66 | + |
| 67 | + $order = PosOrder::create([ |
| 68 | + 'tenant_id' => auth()->user()->tenant_id, |
| 69 | + 'session_id' => $data['session_id'], |
| 70 | + 'customer_name' => $data['customer_name'] ?? null, |
| 71 | + 'customer_email' => $data['customer_email'] ?? null, |
| 72 | + 'subtotal' => $subtotal, |
| 73 | + 'discount_amount' => $discountAmount, |
| 74 | + 'tax_amount' => $taxAmount, |
| 75 | + 'total' => $total, |
| 76 | + 'amount_paid' => $amountPaid, |
| 77 | + 'change_given' => $changeGiven, |
| 78 | + 'payment_method' => $data['payment_method'], |
| 79 | + 'status' => 'completed', |
| 80 | + 'notes' => $data['notes'] ?? null, |
| 81 | + 'served_by' => auth()->id(), |
| 82 | + 'created_by' => auth()->id(), |
| 83 | + ]); |
| 84 | + |
| 85 | + $order->receipt_number = $order->generateReceiptNumber(); |
| 86 | + $order->save(); |
| 87 | + |
| 88 | + foreach ($data['items'] as $item) { |
| 89 | + PosOrderItem::create([ |
| 90 | + 'order_id' => $order->id, |
| 91 | + 'product_id' => $item['product_id'] ?? null, |
| 92 | + 'product_name' => $item['product_name'], |
| 93 | + 'product_sku' => $item['product_sku'] ?? null, |
| 94 | + 'quantity' => $item['quantity'], |
| 95 | + 'unit_price' => $item['unit_price'], |
| 96 | + 'discount_percent' => $item['discount_percent'] ?? 0, |
| 97 | + 'line_total' => $item['line_total'], |
| 98 | + ]); |
| 99 | + } |
| 100 | + |
| 101 | + // Update session total_sales |
| 102 | + $session = PosSession::find($data['session_id']); |
| 103 | + if ($session) { |
| 104 | + $session->total_sales += $total; |
| 105 | + $session->save(); |
| 106 | + } |
| 107 | + |
| 108 | + $order->load('items', 'session', 'servedBy'); |
| 109 | + |
| 110 | + return Inertia::render('POS/Orders/Receipt', [ |
| 111 | + 'order' => [ |
| 112 | + 'id' => $order->id, |
| 113 | + 'receipt_number' => $order->receipt_number, |
| 114 | + 'customer_name' => $order->customer_name, |
| 115 | + 'customer_email' => $order->customer_email, |
| 116 | + 'subtotal' => $order->subtotal, |
| 117 | + 'discount_amount'=> $order->discount_amount, |
| 118 | + 'tax_amount' => $order->tax_amount, |
| 119 | + 'total' => $order->total, |
| 120 | + 'amount_paid' => $order->amount_paid, |
| 121 | + 'change_given' => $order->change_given, |
| 122 | + 'payment_method' => $order->payment_method, |
| 123 | + 'status' => $order->status, |
| 124 | + 'created_at' => $order->created_at, |
| 125 | + 'session' => $order->session ? ['id' => $order->session->id, 'name' => $order->session->name] : null, |
| 126 | + 'items' => $order->items->map(fn ($i) => [ |
| 127 | + 'id' => $i->id, |
| 128 | + 'product_name' => $i->product_name, |
| 129 | + 'product_sku' => $i->product_sku, |
| 130 | + 'quantity' => $i->quantity, |
| 131 | + 'unit_price' => $i->unit_price, |
| 132 | + 'discount_percent' => $i->discount_percent, |
| 133 | + 'line_total' => $i->line_total, |
| 134 | + ]), |
| 135 | + ], |
| 136 | + ]); |
| 137 | + } |
| 138 | + |
| 139 | + public function show(PosOrder $order): Response |
| 140 | + { |
| 141 | + $order->load(['items', 'session', 'servedBy']); |
| 142 | + |
| 143 | + return Inertia::render('POS/Orders/Receipt', [ |
| 144 | + 'order' => [ |
| 145 | + 'id' => $order->id, |
| 146 | + 'receipt_number' => $order->receipt_number, |
| 147 | + 'customer_name' => $order->customer_name, |
| 148 | + 'customer_email' => $order->customer_email, |
| 149 | + 'subtotal' => $order->subtotal, |
| 150 | + 'discount_amount'=> $order->discount_amount, |
| 151 | + 'tax_amount' => $order->tax_amount, |
| 152 | + 'total' => $order->total, |
| 153 | + 'amount_paid' => $order->amount_paid, |
| 154 | + 'change_given' => $order->change_given, |
| 155 | + 'payment_method' => $order->payment_method, |
| 156 | + 'status' => $order->status, |
| 157 | + 'created_at' => $order->created_at, |
| 158 | + 'session' => $order->session ? ['id' => $order->session->id, 'name' => $order->session->name] : null, |
| 159 | + 'items' => $order->items->map(fn ($i) => [ |
| 160 | + 'id' => $i->id, |
| 161 | + 'product_name' => $i->product_name, |
| 162 | + 'product_sku' => $i->product_sku, |
| 163 | + 'quantity' => $i->quantity, |
| 164 | + 'unit_price' => $i->unit_price, |
| 165 | + 'discount_percent' => $i->discount_percent, |
| 166 | + 'line_total' => $i->line_total, |
| 167 | + ]), |
| 168 | + ], |
| 169 | + ]); |
| 170 | + } |
| 171 | + |
| 172 | + public function refund(PosOrder $order): \Illuminate\Http\RedirectResponse |
| 173 | + { |
| 174 | + $order->status = 'refunded'; |
| 175 | + $order->save(); |
| 176 | + |
| 177 | + $session = $order->session; |
| 178 | + if ($session) { |
| 179 | + $session->total_refunds += $order->total; |
| 180 | + $session->save(); |
| 181 | + } |
| 182 | + |
| 183 | + return redirect()->back()->with('success', 'Order refunded successfully.'); |
| 184 | + } |
| 185 | +} |
0 commit comments