From a9a761a0b3150f6b95b5f3947722337322bf6c52 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 1 Mar 2026 16:02:16 +0000 Subject: [PATCH] Actualizar contabilidad.html --- contabilidad.html | 100 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 6 deletions(-) diff --git a/contabilidad.html b/contabilidad.html index 6b548e3..849e2c5 100644 --- a/contabilidad.html +++ b/contabilidad.html @@ -516,21 +516,109 @@ loadBudgets(); } - function openConvertModal(id) { + // ========================================== + // LÓGICA DE CONVERSIÓN A SERVICIO (MODAL) + // ========================================== + async function openConvertModal(id) { document.getElementById('convBudgetId').value = id; + + // 1. Limpiamos y cargamos los gremios directamente de la base de datos + const gSel = document.getElementById('convGuild'); + gSel.innerHTML = ''; + + try { + const res = await fetch(`${API_URL}/guilds`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); + const data = await res.json(); + + gSel.innerHTML = ''; + if (data.ok && data.guilds) { + data.guilds.forEach(g => { + gSel.innerHTML += ``; + }); + } + } catch (e) { + gSel.innerHTML = ''; + } + + // 2. Reseteamos el resto del formulario + document.getElementById('convOperator').innerHTML = ''; + document.getElementById('convDateContainer').classList.add('hidden'); + document.getElementById('convDate').value = ""; + document.getElementById('convTime').value = ""; + document.getElementById('convertModal').classList.remove('hidden'); } + async function loadOpsForGuildConv(guildId) { + const sel = document.getElementById('convOperator'); + const dateCont = document.getElementById('convDateContainer'); + + sel.innerHTML = ''; + // Ocultamos la fecha si cambiamos de gremio (vuelve a Auto por defecto) + dateCont.classList.add('hidden'); + document.getElementById('convDate').value = ""; + document.getElementById('convTime').value = ""; + + if(!guildId) return; + + try { + const res = await fetch(`${API_URL}/operators?guild_id=${guildId}`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); + const data = await res.json(); + if(data.ok && data.operators) { + data.operators.forEach(op => { + sel.innerHTML += ``; + }); + } + } catch(e) {} + } + + function handleOperatorChange(val) { + const dateCont = document.getElementById('convDateContainer'); + if(val === 'AUTO') { + dateCont.classList.add('hidden'); + } else { + // Si elige un técnico concreto, mostramos los campos de fecha + dateCont.classList.remove('hidden'); + } + } + async function confirmConversion() { const id = document.getElementById('convBudgetId').value; + const guild_id = document.getElementById('convGuild').value; + const assigned_to = document.getElementById('convOperator').value; + const date = document.getElementById('convDate').value; const time = document.getElementById('convTime').value; - if(!date || !time) return showToast("Debes elegir fecha y hora."); + + if (!guild_id) return showToast("⚠️ El Gremio es obligatorio."); + + const use_automation = (assigned_to === 'AUTO'); - await fetch(`${API_URL}/budgets/${id}/convert`, { method: 'POST', headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify({date, time}) }); - document.getElementById('convertModal').classList.add('hidden'); - showToast("¡Convertido a Servicio con éxito!"); - loadBudgets(); + if (!use_automation && (!date || !time)) { + return showToast("⚠️ Si asignas a un técnico, debes fijar Fecha y Hora."); + } + + const payload = { + guild_id: guild_id, + assigned_to: use_automation ? null : assigned_to, + use_automation: use_automation, + date: use_automation ? null : date, + time: use_automation ? null : time + }; + + try { + await fetch(`${API_URL}/budgets/${id}/convert`, { + method: 'POST', + headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, + body: JSON.stringify(payload) + }); + + document.getElementById('convertModal').classList.add('hidden'); + showToast(use_automation ? "✅ ¡Enviado a la bolsa con éxito!" : "✅ ¡Servicio agendado con éxito!"); + loadBudgets(); + } catch(e) { + showToast("❌ Error al convertir el presupuesto."); + } } function openBudgetModal() {