+page.js
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { error } from '@sveltejs/kit';
export async function load({ params, fetch }) {
const codigo = params.codigo;
const isDA = codigo.includes('.');
const entidadCode = isDA ? codigo.split('.')[0] : codigo;
const entidadNum = parseInt(entidadCode);
if (isNaN(entidadNum)) {
throw error(400, 'Código de entidad inválido');
}
// Cargar metadata desde proxy local + población en paralelo
const [entidadRes, pobRes] = await Promise.all([
fetch(`/api/entidad-data?codigo=${entidadNum}&tipo=detalle`).then(r => r.ok ? r.json() : null),
fetch('/poblacion.csv').then(r => r.text())
]);
if (!entidadRes) {
throw error(404, 'Entidad no encontrada');
}
// Población
const poblacionEntidad = {};
const poblacionNacional = {};
pobRes.split('\n').slice(1).forEach(line => {
const [, ent, gestion, pob] = line.split(',');
const g = parseInt(gestion);
const p = parseInt(pob);
if (!g || !p) return;
poblacionNacional[g] = (poblacionNacional[g] || 0) + p;
if (ent === entidadCode) {
poblacionEntidad[g] = (poblacionEntidad[g] || 0) + p;
}
});
const tienePobEntidad = Object.keys(poblacionEntidad).length > 0;
const poblacionMap = tienePobEntidad ? poblacionEntidad : poblacionNacional;
// gastos_ingresos → resumenData (compatible con el formato que espera la página)
const gastosIngresos = entidadRes.gastos_ingresos || [];
const resumenData = gastosIngresos.map(d => ({
tipo: d.tipo,
gestion: d.gestion,
devengado: d.devengado,
ranking: d.ranking,
codigo: codigo,
desc: entidadRes.desc_entidad
}));
// Determinar última gestión
const gestiones = [...new Set(gastosIngresos.map(d => d.gestion))].filter(g => g <= 2025).sort((a, b) => b - a);
const ultimaGestion = gestiones[0] || 2025;
// Cargar distribuciones de la última gestión (objetos, finfuns, etc.)
const distEndpoints = [
{ api: 'objetos', dim: 'objeto' },
{ api: 'finfuns', dim: 'finfun' },
{ api: 'actecos', dim: 'acteco' },
{ api: 'rubros', dim: 'rubro' },
{ api: 'organismos', dim: 'organismo' }
];
const distResults = await Promise.all(
distEndpoints.map(async ({ api, dim }) => {
try {
const res = await fetch(`/api/entidad-data?codigo=${entidadNum}&tipo=${api}`);
if (!res.ok) return [];
const data = await res.json();
if (!Array.isArray(data)) return [];
return data
.filter(d => d.gestion === ultimaGestion)
.map(d => ({
tipo: d.tipo || 'gastos',
dimension: dim,
gestion: d.gestion,
padre: d.padre,
desc_padre: d.desc_padre,
hijo: d.hijo,
desc_hijo: d.desc_hijo,
devengado: d.devengado
}));
} catch { return []; }
})
);
const distribucionesData = distResults.flat();
// DA info
let nombreDA = null;
let nombreEntidadMadre = null;
if (isDA) {
nombreDA = entidadRes.desc_entidad || null;
nombreEntidadMadre = entidadRes.desc_entidad || null;
}
return {
entidad: entidadRes,
isDA,
nombreDA,
nombreEntidadMadre,
codigoEntidadPadre: isDA ? entidadCode : null,
resumenData,
distribucionesData,
gestionInicial: ultimaGestion,
poblacionMap,
tienePobEntidad
};
}