245 lines
11 KiB
PHP
245 lines
11 KiB
PHP
<!-- views/proveedores/crear.php -->
|
||
<?php include __DIR__ . '/../partials/sidebar_importador.php'; ?>
|
||
|
||
<!DOCTYPE html>
|
||
<html lang="es">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>➕ Registro de Proveedor</title>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<!-- Bootstrap CSS -->
|
||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||
<!-- 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; }
|
||
.sidebar .nav-link { color: #ccc; padding: 12px 20px; }
|
||
.sidebar .nav-link:hover,
|
||
.sidebar .nav-link.active { background-color: #495057; color: #fff; }
|
||
.content { margin-top: 56px; padding: 40px 20px; background-color: #f4f6f9; transition: margin-left .3s ease; }
|
||
@media (min-width: 768px) { .content { margin-left: 250px; } }
|
||
@media (max-width: 767.98px) { .content { margin-left: 0; } }
|
||
.card { border-radius: 12px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
|
||
<?php
|
||
// Mostrar alerta si hay error en sesión
|
||
if (!empty($_SESSION['flash_error'])):
|
||
?>
|
||
<script>
|
||
Swal.fire({
|
||
icon: 'error',
|
||
title: 'Error al registrar',
|
||
text: '<?= addslashes($_SESSION['flash_error']) ?>'
|
||
});
|
||
</script>
|
||
<?php
|
||
unset($_SESSION['flash_error']);
|
||
endif;
|
||
?>
|
||
|
||
<div class="content">
|
||
<h4 class="mb-4">➕ Registro de Proveedor</h4>
|
||
<div class="card p-4 shadow-sm">
|
||
<form action="/IMPORTADORES/proveedores/guardar" method="POST" enctype="multipart/form-data" id="formAltaProveedores">
|
||
<div class="row g-3">
|
||
<div class="col-md-4">
|
||
<label for="Clave" class="form-label">Clave *</label>
|
||
<input type="text" name="Clave" id="Clave" class="form-control" maxlength="20" required>
|
||
</div>
|
||
<div class="col-md-8">
|
||
<label for="Nombre" class="form-label">Nombre *</label>
|
||
<input type="text" name="Nombre" id="Nombre" class="form-control" required>
|
||
</div>
|
||
<div class="col-md-4">
|
||
<label for="RFC" class="form-label">RFC *</label>
|
||
<input type="text" name="RFC" id="RFC" class="form-control" maxlength="13" required>
|
||
</div>
|
||
<div class="col-md-4">
|
||
<label for="Ciudad" class="form-label">Ciudad *</label>
|
||
<input type="text" name="Ciudad" id="Ciudad" class="form-control" required>
|
||
</div>
|
||
<div class="col-md-4">
|
||
<label for="Telefono" class="form-label">Teléfono</label>
|
||
<input type="tel" name="Telefono" id="Telefono" maxlength="11" class="form-control">
|
||
</div>
|
||
<div class="col-md-6">
|
||
<label for="Pais" class="form-label">País</label>
|
||
<input type="text" name="Pais" id="Pais" class="form-control">
|
||
</div>
|
||
<div class="col-md-6">
|
||
<label for="EntidadFederativa" class="form-label">Entidad Federativa</label>
|
||
<input type="text" name="EntidadFederativa" id="EntidadFederativa" class="form-control">
|
||
</div>
|
||
<div class="col-md-4">
|
||
<label for="Municipio" class="form-label">Municipio</label>
|
||
<input type="text" name="Municipio" id="Municipio" class="form-control">
|
||
</div>
|
||
<div class="col-md-4">
|
||
<label for="Colonia" class="form-label">Colonia</label>
|
||
<input type="text" name="Colonia" id="Colonia" class="form-control">
|
||
</div>
|
||
<div class="col-md-4">
|
||
<label for="CodigoPostal" class="form-label">Código Postal</label>
|
||
<input type="text" name="CodigoPostal" id="CodigoPostal" maxlength="5" class="form-control">
|
||
</div>
|
||
</div>
|
||
<div class="mt-4 text-end">
|
||
<a href="/IMPORTADORES/proveedores" class="btn btn-secondary me-2">Cancelar</a>
|
||
<button type="submit" class="btn btn-primary">Guardar Proveedor</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Bootstrap JS Bundle -->
|
||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||
|
||
<script>
|
||
document.getElementById('formAltaProveedores').addEventListener('submit', function(e) {
|
||
e.preventDefault(); // Prevenir envío por defecto
|
||
|
||
// Obtener todos los campos
|
||
const clave = document.getElementById('Clave').value.trim();
|
||
const nombre = document.getElementById('Nombre').value.trim();
|
||
const rfc = document.getElementById('RFC').value.trim().toUpperCase();
|
||
const ciudad = document.getElementById('Ciudad').value.trim();
|
||
const telefono = document.getElementById('Telefono').value.trim();
|
||
const pais = document.getElementById('Pais').value.trim();
|
||
const entidadFederativa = document.getElementById('EntidadFederativa').value.trim();
|
||
const municipio = document.getElementById('Municipio').value.trim();
|
||
const colonia = document.getElementById('Colonia').value.trim();
|
||
const codigoPostal = document.getElementById('CodigoPostal').value.trim();
|
||
|
||
// Expresiones regulares
|
||
const rfcRegex = /^[A-ZÑ&]{3,4}\d{6}[A-Z0-9]{3}$/;
|
||
const telefonoRegex = /^\d{3}\s\d{7}$/;
|
||
const codigoPostalRegex = /^\d{5}$/;
|
||
const soloLetrasRegex = /^[a-zA-ZáéíóúÁÉÍÓÚñÑ\s]+$/;
|
||
const alfanumericoRegex = /^[a-zA-Z0-9áéíóúÁÉÍÓÚñÑ\s]+$/;
|
||
|
||
// VALIDAR CAMPOS OBLIGATORIOS
|
||
if (!clave) {
|
||
Swal.fire({ icon: 'warning', title: 'Clave requerida', text: 'Por favor ingresa la clave del proveedor.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Clave').focus();
|
||
return;
|
||
}
|
||
if (clave.length < 2 || clave.length > 20) {
|
||
Swal.fire({ icon: 'error', title: 'Clave inválida', text: 'La clave debe tener entre 2 y 20 caracteres.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Clave').focus();
|
||
return;
|
||
}
|
||
if (!alfanumericoRegex.test(clave)) {
|
||
Swal.fire({ icon: 'error', title: 'Clave inválida', text: 'La clave solo puede contener letras, números y espacios.', confirmButtonColor: '#dc3545'
|
||
});
|
||
document.getElementById('Clave').focus();
|
||
return;
|
||
}
|
||
if (!nombre) {
|
||
Swal.fire({ icon: 'warning', title: 'Nombre requerido', text: 'Por favor ingresa el nombre del proveedor.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Nombre').focus();
|
||
return;
|
||
}
|
||
if (nombre.length < 2 || nombre.length > 100) {
|
||
Swal.fire({ icon: 'error', title: 'Nombre inválido', text: 'El nombre debe tener entre 2 y 100 caracteres.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Nombre').focus();
|
||
return;
|
||
}
|
||
if (!rfc) {
|
||
Swal.fire({ icon: 'warning', title: 'RFC requerido', text: 'Por favor ingresa el RFC del proveedor.', 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 (!ciudad) {
|
||
Swal.fire({ icon: 'warning', title: 'Ciudad requerida', text: 'Por favor ingresa la ciudad del proveedor.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Ciudad').focus();
|
||
return;
|
||
}
|
||
if (!soloLetrasRegex.test(ciudad)) {
|
||
Swal.fire({ icon: 'error', title: 'Ciudad inválida', text: 'La ciudad solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Ciudad').focus();
|
||
return;
|
||
}
|
||
// VALIDAR CAMPOS OPCIONALES (solo si tienen contenido)
|
||
if (telefono) {
|
||
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 (pais) {
|
||
if (!soloLetrasRegex.test(pais)) {
|
||
Swal.fire({ icon: 'error', title: 'País inválido', text: 'El país solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Pais').focus();
|
||
return;
|
||
}
|
||
if (pais.length < 2 || pais.length > 50) {
|
||
Swal.fire({ icon: 'error', title: 'País inválido', text: 'El país debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545'
|
||
});
|
||
document.getElementById('Pais').focus();
|
||
return;
|
||
}
|
||
}
|
||
if (entidadFederativa) {
|
||
if (!soloLetrasRegex.test(entidadFederativa)) {
|
||
Swal.fire({ icon: 'error', title: 'Entidad Federativa inválida', text: 'La entidad federativa solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('EntidadFederativa').focus();
|
||
return;
|
||
}
|
||
if (entidadFederativa.length < 2 || entidadFederativa.length > 50) {
|
||
Swal.fire({ icon: 'error', title: 'Entidad Federativa inválida', text: 'La entidad federativa debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('EntidadFederativa').focus();
|
||
return;
|
||
}
|
||
}
|
||
if (municipio) {
|
||
if (!soloLetrasRegex.test(municipio)) {
|
||
Swal.fire({ icon: 'error', title: 'Municipio inválido', text: 'El municipio solo puede contener letras y espacios.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Municipio').focus();
|
||
return;
|
||
}
|
||
if (municipio.length < 2 || municipio.length > 50) {
|
||
Swal.fire({ icon: 'error', title: 'Municipio inválido', text: 'El municipio debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Municipio').focus();
|
||
return;
|
||
}
|
||
}
|
||
if (colonia) {
|
||
if (!alfanumericoRegex.test(colonia)) {
|
||
Swal.fire({ icon: 'error', title: 'Colonia inválida', text: 'La colonia solo puede contener letras, números y espacios.', confirmButtonColor: '#dc3545'
|
||
});
|
||
document.getElementById('Colonia').focus();
|
||
return;
|
||
}
|
||
if (colonia.length < 2 || colonia.length > 50) {
|
||
Swal.fire({ icon: 'error', title: 'Colonia inválida', text: 'La colonia debe tener entre 2 y 50 caracteres.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('Colonia').focus();
|
||
return;
|
||
}
|
||
}
|
||
if (codigoPostal) {
|
||
if (!codigoPostalRegex.test(codigoPostal)) {
|
||
Swal.fire({ icon: 'error', title: 'Código Postal inválido', text: 'El código postal debe tener exactamente 5 dígitos.', confirmButtonColor: '#dc3545' });
|
||
document.getElementById('CodigoPostal').focus();
|
||
return;
|
||
}
|
||
}
|
||
// Actualizar el valor del RFC en mayúsculas antes de enviar
|
||
document.getElementById('RFC').value = rfc;
|
||
|
||
// SI TODAS LAS VALIDACIONES PASAN, ENVIAR EL FORMULARIO
|
||
this.submit();
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html>
|