diff --git a/robot_cobros.js b/robot_cobros.js index f7e675e..da3a230 100644 --- a/robot_cobros.js +++ b/robot_cobros.js @@ -28,8 +28,7 @@ app.use(express.json()); // --- ENDPOINT UNIFICADO --- app.post('/api/robot-cobros', async (req, res) => { - const { action, urls } = req.body; // action: 'scan' o 'process' - + const { action, urls } = req.body; console.log(`🔔 Orden recibida: ${action.toUpperCase()}`); try { @@ -51,7 +50,7 @@ app.post('/api/robot-cobros', async (req, res) => { } }); -// --- FUNCIÓN 1: ESCANEAR FECHAS DISPONIBLES --- +// --- FUNCIÓN 1: ESCANEAR FECHAS (SCAN) --- async function runScanner() { let browser = null; try { @@ -60,20 +59,14 @@ async function runScanner() { console.log("🔍 Buscando lista de liquidaciones..."); - // Extraemos todos los enlaces de fechas const liquidaciones = await page.evaluate(() => { const links = Array.from(document.querySelectorAll('a')); const results = []; - links.forEach(l => { const txt = l.innerText.trim(); - // Filtramos por formato fecha (DD/MM/YYYY) o similar + // Detectar formato fecha (DD/MM/YYYY) if (/\d{2}\/\d{2}\/\d{4}/.test(txt)) { - // Guardamos la URL absoluta para navegar directo luego - results.push({ - fecha: txt, - url: l.href - }); + results.push({ fecha: txt, url: l.href }); } }); return results; @@ -85,49 +78,69 @@ async function runScanner() { } catch (e) { throw e; } finally { if(browser) await browser.close(); } } -// --- FUNCIÓN 2: PROCESAR SELECCIONADAS --- +// --- FUNCIÓN 2: PROCESAR SELECCIONADAS (PROCESS) --- async function runProcessor(urlsAProcesar) { let browser = null; let totalActualizados = 0; try { - const { browser: b, page } = await loginAndGoToLiquidaciones(); // Login inicial + const { browser: b, page } = await loginAndGoToLiquidaciones(); browser = b; for (const targetUrl of urlsAProcesar) { console.log(`➡️ Procesando URL: ${targetUrl}`); - // 1. Navegar a la fecha específica + // 1. Ir a la fecha await page.goto(targetUrl, { timeout: 60000 }); - await page.waitForTimeout(2000); + await page.waitForTimeout(1500); - // 2. Buscar botón "Desglose" y pulsar si existe - const hayDesglose = await page.evaluate(() => { - const btn = Array.from(document.querySelectorAll('input[type="button"], button, a')) - .find(el => (el.value || el.innerText || "").toLowerCase().includes('desglose')); - if(btn) { btn.click(); return true; } + // 2. BUSCAR BOTÓN ESPECÍFICO "DESGLOSE SERVICIOS" + // Buscamos cualquier botón/input que contenga la palabra "SERVICIOS" (ignorando mayusc/minusc) + console.log(" 🔘 Buscando botón 'Desglose Servicios'..."); + + const botonPulsado = await page.evaluate(() => { + // Buscamos en inputs (tipo submit/button), botones y enlaces + const elementos = Array.from(document.querySelectorAll('input[type="button"], input[type="submit"], button, a')); + + // Filtramos el que tenga "SERVICIOS" y "DESGLOSE" en su texto o valor + const target = elementos.find(el => { + const texto = (el.value || el.innerText || "").toUpperCase(); + return texto.includes("SERVICIO") && texto.includes("DESGLOSE"); + }); + + // Si no encontramos uno tan específico, probamos solo con "SERVICIOS" dentro de la tabla de botones + const targetSecundario = elements.find(el => (el.value || el.innerText || "").toUpperCase().includes("SERVICIOS")); + + const finalTarget = target || targetSecundario; + + if (finalTarget) { + finalTarget.click(); + return true; + } return false; }); - - if(hayDesglose) { - console.log(" 🔘 Entrando al Desglose..."); - await page.waitForTimeout(3000); + + if (botonPulsado) { + console.log(" ✅ Botón pulsado. Esperando tabla..."); + await page.waitForTimeout(3000); // Esperar a que cargue la tabla de datos + } else { + console.warn(" ⚠️ No encontré el botón de Servicios. Intentando leer por si ya estamos dentro."); } - // 3. LEER LA TABLA (EXTRAER SALDO) + // 3. LEER LA TABLA DE DATOS const datosTabla = await page.evaluate(() => { const filas = Array.from(document.querySelectorAll('tr')); const datos = []; filas.forEach(tr => { const tds = tr.querySelectorAll('td'); - // Según tu saldo.html: - // Col 0: Servicio | Col 5 (Sexta columna): Saldo + // Estructura saldo.html: + // Col 0: Servicio | Col 5: Saldo if (tds.length >= 6) { const servicio = tds[0].innerText.trim(); - const saldoRaw = tds[5].innerText.trim(); // <--- COLUMNA SALDO + const saldoRaw = tds[5].innerText.trim(); - // Validar que sea un servicio numérico + // Validar que sea un número de servicio real if (/^\d{5,}$/.test(servicio)) { datos.push({ servicio, saldoRaw }); } @@ -136,7 +149,7 @@ async function runProcessor(urlsAProcesar) { return datos; }); - console.log(` 💰 Leídos ${datosTabla.length} servicios. Guardando en Firebase...`); + console.log(` 💰 Leídos ${datosTabla.length} servicios en esta liquidación.`); // 4. GUARDAR EN FIREBASE if (db && datosTabla.length > 0) { @@ -145,23 +158,21 @@ async function runProcessor(urlsAProcesar) { const nowISO = new Date().toISOString(); for (const item of datosTabla) { - // Limpieza: "-33.48" -> 33.48 (Positivo) - let cleanSaldo = item.saldoRaw.replace(/[^\d.-]/g, ''); // Deja solo numeros, punto y menos + // Limpieza "-33.48" -> 33.48 + let cleanSaldo = item.saldoRaw.replace(/[^\d.-]/g, ''); let importe = parseFloat(cleanSaldo); if (isNaN(importe)) continue; - // IMPORTANTE: Convertimos a positivo para paidAmount - // Si prefieres negativo, quita Math.abs() + // Convertimos a positivo para guardar en 'paidAmount' importe = Math.abs(importe); - // Buscar el documento en appointments const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.servicio).get(); q.forEach(doc => { batch.update(doc.ref, { - paidAmount: importe, // <--- GUARDAMOS EL SALDO AQUÍ - status: 'completed', // Asumimos completado + paidAmount: importe, + status: 'completed', paymentDate: nowISO, homeservePaymentStatus: 'paid_saldo', lastUpdatedByRobot: nowISO @@ -173,6 +184,7 @@ async function runProcessor(urlsAProcesar) { if (batchCount > 0) { await batch.commit(); totalActualizados += batchCount; + console.log(` 💾 Guardados ${batchCount} registros.`); } } } @@ -182,7 +194,7 @@ async function runProcessor(urlsAProcesar) { } catch (e) { throw e; } finally { if(browser) await browser.close(); } } -// --- AUXILIAR LOGIN --- +// --- LOGIN --- async function loginAndGoToLiquidaciones() { let user = "SIN_USER", pass = "SIN_PASS"; if (db) {