feat(cobranza): texto editable y precargado por bloque del cuerpo

En la plantilla del mensaje, cada bloque del cuerpo (cliente, vehículo, monto,
fecha límite, link) muestra al activarse un campo de texto a la derecha,
precargado con su valor por defecto y editable. Acepta variables
({cliente},{vehiculo},{monto},{fecha},{concepto},{mes}).

- Frontend: filas casilla+campo (se muestra solo si la casilla está activa),
  rellenado/guardado de los 5 texto_*; armarMensajeCobro y la vista previa
  usan los textos; se agrega la variable {fecha}.
- Backend: PlantillaBody y DEFAULT_PLANTILLA con texto_cliente/vehiculo/monto/
  fecha/link. Merge por parche retrocompatible.
22 tests OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jorem JuárezandClaude Opus 4.8 committed 2026-06-24 14:08:48 -06:00
1 parent fba9785fd3
commit dc9af65ae4
3 files changed
+78 -13

No files matched your search

+65 -13
View File
@@ -146,6 +146,10 @@
.check-row { display: flex; align-items: center; gap: 11px; padding: 9px 2px; font-size: 13px; color: var(--text-primary); cursor: pointer; }
.check-row input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); cursor: pointer; }
.bloque-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.bloque-row .check-row { flex: 0 0 auto; min-width: 175px; }
.bloque-row .bloque-txt { flex: 1; min-width: 160px; padding: 8px 11px; font-size: 12.5px; }
.bloque-row .bloque-txt[hidden] { display: none; }
.preview-msg { background: var(--surface-2); border: 1px solid var(--line); border-radius: 13px; padding: 1rem 1.1rem; font-size: 12.5px; color: var(--text-primary); white-space: pre-wrap; line-height: 1.6; font-family: 'DM Sans', sans-serif; }
@@ -405,11 +409,27 @@
<div class="form-group">
<label class="form-label">Bloques del cuerpo</label>
<label class="check-row"><input type="checkbox" id="pl-cliente" onchange="renderVistaPreviaMsg()"> Incluir cliente</label>
<label class="check-row"><input type="checkbox" id="pl-vehiculo" onchange="renderVistaPreviaMsg()"> Incluir vehículo</label>
<label class="check-row"><input type="checkbox" id="pl-monto" onchange="renderVistaPreviaMsg()"> Incluir monto</label>
<label class="check-row"><input type="checkbox" id="pl-fecha" onchange="renderVistaPreviaMsg()"> Incluir fecha límite (hoy + 5 días)</label>
<label class="check-row"><input type="checkbox" id="pl-link" onchange="renderVistaPreviaMsg()"> Incluir link de pago (Conekta)</label>
<div class="form-hint" style="margin-bottom:8px">Al activar cada bloque puedes editar su texto (acepta variables).</div>
<div class="bloque-row">
<label class="check-row"><input type="checkbox" id="pl-cliente" onchange="bloqueChanged()"> Incluir cliente</label>
<input type="text" class="form-input bloque-txt" id="pl-cliente-txt" oninput="renderVistaPreviaMsg()" placeholder="Cliente: {cliente}">
</div>
<div class="bloque-row">
<label class="check-row"><input type="checkbox" id="pl-vehiculo" onchange="bloqueChanged()"> Incluir vehículo</label>
<input type="text" class="form-input bloque-txt" id="pl-vehiculo-txt" oninput="renderVistaPreviaMsg()" placeholder="Vehículo: {vehiculo}">
</div>
<div class="bloque-row">
<label class="check-row"><input type="checkbox" id="pl-monto" onchange="bloqueChanged()"> Incluir monto</label>
<input type="text" class="form-input bloque-txt" id="pl-monto-txt" oninput="renderVistaPreviaMsg()" placeholder="Monto: {monto}">
</div>
<div class="bloque-row">
<label class="check-row"><input type="checkbox" id="pl-fecha" onchange="bloqueChanged()"> Incluir fecha límite (hoy + 5 días)</label>
<input type="text" class="form-input bloque-txt" id="pl-fecha-txt" oninput="renderVistaPreviaMsg()" placeholder="Fecha límite: {fecha}">
</div>
<div class="bloque-row">
<label class="check-row"><input type="checkbox" id="pl-link" onchange="bloqueChanged()"> Incluir link de pago (Conekta)</label>
<input type="text" class="form-input bloque-txt" id="pl-link-txt" oninput="renderVistaPreviaMsg()" placeholder="Puede pagar desde este link:">
</div>
</div>
<button class="btn-save" onclick="guardarPlantilla()">Guardar plantilla</button>
@@ -1685,6 +1705,24 @@ async function eliminarCuenta(id){
} catch(e){ toast("No se pudo eliminar: " + e.message); }
}
// Bloques del cuerpo: casilla + texto editable (precargado) + clave de plantilla.
const BLOQUES = [
{chk:"pl-cliente", txt:"pl-cliente-txt", key:"texto_cliente", def:"Cliente: {cliente}"},
{chk:"pl-vehiculo", txt:"pl-vehiculo-txt", key:"texto_vehiculo", def:"Vehículo: {vehiculo}"},
{chk:"pl-monto", txt:"pl-monto-txt", key:"texto_monto", def:"Monto: {monto}"},
{chk:"pl-fecha", txt:"pl-fecha-txt", key:"texto_fecha", def:"Fecha límite: {fecha}"},
{chk:"pl-link", txt:"pl-link-txt", key:"texto_link", def:"Puede pagar con tarjeta, SPEI o efectivo en Oxxo desde este link:"},
];
// Muestra el campo de texto solo cuando su casilla está activa.
function sincBloquesUI(){
BLOQUES.forEach(b => {
const chk = document.getElementById(b.chk), txt = document.getElementById(b.txt);
if(chk && txt) txt.hidden = !chk.checked;
});
}
function bloqueChanged(){ sincBloquesUI(); renderVistaPreviaMsg(); }
function rellenarPlantillaInputs(){
const p = (configGlobal && configGlobal.plantilla) ? configGlobal.plantilla : {};
document.getElementById("pl-saludo").value = p.saludo || "";
@@ -1694,6 +1732,12 @@ function rellenarPlantillaInputs(){
document.getElementById("pl-monto").checked = p.incluir_monto !== false;
document.getElementById("pl-fecha").checked = !!p.incluir_fecha_limite;
document.getElementById("pl-link").checked = p.incluir_link !== false;
// Texto de cada bloque: usa el guardado o el default precargado.
BLOQUES.forEach(b => {
const e = document.getElementById(b.txt);
if(e) e.value = (p[b.key] != null ? p[b.key] : b.def);
});
sincBloquesUI();
}
async function guardarPlantilla(){
@@ -1706,6 +1750,7 @@ async function guardarPlantilla(){
incluir_fecha_limite: document.getElementById("pl-fecha").checked,
incluir_link: document.getElementById("pl-link").checked,
};
BLOQUES.forEach(b => { body[b.key] = document.getElementById(b.txt).value; });
try {
const pl = await api("/config/plantilla", {method:"POST", body: JSON.stringify(body)});
if(configGlobal) configGlobal.plantilla = pl;
@@ -1748,12 +1793,22 @@ function armarMensajeCobro(r, monto, concepto, link){
const pl = cfg.plantilla || {};
const cuentas = (cfg.cuentas || []).filter(c => c.incluir_en_mensaje);
const d = new Date();
const fLim = new Date(); fLim.setDate(fLim.getDate() + 5);
const vars = {
cliente: r.cliente || "",
vehiculo: r.vehiculo || "",
monto: fmt(monto),
concepto: concepto || "",
mes: _MESES_CORTO[d.getMonth()] + " " + d.getFullYear(),
fecha: fLim.toLocaleDateString("es-MX",{day:"2-digit",month:"2-digit",year:"numeric"}),
};
// Texto editable por bloque (cae al default si la plantilla no lo trae).
const T = {
cliente: pl.texto_cliente != null ? pl.texto_cliente : "Cliente: {cliente}",
vehiculo: pl.texto_vehiculo != null ? pl.texto_vehiculo : "Vehículo: {vehiculo}",
monto: pl.texto_monto != null ? pl.texto_monto : "Monto: {monto}",
fecha: pl.texto_fecha != null ? pl.texto_fecha : "Fecha límite: {fecha}",
link: pl.texto_link != null ? pl.texto_link : "Puede pagar con tarjeta, SPEI o efectivo en Oxxo desde este link:",
};
const incluirLink = (pl.incluir_link !== false) && !!link;
@@ -1761,13 +1816,10 @@ function armarMensajeCobro(r, monto, concepto, link){
if(pl.saludo) out += _aplicarVariables(pl.saludo, vars) + "\n\n";
out += "Le compartimos los datos para realizar su pago:\n\n";
out += `Concepto: ${concepto}\n`;
if(pl.incluir_cliente !== false && r.cliente) out += `Cliente: ${r.cliente}\n`;
if(pl.incluir_vehiculo !== false && r.vehiculo) out += `Vehículo: ${r.vehiculo}\n`;
if(pl.incluir_monto !== false) out += `Monto: ${fmt(monto)}\n`;
if(pl.incluir_fecha_limite){
const f = new Date(); f.setDate(f.getDate() + 5);
out += `Fecha límite: ${f.toLocaleDateString("es-MX",{day:"2-digit",month:"2-digit",year:"numeric"})}\n`;
}
if(pl.incluir_cliente !== false && r.cliente) out += _aplicarVariables(T.cliente, vars) + "\n";
if(pl.incluir_vehiculo !== false && r.vehiculo) out += _aplicarVariables(T.vehiculo, vars) + "\n";
if(pl.incluir_monto !== false) out += _aplicarVariables(T.monto, vars) + "\n";
if(pl.incluir_fecha_limite) out += _aplicarVariables(T.fecha, vars) + "\n";
out += "\n";
if(cuentas.length){
cuentas.forEach(c => {
@@ -1779,7 +1831,7 @@ function armarMensajeCobro(r, monto, concepto, link){
});
}
if(incluirLink){
out += `Puede pagar con tarjeta, SPEI o efectivo en Oxxo desde este link:\n${link}\n\n`;
out += _aplicarVariables(T.link, vars) + `\n${link}\n\n`;
}
if(!cuentas.length && !incluirLink){
out += "⚠️ No hay cuentas activadas ni link configurado.\n\n";