up
This commit is contained in:
@@ -98,67 +98,48 @@ try {
|
||||
|
||||
notificarSSE($sesionId);
|
||||
|
||||
// ── Auto-crear consentimiento principal + enviar WhatsApp ──
|
||||
try {
|
||||
$stmtPc = $pdo->prepare(
|
||||
"SELECT id, nombre FROM lab_formularios WHERE is_principal = 1 AND tipo = 'consentimiento' AND is_active = 1 LIMIT 1"
|
||||
);
|
||||
$stmtPc->execute();
|
||||
$principalForm = $stmtPc->fetch(PDO::FETCH_ASSOC);
|
||||
if ($principalForm) {
|
||||
$tokenPrincipal = sprintf(
|
||||
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
// ── Enviar notificación WhatsApp con la plantilla configurada ──
|
||||
if ($pacienteTel) {
|
||||
try {
|
||||
$stmtCfg = $pdo->prepare(
|
||||
"SELECT clave, valor FROM lab_config WHERE clave IN ('turnero_wa_template','turnero_wa_lang')"
|
||||
);
|
||||
$stmtIns = $pdo->prepare(
|
||||
"INSERT IGNORE INTO turnero_consentimientos (turno_id, formulario_id, token, estado)
|
||||
VALUES (?, ?, ?, 'pendiente')"
|
||||
);
|
||||
$stmtIns->execute([$turnoId, (int)$principalForm['id'], $tokenPrincipal]);
|
||||
$stmtCfg->execute();
|
||||
$cfgWA = $stmtCfg->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
$waTemplate = $cfgWA['turnero_wa_template'] ?? 'consentimiento_turno';
|
||||
$waLang = $cfgWA['turnero_wa_lang'] ?? 'es_CO';
|
||||
} catch (\Throwable $e) {
|
||||
$waTemplate = 'consentimiento_turno';
|
||||
$waLang = 'es_CO';
|
||||
}
|
||||
|
||||
// Enviar WhatsApp si tenemos celular
|
||||
if ($pacienteTel) {
|
||||
$cel = preg_replace('/[\s\-\.]/', '', $pacienteTel);
|
||||
if (!str_starts_with($cel, '+')) {
|
||||
$cel = '+57' . ltrim($cel, '0');
|
||||
}
|
||||
$enlaceFirma = (defined('BASE_URL') ? rtrim(BASE_URL, '/') : '')
|
||||
. '/ver_formulario_enviado.php?token=' . urlencode($tokenPrincipal);
|
||||
$nombrePacWA = $pacienteNombre ?: 'Paciente';
|
||||
require_once __DIR__ . '/../../../services/WhatsAppService.php';
|
||||
try {
|
||||
$wa = new WhatsAppService();
|
||||
$wa->sendTemplateMessage(
|
||||
$cel,
|
||||
'consentimiento_turno',
|
||||
'es',
|
||||
[
|
||||
htmlspecialchars($nombrePacWA, ENT_QUOTES),
|
||||
htmlspecialchars($principalForm['nombre'], ENT_QUOTES),
|
||||
$codigo,
|
||||
],
|
||||
[$enlaceFirma]
|
||||
);
|
||||
} catch (\Throwable $eTmpl) {
|
||||
try {
|
||||
$mensajeTexto = "Hola {$nombrePacWA}, le informamos que para su turno *{$codigo}* "
|
||||
. "debe firmar el siguiente consentimiento informado:\n\n"
|
||||
. "*{$principalForm['nombre']}*\n\n"
|
||||
. "Puede firmarlo en el siguiente enlace:\n{$enlaceFirma}\n\n"
|
||||
. "Si ya firmó este documento presencial, ignore este mensaje.";
|
||||
$wa->sendTextMessage($cel, $mensajeTexto);
|
||||
} catch (\Throwable $eTxt) {
|
||||
// Silenciar error de WhatsApp, el turno ya se creó
|
||||
}
|
||||
}
|
||||
$cel = preg_replace('/[\s\-\.]/', '', $pacienteTel);
|
||||
if (!str_starts_with($cel, '+')) {
|
||||
$cel = '+57' . ltrim($cel, '0');
|
||||
}
|
||||
$nombrePacWA = $pacienteNombre ?: 'Paciente';
|
||||
require_once __DIR__ . '/../../../services/WhatsAppService.php';
|
||||
try {
|
||||
$wa = new WhatsAppService();
|
||||
$wa->sendTemplateMessage(
|
||||
$cel,
|
||||
$waTemplate,
|
||||
$waLang,
|
||||
[
|
||||
htmlspecialchars($nombrePacWA, ENT_QUOTES),
|
||||
$codigo,
|
||||
'',
|
||||
],
|
||||
[]
|
||||
);
|
||||
} catch (\Throwable $eTmpl) {
|
||||
try {
|
||||
$mensajeTexto = "Hola {$nombrePacWA}, su turno *{$codigo}* ha sido registrado en {$codigo}. Preséntese al laboratorio.";
|
||||
$wa->sendTextMessage($cel, $mensajeTexto);
|
||||
} catch (\Throwable $eTxt) {
|
||||
// Silenciar
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// Silenciar error — el turno ya se creó exitosamente
|
||||
}
|
||||
|
||||
// Contar posición en la cola
|
||||
|
||||
Reference in New Issue
Block a user