191 lines
4.8 KiB
JavaScript
191 lines
4.8 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
if (serverErrors.length > 0) mostrarErrores(serverErrors);
|
|
if (serverSuccess.length > 0) mostrarSuccess(serverSuccess);
|
|
|
|
const form = document.getElementById("form-crear-presupuesto");
|
|
if (!form) return;
|
|
|
|
// Utilidades
|
|
|
|
function isEmpty(value) {
|
|
return !value || value.trim() === '';
|
|
}
|
|
|
|
function toFloat(value) {
|
|
return parseFloat(value) || 0;
|
|
}
|
|
|
|
// Submit
|
|
|
|
form.addEventListener("submit", (e) => {
|
|
|
|
let errores = [];
|
|
|
|
const condIva = form.querySelector("[name='cond_iva']");
|
|
const cuit = form.querySelector("[name='cuit']");
|
|
const formaPago = form.querySelector("[name='forma_pago']");
|
|
const tipoPago = form.querySelector("[name='tipo_pago']");
|
|
const descuento = form.querySelector("[name='descuento']");
|
|
const recargo = form.querySelector("[name='recargo']");
|
|
|
|
// limpiar estados previos
|
|
|
|
form.querySelectorAll(".is-invalid").forEach(el =>
|
|
el.classList.remove("is-invalid")
|
|
);
|
|
|
|
// FORMA Y TIPO DE PAGO
|
|
|
|
const reglasPago = {
|
|
'efectivo': ['pago_unico'],
|
|
'transferencia': ['pago_unico'],
|
|
'echeq': ['pago_unico'],
|
|
'debito': ['pago_unico'],
|
|
'credito': ['pago_unico', '3_cuotas', '6_cuotas', '12_cuotas'],
|
|
};
|
|
|
|
if (formaPago && tipoPago) {
|
|
|
|
const tiposValidos = reglasPago[formaPago.value] || [];
|
|
|
|
if (!tiposValidos.includes(tipoPago.value)) {
|
|
errores.push("Combinación de forma y tipo de pago inválida.");
|
|
formaPago.classList.add("is-invalid");
|
|
tipoPago.classList.add("is-invalid");
|
|
}
|
|
}
|
|
|
|
// DESCUENTO / RECARGO
|
|
|
|
if (descuento) {
|
|
const d = toFloat(descuento.value);
|
|
|
|
if (d < 0 || d > 100) {
|
|
errores.push("El descuento debe estar entre 0 y 100%.");
|
|
descuento.classList.add("is-invalid");
|
|
}
|
|
}
|
|
|
|
if (recargo) {
|
|
const r = toFloat(recargo.value);
|
|
|
|
if (r < 0 || r > 100) {
|
|
errores.push("El recargo debe estar entre 0 y 100%.");
|
|
recargo.classList.add("is-invalid");
|
|
}
|
|
}
|
|
|
|
// Resultado
|
|
|
|
if (errores.length > 0) {
|
|
e.preventDefault();
|
|
mostrarErrores(errores);
|
|
}
|
|
});
|
|
|
|
// FUNCIÓN PARA MOSTRAR ERRORES
|
|
|
|
function mostrarErrores(errores) {
|
|
|
|
// Buscar si hay un toast previo
|
|
|
|
const viejo = document.getElementById("toastErrores");
|
|
|
|
// Si existe, se elimina (evita duplicados)
|
|
|
|
if (viejo) viejo.remove();
|
|
|
|
// Crea el contenedor del toast
|
|
|
|
const toast = document.createElement("div");
|
|
|
|
// Se le asigna ID para identificarlo
|
|
|
|
toast.id = "toastErrores";
|
|
|
|
// Clases de estilo (Bootstrap + CSS)
|
|
|
|
toast.className = "toast-flotante alert alert-danger";
|
|
|
|
// Inserta el contenido dinámico (lista de errores)
|
|
|
|
toast.innerHTML = `
|
|
<strong>Se encontraron errores:</strong>
|
|
<ul class="mb-0 mt-2">
|
|
${errores.map(e => `<li>${e}</li>`).join("")} <!-- convierte array a <li> -->
|
|
</ul>
|
|
`;
|
|
|
|
// Agrega el toast al body (lo hace visible)
|
|
|
|
document.body.appendChild(toast);
|
|
|
|
// Auto-cierre
|
|
|
|
setTimeout(() => {
|
|
|
|
// Agrega clase para animación de salida (CSS)
|
|
|
|
toast.classList.add("toast-out");
|
|
|
|
// Elimina el elemento después de la animación
|
|
|
|
setTimeout(() => {
|
|
toast.remove(); // Elimina el DOM
|
|
}, 350);
|
|
|
|
}, 5000); // Visible durante 5 segundos
|
|
}
|
|
});
|
|
|
|
// FUNCIÓN PARA MOSTRAR SUCCESS
|
|
|
|
function mostrarSuccess(mensajes) {
|
|
|
|
// Elimina toast previo de éxito si existe
|
|
|
|
const viejo = document.getElementById("toastSuccess");
|
|
if (viejo) viejo.remove();
|
|
|
|
// Crea el contenedor del toast
|
|
|
|
const toast = document.createElement("div");
|
|
|
|
// Se le asigna ID único
|
|
|
|
toast.id = "toastSuccess";
|
|
|
|
// Estilo
|
|
|
|
toast.className = "toast-flotante alert alert-success";
|
|
|
|
// Inserta mensajes dinámicos
|
|
|
|
toast.innerHTML = `
|
|
<strong>Mensajes de éxito:</strong>
|
|
<ul class="mb-0 mt-2">
|
|
${mensajes.map(m => `<li>${m}</li>`).join("")}
|
|
</ul>
|
|
`;
|
|
|
|
// Agrega al DOM
|
|
|
|
document.body.appendChild(toast);
|
|
|
|
// Auto-cierre a los 5 segundos
|
|
|
|
setTimeout(() => {
|
|
|
|
// Animación de salida
|
|
|
|
toast.classList.add("toast-out");
|
|
|
|
// Eliminación final
|
|
|
|
setTimeout(() => {
|
|
toast.remove();
|
|
}, 350);
|
|
|
|
}, 5000);
|
|
} |