engine->currentStage($visit); if ($stage === null || $stage->queue_context === null) { return; } $context = $stage->queue_context; if ($context === CareQueueContexts::CONSULTATION || CareQueueContexts::isSpecialty($context)) { $appointment = Appointment::query() ->where('visit_id', $visit->id) ->with(['patient', 'organization', 'practitioner', 'visit']) ->latest('id') ->first(); if ($appointment) { if ($appointment->status === Appointment::STATUS_COMPLETED) { $nextPosition = ((int) Appointment::owned($appointment->owner_ref) ->where('branch_id', $appointment->branch_id) ->whereIn('status', [Appointment::STATUS_WAITING, Appointment::STATUS_CHECKED_IN]) ->max('queue_position')) + 1; $appointment->forceFill([ 'status' => Appointment::STATUS_WAITING, 'waiting_at' => now(), 'started_at' => null, 'completed_at' => null, 'queue_position' => $nextPosition, ])->save(); } if (filled($appointment->queue_ticket_uuid) && in_array($appointment->queue_ticket_status, ['completed', 'cancelled'], true)) { $appointment->forceFill([ 'queue_ticket_uuid' => null, 'queue_ticket_number' => null, 'queue_ticket_status' => null, 'queue_service_point_uuid' => null, 'queue_destination' => null, 'queue_staff_display_name' => null, 'queue_routing_status' => null, ])->save(); } $this->queue->issueForAppointment($organization, $appointment); } return; } if ($context === CareQueueContexts::LABORATORY || $context === CareQueueContexts::IMAGING) { InvestigationRequest::query() ->where('visit_id', $visit->id) ->with(['patient', 'visit']) ->get() ->each(fn (InvestigationRequest $request) => $this->queue->issueForInvestigation($organization, $request)); return; } if ($context === CareQueueContexts::PHARMACY) { Prescription::query() ->where('visit_id', $visit->id) ->with(['patient', 'visit']) ->get() ->each(fn (Prescription $prescription) => $this->queue->issueForPrescription($organization, $prescription)); return; } if ($context === CareQueueContexts::BILLING) { Bill::query() ->where('visit_id', $visit->id) ->whereNotIn('status', [Bill::STATUS_PAID, Bill::STATUS_VOID]) ->get() ->each(fn (Bill $bill) => $this->queue->issueForBill($organization, $bill)); } } }