feat(domicilios): enfermero puede editar domicilios asignados desde el portal

- save_domicilio.php: permite editar si el enfermero está asignado (no solo si lo creó)
- enfermero_portal.php: botón Editar en domcard, método abrirEditar() pre-carga el modal existente

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-30 20:44:38 -05:00
co-authored by Claude Sonnet 4.6
parent 1efdc7bbad
commit 79a5438531
2 changed files with 73 additions and 7 deletions
+10 -4
View File
@@ -55,12 +55,18 @@ try {
if (!empty($datos['id'])) {
// Actualización general
$id = (int)$datos['id'];
// Enfermero solo puede editar domicilios que él mismo creó
// Enfermero puede editar domicilios que creó o que tiene asignados
if (userRole() === 'enfermero') {
$dbCheck = Database::getInstance();
$chk = $dbCheck->fetchOne("SELECT creado_por FROM lab_domicilios WHERE id = ?", [$id]);
if (!$chk || (int)($chk['creado_por'] ?? 0) !== adminId()) {
jsonError('Solo puedes editar domicilios que tú mismo agendaste.');
$chk = $dbCheck->fetchOne(
"SELECT d.creado_por,
(SELECT COUNT(*) FROM lab_asignaciones a
WHERE a.domicilio_id = d.id AND a.enfermera_id = ?) AS asignado
FROM lab_domicilios d WHERE d.id = ?",
[enfermeraId(), $id]
);
if (!$chk || ((int)($chk['creado_por'] ?? 0) !== adminId() && !(int)$chk['asignado'])) {
jsonError('Solo puedes editar domicilios que agendaste o que tienes asignados.');
}
}
unset($datos['id'], $datos['solo_estado']);
+63 -3
View File
@@ -783,6 +783,11 @@ const portal = {
onclick="formVer.abrir(${item.paciente_id}, '${esc(item.paciente_nombre||'')}')">
<i class="fas fa-folder-open me-1"></i>Ver llenados
</button>` : ''}
${!['completado','cancelado'].includes(est) ? `
<button class="btn btn-accion" style="background:#0d6efd;color:#fff"
onclick="agendaNueva.abrirEditar(${item.domicilio_id})">
<i class="fas fa-pen me-1"></i>Editar
</button>` : ''}
</div>
</div>
</div>`;
@@ -1452,6 +1457,7 @@ const agendaNueva = {
_modal: null,
_buscarTimer: null,
_nuevoPac: false,
_editId: null,
init() {
this._modal = new bootstrap.Modal('#modalNuevaAgenda');
@@ -1500,12 +1506,63 @@ const agendaNueva = {
document.getElementById('na-np-email').value = '';
document.getElementById('na-crear-toggle-txt').textContent = 'Crear nuevo paciente';
document.getElementById('na-paciente-buscar').disabled = false;
this._editId = null;
const btn = document.getElementById('na-btn-guardar');
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-calendar-check me-1"></i>Agendar';
document.querySelector('#modalNuevaAgenda .modal-title').textContent = 'Nuevo domicilio';
this._modal.show();
},
async abrirEditar(domId) {
try {
const r = await fetch(`api/lab/get_domicilios.php?id=${domId}`);
const d = await r.json();
const dom = d.domicilio || d.data?.[0] || null;
if (!dom) { alert('No se pudo cargar el domicilio.'); return; }
this.abrir(); // resetea el form
this._editId = domId;
// Paciente
if (dom.paciente_id) {
document.getElementById('na-paciente-id').value = dom.paciente_id;
document.getElementById('na-paciente-buscar').value = dom.paciente_nombre || '';
document.getElementById('na-paciente-label').textContent = dom.paciente_nombre || '';
document.getElementById('na-paciente-elegido').classList.remove('d-none');
document.getElementById('na-sugerencias').classList.add('d-none');
}
// Campos
document.getElementById('na-direccion').value = dom.direccion || '';
document.getElementById('na-barrio').value = dom.barrio || '';
document.getElementById('na-ciudad').value = dom.ciudad || '';
document.getElementById('na-indicaciones').value = dom.indicaciones_dir || '';
document.getElementById('na-fecha').value = dom.fecha_programada || '';
document.getElementById('na-hora').value = (dom.hora_programada || '').slice(0,5);
document.getElementById('na-notas').value = dom.notas_admin || '';
document.getElementById('na-valor-dom').value = dom.valor_domicilio || '';
document.getElementById('na-valor-cop').value = dom.valor_copago || '';
// Tipo cliente
const tc = dom.tipo_cliente === 'seguro' ? 'na-tc-seguro' : 'na-tc-particular';
const tcEl = document.getElementById(tc);
if (tcEl) { tcEl.checked = true; this._toggleSeguro(); }
if (dom.seguro_nombre) {
const sn = document.getElementById('na-seguro-nombre');
if (sn) sn.value = dom.seguro_nombre;
}
// Botón y título
const btn = document.getElementById('na-btn-guardar');
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-save me-1"></i>Guardar cambios';
document.querySelector('#modalNuevaAgenda .modal-title').textContent = 'Editar domicilio';
} catch(e) {
alert('Error al cargar domicilio: ' + e.message);
}
},
async _buscarPacientes(q) {
if (q.length < 2) {
document.getElementById('na-sugerencias').classList.add('d-none');
@@ -1783,8 +1840,8 @@ const agendaNueva = {
valor_domicilio: parseFloat(document.getElementById('na-valor-dom').value) || null,
valor_copago: parseFloat(document.getElementById('na-valor-cop').value) || null,
notas_admin: document.getElementById('na-notas').value.trim() || null,
estado: 'programado',
...(ENFERMERA_ID ? { enfermera_id: ENFERMERA_ID } : {}),
...(this._editId ? { id: this._editId } : { estado: 'programado' }),
...(ENFERMERA_ID && !this._editId ? { enfermera_id: ENFERMERA_ID } : {}),
};
try {
@@ -1814,10 +1871,13 @@ const agendaNueva = {
}
this._modal.hide();
this._editId = null;
portal.cargar();
} catch (e) {
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-calendar-check me-1"></i>Agendar';
btn.innerHTML = this._editId
? '<i class="fas fa-save me-1"></i>Guardar cambios'
: '<i class="fas fa-calendar-check me-1"></i>Agendar';
this._mostrarError(e.message);
}
},