From e1f0fe345084642bddb665744327f05d5a8fcda3 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Fri, 3 Jul 2026 11:44:07 -0500 Subject: [PATCH] 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 --- form_cliente.php | 45 +++++++++++----------- lab_formulario_builder.php | 78 +++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/form_cliente.php b/form_cliente.php index b04100e..1444e73 100644 --- a/form_cliente.php +++ b/form_cliente.php @@ -555,33 +555,34 @@ function renderCampos(esquema, prefilled) { `).join(''); html += wrapOpen + `
${lbl}${opts}${linkedNote}
` + 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 + `
${lbl} + +
` + 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 + `
${lbl} - - Prellenado automáticamente — puedes modificarlo +
` + wrapClose; } else { const t = c.tipo === 'numero' ? 'number' diff --git a/lab_formulario_builder.php b/lab_formulario_builder.php index 46ad85b..31d10bf 100644 --- a/lab_formulario_builder.php +++ b/lab_formulario_builder.php @@ -332,6 +332,8 @@ $formId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
Vinculados al paciente
+
Calculados
+
@@ -515,7 +517,10 @@ const TIPOS_CAMPO = [ { tipo:'parrafo', label:'Párrafo de texto', icon:'fa-paragraph' }, { tipo:'parrafo_inline', label:'Párrafo con campos', icon:'fa-align-left' }, { tipo:'lista_marcable', label:'Lista marcable', icon:'fa-list-ol' }, - { tipo:'calculado', label:'Campo calculado', icon:'fa-calculator' }, +]; +const TIPOS_CALC = [ + { tipo:'fecha_hoy', label:'Fecha actual', icon:'fa-calendar-day' }, + { tipo:'edad', label:'Edad del paciente', icon:'fa-user-clock' }, ]; const TIPOS_LINKED = [ { key:'nombre_completo', label:'Nombre completo', icon:'fa-user' }, @@ -574,20 +579,20 @@ function renderPalette() { $('palette-linked').innerHTML = TIPOS_LINKED.map(t => `
${t.label}
`).join(''); - // Click también agrega campo (además del drag) - document.querySelectorAll('.palette-item').forEach(el => { - el.addEventListener('click', () => { - const tipo = el.dataset.tipo || 'linked'; - const linked = el.dataset.linked || null; - agregarCampo({ tipo, linked }); - }); - }); + $('palette-calc').innerHTML = TIPOS_CALC.map(t => ` +
+ ${t.label} +
`).join(''); } // ════════════════════════════════════════════════════════════════════ @@ -635,6 +640,10 @@ function agregarCampo({ tipo, linked }) { campo = { id, tipo:'parrafo_inline', contenido:'Yo, {nombre_completo}, con N.º de identificación {numero_documento}, declaro que…' }; } else if (tipo === 'lista_marcable') { campo = { id, tipo:'lista_marcable', label:'Marque con X la prueba solicitada', items:['Opción 1','Opción 2'], required:false }; + } else if (tipo === 'fecha_hoy') { + campo = { id, tipo:'fecha_hoy', label:'Fecha', required:false }; + } else if (tipo === 'edad') { + campo = { id, tipo:'edad', label:'Edad', required:false }; } else { campo = { id, tipo, label:'Campo sin título', placeholder:'', required:false }; } @@ -651,7 +660,7 @@ const TIPO_ICON = { fecha:'fa-calendar', hora:'fa-clock', select:'fa-list', radio:'fa-dot-circle', checkbox:'fa-check-square', firma:'fa-signature', firma_profesional:'fa-user-md', separador:'fa-minus', linked:'fa-link', parrafo:'fa-paragraph', parrafo_inline:'fa-align-left', lista_marcable:'fa-list-ol', - calculado:'fa-calculator' + fecha_hoy:'fa-calendar-day', edad:'fa-user-clock' }; function renderCanvas() { @@ -916,14 +925,18 @@ function renderCampoPreview(c) { ).join(''); return `
${lbl}${items}
`; } - if (c.tipo === 'calculado') { - const subLabel = c.calc_tipo === 'edad' ? 'Edad (años) — calculada del paciente' - : 'Fecha de hoy — prellenada y editable'; - const t = c.calc_tipo === 'edad' ? 'number' : 'date'; - return `
- ${lbl} - - Campo calculado — editable por el paciente + if (c.tipo === 'fecha_hoy') { + const hoy = new Date(); + const val = hoy.getFullYear()+'-'+String(hoy.getMonth()+1).padStart(2,'0')+'-'+String(hoy.getDate()).padStart(2,'0'); + return `
${lbl} + + Fecha actual — editable +
`; + } + if (c.tipo === 'edad') { + return `
${lbl} + + Edad calculada del paciente — editable
`; } const t = c.tipo==='numero'?'number':c.tipo==='fecha'?'date':c.tipo==='hora'?'time':'text'; @@ -1039,21 +1052,13 @@ function abrirEditorCampo(idx) {
`; - } else if (c.tipo === 'calculado') { - const isFecha = (c.calc_tipo || 'fecha_hoy') === 'fecha_hoy'; - html += `
- -
-
- - -
-
- - -
-
-
El campo se pre-llena automáticamente pero el paciente puede editarlo.
+ } else if (c.tipo === 'fecha_hoy' || c.tipo === 'edad') { + html += `
+ + ${c.tipo === 'fecha_hoy' + ? 'Se pre-llenará con la fecha actual al abrir el formulario.' + : 'Se pre-llenará con la edad calculada a partir de la fecha de nacimiento del paciente.'} + El paciente puede editarlo.
@@ -1145,9 +1150,6 @@ function aplicarCampo() { const items = document.getElementById('ce-items'); if (items) c.items = items.value.split('\n').map(s=>s.trim()).filter(Boolean); - const calcTipo = document.querySelector('input[name="ce-calc-tipo"]:checked'); - if (calcTipo) c.calc_tipo = calcTipo.value; - const modoCanvas = document.getElementById('ce-modo-canvas'); const modoFoto = document.getElementById('ce-modo-foto'); if (modoCanvas !== null || modoFoto !== null) {