authorizeAbility($request, 'appointments.view'); $organization = $this->organization($request); $branchScope = app(OrganizationResolver::class)->branchScope($this->member($request)); $branches = Branch::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->where('is_active', true) ->when($branchScope, fn ($q) => $q->where('id', $branchScope)) ->orderBy('name') ->get(); $branchId = (int) ($request->input('branch_id') ?: $branchScope ?: $branches->first()?->id); $practitionerId = $request->input('practitioner_id') ? (int) $request->input('practitioner_id') : null; $queue = $branchId ? $this->appointments->queue($this->ownerRef($request), $branchId, $practitionerId) : collect(); $inConsultation = Appointment::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->where('status', Appointment::STATUS_IN_CONSULTATION) ->when($branchId, fn ($q) => $q->where('branch_id', $branchId)) ->when($practitionerId, fn ($q) => $q->where('practitioner_id', $practitionerId)) ->with(['patient', 'practitioner', 'consultation']) ->orderBy('started_at') ->get(); $canManageQueue = app(\App\Services\Care\CarePermissions::class) ->can($this->member($request), 'queue.manage'); $canConsult = app(\App\Services\Care\CarePermissions::class) ->can($this->member($request), 'consultations.manage'); $heroStats = [ 'waiting' => $queue->count(), 'in_consultation' => $inConsultation->count(), 'today' => Appointment::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->when($branchId, fn ($q) => $q->where('branch_id', $branchId)) ->whereDate('scheduled_at', today()) ->count(), ]; return view('care.queue.index', [ 'organization' => $organization, 'branches' => $branches, 'branchId' => $branchId, 'practitioners' => Practitioner::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->where('is_active', true) ->orderBy('name') ->get(), 'practitionerId' => $practitionerId, 'queue' => $queue, 'inConsultation' => $inConsultation, 'canManageQueue' => $canManageQueue, 'canConsult' => $canConsult, 'heroStats' => $heroStats, ]); } public function start(Request $request, Appointment $appointment): RedirectResponse { $this->authorizeAbility($request, 'consultations.manage'); $this->authorizeAppointment($request, $appointment); $practitionerId = $request->input('practitioner_id') ? (int) $request->input('practitioner_id') : $appointment->practitioner_id; $this->appointments->startConsultation( $appointment, $this->ownerRef($request), $practitionerId, $this->ownerRef($request), ); $consultation = $this->consultations->startFromAppointment( $appointment->fresh(), $this->ownerRef($request), $this->ownerRef($request), ); return redirect()->route('care.consultations.show', $consultation) ->with('success', 'Consultation started.'); } protected function authorizeAppointment(Request $request, Appointment $appointment): void { $this->authorizeOwner($request, $appointment); abort_unless($appointment->organization_id === $this->organization($request)->id, 404); $branchId = app(OrganizationResolver::class)->branchScope($this->member($request)); if ($branchId !== null && $appointment->branch_id !== $branchId) { abort(404); } } }