feat(cobranza): selector de cliente buscable y seccionado en cobro personalizado
Reemplaza el <select> nativo por un combobox propio (input + lista inline): - Búsqueda en vivo por nombre (insensible a acentos/mayúsculas). - Orden: "Agregar contacto nuevo" arriba, luego "Personalizados" (contactos manuales) y al final "Directorio (Sheet)". - Selección por mousedown (registra antes del blur que cierra la lista); reenfocar muestra toda la lista y selecciona el texto para reescribir. Se elimina contactosDirectorio() (sustituida por rmCliListas) y onClienteSelChange. Solo frontend. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b28ed0d101
commit
0d33390570
1 file changed
+80
-32
+80
-32
@@ -241,6 +241,15 @@
|
||||
.modal-close { width: 32px; height: 32px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--line); cursor: pointer; color: var(--text-secondary); font-size: 18px; line-height: 1; transition: all 0.15s ease; }
|
||||
.modal-close:hover { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
|
||||
|
||||
/* Combobox buscable de cliente (alta de cobro personalizado) */
|
||||
.cli-list { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); margin-top: 8px; max-height: 260px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
|
||||
.cli-head { font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-tertiary); font-weight: 700; padding: 9px 12px 4px; position: sticky; top: 0; background: var(--surface); }
|
||||
.cli-item { padding: 9px 12px; font-size: 13.5px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; align-items: center; }
|
||||
.cli-item:hover { background: var(--renta-soft); }
|
||||
.cli-item .tel { color: var(--text-tertiary); font-size: 12px; white-space: nowrap; }
|
||||
.cli-item.nuevo { color: var(--accent); font-weight: 600; }
|
||||
.cli-empty { padding: 12px; text-align: center; color: var(--text-tertiary); font-size: 12.5px; }
|
||||
|
||||
.opt-card { background: var(--surface); border-radius: 13px; box-shadow: none; border: 1.5px solid var(--line); padding: 1rem 1.15rem; margin-bottom: 11px; cursor: pointer; transition: all 0.17s ease; }
|
||||
.opt-card:hover { border-color: var(--accent); background: var(--accent-soft); }
|
||||
.opt-card:active { transform: scale(0.99); }
|
||||
@@ -1207,35 +1216,21 @@ function normNombre(s){
|
||||
return (s||"").toUpperCase().replace(/\./g,"").replace(/\s+/g," ").trim();
|
||||
}
|
||||
|
||||
// Contactos del directorio deduplicados por nombre_norm (prefiere el que
|
||||
// tenga teléfono), ordenados alfabéticamente. Sirve al selector de cliente.
|
||||
function contactosDirectorio(){
|
||||
const map = new Map();
|
||||
(directorio||[]).forEach(c => {
|
||||
const k = c.nombre_norm || normNombre(c.nombre);
|
||||
const prev = map.get(k);
|
||||
if(!prev || (!prev.telefono && c.telefono)) {
|
||||
map.set(k, {nombre_norm: k, nombre: c.nombre, telefono: c.telefono||""});
|
||||
}
|
||||
});
|
||||
return [...map.values()].sort((a,b)=>a.nombre.localeCompare(b.nombre,'es'));
|
||||
}
|
||||
let rmCliSel = null; // contacto elegido: {nombre,nombre_norm,telefono} | "__nuevo__" | null
|
||||
let rmCliShown = []; // items actualmente listados (para resolver por índice)
|
||||
|
||||
function abrirCrearRenglon(){
|
||||
document.getElementById("modal-inner").classList.remove("lote");
|
||||
const hoyISO = new Date().toISOString().slice(0,10);
|
||||
const opciones = contactosDirectorio().map(c =>
|
||||
`<option value="${escapeAttr(c.nombre_norm)}">${c.nombre}${c.telefono ? " · "+fmtTel(c.telefono) : ""}</option>`
|
||||
).join("");
|
||||
rmCliSel = null; rmCliShown = [];
|
||||
document.getElementById("modal-inner").innerHTML = `
|
||||
<div class="modal-header"><span class="modal-title">Nuevo cobro personalizado</span><button class="modal-close" onclick="cerrar()">×</button></div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Cliente o razón social</label>
|
||||
<select id="rm-cliente-sel" class="form-input" onchange="onClienteSelChange()">
|
||||
<option value="">— Selecciona un cliente —</option>
|
||||
${opciones}
|
||||
<option value="__nuevo__">➕ Agregar contacto nuevo…</option>
|
||||
</select>
|
||||
<input type="text" class="form-input" id="rm-cli-search" autocomplete="off" placeholder="Buscar o seleccionar cliente…"
|
||||
oninput="rmCliRender(this.value)" onfocus="this.select(); rmCliRender('')"
|
||||
onblur="setTimeout(()=>{var l=document.getElementById('rm-cli-list'); if(l) l.style.display='none';},160)">
|
||||
<div class="cli-list" id="rm-cli-list" style="display:none"></div>
|
||||
</div>
|
||||
<div id="rm-nuevo-contacto" style="display:none">
|
||||
<div class="form-group">
|
||||
@@ -1273,22 +1268,75 @@ function abrirCrearRenglon(){
|
||||
</div>
|
||||
<button class="btn-save" onclick="crearRenglonSubmit()">Crear cobro</button>`;
|
||||
document.getElementById("modal").classList.add("open");
|
||||
setTimeout(() => document.getElementById("rm-cliente-sel").focus(), 100);
|
||||
setTimeout(() => document.getElementById("rm-cli-search").focus(), 100);
|
||||
}
|
||||
|
||||
// Muestra los campos de "nuevo contacto" solo cuando se elige esa opción.
|
||||
function onClienteSelChange(){
|
||||
const sel = document.getElementById("rm-cliente-sel");
|
||||
const blk = document.getElementById("rm-nuevo-contacto");
|
||||
if(!sel || !blk) return;
|
||||
const esNuevo = sel.value === "__nuevo__";
|
||||
blk.style.display = esNuevo ? "block" : "none";
|
||||
if(esNuevo) setTimeout(() => document.getElementById("rm-new-nombre").focus(), 50);
|
||||
// Normaliza para búsqueda: minúsculas + sin acentos.
|
||||
function _busqStr(s){ return (s||"").toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g,""); }
|
||||
|
||||
// Listas seccionadas del directorio: personalizados (manuales) y Sheet,
|
||||
// deduplicadas por nombre_norm. Un nombre que exista como manual NO se repite
|
||||
// en la sección del Sheet.
|
||||
function rmCliListas(){
|
||||
const dedupe = (arr) => {
|
||||
const m = new Map();
|
||||
arr.forEach(c => {
|
||||
const k = c.nombre_norm || normNombre(c.nombre);
|
||||
const prev = m.get(k);
|
||||
if(!prev || (!prev.telefono && c.telefono)) m.set(k, {nombre_norm:k, nombre:c.nombre, telefono:c.telefono||""});
|
||||
});
|
||||
return [...m.values()].sort((a,b)=>a.nombre.localeCompare(b.nombre,'es'));
|
||||
};
|
||||
const manuales = dedupe((directorio||[]).filter(c => c.origen === "manual"));
|
||||
const claveManual = new Set(manuales.map(c => c.nombre_norm));
|
||||
const sheet = dedupe((directorio||[]).filter(c => c.origen !== "manual"))
|
||||
.filter(c => !claveManual.has(c.nombre_norm));
|
||||
return {manuales, sheet};
|
||||
}
|
||||
|
||||
// Render del desplegable buscable. Orden: Agregar nuevo → Personalizados → Sheet.
|
||||
function rmCliRender(filtro){
|
||||
const list = document.getElementById("rm-cli-list");
|
||||
if(!list) return;
|
||||
list.style.display = "block";
|
||||
const q = _busqStr(filtro);
|
||||
const {manuales, sheet} = rmCliListas();
|
||||
const fil = (arr) => arr.filter(c => !q || _busqStr(c.nombre).includes(q));
|
||||
const fm = fil(manuales), fs = fil(sheet);
|
||||
rmCliShown = [];
|
||||
const item = (c) => {
|
||||
const i = rmCliShown.push(c) - 1;
|
||||
return `<div class="cli-item" onmousedown="rmCliPickIdx(${i})"><span>${c.nombre}</span>${c.telefono?`<span class="tel">+521 ${fmtTel(c.telefono)}</span>`:""}</div>`;
|
||||
};
|
||||
let html = `<div class="cli-item nuevo" onmousedown="rmCliPickNuevo()"><span>➕ Agregar contacto nuevo…</span></div>`;
|
||||
if(fm.length){ html += `<div class="cli-head">Personalizados</div>` + fm.map(item).join(""); }
|
||||
if(fs.length){ html += `<div class="cli-head">Directorio (Sheet)</div>` + fs.map(item).join(""); }
|
||||
if(!fm.length && !fs.length){ html += `<div class="cli-empty">Sin coincidencias. Usa “Agregar contacto nuevo”.</div>`; }
|
||||
list.innerHTML = html;
|
||||
}
|
||||
|
||||
function rmCliPickIdx(i){
|
||||
const c = rmCliShown[i];
|
||||
if(!c) return;
|
||||
rmCliSel = c;
|
||||
const inp = document.getElementById("rm-cli-search");
|
||||
if(inp) inp.value = c.nombre + (c.telefono ? " · +521 "+fmtTel(c.telefono) : "");
|
||||
const list = document.getElementById("rm-cli-list"); if(list) list.style.display = "none";
|
||||
const blk = document.getElementById("rm-nuevo-contacto"); if(blk) blk.style.display = "none";
|
||||
}
|
||||
|
||||
function rmCliPickNuevo(){
|
||||
rmCliSel = "__nuevo__";
|
||||
const inp = document.getElementById("rm-cli-search");
|
||||
if(inp) inp.value = "➕ Contacto nuevo";
|
||||
const list = document.getElementById("rm-cli-list"); if(list) list.style.display = "none";
|
||||
const blk = document.getElementById("rm-nuevo-contacto"); if(blk) blk.style.display = "block";
|
||||
setTimeout(() => { const e = document.getElementById("rm-new-nombre"); if(e) e.focus(); }, 50);
|
||||
}
|
||||
|
||||
async function crearRenglonSubmit(){
|
||||
// El alta manual siempre crea un cobro personalizado (tipo 'otro').
|
||||
const sel = document.getElementById("rm-cliente-sel").value;
|
||||
const sel = (rmCliSel === "__nuevo__") ? "__nuevo__" : (rmCliSel ? rmCliSel.nombre_norm : "");
|
||||
const vehiculo = (document.getElementById("rm-vehiculo").value || "").trim();
|
||||
const etiqueta = (document.getElementById("rm-etiqueta").value || "").trim();
|
||||
const monto = parseFloat(document.getElementById("rm-monto").value);
|
||||
@@ -1307,7 +1355,7 @@ async function crearRenglonSubmit(){
|
||||
if(!nuevoNombre){ toast("Falta el nombre del nuevo contacto"); return; }
|
||||
if(nuevoTel.length !== 10){ toast("El teléfono debe tener 10 dígitos"); return; }
|
||||
} else {
|
||||
contacto = contactosDirectorio().find(x => x.nombre_norm === sel);
|
||||
contacto = (rmCliSel && typeof rmCliSel === "object") ? rmCliSel : null;
|
||||
if(!contacto){ toast("Cliente no válido"); return; }
|
||||
}
|
||||
if(!vehiculo){ toast("Falta vehículo"); return; }
|
||||
|
||||
Reference in new issue
Block a user