From d87c83cea508baa70b75e3c0d2b7604a475532b9 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 2 Aug 2026 13:37:36 +0000 Subject: [PATCH] Actualizar contabilidad.html --- contabilidad.html | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/contabilidad.html b/contabilidad.html index bdf7127..a8eb9f0 100644 --- a/contabilidad.html +++ b/contabilidad.html @@ -866,16 +866,28 @@ const editId = document.getElementById('editBudgetId').value; - if (editId) { - await fetch(`${API_URL}/budgets/${editId}`, { method: 'PUT', headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify(payload) }); - showToast("Presupuesto actualizado"); - } else { - await fetch(`${API_URL}/budgets`, { method: 'POST', headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify(payload) }); - showToast("Presupuesto guardado"); + try { + let res; + if (editId) { + res = await fetch(`${API_URL}/budgets/${editId}`, { method: 'PUT', headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify(payload) }); + } else { + res = await fetch(`${API_URL}/budgets`, { method: 'POST', headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify(payload) }); + } + + const data = await res.json(); + + if (res.ok && data.ok !== false) { + document.getElementById('budgetModal').classList.add('hidden'); + showToast(editId ? "✅ Presupuesto actualizado" : "✅ Presupuesto guardado"); + loadBudgets(); + } else { + showToast("❌ Error: " + (data.error || "El servidor no lo actualizó")); + console.error("Respuesta del servidor:", data); + } + } catch (error) { + showToast("❌ Error de conexión al guardar."); + console.error(error); } - - document.getElementById('budgetModal').classList.add('hidden'); - loadBudgets(); } function openArticlesModal() {