Actualizar robot_cobros.js
This commit is contained in:
parent
47bac60db6
commit
3cb5cdcaae
230
robot_cobros.js
230
robot_cobros.js
|
|
@ -24,25 +24,33 @@ const APPOINTMENTS_COL = "appointments";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors({ origin: '*' }));
|
app.use(cors({ origin: '*' }));
|
||||||
app.use(express.json());
|
app.use(express.json({ limit: '10mb' })); // Permitir payloads grandes para guardar
|
||||||
|
|
||||||
// --- ENDPOINT UNIFICADO ---
|
// --- ENDPOINT PRINCIPAL ---
|
||||||
app.post('/api/robot-cobros', async (req, res) => {
|
app.post('/api/robot-cobros', async (req, res) => {
|
||||||
const { action, urls } = req.body;
|
const { action, url, provider, dataToSave } = req.body;
|
||||||
console.log(`🔔 Orden recibida: ${action ? action.toUpperCase() : 'DESCONOCIDA'}`);
|
console.log(`🔔 Orden: ${action.toUpperCase()} (${provider || 'HS'})`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (action === 'scan') {
|
if (action === 'scan') {
|
||||||
|
// Paso 1: Buscar fechas
|
||||||
const lista = await runScanner();
|
const lista = await runScanner();
|
||||||
res.json({ success: true, mode: 'scan', data: lista });
|
res.json({ success: true, data: lista });
|
||||||
}
|
|
||||||
else if (action === 'process') {
|
|
||||||
if (!urls || urls.length === 0) throw new Error("No has seleccionado ninguna fecha.");
|
|
||||||
const procesados = await runProcessor(urls);
|
|
||||||
res.json({ success: true, mode: 'process', count: procesados });
|
|
||||||
}
|
}
|
||||||
|
else if (action === 'analyze') {
|
||||||
|
// Paso 2: Leer fecha y cruzar con DB (Sin guardar)
|
||||||
|
if (!url) throw new Error("Falta URL");
|
||||||
|
const analisis = await runAnalyzer(url);
|
||||||
|
res.json({ success: true, ...analisis });
|
||||||
|
}
|
||||||
|
else if (action === 'save_data') {
|
||||||
|
// Paso 3: Guardar lo confirmado
|
||||||
|
if (!dataToSave || !Array.isArray(dataToSave)) throw new Error("No hay datos");
|
||||||
|
const count = await runSaver(dataToSave);
|
||||||
|
res.json({ success: true, count });
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw new Error("Acción no válida o faltan parámetros.");
|
throw new Error("Acción desconocida");
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("❌ Error:", err.message);
|
console.error("❌ Error:", err.message);
|
||||||
|
|
@ -50,175 +58,151 @@ app.post('/api/robot-cobros', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- FUNCIÓN 1: ESCANEAR FECHAS (SCAN) ---
|
// --- 1. ESCÁNER DE FECHAS ---
|
||||||
async function runScanner() {
|
async function runScanner() {
|
||||||
let browser = null;
|
let browser = null;
|
||||||
try {
|
try {
|
||||||
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
||||||
browser = b;
|
browser = b;
|
||||||
|
|
||||||
console.log("🔍 Buscando lista de liquidaciones...");
|
|
||||||
|
|
||||||
const liquidaciones = await page.evaluate(() => {
|
const liquidaciones = await page.evaluate(() => {
|
||||||
const links = Array.from(document.querySelectorAll('a'));
|
const links = Array.from(document.querySelectorAll('a'));
|
||||||
const results = [];
|
const results = [];
|
||||||
links.forEach(l => {
|
links.forEach(l => {
|
||||||
const txt = l.innerText.trim();
|
const txt = l.innerText.trim();
|
||||||
// Detectar formato fecha (DD/MM/YYYY)
|
|
||||||
if (/\d{2}\/\d{2}\/\d{4}/.test(txt)) {
|
if (/\d{2}\/\d{2}\/\d{4}/.test(txt)) {
|
||||||
results.push({ fecha: txt, url: l.href });
|
results.push({ fecha: txt, url: l.href });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return results;
|
return results;
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`✅ Encontradas ${liquidaciones.length} fechas.`);
|
|
||||||
return liquidaciones;
|
return liquidaciones;
|
||||||
|
|
||||||
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- FUNCIÓN 2: PROCESAR SELECCIONADAS (PROCESS) ---
|
// --- 2. ANALIZADOR (PREVISUALIZACIÓN) ---
|
||||||
async function runProcessor(urlsAProcesar) {
|
async function runAnalyzer(targetUrl) {
|
||||||
let browser = null;
|
let browser = null;
|
||||||
let totalActualizados = 0;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
||||||
browser = b;
|
browser = b;
|
||||||
|
|
||||||
for (const targetUrl of urlsAProcesar) {
|
console.log(`➡️ Analizando: ${targetUrl}`);
|
||||||
console.log(`➡️ Procesando URL: ${targetUrl}`);
|
await page.goto(targetUrl, { timeout: 60000 });
|
||||||
|
await page.waitForTimeout(1500);
|
||||||
// 1. Ir a la fecha
|
|
||||||
await page.goto(targetUrl, { timeout: 60000 });
|
|
||||||
await page.waitForTimeout(1500);
|
|
||||||
|
|
||||||
// 2. BUSCAR BOTÓN ESPECÍFICO "DESGLOSE SERVICIOS"
|
// Buscar botón "Desglose Servicios"
|
||||||
console.log(" 🔘 Buscando botón 'Desglose Servicios'...");
|
const botonPulsado = await page.evaluate(() => {
|
||||||
|
const elementos = Array.from(document.querySelectorAll('input, button, a'));
|
||||||
const botonPulsado = await page.evaluate(() => {
|
const target = elementos.find(el => {
|
||||||
// Buscamos inputs, botones y enlaces
|
const txt = (el.value || el.innerText || "").toUpperCase();
|
||||||
const elementos = Array.from(document.querySelectorAll('input[type="button"], input[type="submit"], button, a'));
|
return txt.includes("SERVICIO") && txt.includes("DESGLOSE");
|
||||||
|
|
||||||
// Opción A: Buscar texto exacto "DESGLOSE" y "SERVICIO"
|
|
||||||
const target = elementos.find(el => {
|
|
||||||
const texto = (el.value || el.innerText || "").toUpperCase();
|
|
||||||
return texto.includes("SERVICIO") && texto.includes("DESGLOSE");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Opción B (Fallback): Buscar solo "SERVICIOS" si falla lo anterior
|
|
||||||
// CORRECCIÓN AQUÍ: antes ponía 'elements' y fallaba
|
|
||||||
const targetSecundario = elementos.find(el => (el.value || el.innerText || "").toUpperCase().includes("SERVICIOS"));
|
|
||||||
|
|
||||||
const finalTarget = target || targetSecundario;
|
|
||||||
|
|
||||||
if (finalTarget) {
|
|
||||||
finalTarget.click();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
|
if (target) { target.click(); return true; }
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
if (botonPulsado) {
|
if (botonPulsado) await page.waitForTimeout(3000);
|
||||||
console.log(" ✅ Botón pulsado. Esperando tabla...");
|
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
} else {
|
|
||||||
console.warn(" ⚠️ No encontré el botón de Servicios. Intentando leer por si ya estamos dentro.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. LEER LA TABLA DE DATOS
|
// Extraer datos visuales
|
||||||
const datosTabla = await page.evaluate(() => {
|
const datosRaw = await page.evaluate(() => {
|
||||||
const filas = Array.from(document.querySelectorAll('tr'));
|
const filas = Array.from(document.querySelectorAll('tr'));
|
||||||
const datos = [];
|
const datos = [];
|
||||||
|
filas.forEach(tr => {
|
||||||
filas.forEach(tr => {
|
const tds = tr.querySelectorAll('td');
|
||||||
const tds = tr.querySelectorAll('td');
|
// Col 0: Servicio, Col 1: Dirección, Col 5: Saldo
|
||||||
// Estructura saldo.html:
|
if (tds.length >= 6) {
|
||||||
// Col 0: Servicio | Col 5: Saldo
|
const servicio = tds[0].innerText.trim();
|
||||||
if (tds.length >= 6) {
|
const direccion = tds[1].innerText.trim();
|
||||||
const servicio = tds[0].innerText.trim();
|
const saldoRaw = tds[5].innerText.trim();
|
||||||
const saldoRaw = tds[5].innerText.trim();
|
if (/^\d{5,}$/.test(servicio)) {
|
||||||
|
datos.push({ servicio, direccion, saldoRaw });
|
||||||
// Validar que sea un número de servicio real (5 o más dígitos)
|
|
||||||
if (/^\d{5,}$/.test(servicio)) {
|
|
||||||
datos.push({ servicio, saldoRaw });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
return datos;
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(` 💰 Leídos ${datosTabla.length} servicios en esta liquidación.`);
|
|
||||||
|
|
||||||
// 4. GUARDAR EN FIREBASE
|
|
||||||
if (db && datosTabla.length > 0) {
|
|
||||||
const batch = db.batch();
|
|
||||||
let batchCount = 0;
|
|
||||||
const nowISO = new Date().toISOString();
|
|
||||||
|
|
||||||
for (const item of datosTabla) {
|
|
||||||
// Limpieza "-33.48" -> 33.48
|
|
||||||
let cleanSaldo = item.saldoRaw.replace(/[^\d.-]/g, '');
|
|
||||||
let importe = parseFloat(cleanSaldo);
|
|
||||||
|
|
||||||
if (isNaN(importe)) continue;
|
|
||||||
|
|
||||||
// Convertimos a positivo para guardar en 'paidAmount'
|
|
||||||
importe = Math.abs(importe);
|
|
||||||
|
|
||||||
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.servicio).get();
|
|
||||||
|
|
||||||
q.forEach(doc => {
|
|
||||||
batch.update(doc.ref, {
|
|
||||||
paidAmount: importe,
|
|
||||||
status: 'completed',
|
|
||||||
paymentDate: nowISO,
|
|
||||||
homeservePaymentStatus: 'paid_saldo',
|
|
||||||
lastUpdatedByRobot: nowISO
|
|
||||||
});
|
|
||||||
batchCount++;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
return datos;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cruzar con Firebase (Lectura)
|
||||||
|
const encontrados = [];
|
||||||
|
const noEncontrados = [];
|
||||||
|
|
||||||
|
if (db) {
|
||||||
|
for (const item of datosRaw) {
|
||||||
|
let cleanSaldo = item.saldoRaw.replace(/[^\d.-]/g, '');
|
||||||
|
let importe = Math.abs(parseFloat(cleanSaldo) || 0);
|
||||||
|
|
||||||
|
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.servicio).get();
|
||||||
|
|
||||||
if (batchCount > 0) {
|
if (!q.empty) {
|
||||||
await batch.commit();
|
// Si hay duplicados, cogemos el primero o todos
|
||||||
totalActualizados += batchCount;
|
q.forEach(doc => {
|
||||||
console.log(` 💾 Guardados ${batchCount} registros.`);
|
encontrados.push({
|
||||||
|
servicio: item.servicio,
|
||||||
|
direccion: item.direccion,
|
||||||
|
importe: importe,
|
||||||
|
docId: doc.id
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
noEncontrados.push({
|
||||||
|
servicio: item.servicio,
|
||||||
|
direccion: item.direccion,
|
||||||
|
importe: importe
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return totalActualizados;
|
console.log(`✅ Análisis: ${encontrados.length} encontrados, ${noEncontrados.length} no encontrados.`);
|
||||||
|
return { encontrados, noEncontrados };
|
||||||
|
|
||||||
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- 3. GUARDADOR (RÁPIDO) ---
|
||||||
|
async function runSaver(items) {
|
||||||
|
if (!db) return 0;
|
||||||
|
const batch = db.batch();
|
||||||
|
const nowISO = new Date().toISOString();
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
items.forEach(item => {
|
||||||
|
const ref = db.collection(APPOINTMENTS_COL).doc(item.docId);
|
||||||
|
batch.update(ref, {
|
||||||
|
paidAmount: item.importe,
|
||||||
|
status: 'completed',
|
||||||
|
paymentDate: nowISO,
|
||||||
|
homeservePaymentStatus: 'paid_saldo',
|
||||||
|
lastUpdatedByRobot: nowISO
|
||||||
|
});
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
|
||||||
|
await batch.commit();
|
||||||
|
console.log(`💾 Guardados ${count} documentos en DB.`);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
// --- LOGIN ---
|
// --- LOGIN ---
|
||||||
async function loginAndGoToLiquidaciones() {
|
async function loginAndGoToLiquidaciones() {
|
||||||
let user = "SIN_USER", pass = "SIN_PASS";
|
let user = "", pass = "";
|
||||||
if (db) {
|
if (db) {
|
||||||
const doc = await db.collection("providerCredentials").doc("homeserve").get();
|
const doc = await db.collection("providerCredentials").doc("homeserve").get();
|
||||||
if (doc.exists) { user = doc.data().user; pass = doc.data().pass; }
|
if (doc.exists) { user = doc.data().user; pass = doc.data().pass; }
|
||||||
}
|
}
|
||||||
|
|
||||||
const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
|
const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
|
||||||
console.log("🌍 Login...");
|
|
||||||
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
|
||||||
|
|
||||||
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
||||||
if (await page.isVisible('input[name="CODIGO"]')) {
|
if (await page.isVisible('input[name="CODIGO"]')) {
|
||||||
await page.fill('input[name="CODIGO"]', user);
|
await page.fill('input[name="CODIGO"]', user);
|
||||||
await page.fill('input[type="password"]', pass);
|
await page.fill('input[type="password"]', pass);
|
||||||
await page.keyboard.press('Enter');
|
await page.keyboard.press('Enter');
|
||||||
await page.waitForTimeout(4000);
|
await page.waitForTimeout(4000);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("📂 Sección Liquidaciones...");
|
|
||||||
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB');
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB');
|
||||||
await page.waitForTimeout(2000);
|
await page.waitForTimeout(2000);
|
||||||
|
|
||||||
return { browser, page };
|
return { browser, page };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue