From ebc2fe19ca24a4a93194929bad4150100aeb1de7 Mon Sep 17 00:00:00 2001 From: marsalva Date: Wed, 25 Mar 2026 08:49:33 +0000 Subject: [PATCH] Actualizar presupuestos.html --- presupuestos.html | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/presupuestos.html b/presupuestos.html index 1742ffa..9998220 100644 --- a/presupuestos.html +++ b/presupuestos.html @@ -64,8 +64,12 @@

Cliente

+
+ + +
+ -
@@ -511,6 +515,39 @@ async function downloadPDF(id) { lucide.createIcons(); } + // ------------------ BUSCADOR DE CLIENTES AUTOMÁTICO ------------------ + async function searchClientByPhone() { + const phoneInput = document.getElementById('c_phone'); + const phone = phoneInput.value.trim(); + const loading = document.getElementById('phoneLoading'); + + // Si el teléfono tiene menos de 9 dígitos, no hacemos nada + if (!phone || phone.length < 9) return; + + loading.classList.remove('hidden'); // Mostramos el iconito de pensar + + try { + const res = await fetch(`${API_URL}/clients/search?phone=${encodeURIComponent(phone)}`, { + headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } + }); + const data = await res.json(); + + if (data.ok && data.client) { + // Si encuentra al cliente, rellenamos los datos + document.getElementById('c_name').value = data.client.full_name || ''; + if (data.client.addresses && data.client.addresses.length > 0) { + // Cogemos la primera dirección que tenga guardada + document.getElementById('c_address').value = data.client.addresses[0] || ''; + } + showToast("✅ Cliente encontrado y cargado"); + } + } catch (e) { + console.error("Error buscando cliente", e); + } finally { + loading.classList.add('hidden'); // Ocultamos el iconito + } + } + // ------------------ CREACIÓN DE PRESUPUESTO (ITEMS) ------------------ function addItem() { const conceptInput = document.getElementById('item_concept');