where('store_id', $request->user()->id) ->where('status', 'pending_payment') ->orderBy('created_at') ->get(); return $this->ok($orders); } public function complete(Request $request, string $uuid): JsonResponse { $request->validate([ 'scanned_qr_data' => 'required|string', ]); $order = QrPurchaseOrder::where('uuid', $uuid) ->where('store_id', $request->user()->id) ->where('status', 'pending_payment') ->firstOrFail(); // Find the scanned QR code $qrCode = QrCode::where('serial', $request->scanned_qr_data)->first(); if (!$qrCode) { return $this->fail('Invalid QR Code. Not found in the system.', null, 404); } if (! $qrCode->status->equals(\App\States\QrCode\Allocated::class) && ! $qrCode->status->equals(\App\States\QrCode\Unassigned::class)) { return $this->fail('This QR Code cannot be assigned (already used or active).', null, 422); } DB::transaction(function () use ($order, $qrCode) { $order->update([ 'status' => 'completed', 'completed_at' => now(), 'qr_code_id' => $qrCode->id, ]); $qrCode->status->transitionTo(\App\States\QrCode\Active::class); $qrCode->update([ 'assigned_to_user_id' => $order->resident_id, 'activated_at' => now(), ]); }); return $this->ok($order->fresh(), 'QR Purchase completed and assigned successfully.'); } }