From 2455417bb4f56bfb95da39ddc93b4d796b251f30 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Wed, 29 Jul 2026 21:23:07 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20encuesta=20de=20satisfacci=C3=B3n=20pos?= =?UTF-8?q?t-finalizar=20toma=20de=20muestras?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- modules/turnero/api/enviar_encuesta.php | 52 +++++++++++++++++++ modules/turnero/views/lugar.php | 69 ++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 modules/turnero/api/enviar_encuesta.php diff --git a/modules/turnero/api/enviar_encuesta.php b/modules/turnero/api/enviar_encuesta.php new file mode 100644 index 0000000..3835133 --- /dev/null +++ b/modules/turnero/api/enviar_encuesta.php @@ -0,0 +1,52 @@ +prepare( + "SELECT u.id AS user_id, u.phone_number, t.paciente_cel, t.paciente_nombre + FROM turnero_turnos t + LEFT JOIN lab_pacientes lp ON lp.id = t.paciente_id + LEFT JOIN users u ON u.id = lp.user_id + WHERE t.id = ? LIMIT 1" +); +$row->execute([$turnoId]); +$pac = $row->fetch(PDO::FETCH_ASSOC); + +if (!$pac) jsonError('Turno no encontrado.', 404); + +$userId = $pac['user_id'] ? (int)$pac['user_id'] : null; +$phone = $pac['phone_number'] ?? null; + +// Fallback: buscar por paciente_cel si no hay user_id +if (!$userId && !empty($pac['paciente_cel'])) { + $cel = preg_replace('/\D/', '', $pac['paciente_cel']); + $u2 = $pdo->prepare("SELECT id, phone_number FROM users WHERE REPLACE(phone_number,'+','') LIKE ? LIMIT 1"); + $u2->execute(['%' . substr($cel, -10)]); + $uRow = $u2->fetch(PDO::FETCH_ASSOC); + if ($uRow) { $userId = (int)$uRow['id']; $phone = $uRow['phone_number']; } +} + +if (!$userId || !$phone) jsonError('El paciente no tiene número de WhatsApp registrado.'); + +try { + $wa = new WhatsAppService('turnero'); + $meta = ['canal' => 'turnero', 'operator_id' => adminId()]; + $wa->sendTemplateMessage($phone, 'encuesta', 'es_CO', [], [], null, $meta); + jsonOk(['mensaje' => 'Encuesta enviada a ' . ($pac['paciente_nombre'] ?? $phone)]); +} catch (\Throwable $e) { + jsonError('Error al enviar: ' . $e->getMessage()); +} diff --git a/modules/turnero/views/lugar.php b/modules/turnero/views/lugar.php index e845003..10eca26 100644 --- a/modules/turnero/views/lugar.php +++ b/modules/turnero/views/lugar.php @@ -994,6 +994,26 @@ require_once __DIR__ . '/../../../shared/components/sidebar.php'; + +
+ + + + +
+