fix(alertas): combinar regímenes de contribuyentes cuando no hay config a nivel tenant
This commit is contained in:
@@ -45,7 +45,10 @@ export async function getRegimenesActivosClaves(tenantId: string): Promise<strin
|
||||
/**
|
||||
* Resuelve las claves de regímenes activos para la alerta de discrepancia.
|
||||
* Si hay contribuyenteId, lee de contribuyentes.regimen_fiscal (comma-separated).
|
||||
* Si no, fallback a TenantRegimenActivo (tabla central).
|
||||
* Si no, combina TenantRegimenActivo (tabla central) con los regímenes de
|
||||
* todos los contribuyentes activos del tenant. Esto evita que la alerta
|
||||
* aparezca en el correo por-contribuyente pero desaparezca en el dashboard
|
||||
* cuando no hay un contribuyente seleccionado.
|
||||
*/
|
||||
export async function getRegimenesActivosClavesEfectivos(
|
||||
tenantId: string,
|
||||
@@ -63,7 +66,30 @@ export async function getRegimenesActivosClavesEfectivos(
|
||||
}
|
||||
return [];
|
||||
}
|
||||
return getRegimenesActivosClaves(tenantId);
|
||||
|
||||
const tenantRegimenes = await getRegimenesActivosClaves(tenantId);
|
||||
|
||||
// Fallback: si no hay regímenes configurados a nivel tenant, usamos los
|
||||
// regímenes de todos los contribuyentes activos del tenant.
|
||||
if (tenantRegimenes.length > 0) {
|
||||
return tenantRegimenes;
|
||||
}
|
||||
|
||||
const { rows } = await pool.query(
|
||||
`SELECT DISTINCT regimen_fiscal FROM contribuyentes WHERE regimen_fiscal IS NOT NULL AND regimen_fiscal <> ''`,
|
||||
);
|
||||
|
||||
const set = new Set<string>();
|
||||
for (const row of rows) {
|
||||
if (row.regimen_fiscal) {
|
||||
for (const clave of row.regimen_fiscal.split(',')) {
|
||||
const trimmed = clave.trim();
|
||||
if (trimmed) set.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(set);
|
||||
}
|
||||
|
||||
export async function setRegimenesActivos(tenantId: string, regimenIds: number[]) {
|
||||
|
||||
Reference in New Issue
Block a user