Advertisement
losvilos

pago_servicios.js

Mar 14th, 2025
81
0
20 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener("DOMContentLoaded", function () {
  2.     const buscarClienteInput = document.getElementById("buscarCliente");
  3.     const btnBuscarCliente = document.getElementById("btnBuscarCliente");
  4.  
  5.     btnBuscarCliente.addEventListener("click", function () {
  6.         const query = buscarClienteInput.value.trim();
  7.         if (query === "") {
  8.             alert("Ingrese un nombre o documento para buscar.");
  9.             return;
  10.         }
  11.  
  12.         fetch(`/api/clientes/buscar/?query=${encodeURIComponent(query)}`)
  13.             .then(response => response.json())
  14.             .then(data => {
  15.                 if (data.error) {
  16.                     alert("Cliente no encontrado.");
  17.                 } else {
  18.                     actualizarDatosCliente(data[0]);
  19.                 }
  20.             })
  21.             .catch(error => {
  22.                 console.error("Error al buscar cliente:", error);
  23.                 alert("Ocurrió un error al buscar el cliente.");
  24.             });
  25.     });
  26.  
  27.     function actualizarDatosCliente(cliente) {
  28.         document.getElementById("nombreCliente").textContent = cliente.nombre;
  29.         document.getElementById("direccionCliente").textContent = cliente.direccion || "No registrada";
  30.         document.getElementById("telefonoCliente").textContent = cliente.telefono || "No registrado";
  31.         document.getElementById("servicioCliente").textContent = cliente.servicio || "Sin servicio";
  32.     }
  33. });
  34.  
  35.  
  36.     // --- CARGAR MESES PENDIENTES ---
  37.     function cargarPagosPendientes(pagos) {
  38.         listaPagosPendientes.innerHTML = ""; // Limpiar antes de cargar nuevos datos
  39.         if (!pagos || pagos.length === 0) {
  40.             listaPagosPendientes.innerHTML = "<p class='text-muted'>No hay pagos pendientes.</p>";
  41.             return;
  42.         }
  43.  
  44.         pagos.forEach(pago => {
  45.             const div = document.createElement("div");
  46.             div.classList.add("form-check");
  47.  
  48.             div.innerHTML = `
  49.                 <input type="checkbox" class="form-check-input mes-pago" id="mes-${pago.mes}" value="${pago.monto}" data-mes="${pago.mes}">
  50.                 <label class="form-check-label" for="mes-${pago.mes}">${pago.mes} - $${pago.monto}</label>
  51.             `;
  52.  
  53.             listaPagosPendientes.appendChild(div);
  54.         });
  55.  
  56.         actualizarTotal(); // Asegurar que el total se actualice al cargar
  57.     }
  58.  
  59.     // --- ACTUALIZAR TOTAL CUANDO SE SELECCIONAN MESES ---
  60.     listaPagosPendientes.addEventListener("change", function () {
  61.         actualizarTotal();
  62.     });
  63.  
  64.     function actualizarTotal() {
  65.         let total = 0;
  66.         mesesSeleccionados = [];
  67.         document.querySelectorAll(".mes-pago:checked").forEach(checkbox => {
  68.             total += parseFloat(checkbox.value);
  69.             mesesSeleccionados.push(checkbox.dataset.mes);
  70.         });
  71.  
  72.         totalCobrar.textContent = `$${total.toFixed(2)}`;
  73.     }
  74.  
  75.     // --- ABRIR MODAL AL COBRAR ---
  76.     btnCobrar.addEventListener("click", function () {
  77.         if (!clienteSeleccionado) {
  78.             alert("Debe seleccionar un cliente primero.");
  79.             return;
  80.         }
  81.  
  82.         if (mesesSeleccionados.length === 0) {
  83.             alert("Seleccione al menos un mes para cobrar.");
  84.             return;
  85.         }
  86.  
  87.         if (formaPago.value === "") {
  88.             alert("Seleccione una forma de pago.");
  89.             return;
  90.         }
  91.  
  92.         modalBoleta.show();
  93.     });
  94.  
  95.     // --- CONFIRMAR COBRO ---
  96.     btnConfirmarCobro.addEventListener("click", function () {
  97.         const numeroBoleta = inputNumeroBoleta.value.trim();
  98.         if (numeroBoleta === "") {
  99.             alert("Ingrese un número de boleta.");
  100.             return;
  101.         }
  102.  
  103.         // ENVIAR DATOS AL BACKEND
  104.         fetch("/api/pagos/registrar/", {
  105.             method: "POST",
  106.             headers: {
  107.                 "Content-Type": "application/json",
  108.                 "X-CSRFToken": document.querySelector("[name=csrfmiddlewaretoken]").value
  109.             },
  110.             body: JSON.stringify({
  111.                 cliente_id: clienteSeleccionado.id,
  112.                 meses: mesesSeleccionados,
  113.                 total: parseFloat(totalCobrar.textContent.replace("$", "")),
  114.                 forma_pago: formaPago.value,
  115.                 numero_boleta: numeroBoleta
  116.             })
  117.         })
  118.         .then(response => {
  119.             if (!response.ok) {
  120.                 throw new Error("Error en el registro del pago.");
  121.             }
  122.             return response.json();
  123.         })
  124.         .then(data => {
  125.             if (data.success) {
  126.                 alert("Pago registrado correctamente.");
  127.                 location.reload();
  128.             } else {
  129.                 alert("Error al registrar el pago.");
  130.             }
  131.         })
  132.         .catch(error => {
  133.             console.error("Error en el pago:", error);
  134.             alert("Ocurrió un error al procesar el pago.");
  135.         });
  136.     });
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement