fix(turno): crear consentimiento siempre, enviar WA solo si hay teléfono
Estado queda 'pendiente' si no hay cel; se actualiza a 'enviado' al enviar WA. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
1a92707e3e
commit
434df7b741
@@ -102,88 +102,85 @@ try {
|
|||||||
|
|
||||||
notificarSSE($sesionId);
|
notificarSSE($sesionId);
|
||||||
|
|
||||||
// ── Enviar notificación WhatsApp con la plantilla configurada ──
|
// ── Crear registro de consentimiento (siempre, con o sin teléfono) ──
|
||||||
|
$enlaceConsentimiento = null;
|
||||||
|
$esSoloMuestra = ($prioCodigo === 'F');
|
||||||
|
if (!$esSoloMuestra) {
|
||||||
|
try {
|
||||||
|
$stmtForms = $pdo->prepare(
|
||||||
|
"SELECT tlc.formulario_id
|
||||||
|
FROM turnero_lugar_consentimientos tlc
|
||||||
|
JOIN lab_formularios f ON f.id = tlc.formulario_id AND f.is_active = 1
|
||||||
|
JOIN turnero_lugares l ON l.id = tlc.lugar_id AND l.tipo = 'recepcion' AND l.activo = 1
|
||||||
|
ORDER BY tlc.formulario_id ASC
|
||||||
|
LIMIT 1"
|
||||||
|
);
|
||||||
|
$stmtForms->execute();
|
||||||
|
$formRow = $stmtForms->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
if ($formRow) {
|
||||||
|
$conToken = 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)
|
||||||
|
);
|
||||||
|
$pdo->prepare(
|
||||||
|
"INSERT IGNORE INTO turnero_consentimientos
|
||||||
|
(turno_id, formulario_id, token, estado, creado_at)
|
||||||
|
VALUES (?, ?, ?, 'pendiente', NOW())"
|
||||||
|
)->execute([$turnoId, (int)$formRow['formulario_id'], $conToken]);
|
||||||
|
|
||||||
|
$baseUrl = defined('BASE_URL') ? rtrim(BASE_URL, '/') : '';
|
||||||
|
$enlaceConsentimiento = $baseUrl . '/ver_formulario_enviado.php?token=' . urlencode($conToken);
|
||||||
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// Sin consentimiento disponible, continuar sin link
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Enviar notificación WhatsApp ──
|
||||||
if ($pacienteTel) {
|
if ($pacienteTel) {
|
||||||
try {
|
try {
|
||||||
$stmtCfg = $pdo->prepare(
|
$stmtCfg = $pdo->prepare(
|
||||||
"SELECT clave, valor FROM lab_config WHERE clave IN ('turnero_wa_template','turnero_wa_lang','turnero_wa_kiosko_enabled')"
|
"SELECT clave, valor FROM lab_config WHERE clave IN ('turnero_wa_template','turnero_wa_template_muestra','turnero_wa_lang','turnero_wa_kiosko_enabled')"
|
||||||
);
|
);
|
||||||
$stmtCfg->execute();
|
$stmtCfg->execute();
|
||||||
$cfgWA = $stmtCfg->fetchAll(PDO::FETCH_KEY_PAIR);
|
$cfgWA = $stmtCfg->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||||
$waKioskoEnabled = ($cfgWA['turnero_wa_kiosko_enabled'] ?? '0') === '1';
|
$waKioskoEnabled = ($cfgWA['turnero_wa_kiosko_enabled'] ?? '0') === '1';
|
||||||
} catch (\Throwable $e) {
|
|
||||||
$waKioskoEnabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($pacienteTel && $waKioskoEnabled) {
|
|
||||||
try {
|
|
||||||
$cfgWA = [];
|
|
||||||
$stmtCfg = $pdo->prepare(
|
|
||||||
"SELECT clave, valor FROM lab_config WHERE clave IN ('turnero_wa_template','turnero_wa_template_muestra','turnero_wa_lang')"
|
|
||||||
);
|
|
||||||
$stmtCfg->execute();
|
|
||||||
$cfgWA = $stmtCfg->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
||||||
$esSoloMuestra = ($prioCodigo === 'F');
|
|
||||||
$waTemplate = $esSoloMuestra
|
$waTemplate = $esSoloMuestra
|
||||||
? ($cfgWA['turnero_wa_template_muestra'] ?? 'turno_muestra_pendiente')
|
? ($cfgWA['turnero_wa_template_muestra'] ?? 'turno_muestra_pendiente')
|
||||||
: ($cfgWA['turnero_wa_template'] ?? 'consentimiento_turno');
|
: ($cfgWA['turnero_wa_template'] ?? 'consentimiento_turno');
|
||||||
$waLang = $cfgWA['turnero_wa_lang'] ?? 'es_CO';
|
$waLang = $cfgWA['turnero_wa_lang'] ?? 'es_CO';
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$waTemplate = 'consentimiento_turno';
|
$waKioskoEnabled = true;
|
||||||
$waLang = 'es_CO';
|
$waTemplate = 'consentimiento_turno';
|
||||||
|
$waLang = 'es_CO';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pacienteTel && $waKioskoEnabled) {
|
||||||
$cel = preg_replace('/[\s\-\.]/', '', $pacienteTel);
|
$cel = preg_replace('/[\s\-\.]/', '', $pacienteTel);
|
||||||
if (!str_starts_with($cel, '+')) {
|
if (!str_starts_with($cel, '+')) {
|
||||||
$cel = '+57' . ltrim($cel, '0');
|
$cel = '+57' . ltrim($cel, '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Buscar consentimiento configurado en recepción y crear token ──
|
// Marcar como enviado si vamos a mandar WA
|
||||||
$enlaceConsentimiento = null;
|
if ($enlaceConsentimiento) {
|
||||||
$formularioNomWA = '';
|
|
||||||
if (!$esSoloMuestra) {
|
|
||||||
try {
|
try {
|
||||||
$stmtForms = $pdo->prepare(
|
$pdo->prepare(
|
||||||
"SELECT tlc.formulario_id, f.nombre AS formulario_nombre
|
"UPDATE turnero_consentimientos SET estado='enviado', enviado_at=NOW()
|
||||||
FROM turnero_lugar_consentimientos tlc
|
WHERE turno_id=? AND estado='pendiente'"
|
||||||
JOIN lab_formularios f ON f.id = tlc.formulario_id AND f.is_active = 1
|
)->execute([$turnoId]);
|
||||||
JOIN turnero_lugares l ON l.id = tlc.lugar_id AND l.tipo = 'recepcion' AND l.activo = 1
|
} catch (\Throwable $_) {}
|
||||||
ORDER BY tlc.formulario_id ASC
|
|
||||||
LIMIT 1"
|
|
||||||
);
|
|
||||||
$stmtForms->execute();
|
|
||||||
$formRow = $stmtForms->fetch(\PDO::FETCH_ASSOC);
|
|
||||||
|
|
||||||
if ($formRow) {
|
|
||||||
$token = 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)
|
|
||||||
);
|
|
||||||
$pdo->prepare(
|
|
||||||
"INSERT IGNORE INTO turnero_consentimientos
|
|
||||||
(turno_id, formulario_id, token, estado, enviado_at)
|
|
||||||
VALUES (?, ?, ?, 'enviado', NOW())"
|
|
||||||
)->execute([$turnoId, (int)$formRow['formulario_id'], $token]);
|
|
||||||
|
|
||||||
$baseUrl = defined('BASE_URL') ? rtrim(BASE_URL, '/') : '';
|
|
||||||
$enlaceConsentimiento = $baseUrl . '/ver_formulario_enviado.php?token=' . urlencode($token);
|
|
||||||
$formularioNomWA = $formRow['formulario_nombre'];
|
|
||||||
}
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
// Sin consentimiento disponible, continuar sin link
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
require_once __DIR__ . '/../../../services/WhatsAppService.php';
|
require_once __DIR__ . '/../../../services/WhatsAppService.php';
|
||||||
$nombreWA = $pacienteNombre ?: 'Paciente';
|
$nombreWA = $pacienteNombre ?: 'Paciente';
|
||||||
try {
|
try {
|
||||||
$wa = new WhatsAppService('turnero');
|
$wa = new WhatsAppService('turnero');
|
||||||
// La plantilla solo tiene {{1}} = código de turno en el body + botón URL
|
|
||||||
$wa->sendTemplateMessage(
|
$wa->sendTemplateMessage(
|
||||||
$cel,
|
$cel,
|
||||||
$waTemplate,
|
$waTemplate,
|
||||||
|
|||||||
Reference in New Issue
Block a user