# Utilidades(Consultar Utilidades (1)) - [getYearUtilidades]

## 🧾 Descripción

Este servicio obtiene:

1. **La lista de años registrados en el ERP** (tabla *Year*), ordenados de forma descendente.
2. **Valida si el empleado tiene utilidades asignadas** para al menos un año (tabla *Utilidades*).

El objetivo es determinar si el empleado cuenta con utilidades cargadas y retornar la lista de años disponibles para consulta.

---

## 🚀 Endpoint

<span style="color: rgb(224, 62, 45);">**`POST /get-year-utilidades`**</span>

---

## 📥 Parámetros de entrada (Request Body)

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22employee%22%3A-%22emp-0"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"employee"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"EMP-0001"</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### Campos

<div class="_tableContainer_1rjym_1" id="bkmrk-campo-tipo-obligator"><div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1"><table class="w-fit min-w-(--thread-content-width)" data-end="949" data-start="782"><thead data-end="826" data-start="782"><tr data-end="826" data-start="782"><th data-col-size="sm" data-end="790" data-start="782">Campo</th><th data-col-size="sm" data-end="797" data-start="790">Tipo</th><th data-col-size="sm" data-end="811" data-start="797">Obligatorio</th><th data-col-size="md" data-end="826" data-start="811">Descripción</th></tr></thead><tbody data-end="949" data-start="872"><tr data-end="949" data-start="872"><td data-col-size="sm" data-end="883" data-start="872">employee</td><td data-col-size="sm" data-end="892" data-start="883">string</td><td data-col-size="sm" data-end="896" data-start="892">✔</td><td data-col-size="md" data-end="949" data-start="896">ID del empleado para validar si tiene utilidades.</td></tr></tbody></table>

</div></div>---

## 🔐 Seguridad

Utiliza autenticación mediante `ServiceErp()` (token interno del ERP).  
No requiere permisos adicionales, ya que solo consulta información.

---

## 🧠 Flujo del Servicio (explicación real)

### 1️⃣ Obtener los años disponibles (tabla Year)

Llama al ERP: `<span class="hljs-keyword">GET</span> <span class="hljs-keyword">Year</span>?limit<span class="hljs-operator">=</span><span class="hljs-keyword">None</span><span class="hljs-operator">&</span>fields<span class="hljs-operator">=</span>["name"]`

- Extrae todos los registros.
- Toma únicamente la columna `name`.
- Ordena los años de forma descendente.

---

### 2️⃣ Validar si el empleado tiene utilidades registradas

Consulta al ERP: `GET Utilidades?limit=None&fields=[<span class="hljs-string">"name"</span>]&filters=<span class="hljs-string">[["empleado","=", employee]]</span>`

- Si no existe ningún registro → El empleado **no tiene utilidades asignadas**.
- Si existe al menos un registro → El empleado **sí tiene utilidades asignadas**.

---

## 📤 Response 200 – Ejemplo

### Caso 1 — El empleado **NO tiene utilidades**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-false%2C-%22m"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Hemos verificado que no tiene utilidades asignadas. Para más detalles, consultar con el área de RRHH"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">"2024"</span><span class="hljs-punctuation">,</span> <span class="hljs-string">"2023"</span><span class="hljs-punctuation">,</span> <span class="hljs-string">"2022"</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>### Caso 2 — El empleado **SÍ tiene utilidades**

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-%7B-%22valor%22%3A-true%2C-%22ms"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"valor"</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"msn"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Hemos verificado que no tiene utilidades asignadas. Para más detalles, consultar con el área de RRHH"</span><span class="hljs-punctuation">,</span>  <span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">"2024"</span><span class="hljs-punctuation">,</span> <span class="hljs-string">"2023"</span><span class="hljs-punctuation">,</span> <span class="hljs-string">"2022"</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">}</span>`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>📌 *Nota:* El mensaje es el mismo en ambos escenarios, siguiendo el comportamiento del código original.

---

## ❗ Posibles Errores

<div class="_tableContainer_1rjym_1" id="bkmrk-caso-respuesta-error"><div class="group _tableWrapper_1rjym_13 flex w-fit flex-col-reverse" tabindex="-1"><table class="w-fit min-w-(--thread-content-width)" data-end="2614" data-start="2350"><thead data-end="2370" data-start="2350"><tr data-end="2370" data-start="2350"><th data-col-size="sm" data-end="2357" data-start="2350">Caso</th><th data-col-size="sm" data-end="2370" data-start="2357">Respuesta</th></tr></thead><tbody data-end="2614" data-start="2392"><tr data-end="2458" data-start="2392"><td data-col-size="sm" data-end="2424" data-start="2392">Error al obtener años del ERP</td><td data-col-size="sm" data-end="2458" data-start="2424">years → \[\] (no rompe el flujo)</td></tr><tr data-end="2504" data-start="2459"><td data-col-size="sm" data-end="2490" data-start="2459">Error de conexión con el ERP</td><td data-col-size="sm" data-end="2504" data-start="2490">years → \[\]</td></tr><tr data-end="2559" data-start="2505"><td data-col-size="sm" data-end="2539" data-start="2505">El empleado no tiene utilidades</td><td data-col-size="sm" data-end="2559" data-start="2539">`"valor": false`</td></tr><tr data-end="2614" data-start="2560"><td data-col-size="sm" data-end="2594" data-start="2560">El empleado sí tiene utilidades</td><td data-col-size="sm" data-end="2614" data-start="2594">`"valor": true"`</td></tr></tbody></table>

</div></div>---

## 📚 Tablas usadas (schemas)

### 📄 Year (GET)

Campos usados: `<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">}</span>`

### 📄 Utilidades (GET)

Campos usados: `<span class="hljs-punctuation">{</span>  <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"string"</span><span class="hljs-punctuation">}</span>`

---

## 🗃 Lógica en pseudo-código

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-employee-%3D-request.e"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs"><table border="1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>`employee = request.employeeyears = GET Yearyears = list of namessort years descutilidades = GET Utilidades where empleado = employeeif utilidades.count == 0:    return { valor: false, msn: "...", data: years }return { valor: true, msn: "...", data: years }`</td></tr></tbody></table>

</div></div></div><div class="overflow-y-auto p-4" dir="ltr"></div></div>