- Nuevo módulo lab_empresas: lista paginada, modal create/edit empresa, gestión de subgrupos inline y CRUD de catálogo lab_tarifas_id - APIs: empresas.php (list/get/save/toggle/delete), empresa_subgrupos.php, tarifas_id.php - Migración 09 registra el módulo en system_modules y asigna permiso admin Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
557 lines
28 KiB
PHP
557 lines
28 KiB
PHP
<?php
|
|
/**
|
|
* modules/lab_empresas/views/index.php
|
|
* CRUD de empresas/convenios y sus subgrupos.
|
|
*/
|
|
require_once APP_ROOT . '/config/config.php';
|
|
Layout::open('Empresas y Convenios', 'fas fa-building');
|
|
?>
|
|
<div class="container-fluid py-3">
|
|
|
|
<!-- Encabezado -->
|
|
<div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
|
|
<div>
|
|
<h4 class="mb-0"><i class="fas fa-building me-2 text-primary"></i>Empresas y Convenios</h4>
|
|
<small class="text-muted">EPS, IPS, convenios y particulares con tarifa especial</small>
|
|
</div>
|
|
<div class="d-flex gap-2">
|
|
<a href="<?= BASE_URL ?>/erp.php?m=lab_empresas&v=tarifas"
|
|
class="btn btn-outline-secondary btn-sm">
|
|
<i class="fas fa-tags me-1"></i>Tarifas
|
|
</a>
|
|
<button class="btn btn-primary btn-sm" onclick="abrirModal()">
|
|
<i class="fas fa-plus me-1"></i>Nueva empresa
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Filtros -->
|
|
<div class="card shadow-sm mb-3">
|
|
<div class="card-body py-2">
|
|
<div class="row g-2 align-items-end">
|
|
<div class="col-sm-5 col-md-4">
|
|
<input type="search" id="filtroSearch" class="form-control form-control-sm"
|
|
placeholder="Buscar por nombre, NIT…" oninput="debounceCargar()">
|
|
</div>
|
|
<div class="col-auto">
|
|
<select id="filtroActiva" class="form-select form-select-sm" onchange="cargarEmpresas()">
|
|
<option value="">Todas</option>
|
|
<option value="1" selected>Activas</option>
|
|
<option value="0">Inactivas</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-auto ms-auto">
|
|
<span class="text-muted small" id="lblTotal"></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Tabla -->
|
|
<div class="card shadow-sm">
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover table-sm mb-0" id="tablaEmpresas">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>NIT</th>
|
|
<th>Nombre</th>
|
|
<th>Tarifa</th>
|
|
<th class="text-end">Dcto%</th>
|
|
<th class="text-center">Subgrupos</th>
|
|
<th class="text-center">Autoriza</th>
|
|
<th class="text-center">Estado</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="tbodyEmpresas">
|
|
<tr><td colspan="8" class="text-center py-4 text-muted">Cargando…</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<!-- Paginación -->
|
|
<div class="card-footer d-flex justify-content-between align-items-center py-1">
|
|
<button class="btn btn-outline-secondary btn-sm" id="btnPrev" onclick="cambiarPagina(-1)">
|
|
<i class="fas fa-chevron-left"></i>
|
|
</button>
|
|
<span class="small text-muted" id="lblPagina"></span>
|
|
<button class="btn btn-outline-secondary btn-sm" id="btnNext" onclick="cambiarPagina(1)">
|
|
<i class="fas fa-chevron-right"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- ═══════════════════════════════════════════════════════════
|
|
Modal empresa (crear / editar)
|
|
═══════════════════════════════════════════════════════════ -->
|
|
<div class="modal fade" id="modalEmpresa" tabindex="-1">
|
|
<div class="modal-dialog modal-lg modal-dialog-scrollable">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="modalEmpresaTitulo">Nueva empresa</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form id="formEmpresa" novalidate>
|
|
<input type="hidden" id="fNitOrig" value="">
|
|
|
|
<!-- Fila 1: NIT + Nombre -->
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-sm-4">
|
|
<label class="form-label fw-semibold small">NIT <span class="text-danger">*</span></label>
|
|
<input type="text" id="fNit" class="form-control form-control-sm"
|
|
placeholder="900123456-7" maxlength="20" required>
|
|
</div>
|
|
<div class="col-sm-8">
|
|
<label class="form-label fw-semibold small">Nombre <span class="text-danger">*</span></label>
|
|
<input type="text" id="fNombre" class="form-control form-control-sm"
|
|
placeholder="Nombre de la empresa" maxlength="200" required>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Fila 2: Razón social -->
|
|
<div class="mb-3">
|
|
<label class="form-label fw-semibold small">Razón social</label>
|
|
<input type="text" id="fRazonSocial" class="form-control form-control-sm"
|
|
placeholder="Razón social completa" maxlength="200">
|
|
</div>
|
|
|
|
<!-- Fila 3: Tarifa + Descuento -->
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-sm-6">
|
|
<label class="form-label fw-semibold small">Tarifa</label>
|
|
<select id="fTarifaId" class="form-select form-select-sm">
|
|
<option value="">— Sin tarifa especial —</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-sm-3">
|
|
<label class="form-label fw-semibold small">Descuento %</label>
|
|
<input type="number" id="fDescuentoPct" class="form-control form-control-sm"
|
|
value="0" min="0" max="100" step="0.01">
|
|
</div>
|
|
<div class="col-sm-3">
|
|
<label class="form-label fw-semibold small">Cód. EPS</label>
|
|
<input type="text" id="fCodigoEps" class="form-control form-control-sm"
|
|
placeholder="EPS001" maxlength="20">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Fila 4: Tipo usuario -->
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-sm-4">
|
|
<label class="form-label fw-semibold small">Tipo usuario</label>
|
|
<select id="fTipoUsuario" class="form-select form-select-sm">
|
|
<option value="">—</option>
|
|
<option value="01">01 - Contributivo</option>
|
|
<option value="02">02 - Subsidiado</option>
|
|
<option value="03">03 - Vinculado</option>
|
|
<option value="04">04 - Particular</option>
|
|
<option value="05">05 - ARP</option>
|
|
<option value="06">06 - Póliza</option>
|
|
<option value="07">07 - Estudiante</option>
|
|
<option value="08">08 - Empleado</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-sm-4">
|
|
<label class="form-label fw-semibold small">Tipo usuario SISPRO</label>
|
|
<input type="text" id="fTipoUsuarioSispro" class="form-control form-control-sm"
|
|
placeholder="Código SISPRO" maxlength="10">
|
|
</div>
|
|
<div class="col-sm-4">
|
|
<label class="form-label fw-semibold small">Cod. tercero</label>
|
|
<input type="text" id="fCodTercero" class="form-control form-control-sm"
|
|
placeholder="Cód. tercero" maxlength="50">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Fila 5: Contrato / Centro costo -->
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-sm-6">
|
|
<label class="form-label fw-semibold small">Cód. contrato</label>
|
|
<input type="text" id="fCodContrato" class="form-control form-control-sm"
|
|
placeholder="Número de contrato" maxlength="50">
|
|
</div>
|
|
<div class="col-sm-6">
|
|
<label class="form-label fw-semibold small">Centro de costo</label>
|
|
<input type="text" id="fCentroCosto" class="form-control form-control-sm"
|
|
placeholder="Centro de costo" maxlength="50">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Fila 6: Checks -->
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-auto">
|
|
<div class="form-check form-switch mt-1">
|
|
<input class="form-check-input" type="checkbox" id="fReqAutoriza">
|
|
<label class="form-check-label small" for="fReqAutoriza">
|
|
Exige número de autorización
|
|
</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-auto">
|
|
<div class="form-check form-switch mt-1">
|
|
<input class="form-check-input" type="checkbox" id="fActiva" checked>
|
|
<label class="form-check-label small" for="fActiva">Activa</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Mensaje de error -->
|
|
<div id="formError" class="alert alert-danger d-none py-2 small"></div>
|
|
</form>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal">Cancelar</button>
|
|
<button type="button" class="btn btn-primary btn-sm" id="btnGuardarEmpresa" onclick="guardarEmpresa()">
|
|
<i class="fas fa-save me-1"></i>Guardar
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- ═══════════════════════════════════════════════════════════
|
|
Modal subgrupos
|
|
═══════════════════════════════════════════════════════════ -->
|
|
<div class="modal fade" id="modalSubgrupos" tabindex="-1">
|
|
<div class="modal-dialog modal-dialog-scrollable">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">
|
|
<i class="fas fa-layer-group me-2 text-secondary"></i>
|
|
Subgrupos — <span id="subNitNombre"></span>
|
|
</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div id="listaSubgrupos" class="mb-3"></div>
|
|
<hr>
|
|
<h6 class="small fw-semibold text-muted text-uppercase mb-2">Agregar / editar subgrupo</h6>
|
|
<input type="hidden" id="subId" value="">
|
|
<input type="hidden" id="subNitEmpresa" value="">
|
|
<div class="row g-2 mb-2">
|
|
<div class="col-12">
|
|
<input type="text" id="subNombre" class="form-control form-control-sm"
|
|
placeholder="Nombre del subgrupo" maxlength="100">
|
|
</div>
|
|
<div class="col-sm-6">
|
|
<select id="subTarifaId" class="form-select form-select-sm">
|
|
<option value="">— Tarifa de la empresa —</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-sm-6">
|
|
<input type="text" id="subRefSubgrupo" class="form-control form-control-sm"
|
|
placeholder="Ref. subgrupo" maxlength="50">
|
|
</div>
|
|
<div class="col-12">
|
|
<input type="text" id="subCodContrato" class="form-control form-control-sm"
|
|
placeholder="Cód. contrato subgrupo" maxlength="50">
|
|
</div>
|
|
</div>
|
|
<div id="subError" class="alert alert-danger d-none py-2 small mb-2"></div>
|
|
<button class="btn btn-sm btn-primary w-100" onclick="guardarSubgrupo()">
|
|
<i class="fas fa-save me-1"></i>Guardar subgrupo
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.badge-tarifa { font-size:.72rem; background:#e0f2fe; color:#0369a1; border-radius:4px; padding:2px 7px; }
|
|
.badge-activa { font-size:.72rem; }
|
|
.sub-row { background:#f8fafc; border-radius:6px; padding:8px 12px; margin-bottom:6px;
|
|
border:1px solid #e2e8f0; display:flex; align-items:center; gap:8px; }
|
|
.sub-row .sub-info { flex:1 }
|
|
.sub-row .sub-info .sub-nombre { font-weight:600; font-size:.9rem; }
|
|
.sub-row .sub-info .sub-meta { font-size:.78rem; color:#64748b; }
|
|
</style>
|
|
|
|
<script>
|
|
const BASE = '<?= BASE_URL ?>';
|
|
let _pagina = 1;
|
|
let _pages = 1;
|
|
let _tarifas = [];
|
|
let _modalEmpresa, _modalSubgrupos;
|
|
|
|
// ─── Init ─────────────────────────────────────────────────────
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
_modalEmpresa = new bootstrap.Modal(document.getElementById('modalEmpresa'));
|
|
_modalSubgrupos = new bootstrap.Modal(document.getElementById('modalSubgrupos'));
|
|
cargarTarifas().then(() => cargarEmpresas());
|
|
});
|
|
|
|
// ─── Tarifas ──────────────────────────────────────────────────
|
|
async function cargarTarifas() {
|
|
const res = await fetch(`${BASE}/api/lab/tarifas_id.php`);
|
|
const j = await res.json();
|
|
_tarifas = j.tarifas || [];
|
|
const opts = _tarifas.map(t =>
|
|
`<option value="${t.id}">${escHtml(t.nombre)}${t.porcentaje > 0 ? ` (+${t.porcentaje}%)` : ''}</option>`
|
|
).join('');
|
|
document.getElementById('fTarifaId').innerHTML = '<option value="">— Sin tarifa especial —</option>' + opts;
|
|
document.getElementById('subTarifaId').innerHTML = '<option value="">— Tarifa de la empresa —</option>' + opts;
|
|
}
|
|
|
|
// ─── Lista empresas ───────────────────────────────────────────
|
|
let _debTimer;
|
|
function debounceCargar() {
|
|
clearTimeout(_debTimer);
|
|
_debTimer = setTimeout(() => { _pagina = 1; cargarEmpresas(); }, 350);
|
|
}
|
|
|
|
async function cargarEmpresas() {
|
|
const search = document.getElementById('filtroSearch').value.trim();
|
|
const activa = document.getElementById('filtroActiva').value;
|
|
const params = new URLSearchParams({ action:'list', page:_pagina, limit:30 });
|
|
if (search) params.append('search', search);
|
|
if (activa !== '') params.append('activa', activa);
|
|
|
|
const tbody = document.getElementById('tbodyEmpresas');
|
|
tbody.innerHTML = '<tr><td colspan="8" class="text-center py-3 text-muted"><i class="fas fa-spinner fa-spin me-2"></i>Cargando…</td></tr>';
|
|
|
|
const res = await fetch(`${BASE}/api/lab/empresas.php?${params}`);
|
|
const j = await res.json();
|
|
|
|
_pages = j.pages || 1;
|
|
document.getElementById('lblTotal').textContent = `${j.total} empresa(s)`;
|
|
document.getElementById('lblPagina').textContent = `Página ${_pagina} de ${_pages}`;
|
|
document.getElementById('btnPrev').disabled = _pagina <= 1;
|
|
document.getElementById('btnNext').disabled = _pagina >= _pages;
|
|
|
|
if (!j.empresas || !j.empresas.length) {
|
|
tbody.innerHTML = '<tr><td colspan="8" class="text-center py-4 text-muted">Sin resultados</td></tr>';
|
|
return;
|
|
}
|
|
|
|
tbody.innerHTML = j.empresas.map(e => `
|
|
<tr>
|
|
<td class="text-monospace small fw-semibold">${escHtml(e.nit)}</td>
|
|
<td>
|
|
<div class="fw-semibold">${escHtml(e.nombre)}</div>
|
|
${e.razon_social ? `<div class="text-muted small">${escHtml(e.razon_social)}</div>` : ''}
|
|
</td>
|
|
<td>${e.tarifa_nombre ? `<span class="badge-tarifa">${escHtml(e.tarifa_nombre)}</span>` : '<span class="text-muted small">—</span>'}</td>
|
|
<td class="text-end small">${parseFloat(e.descuento_pct) > 0 ? escHtml(e.descuento_pct)+'%' : '—'}</td>
|
|
<td class="text-center">
|
|
${parseInt(e.total_subgrupos) > 0
|
|
? `<button class="btn btn-link btn-sm p-0 text-primary" onclick="abrirSubgrupos('${escAttr(e.nit)}','${escAttr(e.nombre)}')">${e.total_subgrupos} <i class="fas fa-layer-group ms-1"></i></button>`
|
|
: `<button class="btn btn-link btn-sm p-0 text-muted" onclick="abrirSubgrupos('${escAttr(e.nit)}','${escAttr(e.nombre)}')">+ subgrupo</button>`
|
|
}
|
|
</td>
|
|
<td class="text-center">${parseInt(e.req_autoriza) ? '<i class="fas fa-check-circle text-warning" title="Exige autorización"></i>' : '—'}</td>
|
|
<td class="text-center">
|
|
<span class="badge ${parseInt(e.activa) ? 'bg-success' : 'bg-secondary'} badge-activa">
|
|
${parseInt(e.activa) ? 'Activa' : 'Inactiva'}
|
|
</span>
|
|
</td>
|
|
<td class="text-end pe-2">
|
|
<button class="btn btn-outline-primary btn-sm py-0 px-2 me-1" onclick="abrirModal('${escAttr(e.nit)}')" title="Editar">
|
|
<i class="fas fa-pen"></i>
|
|
</button>
|
|
<button class="btn btn-outline-${parseInt(e.activa) ? 'warning' : 'success'} btn-sm py-0 px-2" onclick="toggleActiva('${escAttr(e.nit)}')" title="${parseInt(e.activa) ? 'Desactivar' : 'Activar'}">
|
|
<i class="fas fa-${parseInt(e.activa) ? 'ban' : 'check'}"></i>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
`).join('');
|
|
}
|
|
|
|
function cambiarPagina(delta) {
|
|
_pagina = Math.max(1, Math.min(_pages, _pagina + delta));
|
|
cargarEmpresas();
|
|
}
|
|
|
|
// ─── Modal empresa ────────────────────────────────────────────
|
|
async function abrirModal(nit = null) {
|
|
resetFormError();
|
|
document.getElementById('fNitOrig').value = nit || '';
|
|
document.getElementById('modalEmpresaTitulo').textContent = nit ? 'Editar empresa' : 'Nueva empresa';
|
|
document.getElementById('fNit').readOnly = !!nit;
|
|
|
|
// Reset
|
|
['fNit','fNombre','fRazonSocial','fCodigoEps','fTipoUsuarioSispro',
|
|
'fCodTercero','fCodContrato','fCentroCosto'].forEach(id => document.getElementById(id).value = '');
|
|
document.getElementById('fTarifaId').value = '';
|
|
document.getElementById('fTipoUsuario').value = '';
|
|
document.getElementById('fDescuentoPct').value= '0';
|
|
document.getElementById('fReqAutoriza').checked = false;
|
|
document.getElementById('fActiva').checked = true;
|
|
|
|
if (nit) {
|
|
const res = await fetch(`${BASE}/api/lab/empresas.php?action=get&nit=${encodeURIComponent(nit)}`);
|
|
const j = await res.json();
|
|
const e = j.empresa;
|
|
document.getElementById('fNit').value = e.nit;
|
|
document.getElementById('fNombre').value = e.nombre;
|
|
document.getElementById('fRazonSocial').value = e.razon_social || '';
|
|
document.getElementById('fTarifaId').value = e.tarifa_id || '';
|
|
document.getElementById('fDescuentoPct').value = e.descuento_pct || 0;
|
|
document.getElementById('fCodigoEps').value = e.codigo_eps || '';
|
|
document.getElementById('fTipoUsuario').value = e.tipo_usuario || '';
|
|
document.getElementById('fTipoUsuarioSispro').value = e.tipo_usuario_sispro || '';
|
|
document.getElementById('fCodTercero').value = e.cod_tercero || '';
|
|
document.getElementById('fCodContrato').value = e.cod_contrato || '';
|
|
document.getElementById('fCentroCosto').value = e.centro_costo || '';
|
|
document.getElementById('fReqAutoriza').checked = !!parseInt(e.req_autoriza);
|
|
document.getElementById('fActiva').checked = !!parseInt(e.activa);
|
|
}
|
|
_modalEmpresa.show();
|
|
}
|
|
|
|
async function guardarEmpresa() {
|
|
resetFormError();
|
|
const nit = document.getElementById('fNit').value.trim();
|
|
if (!nit) return showFormError('El NIT es obligatorio');
|
|
if (!document.getElementById('fNombre').value.trim()) return showFormError('El nombre es obligatorio');
|
|
|
|
const btn = document.getElementById('btnGuardarEmpresa');
|
|
btn.disabled = true;
|
|
|
|
const payload = {
|
|
action: 'save',
|
|
nit,
|
|
nombre: document.getElementById('fNombre').value.trim(),
|
|
razon_social: document.getElementById('fRazonSocial').value.trim(),
|
|
tarifa_id: document.getElementById('fTarifaId').value,
|
|
descuento_pct: document.getElementById('fDescuentoPct').value,
|
|
codigo_eps: document.getElementById('fCodigoEps').value.trim(),
|
|
tipo_usuario: document.getElementById('fTipoUsuario').value,
|
|
tipo_usuario_sispro: document.getElementById('fTipoUsuarioSispro').value.trim(),
|
|
cod_tercero: document.getElementById('fCodTercero').value.trim(),
|
|
cod_contrato: document.getElementById('fCodContrato').value.trim(),
|
|
centro_costo: document.getElementById('fCentroCosto').value.trim(),
|
|
req_autoriza: document.getElementById('fReqAutoriza').checked,
|
|
activa: document.getElementById('fActiva').checked,
|
|
};
|
|
|
|
try {
|
|
const res = await fetch(`${BASE}/api/lab/empresas.php`, {
|
|
method: 'POST',
|
|
headers: {'Content-Type':'application/json'},
|
|
body: JSON.stringify(payload)
|
|
});
|
|
const j = await res.json();
|
|
if (!j.ok) return showFormError(j.error || 'Error al guardar');
|
|
_modalEmpresa.hide();
|
|
cargarEmpresas();
|
|
} catch(e) {
|
|
showFormError('Error de red: ' + e.message);
|
|
} finally {
|
|
btn.disabled = false;
|
|
}
|
|
}
|
|
|
|
async function toggleActiva(nit) {
|
|
await fetch(`${BASE}/api/lab/empresas.php`, {
|
|
method: 'POST',
|
|
headers: {'Content-Type':'application/json'},
|
|
body: JSON.stringify({action:'toggle', nit})
|
|
});
|
|
cargarEmpresas();
|
|
}
|
|
|
|
function showFormError(msg) { const el = document.getElementById('formError'); el.textContent = msg; el.classList.remove('d-none'); }
|
|
function resetFormError() { document.getElementById('formError').classList.add('d-none'); }
|
|
|
|
// ─── Modal subgrupos ─────────────────────────────────────────
|
|
async function abrirSubgrupos(nit, nombre) {
|
|
document.getElementById('subNitNombre').textContent = nombre;
|
|
document.getElementById('subNitEmpresa').value = nit;
|
|
document.getElementById('subId').value = '';
|
|
['subNombre','subRefSubgrupo','subCodContrato'].forEach(id => document.getElementById(id).value = '');
|
|
document.getElementById('subTarifaId').value = '';
|
|
document.getElementById('subError').classList.add('d-none');
|
|
await recargarSubgrupos(nit);
|
|
_modalSubgrupos.show();
|
|
}
|
|
|
|
async function recargarSubgrupos(nit) {
|
|
if (!nit) nit = document.getElementById('subNitEmpresa').value;
|
|
const res = await fetch(`${BASE}/api/lab/empresa_subgrupos.php?nit_empresa=${encodeURIComponent(nit)}`);
|
|
const j = await res.json();
|
|
const lista = document.getElementById('listaSubgrupos');
|
|
if (!j.subgrupos || !j.subgrupos.length) {
|
|
lista.innerHTML = '<p class="text-muted small text-center">Sin subgrupos</p>';
|
|
return;
|
|
}
|
|
lista.innerHTML = j.subgrupos.map(s => `
|
|
<div class="sub-row">
|
|
<div class="sub-info">
|
|
<div class="sub-nombre">${escHtml(s.subgrupo)}</div>
|
|
<div class="sub-meta">
|
|
${s.tarifa_nombre ? `<span class="badge-tarifa me-2">${escHtml(s.tarifa_nombre)}</span>` : ''}
|
|
${s.ref_subgrupo ? `Ref: ${escHtml(s.ref_subgrupo)}` : ''}
|
|
${s.cod_contrato ? ` · Cto: ${escHtml(s.cod_contrato)}` : ''}
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-outline-primary btn-sm py-0 px-2" onclick="editarSubgrupo(${s.id},'${escAttr(s.subgrupo)}','${escAttr(s.tarifa_id||'')}','${escAttr(s.ref_subgrupo||'')}','${escAttr(s.cod_contrato||'')}')" title="Editar">
|
|
<i class="fas fa-pen"></i>
|
|
</button>
|
|
<button class="btn btn-outline-danger btn-sm py-0 px-2" onclick="eliminarSubgrupo(${s.id})" title="Eliminar">
|
|
<i class="fas fa-trash"></i>
|
|
</button>
|
|
</div>
|
|
`).join('');
|
|
}
|
|
|
|
function editarSubgrupo(id, nombre, tarifaId, ref, codCon) {
|
|
document.getElementById('subId').value = id;
|
|
document.getElementById('subNombre').value = nombre;
|
|
document.getElementById('subTarifaId').value = tarifaId;
|
|
document.getElementById('subRefSubgrupo').value = ref;
|
|
document.getElementById('subCodContrato').value = codCon;
|
|
document.getElementById('subNombre').focus();
|
|
}
|
|
|
|
async function guardarSubgrupo() {
|
|
const errEl = document.getElementById('subError');
|
|
errEl.classList.add('d-none');
|
|
const nit = document.getElementById('subNitEmpresa').value;
|
|
const nombre = document.getElementById('subNombre').value.trim();
|
|
if (!nombre) { errEl.textContent='El nombre es obligatorio'; errEl.classList.remove('d-none'); return; }
|
|
|
|
const payload = {
|
|
action: 'save',
|
|
id: document.getElementById('subId').value || null,
|
|
nit_empresa: nit,
|
|
subgrupo: nombre,
|
|
tarifa_id: document.getElementById('subTarifaId').value,
|
|
ref_subgrupo: document.getElementById('subRefSubgrupo').value.trim(),
|
|
cod_contrato: document.getElementById('subCodContrato').value.trim(),
|
|
};
|
|
const res = await fetch(`${BASE}/api/lab/empresa_subgrupos.php`, {
|
|
method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(payload)
|
|
});
|
|
const j = await res.json();
|
|
if (!j.ok) { errEl.textContent = j.error || 'Error'; errEl.classList.remove('d-none'); return; }
|
|
// Reset form
|
|
document.getElementById('subId').value = '';
|
|
['subNombre','subRefSubgrupo','subCodContrato'].forEach(id => document.getElementById(id).value='');
|
|
document.getElementById('subTarifaId').value = '';
|
|
recargarSubgrupos(nit);
|
|
cargarEmpresas();
|
|
}
|
|
|
|
async function eliminarSubgrupo(id) {
|
|
if (!confirm('¿Eliminar este subgrupo?')) return;
|
|
const nit = document.getElementById('subNitEmpresa').value;
|
|
await fetch(`${BASE}/api/lab/empresa_subgrupos.php`, {
|
|
method:'POST', headers:{'Content-Type':'application/json'},
|
|
body: JSON.stringify({action:'delete', id})
|
|
});
|
|
recargarSubgrupos(nit);
|
|
cargarEmpresas();
|
|
}
|
|
|
|
// ─── Helpers ─────────────────────────────────────────────────
|
|
function escHtml(s) {
|
|
if (s == null) return '';
|
|
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
|
}
|
|
function escAttr(s) { return escHtml(s).replace(/'/g,'''); }
|
|
</script>
|
|
<?php Layout::close(); ?>
|