document.addEventListener("DOMContentLoaded", () => { if (serverErrors.length > 0) mostrarErrores(serverErrors); if (serverSuccess.length > 0) mostrarSuccess(serverSuccess); const form = document.getElementById("form-crear-venta"); 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"); } } // RECARGO 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 = ` Se encontraron errores: