Deploy Ladill Care / deploy (push) Successful in 1m29s
Replace optional “All branches” with required branch checkboxes; doctors may switch only among assigned sites, never org-wide by default. Co-authored-by: Cursor <cursoragent@cursor.com>
246 lines
9.8 KiB
PHP
246 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Care;
|
|
|
|
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Appointment;
|
|
use App\Models\Practitioner;
|
|
use App\Services\Care\AppointmentService;
|
|
use App\Services\Care\BranchContext;
|
|
use App\Services\Care\CarePermissions;
|
|
use App\Services\Care\CareQueueBridge;
|
|
use App\Services\Care\CareQueueContexts;
|
|
use App\Services\Care\ConsultationService;
|
|
use App\Services\Care\OrganizationResolver;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class QueueController extends Controller
|
|
{
|
|
use ScopesToAccount;
|
|
|
|
public function __construct(
|
|
protected AppointmentService $appointments,
|
|
protected ConsultationService $consultations,
|
|
protected CareQueueBridge $queueBridge,
|
|
protected BranchContext $branchContext,
|
|
) {}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$this->authorizeAbility($request, 'appointments.view');
|
|
$organization = $this->organization($request);
|
|
$member = $this->member($request);
|
|
$owner = $this->ownerRef($request);
|
|
$resolver = app(OrganizationResolver::class);
|
|
$practitionerScope = $resolver->practitionerScope($organization, $member);
|
|
$lockToPractitioner = $practitionerScope !== null;
|
|
|
|
$branches = $this->branchContext->branches($request, $organization, $member);
|
|
$branchId = $this->branchContext->resolve($request, $organization, $member);
|
|
$practitionerId = $lockToPractitioner
|
|
? ($practitionerScope[0] ?? null)
|
|
: ($request->input('practitioner_id') ? (int) $request->input('practitioner_id') : null);
|
|
|
|
$queueIntegration = ['enabled' => false];
|
|
if ($this->queueBridge->isEnabled($organization)) {
|
|
$syncBranches = $lockToPractitioner
|
|
? Practitioner::owned($owner)
|
|
->whereIn('id', $practitionerScope ?: [0])
|
|
->pluck('branch_id')
|
|
->map(fn ($id) => (int) $id)
|
|
->unique()
|
|
->filter()
|
|
->values()
|
|
->all()
|
|
: array_filter([$branchId]);
|
|
|
|
foreach ($syncBranches as $syncBranchId) {
|
|
$this->queueBridge->syncMissingAppointmentTickets($organization, $syncBranchId, 100);
|
|
}
|
|
|
|
if ($branchId > 0) {
|
|
$queueIntegration = $this->queueBridge->listMeta($organization, CareQueueContexts::CONSULTATION, $branchId);
|
|
} elseif ($syncBranches !== []) {
|
|
$queueIntegration = $this->queueBridge->listMeta(
|
|
$organization,
|
|
CareQueueContexts::CONSULTATION,
|
|
(int) $syncBranches[0],
|
|
);
|
|
}
|
|
}
|
|
|
|
if ($lockToPractitioner) {
|
|
$queue = $this->appointments->queueForPractitioners(
|
|
$owner,
|
|
$practitionerScope ?? [],
|
|
$branchId > 0 ? $branchId : null,
|
|
);
|
|
$inConsultation = Appointment::owned($owner)
|
|
->where('organization_id', $organization->id)
|
|
->where('status', Appointment::STATUS_IN_CONSULTATION)
|
|
->whereIn('practitioner_id', $practitionerScope ?: [0])
|
|
->with(['patient', 'practitioner', 'consultation'])
|
|
->orderBy('started_at')
|
|
->get();
|
|
} else {
|
|
$queue = $branchId
|
|
? $this->appointments->queue($owner, $branchId, $practitionerId)
|
|
: collect();
|
|
$inConsultation = Appointment::owned($owner)
|
|
->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(CarePermissions::class)->can($member, 'queue.manage');
|
|
$canConsult = app(CarePermissions::class)->can($member, 'consultations.manage');
|
|
|
|
$todayQuery = Appointment::owned($owner)
|
|
->where('organization_id', $organization->id)
|
|
->whereDate('scheduled_at', today());
|
|
if ($lockToPractitioner) {
|
|
$todayQuery->whereIn('practitioner_id', $practitionerScope ?: [0]);
|
|
} elseif ($branchId) {
|
|
$todayQuery->where('branch_id', $branchId);
|
|
}
|
|
|
|
$heroStats = [
|
|
'waiting' => $queue->count(),
|
|
'in_consultation' => $inConsultation->count(),
|
|
'today' => $todayQuery->count(),
|
|
];
|
|
|
|
return view('care.queue.index', [
|
|
'organization' => $organization,
|
|
'branches' => $branches,
|
|
'branchId' => $branchId,
|
|
'canSwitchBranch' => $this->branchContext->canSwitch($member),
|
|
'lockToPractitioner' => $lockToPractitioner,
|
|
'practitioners' => $lockToPractitioner
|
|
? collect()
|
|
: Practitioner::owned($owner)
|
|
->where('organization_id', $organization->id)
|
|
->where('is_active', true)
|
|
->when($branchId, fn ($q) => $q->where('branch_id', $branchId))
|
|
->orderBy('name')
|
|
->get(),
|
|
'practitionerId' => $practitionerId,
|
|
'queue' => $queue,
|
|
'inConsultation' => $inConsultation,
|
|
'canManageQueue' => $canManageQueue,
|
|
'canConsult' => $canConsult,
|
|
'heroStats' => $heroStats,
|
|
'queueIntegration' => $queueIntegration,
|
|
]);
|
|
}
|
|
|
|
public function callNext(Request $request): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'appointments.view');
|
|
$organization = $this->organization($request);
|
|
abort_unless($this->queueBridge->isEnabled($organization), 404);
|
|
|
|
$member = $this->member($request);
|
|
$practitionerScope = app(OrganizationResolver::class)->practitionerScope($organization, $member);
|
|
$branchId = $this->branchContext->resolve($request, $organization, $member);
|
|
$practitionerId = $request->input('practitioner_id') ? (int) $request->input('practitioner_id') : null;
|
|
|
|
if ($practitionerScope !== null) {
|
|
abort_unless($practitionerScope !== [], 422);
|
|
$practitionerId = $practitionerScope[0];
|
|
$prac = Practitioner::owned($this->ownerRef($request))->find($practitionerId);
|
|
$branchId = (int) ($prac?->branch_id ?: $branchId);
|
|
}
|
|
|
|
abort_unless($branchId > 0, 422);
|
|
|
|
$result = $this->queueBridge->callNext(
|
|
$organization,
|
|
CareQueueContexts::CONSULTATION,
|
|
$branchId,
|
|
$member,
|
|
$practitionerId,
|
|
);
|
|
$ticket = $result['ticket'] ?? null;
|
|
if (! $ticket) {
|
|
return back()->with('info', 'No patients waiting at your consultation service point.');
|
|
}
|
|
|
|
return back()->with('success', 'Called '.$ticket['ticket_number'].' — '.($ticket['customer_name'] ?? 'patient').'.');
|
|
}
|
|
|
|
public function completeTicket(Request $request, Appointment $appointment): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'consultations.manage');
|
|
$this->authorizeAppointment($request, $appointment);
|
|
$organization = $this->organization($request);
|
|
abort_unless($this->queueBridge->isEnabled($organization), 404);
|
|
|
|
$ticket = $this->queueBridge->complete($organization, $appointment);
|
|
if (! $ticket) {
|
|
return back()->with('error', 'Could not complete queue ticket.');
|
|
}
|
|
|
|
return back()->with('success', 'Completed '.$ticket['ticket_number'].'.');
|
|
}
|
|
|
|
public function recall(Request $request, Appointment $appointment): RedirectResponse
|
|
{
|
|
$this->authorizeAbility($request, 'consultations.manage');
|
|
$this->authorizeAppointment($request, $appointment);
|
|
$organization = $this->organization($request);
|
|
abort_unless($this->queueBridge->isEnabled($organization), 404);
|
|
|
|
$ticket = $this->queueBridge->recall($organization, $appointment);
|
|
if (! $ticket) {
|
|
return back()->with('error', 'Could not call patient again.');
|
|
}
|
|
|
|
return back()->with('success', 'Called again '.$ticket['ticket_number'].'.');
|
|
}
|
|
|
|
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;
|
|
|
|
try {
|
|
$this->appointments->startConsultation(
|
|
$appointment,
|
|
$this->ownerRef($request),
|
|
$practitionerId,
|
|
$this->ownerRef($request),
|
|
);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return back()->with('error', $e->getMessage());
|
|
}
|
|
|
|
$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);
|
|
$this->authorizeBranch($request, (int) $appointment->branch_id);
|
|
}
|
|
}
|