fix(regimen): fallback a tenant/contribuyentes cuando un contribuyente no tiene regimen_fiscal
This commit is contained in:
@@ -64,7 +64,24 @@ export async function getRegimenesActivosClavesEfectivos(
|
||||
if (rows.length > 0 && rows[0].regimen_fiscal) {
|
||||
return rows[0].regimen_fiscal.split(',').map((c: string) => c.trim()).filter(Boolean);
|
||||
}
|
||||
return [];
|
||||
// Fallback: si el contribuyente no tiene regimen_fiscal, usamos los del tenant
|
||||
// para no perder la alerta si el campo quedó vacío accidentalmente.
|
||||
const tenantRegimenes = await getRegimenesActivosClaves(tenantId);
|
||||
if (tenantRegimenes.length > 0) return tenantRegimenes;
|
||||
|
||||
const { rows: allRows } = 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 allRows) {
|
||||
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);
|
||||
}
|
||||
|
||||
const tenantRegimenes = await getRegimenesActivosClaves(tenantId);
|
||||
|
||||
Reference in New Issue
Block a user