From ded19cbb8d9fff27ad7b2fc7a931f17092cc54ac Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Mon, 16 Mar 2026 18:36:11 -0500 Subject: [PATCH] Cambios --- api/get_broadcast_history.php | 65 +++++++++++++ api/send_broadcast.php | 53 ++++++++++ assets/js/app_simple.js | 4 + classes/lab/Domicilio.php | 1 + index.php | 120 +++++++++++++++++++++++ lab_domicilios.php | 178 +++++++++++++++++++++++++++++++++- 6 files changed, 416 insertions(+), 5 deletions(-) create mode 100644 api/get_broadcast_history.php diff --git a/api/get_broadcast_history.php b/api/get_broadcast_history.php new file mode 100644 index 0000000..fae2924 --- /dev/null +++ b/api/get_broadcast_history.php @@ -0,0 +1,65 @@ + false, 'error' => 'Método no permitido']); + exit; +} + +$db = Database::getInstance(); + +// Crear tabla si no existe todavía +$db->getConnection()->exec(" + CREATE TABLE IF NOT EXISTS broadcast_history ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + sent_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + message_type VARCHAR(20) NOT NULL DEFAULT 'text', + selection_type VARCHAR(20) NOT NULL DEFAULT 'filter', + filter_used VARCHAR(50) NULL, + template_name VARCHAR(120) NULL, + message_preview TEXT NULL, + total_users INT UNSIGNED NOT NULL DEFAULT 0, + sent_count INT UNSIGNED NOT NULL DEFAULT 0, + error_count INT UNSIGNED NOT NULL DEFAULT 0, + sent_by INT UNSIGNED NULL, + sent_by_name VARCHAR(120) NULL, + INDEX idx_sent_at (sent_at) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci +"); + +$page = max(1, (int)($_GET['page'] ?? 1)); +$perPage = min(100, max(5, (int)($_GET['per_page'] ?? 20))); +$offset = ($page - 1) * $perPage; + +try { + $total = (int)$db->getConnection() + ->query("SELECT COUNT(*) FROM broadcast_history") + ->fetchColumn(); + + $rows = $db->fetchAll( + "SELECT * FROM broadcast_history ORDER BY sent_at DESC LIMIT ? OFFSET ?", + [$perPage, $offset] + ); + + echo json_encode([ + 'success' => true, + 'data' => $rows, + 'total' => $total, + 'page' => $page, + 'per_page' => $perPage, + 'pages' => (int)ceil($total / $perPage), + ]); +} catch (Exception $e) { + error_log('get_broadcast_history.php: ' . $e->getMessage()); + http_response_code(500); + echo json_encode(['success' => false, 'error' => 'Error interno del servidor']); +} diff --git a/api/send_broadcast.php b/api/send_broadcast.php index fb38e63..54a4f8c 100644 --- a/api/send_broadcast.php +++ b/api/send_broadcast.php @@ -9,6 +9,27 @@ require_once '../config/config.php'; // Verificar autenticación requireAuthentication(); +// Asegurar que la tabla de historial existe +try { + Database::getInstance()->getConnection()->exec(" + CREATE TABLE IF NOT EXISTS broadcast_history ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + sent_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + message_type VARCHAR(20) NOT NULL DEFAULT 'text', + selection_type VARCHAR(20) NOT NULL DEFAULT 'filter', + filter_used VARCHAR(50) NULL, + template_name VARCHAR(120) NULL, + message_preview TEXT NULL, + total_users INT UNSIGNED NOT NULL DEFAULT 0, + sent_count INT UNSIGNED NOT NULL DEFAULT 0, + error_count INT UNSIGNED NOT NULL DEFAULT 0, + sent_by INT UNSIGNED NULL, + sent_by_name VARCHAR(120) NULL, + INDEX idx_sent_at (sent_at) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci + "); +} catch (Exception $_e) { /* silencioso si ya existe */ } + header('Content-Type: application/json; charset=utf-8'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST'); @@ -271,6 +292,38 @@ try { } } + // --- Guardar historial de broadcast --- + try { + $adminId = (int)($_SESSION['admin_user']['id'] ?? $_SESSION['user_id'] ?? 0); + $adminName = $_SESSION['admin_user']['name'] ?? $_SESSION['username'] ?? 'Sistema'; + + $tplName = null; + $preview = ''; + if ($messageType === 'template' && isset($template)) { + $tplName = $template['template_name'] ?? $template['name'] ?? null; + $preview = mb_substr($template['body'] ?? $tplName ?? '', 0, 200); + } else { + $preview = mb_substr($input['message'] ?? '', 0, 200); + } + + $db->insert('broadcast_history', [ + 'sent_at' => date('Y-m-d H:i:s'), + 'message_type' => $messageType, + 'selection_type' => $selectionType, + 'filter_used' => ($selectionType === 'filter') ? ($input['filter'] ?? 'all') : null, + 'template_name' => $tplName, + 'message_preview' => $preview, + 'total_users' => count($users), + 'sent_count' => $sentCount, + 'error_count' => $errorCount, + 'sent_by' => $adminId ?: null, + 'sent_by_name' => $adminName ?: null, + ]); + } catch (Exception $eHist) { + error_log('broadcast_history insert failed: ' . $eHist->getMessage()); + } + // --- Fin historial --- + echo json_encode([ 'success' => true, 'sent_count' => $sentCount, diff --git a/assets/js/app_simple.js b/assets/js/app_simple.js index 5948910..e47b61a 100644 --- a/assets/js/app_simple.js +++ b/assets/js/app_simple.js @@ -1947,6 +1947,10 @@ class SimpleWhatsAppManager { $('#broadcast-users').val(null).trigger('change'); document.getElementById('broadcast-template-variables').style.display = 'none'; document.getElementById('broadcast-template-preview').style.display = 'none'; + // Refrescar historial automáticamente + if (typeof window.loadBroadcastHistory === 'function') { + setTimeout(() => window.loadBroadcastHistory(1), 800); + } } else { this.showError(result.error || 'Error enviando broadcast'); } diff --git a/classes/lab/Domicilio.php b/classes/lab/Domicilio.php index 410fdb6..ca447fc 100644 --- a/classes/lab/Domicilio.php +++ b/classes/lab/Domicilio.php @@ -118,6 +118,7 @@ class Domicilio { p.direccion AS paciente_direccion, o.examenes_solicitados, o.indicaciones AS indicaciones_orden, + o.local_file AS orden_local_file, e.nombre_completo AS enfermera_nombre, e.telefono AS enfermera_telefono, a.id AS asignacion_id, diff --git a/index.php b/index.php index 76ab231..fe36ae7 100644 --- a/index.php +++ b/index.php @@ -663,6 +663,44 @@ try { + + +
| Fecha | +Tipo | +Mensaje / Plantilla | +Total | +Enviados | +Errores | +Enviado por | +
|---|---|---|---|---|---|---|
| + Cargando historial... + | +||||||