getMessage(); } $pageTitle = "Recursos Humanos - Colaboradores"; $pageDescription = "Gestión del personal: registro, área, cargo y estado."; $areasValidas = ['Ventas', 'Almacén', 'Marketing', 'Administración', 'Finanzas', 'Gerencia']; $estadosValidos = ['Activo', 'Inactivo', 'Suspendido']; $notice = null; $noticeType = 'success'; if (!empty($schemaInitError)) { $notice = 'Error al preparar las tablas de Recursos Humanos.'; $noticeType = 'danger'; error_log('HR schema init error (colaboradores): ' . $schemaInitError); } if (isset($_GET['success']) && $_GET['success'] === '1') { $notice = 'Colaborador guardado correctamente.'; $noticeType = 'success'; } if (isset($_GET['estado_updated']) && $_GET['estado_updated'] === '1') { $notice = 'Estado actualizado correctamente.'; $noticeType = 'success'; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; if ($action === 'add_colaborador') { try { $nombre_completo = trim((string)($_POST['nombre_completo'] ?? '')); $dni = trim((string)($_POST['dni'] ?? '')); $celular = trim((string)($_POST['celular'] ?? '')); $correo = trim((string)($_POST['correo'] ?? '')); $area = (string)($_POST['area'] ?? ''); $cargo = trim((string)($_POST['cargo'] ?? '')); $fecha_ingreso = $_POST['fecha_ingreso'] ?? date('Y-m-d'); $estado = (string)($_POST['estado'] ?? 'Activo'); $observaciones = trim((string)($_POST['observaciones'] ?? '')); if ($nombre_completo === '' || $dni === '' || $celular === '' || $correo === '' || $cargo === '' || $area === '') { throw new RuntimeException('Faltan campos obligatorios.'); } if (!filter_var($correo, FILTER_VALIDATE_EMAIL)) { throw new RuntimeException('Correo inválido.'); } if (!in_array($area, $areasValidas, true)) { throw new RuntimeException('Área inválida.'); } if (!in_array($estado, $estadosValidos, true)) { throw new RuntimeException('Estado inválido.'); } $stmt = $pdo->prepare('INSERT INTO hr_colaboradores (nombre_completo, dni, celular, correo, area, cargo, fecha_ingreso, estado, observaciones) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'); $stmt->execute([ $nombre_completo, $dni, $celular, $correo, $area, $cargo, $fecha_ingreso, $estado, $observaciones, ]); header('Location: hr_colaboradores.php?success=1'); exit; } catch (Throwable $e) { $notice = 'No se pudo guardar el colaborador: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'); $noticeType = 'danger'; } } if ($action === 'update_colaborador_estado') { try { $id = (int)($_POST['id'] ?? 0); $nuevoEstado = (string)($_POST['estado'] ?? ''); if ($id <= 0) { throw new RuntimeException('ID inválido.'); } if (!in_array($nuevoEstado, $estadosValidos, true)) { throw new RuntimeException('Estado inválido.'); } $stmt = $pdo->prepare('UPDATE hr_colaboradores SET estado = ? WHERE id = ?'); $stmt->execute([$nuevoEstado, $id]); header('Location: hr_colaboradores.php?estado_updated=1'); exit; } catch (Throwable $e) { $notice = 'No se pudo actualizar el estado.'; $noticeType = 'danger'; } } } // --- Filtros --- $filter_area = (string)($_GET['area'] ?? 'Todas'); $filter_estado = (string)($_GET['estado'] ?? 'Todos'); $search = trim((string)($_GET['q'] ?? '')); if ($filter_area !== 'Todas' && !in_array($filter_area, $areasValidas, true)) { $filter_area = 'Todas'; } if ($filter_estado !== 'Todos' && !in_array($filter_estado, $estadosValidos, true)) { $filter_estado = 'Todos'; } $where = []; $params = []; if ($filter_area !== 'Todas') { $where[] = 'area = ?'; $params[] = $filter_area; } if ($filter_estado !== 'Todos') { $where[] = 'estado = ?'; $params[] = $filter_estado; } if ($search !== '') { $where[] = '(nombre_completo LIKE ? OR dni LIKE ? OR cargo LIKE ?)'; $like = '%' . $search . '%'; $params[] = $like; $params[] = $like; $params[] = $like; } $sql = 'SELECT id, nombre_completo, dni, celular, correo, area, cargo, fecha_ingreso, estado, observaciones FROM hr_colaboradores'; if (!empty($where)) { $sql .= ' WHERE ' . implode(' AND ', $where); } $sql .= ' ORDER BY fecha_ingreso DESC, id DESC'; $colaboradores = []; try { $stmt = $pdo->prepare($sql); $stmt->execute($params); $colaboradores = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { error_log('HR Colaboradores DB error: ' . $e->getMessage()); $notice = 'No se pudo cargar la lista de colaboradores.'; $noticeType = 'danger'; } $fechaHoy = date('Y-m-d'); include 'layout_header.php'; ?>

Nuevo Colaborador

Colaboradores

Total:
Limpiar
ID Nombre Completo DNI Celular Correo Área Cargo Fecha Ingreso Estado Observaciones Acciones
No hay colaboradores para los filtros seleccionados.