format('Y-m-d'); } // Lista de transportistas $sql2 = " SELECT id_transportista, nombre FROM dbo.transportistas WHERE id_usuario = ? AND activo = 1 ORDER BY nombre "; $stmt2 = sqlsrv_query($conn, $sql2, [$_SESSION['usuario_id']]); if ($stmt2 === false) { die("Error en editar() [transportistas]: " . print_r(sqlsrv_errors(), true)); } $transportistas = []; while ($r = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) { $transportistas[] = $r; } include __DIR__ . '/../../views/choferes/editar.php'; } /** * Procesa la actualización de un chofer */ function actualizar() { if (!($_SESSION['usuario_id'] ?? false)) { die("⚠️ No autorizado."); } $id = $_POST['id_chofer'] ?? null; $transportista_id = $_POST['transportista_id'] ?? null; $nombre = trim($_POST['nombre'] ?? ''); $apellido = trim($_POST['apellido'] ?? ''); $licencia = trim($_POST['numero_licencia']?? ''); $telefono = trim($_POST['telefono'] ?? ''); $email = trim($_POST['email'] ?? ''); $fecha_ingreso = $_POST['fecha_ingreso'] ?: null; $status = isset($_POST['status']) ? 1 : 0; // Validación básica if ( !$id || !is_numeric($id) || !$transportista_id || !is_numeric($transportista_id) || $nombre === '' || $apellido === '' || $licencia === '' ) { die("❌ Datos inválidos o incompletos."); } // Manejo de foto nueva (opcional) $fotoUrl = null; if (!empty($_FILES['foto']['tmp_name']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) { $ext = strtolower(pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION)); $dest = __DIR__ . '/../../public/uploads/chofer_'.uniqid().".{$ext}"; if (!is_dir(dirname($dest))) { mkdir(dirname($dest), 0755, true); } if (move_uploaded_file($_FILES['foto']['tmp_name'], $dest)) { $fotoUrl = "/IMPORTADORES/public/uploads/" . basename($dest); } else { error_log("Error al mover foto en actualizar(): {$dest}"); } } $conn = getConnection(); if ($fotoUrl) { $sql = " UPDATE dbo.choferes SET transportista_id = ?, nombre = ?, apellido = ?, numero_licencia = ?, telefono = ?, email = ?, fecha_ingreso = ?, foto_url = ?, status = ?, updated_at = GETDATE() WHERE id_chofer = ? "; $params = [ (int)$transportista_id, $nombre, $apellido, $licencia, $telefono, $email, $fecha_ingreso, $fotoUrl, $status, (int)$id ]; } else { $sql = " UPDATE dbo.choferes SET transportista_id = ?, nombre = ?, apellido = ?, numero_licencia = ?, telefono = ?, email = ?, fecha_ingreso = ?, status = ?, updated_at = GETDATE() WHERE id_chofer = ? "; $params = [ (int)$transportista_id, $nombre, $apellido, $licencia, $telefono, $email, $fecha_ingreso, $status, (int)$id ]; } $stmt = sqlsrv_query($conn, $sql, $params); if ($stmt === false) { die("❌ Error en actualizar(): " . print_r(sqlsrv_errors(), true)); } header('Location: /IMPORTADORES/choferes/lista?updated=ok'); exit; } /** * “Soft-delete” (status = 0) de un chofer */ function eliminar() { if (!($_SESSION['usuario_id'] ?? false)) { header('Location: /IMPORTADORES/login'); exit; } $id = $_GET['id'] ?? null; if (!$id || !is_numeric($id)) { die("❌ ID inválido."); } $conn = getConnection(); $sql = " UPDATE dbo.choferes SET status = 0, updated_at = GETDATE() WHERE id_chofer = ? "; $stmt = sqlsrv_query($conn, $sql, [(int)$id]); if ($stmt === false) { die("❌ Error en eliminar(): " . print_r(sqlsrv_errors(), true)); } header('Location: /IMPORTADORES/choferes/lista?deleted=ok'); exit; }