Validaciones - Alta de: Proveedores, Transportistas, Transportes y Choferes

This commit is contained in:
2025-06-10 12:45:59 -06:00
parent f1d4b7c98f
commit b0b9672f11
17 changed files with 757 additions and 129 deletions

View File

@@ -33,32 +33,32 @@
<div class="row g-3">
<div class="col-md-4">
<label for="clave" class="form-label">Clave *</label>
<input name="clave" class="form-control" required>
<input name="clave" id="clave" class="form-control" maxlength="8" required>
</div>
<div class="col-md-4">
<label for="nombre" class="form-label">Nombre *</label>
<input name="nombre" class="form-control" required>
<input name="nombre" id="nombre" class="form-control" required>
</div>
<div class="col-md-4">
<label for="rfc" class="form-label">RFC *</label>
<input name="rfc" class="form-control" maxlength="13" required>
<input name="rfc" id="rfc" class="form-control" maxlength="13" required>
</div>
<div class="col-md-4">
<label for="curp" class="form-label">CURP *</label>
<input name="curp" class="form-control" maxlength="18" required>
<input name="curp" id="curp" class="form-control" maxlength="18" required>
</div>
<div class="col-md-4">
<label for="telefono" class="form-label">Teléfono *</label>
<input name="telefono" class="form-control" maxlength="11" required>
<input name="telefono" id="telefono" class="form-control" maxlength="11" required>
</div>
<div class="col-md-4">
<label for="caat" class="form-label">Código CAAT *</label>
<input name="caat" class="form-control" required>
<input name="caat" id="caat" class="form-control" maxlength="20" required>
</div>
<div class="col-md-4">
@@ -75,7 +75,7 @@
<div class="col-md-4">
<label for="entidad" class="form-label">Entidad *</label>
<select id="estado" name="entidad" class="form-select" required disabled>
<select id="entidad" name="entidad" class="form-select" required disabled>
<option value="">Primero país…</option>
</select>
</div>
@@ -89,7 +89,7 @@
<div class="col-md-8">
<label for="domicilio" class="form-label">Domicilio *</label>
<input name="domicilio" class="form-control" required>
<input name="domicilio" id="domicilio" class="form-control" required>
</div>
</div>
@@ -102,79 +102,201 @@
<script>
document.getElementById('formAltaTransportista').addEventListener('submit', function(e) {
const rfcInput = document.querySelector('input[name="rfc"]');
const curpInput = document.querySelector('input[name="curp"]');
const telefonoInput = document.querySelector('input[name="telefono"]');
e.preventDefault(); // Prevenir envío por defecto
const rfcInput = document.getElementById('rfc');
const curpInput = document.getElementById('curp');
const telefonoInput = document.getElementById('telefono');
const rfc = rfcInput.value.trim().toUpperCase();
const curp = curpInput.value.trim();
const telefono = telefonoInput.value.trim();
const clave = document.getElementById('clave').value.trim();
const nombre = document.getElementById('nombre').value.trim();
const caat = document.getElementById('caat').value.trim();
const pais = document.getElementById('pais').value;
const entidad = document.getElementById('entidad').value;
const ciudad = document.getElementById('ciudad').value;
const domicilio = document.getElementById('domicilio').value.trim();
// Expresiones regulares
const curpRegex = /^[A-Z]{4}[0-9]{6}[HM](AS|BC|BS|CC|CL|CM|CS|CH|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS)[A-Z]{3}[A-Z0-9]{2}$/;
const rfcRegex = /^[A-ZÑ&]{3,4}\d{6}[A-Z0-9]{3}$/;
const telefonoRegex = /^\d{3}\s\d{7}$/;
const soloNumerosRegex = /^[0-9]+$/;
// Validación de correo
if (!curpRegex.test(curp)) {
e.preventDefault();
Swal.fire("CURP inválido", "No tiene el formato correcto.", "error");
emailInput.focus();
// Validaciones de campos obligatorios
if (!clave) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'La clave es obligatoria.', confirmButtonColor: '#dc3545' });
document.getElementById('clave').focus();
return;
}
if (clave.length > 8) {
Swal.fire({ icon: 'error', title: 'Clave inválida', text: 'La clave no puede tener más de 8 caracteres.', confirmButtonColor: '#dc3545' });
document.getElementById('clave').focus();
return;
}
if (!nombre) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El nombre es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('nombre').focus();
return;
}
if (!rfc) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El RFC es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('rfc').focus();
return;
}
// Validación de RFC
if (!rfcRegex.test(rfc)) {
e.preventDefault();
Swal.fire("RFC inválido", "El RFC debe tener 12 o 13 caracteres con el formato correcto (ej. ABC123456XYZ).", "error");
rfcInput.focus();
Swal.fire({ icon: 'error', title: 'RFC inválido', text: 'El RFC debe tener 12 o 13 caracteres con el formato correcto (ej. ABC123456XYZ).', confirmButtonColor: '#dc3545' });
document.getElementById('rfc').focus();
return;
}
if (!curp) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El CURP es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('curp').focus();
return;
}
// Validación de CURP (solo si se ingresó)
if (curp && !curpRegex.test(curp)) {
Swal.fire({ icon: 'error', title: 'CURP inválido', text: 'El CURP no tiene el formato correcto.', confirmButtonColor: '#dc3545'
});
document.getElementById('curp').focus();
return;
}
if (!telefono) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El teléfono es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('telefono').focus();
return;
}
// Validación de teléfono
if (!telefonoRegex.test(telefono)) {
e.preventDefault();
Swal.fire("Teléfono inválido", "Debe tener 10 dígitos en el formato: 3 dígitos (LADA), espacio, y 7 dígitos. Ejemplo: 123 4567890", "error");
phoneInput.focus();
Swal.fire({ icon: 'error', title: 'Teléfono inválido', text: 'Debe tener 10 dígitos en el formato: 3 dígitos (LADA), espacio, y 7 dígitos. Ejemplo: 123 4567890', confirmButtonColor: '#dc3545' });
document.getElementById('telefono').focus();
return;
}
// Si no hay errores, enviar el formulario
this.submit(); // Esto sí lo envía manualmente
if (telefono.length > 11) {
Swal.fire({ icon: 'error', title: 'Teléfono inválido', text: 'El teléfono no puede tener más de 11 caracteres.', confirmButtonColor: '#dc3545' });
document.getElementById('telefono').focus();
return;
}
if (!caat) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El código CAAT es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('caat').focus();
return;
}
if (!soloNumerosRegex.test(caat)) {
Swal.fire({ icon: 'error', title: 'Código CAAT inválido', text: 'El código caat solo puede contener números', confirmButtonColor: '#dc3545' });
document.getElementById('caat').focus();
return;
}
if (!pais) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'Debe seleccionar un país.', confirmButtonColor: '#dc3545' });
document.getElementById('pais').focus();
return;
}
if (!entidad) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'Debe seleccionar un estado.', confirmButtonColor: '#dc3545' });
document.getElementById('entidad').focus();
return;
}
if (!ciudad) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'Debe seleccionar una ciudad.', confirmButtonColor: '#dc3545' });
document.getElementById('ciudad').focus();
return;
}
if (!domicilio) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El domicilio es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('domicilio').focus();
return;
}
// Si llegamos aquí, todas las validaciones pasaron
// Actualizar los valores en los campos antes de enviar
document.getElementById('rfc').value = rfc;
document.getElementById('curp').value = curp;
// Enviar el formulario
this.submit();
});
// Active inputs entidad y ciudad
// Funcionalidad de selects en cascada
// 1) Cuando elige país → cargo estados
document.getElementById('pais').addEventListener('change', e => {
const pid = e.target.value;
const sel = document.getElementById('estado');
sel.innerHTML = '<option>Cargando…</option>';
sel.disabled = true;
document.getElementById('ciudad').innerHTML = '<option>Primero estado…</option>';
document.getElementById('ciudad').disabled = true;
const estadoSelect = document.getElementById('entidad');
const ciudadSelect = document.getElementById('ciudad');
// Resetear selects dependientes
estadoSelect.innerHTML = '<option value="">Cargando…</option>';
estadoSelect.disabled = true;
ciudadSelect.innerHTML = '<option value="">Primero estado…</option>';
ciudadSelect.disabled = true;
if (!pid) {
estadoSelect.innerHTML = '<option value="">Primero país…</option>';
return;
}
// Simulación de carga de estados (reemplaza con tu endpoint real)
fetch(`/IMPORTADORES/transportistas/estados?pais=${pid}`)
.then(r=>r.json())
.then(list=>{
sel.innerHTML = '<option value="">Selecciona estado</option>';
list.forEach(st => sel.add(new Option(st.nombre, st.id_estado)));
sel.disabled = false;
.then(r => {
if (!r.ok) throw new Error('Error al cargar estados');
return r.json();
})
.then(list => {
estadoSelect.innerHTML = '<option value="">Selecciona estado</option>';
list.forEach(st => {
const option = new Option(st.nombre, st.id_estado);
estadoSelect.add(option);
});
estadoSelect.disabled = false;
})
.catch(error => {
console.error('Error:', error);
estadoSelect.innerHTML = '<option value="">Error al cargar</option>';
Swal.fire({
icon: 'error',
title: 'Error',
text: 'No se pudieron cargar los estados',
confirmButtonColor: '#dc3545'
});
});
});
// 2) Cuando elige estado → cargo ciudades
document.getElementById('estado').addEventListener('change', e => {
document.getElementById('entidad').addEventListener('change', e => {
const eid = e.target.value;
const sel = document.getElementById('ciudad');
sel.innerHTML = '<option>Cargando…</option>';
sel.disabled = true;
const ciudadSelect = document.getElementById('ciudad');
ciudadSelect.innerHTML = '<option value="">Cargando…</option>';
ciudadSelect.disabled = true;
if (!eid) {
ciudadSelect.innerHTML = '<option value="">Primero estado…</option>';
return;
}
// Simulación de carga de ciudades (reemplaza con tu endpoint real)
fetch(`/IMPORTADORES/transportistas/ciudades?estado=${eid}`)
.then(r=>r.json())
.then(list=>{
sel.innerHTML = '<option value="">Selecciona ciudad</option>';
list.forEach(ct => sel.add(new Option(ct.nombre, ct.id_ciudad)));
sel.disabled = false;
.then(r => {
if (!r.ok) throw new Error('Error al cargar ciudades');
return r.json();
})
.then(list => {
ciudadSelect.innerHTML = '<option value="">Selecciona ciudad</option>';
list.forEach(ct => {
const option = new Option(ct.nombre, ct.id_ciudad);
ciudadSelect.add(option);
});
ciudadSelect.disabled = false;
})
.catch(error => {
console.error('Error:', error);
ciudadSelect.innerHTML = '<option value="">Error al cargar</option>';
Swal.fire({
icon: 'error',
title: 'Error',
text: 'No se pudieron cargar las ciudades',
confirmButtonColor: '#dc3545'
});
});
});
</script>

View File

@@ -8,6 +8,8 @@
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
body { font-family: 'Segoe UI', sans-serif; background-color: #f4f6f9; }
.sidebar { width: 220px; height: 100vh; background-color: #343a40; position: fixed; top: 0; left: 0; padding-top: 56px; z-index: 1040; }
@@ -32,38 +34,38 @@
<div class="content">
<h4 class="mb-4">✏️ Editar Transportista #<?= $t['id_transportista'] ?></h4>
<form action="/IMPORTADORES/transportistas/actualizar" method="POST" class="card p-4 shadow-sm">
<form action="/IMPORTADORES/transportistas/actualizar" method="POST" class="card p-4 shadow-sm" id="formEditTransportista">
<input type="hidden" name="id_transportista" value="<?= $t['id_transportista'] ?>">
<div class="row g-3">
<div class="col-md-4">
<label class="form-label">Clave</label>
<input name="clave" value="<?= htmlspecialchars($t['clave_identificador']) ?>"
class="form-control" required>
id="clave" class="form-control" maxlength="8" required>
</div>
<div class="col-md-4">
<label class="form-label">Nombre</label>
<input name="nombre" value="<?= htmlspecialchars($t['nombre']) ?>"
class="form-control" required>
id="nombre" class="form-control" required>
</div>
<div class="col-md-4">
<label class="form-label">RFC</label>
<input name="rfc" value="<?= htmlspecialchars($t['rfc']) ?>"
class="form-control" required>
id="rfc" class="form-control" maxlength="13" required>
</div>
<div class="col-md-4">
<label class="form-label">CURP</label>
<input name="curp" value="<?= htmlspecialchars($t['curp']) ?>"
class="form-control">
id="curp" class="form-control" maxlength="18">
</div>
<div class="col-md-4">
<label class="form-label">Teléfono</label>
<input name="telefono" value="<?= htmlspecialchars($t['telefono']) ?>"
class="form-control" required>
id="telefono" class="form-control" maxlength="11" required>
</div>
<div class="col-md-4">
<label class="form-label">Código CAAT</label>
<input name="caat" value="<?= htmlspecialchars($t['caat']) ?>"
class="form-control" required>
id="caat" class="form-control" maxlength="20" required>
</div>
<div class="col-md-4">
@@ -80,7 +82,7 @@
</div>
<div class="col-md-4">
<label class="form-label">Estado</label>
<select id="estado" name="entidad" class="form-select" required>
<select id="entidad" name="entidad" class="form-select" required>
<option value="">Selecciona estado</option>
<?php foreach($estados as $e): ?>
<option value="<?= $e['id_estado'] ?>"
@@ -105,7 +107,7 @@
<div class="col-md-12">
<label class="form-label">Domicilio Completo</label>
<textarea name="domicilio" class="form-control" rows="3" required><?= htmlspecialchars($t['domicilio']) ?></textarea>
<textarea name="domicilio" id="domicilio" class="form-control" rows="3" required><?= htmlspecialchars($t['domicilio']) ?></textarea>
</div>
</div>
<div class="text-end mt-4">
@@ -116,31 +118,202 @@
</div>
<script>
// Al cambiar país → recargar estados
document.getElementById('pais').addEventListener('change', function(){
const pid = this.value, selE = document.getElementById('estado');
selE.innerHTML = '<option>Cargando…</option>'; selE.disabled = true;
document.getElementById('formEditTransportista').addEventListener('submit', function(e) {
e.preventDefault(); // Prevenir envío por defecto
const rfcInput = document.getElementById('rfc');
const curpInput = document.getElementById('curp');
const telefonoInput = document.getElementById('telefono');
const rfc = rfcInput.value.trim().toUpperCase();
const curp = curpInput.value.trim();
const telefono = telefonoInput.value.trim();
const clave = document.getElementById('clave').value.trim();
const nombre = document.getElementById('nombre').value.trim();
const caat = document.getElementById('caat').value.trim();
const pais = document.getElementById('pais').value;
const entidad = document.getElementById('entidad').value;
const ciudad = document.getElementById('ciudad').value;
const domicilio = document.getElementById('domicilio').value.trim();
// Expresiones regulares
const curpRegex = /^[A-Z]{4}[0-9]{6}[HM](AS|BC|BS|CC|CL|CM|CS|CH|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS)[A-Z]{3}[A-Z0-9]{2}$/;
const rfcRegex = /^[A-ZÑ&]{3,4}\d{6}[A-Z0-9]{3}$/;
const telefonoRegex = /^\d{3}\s\d{7}$/;
const soloNumerosRegex = /^[0-9]+$/;
// Validaciones de campos obligatorios
if (!clave) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'La clave es obligatoria.', confirmButtonColor: '#dc3545' });
document.getElementById('clave').focus();
return;
}
if (clave.length > 8) {
Swal.fire({ icon: 'error', title: 'Clave inválida', text: 'La clave no puede tener más de 8 caracteres.', confirmButtonColor: '#dc3545' });
document.getElementById('clave').focus();
return;
}
if (!nombre) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El nombre es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('nombre').focus();
return;
}
if (!rfc) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El RFC es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('rfc').focus();
return;
}
if (!rfcRegex.test(rfc)) {
Swal.fire({ icon: 'error', title: 'RFC inválido', text: 'El RFC debe tener 12 o 13 caracteres con el formato correcto (ej. ABC123456XYZ).', confirmButtonColor: '#dc3545' });
document.getElementById('rfc').focus();
return;
}
if (!curp) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El CURP es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('curp').focus();
return;
}
// Validación de CURP (solo si se ingresó)
if (curp && !curpRegex.test(curp)) {
Swal.fire({ icon: 'error', title: 'CURP inválido', text: 'El CURP no tiene el formato correcto.', confirmButtonColor: '#dc3545'
});
document.getElementById('curp').focus();
return;
}
if (!telefono) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El teléfono es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('telefono').focus();
return;
}
if (!telefonoRegex.test(telefono)) {
Swal.fire({ icon: 'error', title: 'Teléfono inválido', text: 'Debe tener 10 dígitos en el formato: 3 dígitos (LADA), espacio, y 7 dígitos. Ejemplo: 123 4567890', confirmButtonColor: '#dc3545' });
document.getElementById('telefono').focus();
return;
}
if (telefono.length > 11) {
Swal.fire({ icon: 'error', title: 'Teléfono inválido', text: 'El teléfono no puede tener más de 11 caracteres.', confirmButtonColor: '#dc3545' });
document.getElementById('telefono').focus();
return;
}
if (!caat) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El código CAAT es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('caat').focus();
return;
}
if (!soloNumerosRegex.test(caat)) {
Swal.fire({ icon: 'error', title: 'Código CAAT inválido', text: 'El código caat solo puede contener números', confirmButtonColor: '#dc3545' });
document.getElementById('caat').focus();
return;
}
if (!pais) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'Debe seleccionar un país.', confirmButtonColor: '#dc3545' });
document.getElementById('pais').focus();
return;
}
if (!entidad) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'Debe seleccionar un estado.', confirmButtonColor: '#dc3545' });
document.getElementById('entidad').focus();
return;
}
if (!ciudad) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'Debe seleccionar una ciudad.', confirmButtonColor: '#dc3545' });
document.getElementById('ciudad').focus();
return;
}
if (!domicilio) {
Swal.fire({ icon: 'error', title: 'Campo requerido', text: 'El domicilio es obligatorio.', confirmButtonColor: '#dc3545' });
document.getElementById('domicilio').focus();
return;
}
// Si llegamos aquí, todas las validaciones pasaron
// Actualizar los valores en los campos antes de enviar
document.getElementById('rfc').value = rfc;
document.getElementById('curp').value = curp;
// Enviar el formulario
this.submit();
});
// Funcionalidad de selects en cascada
// 1) Cuando elige país → cargo estados
document.getElementById('pais').addEventListener('change', e => {
const pid = e.target.value;
const estadoSelect = document.getElementById('entidad');
const ciudadSelect = document.getElementById('ciudad');
// Resetear selects dependientes
estadoSelect.innerHTML = '<option value="">Cargando…</option>';
estadoSelect.disabled = true;
ciudadSelect.innerHTML = '<option value="">Primero estado…</option>';
ciudadSelect.disabled = true;
if (!pid) {
estadoSelect.innerHTML = '<option value="">Primero país…</option>';
return;
}
// Simulación de carga de estados (reemplaza con tu endpoint real)
fetch(`/IMPORTADORES/transportistas/estados?pais=${pid}`)
.then(r=>r.json())
.then(list=>{
selE.innerHTML = '<option value="">Selecciona estado</option>';
list.forEach(e=> selE.add(new Option(e.nombre,e.id_estado)) );
selE.disabled = false;
// Dispara cambio para recargar ciudades
selE.dispatchEvent(new Event('change'));
.then(r => {
if (!r.ok) throw new Error('Error al cargar estados');
return r.json();
})
.then(list => {
estadoSelect.innerHTML = '<option value="">Selecciona estado</option>';
list.forEach(st => {
const option = new Option(st.nombre, st.id_estado);
estadoSelect.add(option);
});
estadoSelect.disabled = false;
})
.catch(error => {
console.error('Error:', error);
estadoSelect.innerHTML = '<option value="">Error al cargar</option>';
Swal.fire({
icon: 'error',
title: 'Error',
text: 'No se pudieron cargar los estados',
confirmButtonColor: '#dc3545'
});
});
});
// Al cambiar estado → recargar ciudades
document.getElementById('estado').addEventListener('change', function(){
const eid = this.value, selC = document.getElementById('ciudad');
selC.innerHTML = '<option>Cargando…</option>'; selC.disabled = true;
// 2) Cuando elige estado → cargo ciudades
document.getElementById('entidad').addEventListener('change', e => {
const eid = e.target.value;
const ciudadSelect = document.getElementById('ciudad');
ciudadSelect.innerHTML = '<option value="">Cargando…</option>';
ciudadSelect.disabled = true;
if (!eid) {
ciudadSelect.innerHTML = '<option value="">Primero estado…</option>';
return;
}
// Simulación de carga de ciudades (reemplaza con tu endpoint real)
fetch(`/IMPORTADORES/transportistas/ciudades?estado=${eid}`)
.then(r=>r.json())
.then(list=>{
selC.innerHTML = '<option value="">Selecciona ciudad</option>';
list.forEach(c=> selC.add(new Option(c.nombre,c.id_ciudad)) );
selC.disabled = false;
.then(r => {
if (!r.ok) throw new Error('Error al cargar ciudades');
return r.json();
})
.then(list => {
ciudadSelect.innerHTML = '<option value="">Selecciona ciudad</option>';
list.forEach(ct => {
const option = new Option(ct.nombre, ct.id_ciudad);
ciudadSelect.add(option);
});
ciudadSelect.disabled = false;
})
.catch(error => {
console.error('Error:', error);
ciudadSelect.innerHTML = '<option value="">Error al cargar</option>';
Swal.fire({
icon: 'error',
title: 'Error',
text: 'No se pudieron cargar las ciudades',
confirmButtonColor: '#dc3545'
});
});
});
</script>