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() {