diff --git a/admin/carga-historial.php b/admin/carga-historial.php new file mode 100644 index 0000000..0f6f430 --- /dev/null +++ b/admin/carga-historial.php @@ -0,0 +1,361 @@ + 'Visa', + 'sentri' => 'Sentri/Global', + 'pasaporte' => 'Pasaporte', + 'adelanto_cita' => 'Adelanto Cita', + 'doble_nacionalidad' => 'Doble Nacionalidad', +]; +$estadoLabels = [ + 'nuevo' => 'Nuevo', + 'en_proceso' => 'En Proceso', + 'en_revision' => 'En Revisión', + 'completado' => 'Completado', + 'cancelado' => 'Cancelado', +]; + +$errors = []; +$success = ''; + +// ── Track recently added client IDs in session ────────────────── +if (!isset($_SESSION['carga_historial_ids'])) { + $_SESSION['carga_historial_ids'] = []; +} + +// ── Handle POST ───────────────────────────────────────────────── +if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrfValidate()) { + + $nombre = trim($_POST['nombre'] ?? ''); + $telefono = trim($_POST['telefono'] ?? ''); + $email = trim($_POST['email'] ?? ''); + $direccion = trim($_POST['direccion'] ?? ''); + $notas = trim($_POST['notas'] ?? ''); + + // Validate + if ($nombre === '') { + $errors[] = 'El nombre es obligatorio.'; + } + if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) { + $errors[] = 'El email no es válido.'; + } + + // Tramite fields (only used if checkbox is on) + $agregarTramite = isset($_POST['agregar_tramite']); + $tramTipo = trim($_POST['tram_tipo'] ?? ''); + $tramEstado = trim($_POST['tram_estado'] ?? 'nuevo'); + $tramFechaSolicitud = trim($_POST['tram_fecha_solicitud'] ?? ''); + $tramFechaCita = trim($_POST['tram_fecha_cita'] ?? ''); + $tramFechaResolucion = trim($_POST['tram_fecha_resolucion'] ?? ''); + $tramPrecio = $_POST['tram_precio'] ?? ''; + $tramNotas = trim($_POST['tram_notas'] ?? ''); + + if ($agregarTramite && $tramTipo !== '') { + if (!array_key_exists($tramTipo, $tipoLabels)) { + $errors[] = 'El tipo de trámite no es válido.'; + } + if (!array_key_exists($tramEstado, $estadoLabels)) { + $errors[] = 'El estado del trámite no es válido.'; + } + } + + if (empty($errors)) { + // INSERT client + $stmt = $db->prepare("INSERT INTO clientes (nombre, telefono, email, direccion, notas) VALUES (?,?,?,?,?)"); + $stmt->execute([$nombre, $telefono, $email, $direccion, $notas]); + $newClienteId = (int)$db->lastInsertId(); + + // INSERT tramite if requested + if ($agregarTramite && $tramTipo !== '') { + $fechaSol = $tramFechaSolicitud !== '' ? $tramFechaSolicitud : null; + $fechaCit = $tramFechaCita !== '' ? $tramFechaCita : null; + $fechaRes = $tramFechaResolucion !== '' ? $tramFechaResolucion : null; + $precio = $tramPrecio !== '' ? (float)$tramPrecio : null; + + $stmtT = $db->prepare("INSERT INTO tramites (cliente_id, tipo, estado, fecha_solicitud, fecha_cita, fecha_resolucion, precio, notas) VALUES (?,?,?,?,?,?,?,?)"); + $stmtT->execute([ + $newClienteId, $tramTipo, $tramEstado, + $fechaSol, $fechaCit, $fechaRes, + $precio, $tramNotas + ]); + } + + // Track in session + $_SESSION['carga_historial_ids'][] = $newClienteId; + // Keep only the last 50 to avoid session bloat + if (count($_SESSION['carga_historial_ids']) > 50) { + $_SESSION['carga_historial_ids'] = array_slice($_SESSION['carga_historial_ids'], -50); + } + + // Determine redirect + $submitAction = $_POST['submit_action'] ?? 'add_another'; + + if ($submitAction === 'view_client') { + header("Location: cliente-detalle.php?id=$newClienteId&saved=1"); + exit; + } + + // Default: add another + header("Location: carga-historial.php?saved=1"); + exit; + } +} + +// Success message from redirect +if (isset($_GET['saved'])) { + $success = 'Cliente guardado correctamente. Puede continuar agregando más registros.'; +} + +// ── Fetch recently added clients for the table below ──────────── +$recentClients = []; +$sessionIds = $_SESSION['carga_historial_ids'] ?? []; + +if (!empty($sessionIds)) { + // Get the last 10 IDs (most recent first) + $lastIds = array_slice($sessionIds, -10); + $lastIds = array_reverse($lastIds); + + $placeholders = implode(',', array_fill(0, count($lastIds), '?')); + $sql = "SELECT c.id, c.nombre, c.telefono, + (SELECT t.tipo FROM tramites t WHERE t.cliente_id = c.id ORDER BY t.id DESC LIMIT 1) AS ultimo_tramite_tipo + FROM clientes c + WHERE c.id IN ($placeholders) + ORDER BY c.id DESC"; + $stmtRecent = $db->prepare($sql); + $stmtRecent->execute($lastIds); + $recentClients = $stmtRecent->fetchAll(); +} +?> + +
Entrada rápida para cargar clientes y trámites históricos
+No se han agregado clientes en esta sesión aún.
+| Nombre | +Teléfono | +Trámite | +Acción | +
|---|---|---|---|
| = htmlspecialchars($rc['nombre']) ?> | += htmlspecialchars($rc['telefono'] ?: '-') ?> | ++ + = htmlspecialchars($tipoLabels[$rc['ultimo_tramite_tipo']] ?? $rc['ultimo_tramite_tipo']) ?> + + - + + | ++ + Ver + + | +