Files
ladill-queue/app/Http/Controllers/Qms/DeviceController.php
T
isaaccladandCursor bf879d6abe
Deploy Ladill Queue / deploy (push) Successful in 36s
Add hero sections to Queue index pages and soften analytics cards.
Introduce shared x-qms.page-hero with summary stats across queues,
tickets, counters, admin pages, and insights; remove bordered cards on
Analytics, Reports, Feedback, and Branches list views.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 07:32:10 +00:00

143 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers\Qms;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Qms\Concerns\ScopesToAccount;
use App\Models\Branch;
use App\Models\Device;
use App\Models\ServiceQueue;
use App\Services\Qms\AuditLogger;
use App\Services\Qms\DeviceService;
use App\Services\Qms\OrganizationResolver;
use App\Services\Qms\QmsPermissions;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class DeviceController extends Controller
{
use ScopesToAccount;
public function __construct(
protected DeviceService $devices,
) {}
public function index(Request $request): View
{
$this->authorizeAbility($request, 'displays.view');
$owner = $this->ownerRef($request);
$organization = $this->organization($request);
$member = app(OrganizationResolver::class)->memberFor($request->user());
$permissions = app(QmsPermissions::class);
$devices = Device::owned($owner)
->where('organization_id', $organization->id)
->with('branch')
->orderBy('name')
->paginate(20);
$deviceStats = Device::owned($owner)
->where('organization_id', $organization->id)
->get();
$heroStats = [
'total' => $deviceStats->count(),
'online' => $deviceStats->where('status', 'online')->count(),
'kiosks' => $deviceStats->where('type', 'kiosk')->count(),
];
return view('qms.devices.index', [
'devices' => $devices,
'organization' => $organization,
'deviceTypes' => config('qms.device_types'),
'canManage' => $permissions->can($member, 'displays.manage'),
'heroStats' => $heroStats,
]);
}
public function create(Request $request): View
{
$this->authorizeAbility($request, 'displays.manage');
$owner = $this->ownerRef($request);
$organization = $this->organization($request);
$branches = Branch::owned($owner)->where('organization_id', $organization->id)->orderBy('name')->get();
return view('qms.devices.create', [
'organization' => $organization,
'branches' => $branches,
'queues' => ServiceQueue::owned($owner)->where('organization_id', $organization->id)->orderBy('name')->get(),
'deviceTypes' => config('qms.device_types'),
]);
}
public function store(Request $request): RedirectResponse
{
$this->authorizeAbility($request, 'displays.manage');
$owner = $this->ownerRef($request);
$organization = $this->organization($request);
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'type' => ['required', 'string', 'in:'.implode(',', array_keys(config('qms.device_types')))],
'branch_id' => ['nullable', 'exists:queue_branches,id'],
'queue_ids' => ['nullable', 'array'],
'queue_ids.*' => ['integer', 'exists:queue_service_queues,id'],
'collect_name' => ['nullable', 'boolean'],
'collect_phone' => ['nullable', 'boolean'],
'reset_seconds' => ['nullable', 'integer', 'min:5', 'max:120'],
'welcome_message' => ['nullable', 'string', 'max:255'],
]);
$config = null;
if ($validated['type'] === 'kiosk') {
$queueIds = array_map('intval', $validated['queue_ids'] ?? []);
if ($queueIds !== []) {
$allowed = ServiceQueue::owned($owner)
->where('organization_id', $organization->id)
->whereIn('id', $queueIds)
->pluck('id')
->all();
$queueIds = array_values(array_intersect($queueIds, $allowed));
}
$config = [
'service_queue_ids' => $queueIds,
'collect_name' => $request->boolean('collect_name'),
'collect_phone' => $request->boolean('collect_phone'),
'reset_seconds' => $validated['reset_seconds'] ?? 15,
'welcome_message' => filled($validated['welcome_message'] ?? null)
? $validated['welcome_message']
: null,
];
}
$device = Device::create([
'owner_ref' => $owner,
'organization_id' => $organization->id,
'branch_id' => $validated['branch_id'] ?? null,
'name' => $validated['name'],
'type' => $validated['type'],
'status' => 'offline',
'config' => $config,
]);
$token = $this->devices->generateToken($device);
AuditLogger::record($owner, 'device.created', $organization->id, $owner, Device::class, $device->id);
return redirect()->route('qms.devices.index')
->with('success', 'Device registered. Token: '.$token);
}
public function regenerateToken(Request $request, Device $device): RedirectResponse
{
$this->authorizeAbility($request, 'displays.manage');
$this->authorizeOwner($request, $device);
$token = $this->devices->generateToken($device);
return back()->with('success', 'New device token: '.$token);
}
}