status === 'scheduled', 422, 'Appointment cannot be checked in.'); $queue = $appointment->serviceQueue ?? $this->defaultQueue($appointment); abort_unless($queue, 422, 'No queue configured for this appointment.'); $ticket = $this->engine->issueTicket($queue, $appointment->owner_ref, [ 'customer_name' => $appointment->customer_name, 'customer_phone' => $appointment->customer_phone, 'customer_email' => $appointment->customer_email, 'priority' => 'appointment', 'source' => 'appointment', 'actor_ref' => $actorRef, 'metadata' => ['appointment_id' => $appointment->id], ]); $appointment->update([ 'status' => 'checked_in', 'checked_in_at' => now(), 'ticket_id' => $ticket->id, 'service_queue_id' => $queue->id, ]); AuditLogger::record($appointment->owner_ref, 'appointment.checked_in', $appointment->organization_id, $actorRef, QueueAppointment::class, $appointment->id); $org = $appointment->organization; if ($org) { app(StaffNotifier::class)->queueEvent( $org, 'Appointment checked in', "{$appointment->customer_name} checked in for {$appointment->scheduled_at->format('H:i')}.", route('qms.tickets.show', $ticket), 'calendar', ); } return $ticket; } protected function defaultQueue(QueueAppointment $appointment): ?ServiceQueue { return ServiceQueue::owned($appointment->owner_ref) ->where('branch_id', $appointment->branch_id) ->where('is_active', true) ->orderBy('name') ->first(); } }