Revisión 2.1
This commit is contained in:
@@ -14,7 +14,7 @@ function lista() {
|
||||
|
||||
// Sólo mostrar transportes de los transportistas que le pertenecen al usuario
|
||||
$sql = "
|
||||
SELECT t.*, tr.nombre AS transportista
|
||||
SELECT t.*, (tr.clave_identificador + ' - ' + tr.nombre) AS transportista
|
||||
FROM dbo.transportes t
|
||||
JOIN dbo.transportistas tr
|
||||
ON t.id_transportista = tr.id_transportista
|
||||
@@ -41,8 +41,10 @@ function crear() {
|
||||
|
||||
// Traer transportistas propios para el select
|
||||
$sql = "
|
||||
SELECT id_transportista, clave_identificador, nombre
|
||||
FROM dbo.transportistas
|
||||
SELECT t.id_transportista, t.clave_identificador, t.nombre, t.ciudad, t.domicilio,
|
||||
c.nombre AS ciudad_nombre
|
||||
FROM dbo.transportistas t
|
||||
LEFT JOIN dbo.ciudades c ON t.ciudad = c.id_ciudad
|
||||
WHERE id_usuario = ? AND activo = 1
|
||||
ORDER BY nombre
|
||||
";
|
||||
@@ -56,31 +58,62 @@ function crear() {
|
||||
}
|
||||
|
||||
/** Procesa la creación de un nuevo transporte **/
|
||||
function guardar() {
|
||||
|
||||
function guardar()
|
||||
{
|
||||
if (!($_SESSION['usuario_id'] ?? false)) {
|
||||
die("⚠️ No autorizado.");
|
||||
}
|
||||
$vehiculo = trim($_POST['vehiculo'] ?? '');
|
||||
$identFiscal= trim($_POST['identificador_fiscal'] ?? '');
|
||||
$idTrans = $_POST['id_transportista'] ?? null;
|
||||
|
||||
$vehiculo = trim($_POST['vehiculo'] ?? '');
|
||||
$identFiscal = trim($_POST['identificador_fiscal'] ?? '');
|
||||
$idTrans = $_POST['id_transportista'] ?? null;
|
||||
|
||||
if ($vehiculo === '' || $identFiscal === '' || !$idTrans) {
|
||||
die("❌ Todos los campos son obligatorios.");
|
||||
}
|
||||
|
||||
// Manejo de foto
|
||||
// Validar que el transportista pertenece al usuario actual
|
||||
$conn = getConnection();
|
||||
$sqlCheck = "
|
||||
SELECT 1 FROM dbo.transportistas
|
||||
WHERE id_transportista = ? AND id_usuario = ? AND activo = 1
|
||||
";
|
||||
$stmtCheck = sqlsrv_query($conn, $sqlCheck, [$idTrans, $_SESSION['usuario_id']]);
|
||||
if (!sqlsrv_fetch($stmtCheck)) {
|
||||
die("❌ Transportista no válido o no autorizado.");
|
||||
}
|
||||
|
||||
// Manejo de foto mejorado
|
||||
$fotoUrl = null;
|
||||
if (!empty($_FILES['foto']['tmp_name'])) {
|
||||
$ext = pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION);
|
||||
$dest = __DIR__ . '/../../public/uploads/transporte_'.uniqid().".{$ext}";
|
||||
if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
|
||||
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
$ext = strtolower(pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION));
|
||||
|
||||
if (!in_array($ext, $allowedExtensions)) {
|
||||
die("❌ Formato de imagen no válido. Solo se permiten: " . implode(', ', $allowedExtensions));
|
||||
}
|
||||
|
||||
// Validar tamaño (2MB max)
|
||||
if ($_FILES['foto']['size'] > 2 * 1024 * 1024) {
|
||||
die("❌ La imagen no debe exceder 2 MB.");
|
||||
}
|
||||
|
||||
$uploadDir = __DIR__ . '/../../public/uploads/';
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0755, true);
|
||||
}
|
||||
|
||||
$dest = $uploadDir . 'transporte_' . uniqid() . ".{$ext}";
|
||||
|
||||
if (move_uploaded_file($_FILES['foto']['tmp_name'], $dest)) {
|
||||
// ruta relativa
|
||||
$fotoUrl = "/IMPORTADORES/public/uploads/" . basename($dest);
|
||||
} else {
|
||||
error_log("Error al mover archivo en guardar(): {$dest}");
|
||||
die("❌ Error al subir la imagen.");
|
||||
}
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
// Insertar el nuevo transporte
|
||||
$sql = "
|
||||
INSERT INTO dbo.transportes
|
||||
(vehiculo, identificador_fiscal, foto_url, status, id_transportista)
|
||||
@@ -88,8 +121,17 @@ function guardar() {
|
||||
";
|
||||
$params = [$vehiculo, $identFiscal, $fotoUrl, $idTrans];
|
||||
$stmt = sqlsrv_query($conn, $sql, $params);
|
||||
|
||||
if ($stmt === false) {
|
||||
die("❌ Error al guardar: ".print_r(sqlsrv_errors(),true));
|
||||
$errors = sqlsrv_errors();
|
||||
error_log("Error SQL en guardar transporte: " . print_r($errors, true));
|
||||
die("❌ Error al guardar: " . $errors[0]['message']);
|
||||
}
|
||||
|
||||
// Verificar que se insertó correctamente
|
||||
$rowsAffected = sqlsrv_rows_affected($stmt);
|
||||
if ($rowsAffected === 0) {
|
||||
die("❌ No se pudo crear el registro.");
|
||||
}
|
||||
|
||||
header('Location: /IMPORTADORES/transportes/lista?created=ok');
|
||||
@@ -124,8 +166,10 @@ function editar() {
|
||||
|
||||
// Mismo select de transportistas que en crear()
|
||||
$sql2 = "
|
||||
SELECT id_transportista, nombre
|
||||
FROM dbo.transportistas
|
||||
SELECT t.id_transportista, t.clave_identificador, t.nombre, t.ciudad, t.domicilio,
|
||||
c.nombre AS ciudad_nombre
|
||||
FROM dbo.transportistas t
|
||||
LEFT JOIN dbo.ciudades c ON t.ciudad = c.id_ciudad
|
||||
WHERE id_usuario = ? AND activo = 1
|
||||
ORDER BY nombre
|
||||
";
|
||||
@@ -145,24 +189,49 @@ function actualizar() {
|
||||
$vehiculo = trim($_POST['vehiculo'] ?? '');
|
||||
$identFiscal = trim($_POST['identificador_fiscal'] ?? '');
|
||||
$idTrans = $_POST['id_transportista'] ?? null;
|
||||
|
||||
if (!$id || !is_numeric($id) || $vehiculo === '' || $identFiscal === '' || !$idTrans) {
|
||||
die("❌ Faltan datos.");
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
|
||||
// Antes del UPDATE, validar que el transporte pertenece al usuario
|
||||
$sqlCheck = "
|
||||
SELECT 1 FROM dbo.transportes t
|
||||
JOIN dbo.transportistas tr ON t.id_transportista = tr.id_transportista
|
||||
wHERE t.id_transporte = ? AND tr.id_usuario = ?
|
||||
";
|
||||
$stmtCheck = sqlsrv_query($conn, $sqlCheck, [$id, $_SESSION['usuario_id']]);
|
||||
if ($stmtCheck === false) {
|
||||
$errors = sqlsrv_errors();
|
||||
error_log("Error SQL al validar transporte: " . print_r($errors, true));
|
||||
die("❌ Error en la validación SQL.");
|
||||
}
|
||||
if (!sqlsrv_fetch($stmtCheck)) {
|
||||
die("❌ No autorizado para modificar este transporte.");
|
||||
}
|
||||
|
||||
// —– Manejo de nueva foto —–
|
||||
$fotoUrl = null;
|
||||
if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
|
||||
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
$ext = strtolower(pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION));
|
||||
|
||||
if (!in_array($ext, $allowedExtensions)) {
|
||||
die("❌ Formato de imagen no válido.");
|
||||
}
|
||||
|
||||
$dest = __DIR__ . '/../../public/uploads/transporte_'.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 archivo en actualizar(): {$dest}");
|
||||
die("❌ Error al subir la imagen.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +243,7 @@ function actualizar() {
|
||||
identificador_fiscal = ?,
|
||||
id_transportista = ?,
|
||||
foto_url = ?
|
||||
WHERE id_transporte = ?
|
||||
WHERE id_transporte = ?
|
||||
";
|
||||
$params = [$vehiculo, $identFiscal, $idTrans, $fotoUrl, $id];
|
||||
} else {
|
||||
@@ -190,7 +259,15 @@ function actualizar() {
|
||||
|
||||
$stmt = sqlsrv_query($conn, $sql, $params);
|
||||
if ($stmt === false) {
|
||||
die("❌ Error al actualizar: " . print_r(sqlsrv_errors(), true));
|
||||
$errors = sqlsrv_errors();
|
||||
error_log("Error SQL en actualizar transporte: " . print_r($errors, true));
|
||||
die("❌ Error al actualizar: " . $errors[0]['message']);
|
||||
}
|
||||
|
||||
// Verificar si se afectó alguna fila
|
||||
$rowsAffected = sqlsrv_rows_affected($stmt);
|
||||
if ($rowsAffected === 0) {
|
||||
die("❌ No se pudo actualizar el registro.");
|
||||
}
|
||||
|
||||
header('Location: /IMPORTADORES/transportes/lista?updated=ok');
|
||||
|
||||
Reference in New Issue
Block a user