diff --git a/server.js b/server.js index 338b18f..353912d 100644 --- a/server.js +++ b/server.js @@ -2178,6 +2178,95 @@ app.delete("/budgets/:id", authMiddleware, async (req, res) => { } }); +// Convertir Presupuesto en Servicio Activo (CON SOPORTE RED INTERNA Y TRAZABILIDAD) +app.post("/budgets/:id/convert", authMiddleware, async (req, res) => { + try { + const { date, time, guild_id, assigned_to, use_automation } = req.body; + const bq = await pool.query("SELECT * FROM budgets WHERE id=$1 AND owner_id=$2", [req.params.id, req.user.accountId]); + if (bq.rowCount === 0) return res.status(404).json({ok: false}); + const budget = bq.rows[0]; + + // 1. Montamos el Raw Data para el servicio + const rawData = { + "Nombre Cliente": budget.client_name, + "Teléfono": budget.client_phone, + "Dirección": budget.client_address, + "Compañía": "Particular", + "Descripción": "PRESUPUESTO ACEPTADO.\n" + budget.items.map(i => `${i.qty}x ${i.concept}`).join("\n"), + "guild_id": guild_id || null, + "assigned_to": assigned_to || null, + "scheduled_date": date || "", + "scheduled_time": time || "" + }; + + // 2. Insertamos en el Panel Operativo (Buzón) empezando en manual + const insertSvc = await pool.query( + "INSERT INTO scraped_services (owner_id, provider, service_ref, status, automation_status, assigned_to, raw_data) VALUES ($1, 'particular', $2, 'pending', 'manual', $3, $4) RETURNING id", + [ + req.user.accountId, + `PRE-${budget.id}`, + assigned_to || null, + JSON.stringify(rawData) + ] + ); + + const newServiceId = insertSvc.rows[0].id; + + // 3. Marcamos presupuesto como convertido y le enlazamos la ficha financiera por el total + await pool.query("UPDATE budgets SET status='converted' WHERE id=$1", [budget.id]); + await pool.query( + "INSERT INTO service_financials (scraped_id, amount, payment_method) VALUES ($1, $2, 'Pendiente')", + [newServiceId, budget.total] + ); + + // 4. Si pide automatización, la disparamos internamente llamando a nuestra propia IP (127.0.0.1) + if (use_automation && guild_id) { + const cpMatch = budget.client_address ? budget.client_address.match(/\b\d{5}\b/) : null; + const cp = cpMatch ? cpMatch[0] : "00000"; + + const port = process.env.PORT || 3000; + const autoUrl = `http://127.0.0.1:${port}/providers/automate/${newServiceId}`; + + fetch(autoUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': req.headers.authorization + }, + body: JSON.stringify({ guild_id, cp, useDelay: false }) + }).catch(e => console.error("Error lanzando automatización interna:", e)); + + if (budget.client_phone) { + const msg = `✅ *PRESUPUESTO ACEPTADO*\n\nHola ${budget.client_name}, confirmamos la aceptación del presupuesto por un total de *${budget.total}€*.\n\nEn breve un técnico se pondrá en contacto contigo para agendar la cita. ¡Gracias por confiar en nosotros!`; + sendWhatsAppAuto(budget.client_phone, msg, `cliente_${req.user.accountId}`, false).catch(console.error); + } + } + else if (budget.client_phone && date && time) { + // Asignación directa a un técnico con fecha y hora + const [y, m, d] = date.split('-'); + const msg = `✅ *PRESUPUESTO ACEPTADO*\n\nHola ${budget.client_name}, confirmamos la aceptación del presupuesto por un total de *${budget.total}€*.\n\nEl servicio ha sido agendado para el *${d}/${m}/${y} a las ${time}*. ¡Gracias por confiar en nosotros!`; + sendWhatsAppAuto(budget.client_phone, msg, `cliente_${req.user.accountId}`, false).catch(console.error); + + if (assigned_to) { + const statusQ = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name ILIKE '%asignado%' LIMIT 1", [req.user.accountId]); + if (statusQ.rowCount > 0) { + rawData.status_operativo = statusQ.rows[0].id; + await pool.query("UPDATE scraped_services SET raw_data = $1 WHERE id = $2", [JSON.stringify(rawData), newServiceId]); + } + } + } + + // --- INICIO TRAZABILIDAD --- + await registrarMovimiento(newServiceId, req.user.sub, "Aviso Creado", `Servicio generado a raíz del presupuesto aceptado #PRE-${budget.id}.`); + // --- FIN TRAZABILIDAD --- + + res.json({ ok: true }); + } catch(e) { + console.error("Error convirtiendo presupuesto:", e); + res.status(500).json({ok: false}); + } +}); + // Convertir Presupuesto en Servicio Activo (CON SOPORTE RED INTERNA) app.post("/budgets/:id/convert", authMiddleware, async (req, res) => { try {