refactor(formularios): split 'calculado' into two direct field types

Instead of one field with a sub-option, there are now two independent palette
items under a 'Calculados' section:
- fecha_hoy: renders type=date pre-filled with today's date (YYYY-MM-DD)
- edad: renders type=number pre-filled with age calculated from paciente.fecha_nacimiento

Both are editable by the patient and have a yellow tint.
Preview in the builder also shows the live date value for fecha_hoy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-03 11:44:11 -05:00
co-authored by Claude Sonnet 4.6
parent f8245b4d6f
commit e1f0fe3450
2 changed files with 63 additions and 60 deletions
+23 -22
View File
@@ -555,33 +555,34 @@ function renderCampos(esquema, prefilled) {
<label class="form-check-label">${esc(o)}</label>
</div>`).join('');
html += wrapOpen + `<div class="mb-3">${lbl}${opts}${linkedNote}</div>` + wrapClose;
} else if (c.tipo === 'calculado') {
// Pre-calcular valor según sub-tipo
} else if (c.tipo === 'fecha_hoy') {
let calcVal = pad[c.id] || '';
if (!calcVal) {
if ((c.calc_tipo || 'fecha_hoy') === 'fecha_hoy') {
const now = new Date();
calcVal = now.getFullYear() + '-'
+ String(now.getMonth()+1).padStart(2,'0') + '-'
+ String(now.getDate()).padStart(2,'0');
} else if (c.calc_tipo === 'edad') {
const fnac = pad['__paciente']?.fecha_nacimiento || pad.fecha_nacimiento || '';
if (fnac) {
const hoy = new Date();
const bday = new Date(fnac);
let edad = hoy.getFullYear() - bday.getFullYear();
const m = hoy.getMonth() - bday.getMonth();
if (m < 0 || (m === 0 && hoy.getDate() < bday.getDate())) edad--;
calcVal = edad >= 0 ? String(edad) : '';
}
const now = new Date();
calcVal = now.getFullYear() + '-'
+ String(now.getMonth()+1).padStart(2,'0') + '-'
+ String(now.getDate()).padStart(2,'0');
}
html += wrapOpen + `<div class="mb-3">${lbl}
<input type="date" class="form-control" style="background:#fff9e6;border-color:#ffc107"
name="${c.id}" value="${esc(calcVal)}" ${req}>
</div>` + wrapClose;
} else if (c.tipo === 'edad') {
let calcVal = pad[c.id] || '';
if (!calcVal) {
const fnac = pad['__paciente']?.fecha_nacimiento || pad.fecha_nacimiento || '';
if (fnac) {
const hoy = new Date();
const bday = new Date(fnac);
let edad = hoy.getFullYear() - bday.getFullYear();
const m = hoy.getMonth() - bday.getMonth();
if (m < 0 || (m === 0 && hoy.getDate() < bday.getDate())) edad--;
calcVal = edad >= 0 ? String(edad) : '';
}
}
const inputType = c.calc_tipo === 'edad' ? 'number' : 'date';
html += wrapOpen + `<div class="mb-3">${lbl}
<input type="${inputType}" class="form-control" style="background:#fff9e6;border-color:#ffc107"
name="${c.id}" value="${esc(calcVal)}" ${req}
placeholder="${c.calc_tipo === 'edad' ? 'Edad en años' : 'Fecha'}">
<small class="text-warning-emphasis"><i class="fas fa-calculator me-1"></i>Prellenado automáticamente — puedes modificarlo</small>
<input type="number" min="0" max="120" class="form-control" style="background:#fff9e6;border-color:#ffc107"
name="${c.id}" value="${esc(calcVal)}" ${req} placeholder="Edad en años">
</div>` + wrapClose;
} else {
const t = c.tipo === 'numero' ? 'number'