diff --git a/package-lock.json b/package-lock.json index 8e2f977..7f35215 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,8 @@ "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1", "typeor": "^1.2.0", - "typeorm": "^0.3.27" + "typeorm": "^0.3.27", + "xlsx": "^0.18.5" }, "devDependencies": { "@eslint/eslintrc": "^3.2.0", @@ -720,9 +721,9 @@ "license": "MIT" }, "node_modules/@borewit/text-codec": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/@borewit/text-codec/-/text-codec-0.1.1.tgz", - "integrity": "sha512-5L/uBxmjaCIX5h8Z+uu+kA9BQLkc/Wl06UGR5ajNRxu+/XjonB5i8JpgFMrPj3LXTCPA0pv8yxUvbUi+QthGGA==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@borewit/text-codec/-/text-codec-0.2.2.tgz", + "integrity": "sha512-DDaRehssg1aNrH4+2hnj1B7vnUGEjU6OIlyRdkMd0aUdIUvKXrJfXsy8LVtXAy7DRvYVluWbMspsRhz2lcW0mQ==", "license": "MIT", "funding": { "type": "github", @@ -2139,14 +2140,14 @@ } }, "node_modules/@nestjs/common": { - "version": "11.1.6", - "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-11.1.6.tgz", - "integrity": "sha512-krKwLLcFmeuKDqngG2N/RuZHCs2ycsKcxWIDgcm7i1lf3sQ0iG03ci+DsP/r3FcT/eJDFsIHnKtNta2LIi7PzQ==", + "version": "11.1.17", + "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-11.1.17.tgz", + "integrity": "sha512-hLODw5Abp8OQgA+mUO4tHou4krKgDtUcM9j5Ihxncst9XeyxYBTt2bwZm4e4EQr5E352S4Fyy6V3iFx9ggxKAg==", "license": "MIT", "dependencies": { - "file-type": "21.0.0", + "file-type": "21.3.2", "iterare": "1.2.1", - "load-esm": "1.0.2", + "load-esm": "1.0.3", "tslib": "2.8.1", "uid": "2.0.2" }, @@ -2599,14 +2600,13 @@ "license": "MIT" }, "node_modules/@tokenizer/inflate": { - "version": "0.2.7", - "resolved": "https://registry.npmjs.org/@tokenizer/inflate/-/inflate-0.2.7.tgz", - "integrity": "sha512-MADQgmZT1eKjp06jpI2yozxaU9uVs4GzzgSL+uEq7bVcJ9V1ZXQkeGNql1fsSI0gMy1vhvNTNbUqrx+pZfJVmg==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@tokenizer/inflate/-/inflate-0.4.1.tgz", + "integrity": "sha512-2mAv+8pkG6GIZiF1kNg1jAjh27IDxEPKwdGul3snfztFerfPGI1LjDezZp3i7BElXompqEtPmoPx6c2wgtWsOA==", "license": "MIT", "dependencies": { - "debug": "^4.4.0", - "fflate": "^0.8.2", - "token-types": "^6.0.0" + "debug": "^4.4.3", + "token-types": "^6.1.1" }, "engines": { "node": ">=18" @@ -3798,6 +3798,15 @@ "node": ">=0.4.0" } }, + "node_modules/adler-32": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/adler-32/-/adler-32-1.3.1.tgz", + "integrity": "sha512-ynZ4w/nUUv5rrsR8UUGoe1VC9hZj6V5hU9Qw1HlMDJGEJw5S7TfTErWTjMys6M7vr0YWcPqs3qAr4ss0nDfP+A==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.8" + } + }, "node_modules/ajv": { "version": "6.14.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.14.0.tgz", @@ -4603,6 +4612,19 @@ ], "license": "CC-BY-4.0" }, + "node_modules/cfb": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/cfb/-/cfb-1.2.2.tgz", + "integrity": "sha512-KfdUZsSOw19/ObEWasvBP/Ac4reZvAGauZhs6S/gqNhXhI7cKwvlH7ulj+dOEYnca4bm4SGo8C1bTAQvnTjgQA==", + "license": "Apache-2.0", + "dependencies": { + "adler-32": "~1.3.0", + "crc-32": "~1.2.0" + }, + "engines": { + "node": ">=0.8" + } + }, "node_modules/chainsaw": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/chainsaw/-/chainsaw-0.1.0.tgz", @@ -4840,6 +4862,15 @@ "node": ">= 0.12.0" } }, + "node_modules/codepage": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/codepage/-/codepage-1.15.0.tgz", + "integrity": "sha512-3g6NUTPd/YtuuGrhMnOMRjFc+LJw/bnMp3+0r/Wcz3IXUuCosKRJvMphm5+Q+bvTVGcJJuRvVLuYba+WojaFaA==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.8" + } + }, "node_modules/collect-v8-coverage": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/collect-v8-coverage/-/collect-v8-coverage-1.0.3.tgz", @@ -5998,12 +6029,6 @@ "bser": "2.1.1" } }, - "node_modules/fflate": { - "version": "0.8.2", - "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", - "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==", - "license": "MIT" - }, "node_modules/file-entry-cache": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz", @@ -6018,14 +6043,14 @@ } }, "node_modules/file-type": { - "version": "21.0.0", - "resolved": "https://registry.npmjs.org/file-type/-/file-type-21.0.0.tgz", - "integrity": "sha512-ek5xNX2YBYlXhiUXui3D/BXa3LdqPmoLJ7rqEx2bKJ7EAUEfmXgW0Das7Dc6Nr9MvqaOnIqiPV0mZk/r/UpNAg==", + "version": "21.3.2", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-21.3.2.tgz", + "integrity": "sha512-DLkUvGwep3poOV2wpzbHCOnSKGk1LzyXTv+aHFgN2VFl96wnp8YA9YjO2qPzg5PuL8q/SW9Pdi6WTkYOIh995w==", "license": "MIT", "dependencies": { - "@tokenizer/inflate": "^0.2.7", - "strtok3": "^10.2.2", - "token-types": "^6.0.0", + "@tokenizer/inflate": "^0.4.1", + "strtok3": "^10.3.4", + "token-types": "^6.1.1", "uint8array-extras": "^1.4.0" }, "engines": { @@ -6101,9 +6126,9 @@ } }, "node_modules/flatted": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.3.tgz", - "integrity": "sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==", + "version": "3.4.2", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.2.tgz", + "integrity": "sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA==", "dev": true, "license": "ISC" }, @@ -6250,6 +6275,15 @@ "node": ">= 0.6" } }, + "node_modules/frac": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/frac/-/frac-1.1.2.tgz", + "integrity": "sha512-w/XBfkibaTl3YDqASwfDUqkna4Z2p9cFSr1aHDt0WoMTECnRfBOv2WArlZILlqgWlmdIlALXGpM2AOhEk5W3IA==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.8" + } + }, "node_modules/fresh": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz", @@ -7916,9 +7950,9 @@ "license": "ISC" }, "node_modules/load-esm": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/load-esm/-/load-esm-1.0.2.tgz", - "integrity": "sha512-nVAvWk/jeyrWyXEAs84mpQCYccxRqgKY4OznLuJhJCa0XsPSfdOIr2zvBZEj3IHEHbX97jjscKRRV539bW0Gpw==", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/load-esm/-/load-esm-1.0.3.tgz", + "integrity": "sha512-v5xlu8eHD1+6r8EHTg6hfmO97LN8ugKtiXcy5e6oN72iD2r6u0RPfLl6fxM+7Wnh2ZRq15o0russMst44WauPA==", "funding": [ { "type": "github", @@ -9801,6 +9835,18 @@ "node": ">= 0.6" } }, + "node_modules/ssf": { + "version": "0.11.2", + "resolved": "https://registry.npmjs.org/ssf/-/ssf-0.11.2.tgz", + "integrity": "sha512-+idbmIXoYET47hH+d7dfm2epdOMUDjqcB4648sTZ+t2JwoyBFL/insLfB/racrDmsKB3diwsDA696pZMieAC5g==", + "license": "Apache-2.0", + "dependencies": { + "frac": "~1.1.2" + }, + "engines": { + "node": ">=0.8" + } + }, "node_modules/stack-utils": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz", @@ -10404,12 +10450,12 @@ } }, "node_modules/token-types": { - "version": "6.1.1", - "resolved": "https://registry.npmjs.org/token-types/-/token-types-6.1.1.tgz", - "integrity": "sha512-kh9LVIWH5CnL63Ipf0jhlBIy0UsrMj/NJDfpsy1SqOXlLKEVyXXYrnFxFT1yOOYVGBSApeVnjPw/sBz5BfEjAQ==", + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-6.1.2.tgz", + "integrity": "sha512-dRXchy+C0IgK8WPC6xvCHFRIWYUbqqdEIKPaKo/AcTUNzwLTK6AH7RjdLWsEZcAN/TBdtfUw3PYEgPr5VPr6ww==", "license": "MIT", "dependencies": { - "@borewit/text-codec": "^0.1.0", + "@borewit/text-codec": "^0.2.1", "@tokenizer/token": "^0.3.0", "ieee754": "^1.2.1" }, @@ -11392,6 +11438,24 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/wmf": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wmf/-/wmf-1.0.2.tgz", + "integrity": "sha512-/p9K7bEh0Dj6WbXg4JG0xvLQmIadrner1bi45VMJTfnbVHsc7yIajZyoSoK60/dtVBs12Fm6WkUI5/3WAVsNMw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.8" + } + }, + "node_modules/word": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/word/-/word-0.3.0.tgz", + "integrity": "sha512-OELeY0Q61OXpdUfTp+oweA/vtLVg5VDOXh+3he3PNzLGG/y0oylSOC1xRVj0+l4vQ3tj/bB1HVHv1ocXkQceFA==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.8" + } + }, "node_modules/word-wrap": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", @@ -11506,6 +11570,27 @@ "node": "^14.17.0 || ^16.13.0 || >=18.0.0" } }, + "node_modules/xlsx": { + "version": "0.18.5", + "resolved": "https://registry.npmjs.org/xlsx/-/xlsx-0.18.5.tgz", + "integrity": "sha512-dmg3LCjBPHZnQp5/F/+nnTa+miPJxUXB6vtk42YjBBKayDNagxGEeIdWApkYPOf3Z3pm3k62Knjzp7lMeTEtFQ==", + "license": "Apache-2.0", + "dependencies": { + "adler-32": "~1.3.0", + "cfb": "~1.2.1", + "codepage": "~1.15.0", + "crc-32": "~1.2.1", + "ssf": "~0.11.2", + "wmf": "~1.0.1", + "word": "~0.3.0" + }, + "bin": { + "xlsx": "bin/xlsx.njs" + }, + "engines": { + "node": ">=0.8" + } + }, "node_modules/xmlchars": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", diff --git a/package.json b/package.json index 0f9e4e2..13f4d7e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1", "typeor": "^1.2.0", - "typeorm": "^0.3.27" + "typeorm": "^0.3.27", + "xlsx": "^0.18.5" }, "devDependencies": { "@eslint/eslintrc": "^3.2.0", diff --git a/src/equipo/dto/grafica.dto.ts b/src/equipo/dto/grafica.dto.ts index 82788a6..b2f52a1 100644 --- a/src/equipo/dto/grafica.dto.ts +++ b/src/equipo/dto/grafica.dto.ts @@ -27,4 +27,32 @@ export class FiltrosGraficaDto { @IsString({ each: true }) antiguedad?: string[] +} + +export class FiltrosTablaDto { + + @IsArray() + @IsString({ each: true }) + adscripciones?: number[] + + @IsOptional() + @IsArray() + @IsString({ each: true }) + procesadores?: number[] + + @IsOptional() + @IsArray() + @IsString({ each: true }) + sistemas?: number[] + + @IsOptional() + @IsArray() + @IsString({ each: true }) + usos?: number[] + + @IsOptional() + @IsArray() + @IsString({ each: true }) + antiguedad?: string[] + } \ No newline at end of file diff --git a/src/equipo/equipo.controller.ts b/src/equipo/equipo.controller.ts index 4c37a05..25291d7 100644 --- a/src/equipo/equipo.controller.ts +++ b/src/equipo/equipo.controller.ts @@ -19,7 +19,7 @@ import { UpdateEquipoDto } from './dto/update-equipo.dto'; import { AuthGuard } from '@nestjs/passport'; import { CreateEquipoDto } from './dto/create-equipo.dto'; import type { Response } from 'express'; -import { FiltrosGraficaDto } from './dto/grafica.dto'; +import { FiltrosGraficaDto, FiltrosTablaDto } from './dto/grafica.dto'; @Controller('equipos') export class EquipoController { @@ -35,13 +35,59 @@ export class EquipoController { } @Post('grafica/:tipo') - async graficaProcesador( + async grafica( @Param("tipo") tipo: 'antiguedad' | 'uso' | 'so' | 'procesador', @Body() filtros: FiltrosGraficaDto ) { return this.equipoService.grafica(tipo, filtros); } + @Post('tabla') + async tabla( + @Body() filtros: FiltrosTablaDto + ) { + return this.equipoService.tabla(filtros); + } + + @Post('tabla/count') + async tablacount( + @Body() filtros: FiltrosTablaDto + ) { + return this.equipoService.tablaCount(filtros); + } + + @Post('excel/tabla') + async exceltabla( + @Res() res: Response, + @Body() filtros: FiltrosGraficaDto + ) { + const buffer = await this.equipoService.tablaExcel(filtros); + + res.set({ + 'Content-Type': + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'Content-Disposition': 'attachment; filename=tabla.xlsx', + }); + + res.send(buffer); + } + + @Post('excel/tabla/count') + async exceltablacount( + @Res() res: Response, + @Body() filtros: FiltrosGraficaDto + ) { + const buffer = await this.equipoService.tablaCountExcel(filtros); + + res.set({ + 'Content-Type': + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'Content-Disposition': 'attachment; filename=tabla.xlsx', + }); + + res.send(buffer); + } + @UseGuards(AuthGuard('jwt')) @Get('buscar/:inv') async serch(@Param('inv') inv: string) { @@ -101,7 +147,7 @@ export class EquipoController { findTiposEquipo() { return this.equipoService.findAllTiposEquipo(); } - + @UseGuards(AuthGuard('jwt')) @Get('sistemas-operativos') findSistemasOperativos() { diff --git a/src/equipo/equipo.service.ts b/src/equipo/equipo.service.ts index 48f47d1..6bd44e1 100644 --- a/src/equipo/equipo.service.ts +++ b/src/equipo/equipo.service.ts @@ -10,6 +10,7 @@ import { UpdateEquipoDto } from './dto/update-equipo.dto'; import axios from 'axios'; import e, { Response } from 'express'; import * as ExcelJS from 'exceljs'; + import { Adscripcion, Estado, @@ -1217,7 +1218,7 @@ export class EquipoService { } - qb.orderBy(`equipo.adscripcion`) + qb.orderBy(`equipo.adscripcion`, "ASC") break @@ -1238,8 +1239,7 @@ export class EquipoService { qb.groupBy('sistemaOperativo.sistema_operativo') } - qb.orderBy(`equipo.adscripcion`) - + qb.orderBy(`equipo.adscripcion`, "ASC") break @@ -1260,7 +1260,7 @@ export class EquipoService { qb.groupBy('procesador.procesador') } - qb.orderBy(`equipo.adscripcion`) + qb.orderBy(`equipo.adscripcion`, "ASC") break @@ -1269,6 +1269,260 @@ export class EquipoService { } + async tabla(filtros?: FiltrosGraficaDto) { + const qb = this.equipoRepository + .createQueryBuilder('equipo') + .select([]) + + qb.leftJoin('equipo.adscripcion', 'adscripcion') + qb.leftJoin('equipo.procesador', 'procesador') + qb.leftJoin('equipo.sistemaOperativo', 'sistemaOperativo') + qb.leftJoin('equipo.tipoUso', 'tipoUso') + + qb.addSelect('equipo.inventario', 'inventario') + qb.addSelect('adscripcion.adscripcion', 'adscripcion') + qb.addSelect('equipo.lugar', 'ubicacion') + + if (filtros?.adscripciones?.length) { + qb.andWhere('adscripcion.id_adscripcion IN (:...adscripciones)', { + adscripciones: filtros.adscripciones + }) + } + + if (filtros?.procesadores?.length) { + qb.andWhere('procesador.id_procesador IN (:...procesadores)', { + procesadores: filtros.procesadores + }) + .addSelect('procesador.procesador', 'procesador') + + } + + if (filtros?.sistemas?.length) { + qb.andWhere('sistemaOperativo.id_sistema_operativo IN (:...sistemas)', { + sistemas: filtros.sistemas + }) + .addSelect('sistemaOperativo.sistema_operativo', 'so') + + } + + if (filtros?.usos?.length) { + qb.andWhere('tipoUso.id_uso IN (:...usos)', { + usos: filtros.usos + }) + .addSelect('tipoUso.tipo_uso', 'uso') + } + + if (filtros?.antiguedad?.length) { + qb.andWhere('equipo.antiguedad IN (:...antiguedad)', { + antiguedad: filtros.antiguedad + }) + .addSelect('equipo.antiguedad', 'antiguedad') + } + + return qb + .orderBy(`equipo.adscripcion`, "DESC") + .getRawMany() + } + + async tablaCount(filtros?: FiltrosGraficaDto) { + const qb = this.equipoRepository + .createQueryBuilder('equipo') + .select([]) + + qb.leftJoin('equipo.adscripcion', 'adscripcion') + qb.leftJoin('equipo.procesador', 'procesador') + qb.leftJoin('equipo.sistemaOperativo', 'sistemaOperativo') + qb.leftJoin('equipo.tipoUso', 'tipoUso') + + if (filtros?.adscripciones?.length) { + qb.andWhere('adscripcion.id_adscripcion IN (:...adscripciones)', { + adscripciones: filtros.adscripciones + }) + qb.addSelect('adscripcion.adscripcion', 'adscripcion') + } + + if (filtros?.procesadores?.length) { + qb.andWhere('procesador.id_procesador IN (:...procesadores)', { + procesadores: filtros.procesadores + }) + } + + if (filtros?.sistemas?.length) { + qb.andWhere('sistemaOperativo.id_sistema_operativo IN (:...sistemas)', { + sistemas: filtros.sistemas + }) + } + + if (filtros?.usos?.length) { + qb.andWhere('tipoUso.id_uso IN (:...usos)', { + usos: filtros.usos + }) + } + + if (filtros?.antiguedad?.length) { + qb.andWhere('equipo.antiguedad IN (:...antiguedad)', { + antiguedad: filtros.antiguedad + }) + } + + return qb + .addSelect('COUNT(*)', 'total') + .addGroupBy('adscripcion.adscripcion') + .orderBy(`equipo.adscripcion`, "DESC") + .getRawMany() + } + + async tablaExcel(filtros?: FiltrosGraficaDto) { + const qb = this.equipoRepository + .createQueryBuilder('equipo') + .select([]); + + qb.leftJoin('equipo.adscripcion', 'adscripcion'); + qb.leftJoin('equipo.procesador', 'procesador'); + qb.leftJoin('equipo.sistemaOperativo', 'sistemaOperativo'); + qb.leftJoin('equipo.tipoUso', 'tipoUso'); + + qb.addSelect('equipo.inventario', 'inventario'); + qb.addSelect('adscripcion.adscripcion', 'adscripcion'); + qb.addSelect('equipo.lugar', 'ubicacion') + + if (filtros?.adscripciones?.length) { + qb.andWhere('adscripcion.id_adscripcion IN (:...adscripciones)', { + adscripciones: filtros.adscripciones + }); + } + + if (filtros?.procesadores?.length) { + qb.andWhere('procesador.id_procesador IN (:...procesadores)', { + procesadores: filtros.procesadores + }) + .addSelect('procesador.procesador', 'procesador'); + } + + if (filtros?.sistemas?.length) { + qb.andWhere('sistemaOperativo.id_sistema_operativo IN (:...sistemas)', { + sistemas: filtros.sistemas + }) + .addSelect('sistemaOperativo.sistema_operativo', 'so'); + } + + if (filtros?.usos?.length) { + qb.andWhere('tipoUso.id_uso IN (:...usos)', { + usos: filtros.usos + }) + .addSelect('tipoUso.tipo_uso', 'uso'); + } + + if (filtros?.antiguedad?.length) { + qb.andWhere('equipo.antiguedad IN (:...antiguedad)', { + antiguedad: filtros.antiguedad + }) + .addSelect('equipo.antiguedad', 'antiguedad'); + } + + const data = await qb + .orderBy(`equipo.adscripcion`, "DESC") + .getRawMany(); + + const workbook = new ExcelJS.Workbook(); + const sheet = workbook.addWorksheet('Equipos'); + + const columns = [ + { header: 'Inventario', key: 'inventario', width: 20 }, + { header: 'Ubicacion', key: 'ubicacion', width: 40 }, + { header: 'Adscripción', key: 'adscripcion', width: 40 }, + ]; + + if (data[0]?.procesador !== undefined) { + columns.push({ header: 'Procesador', key: 'procesador', width: 25 }); + } + + if (data[0]?.so !== undefined) { + columns.push({ header: 'Sistema Operativo', key: 'so', width: 25 }); + } + + if (data[0]?.uso !== undefined) { + columns.push({ header: 'Uso', key: 'uso', width: 20 }); + } + + if (data[0]?.antiguedad !== undefined) { + columns.push({ header: 'Antigüedad', key: 'antiguedad', width: 15 }); + } + + sheet.columns = columns; + + data.forEach(row => { + sheet.addRow(row); + }); + + sheet.getRow(1).font = { bold: true }; + + return await workbook.xlsx.writeBuffer(); + } + + async tablaCountExcel(filtros?: FiltrosGraficaDto) { + const qb = this.equipoRepository + .createQueryBuilder('equipo') + .select([]); + + qb.leftJoin('equipo.adscripcion', 'adscripcion'); + qb.leftJoin('equipo.procesador', 'procesador'); + qb.leftJoin('equipo.sistemaOperativo', 'sistemaOperativo'); + qb.leftJoin('equipo.tipoUso', 'tipoUso'); + + if (filtros?.adscripciones?.length) { + qb.andWhere('adscripcion.id_adscripcion IN (:...adscripciones)', { + adscripciones: filtros.adscripciones + }); + qb.addSelect('adscripcion.adscripcion', 'adscripcion'); + } + + if (filtros?.procesadores?.length) { + qb.andWhere('procesador.id_procesador IN (:...procesadores)', { + procesadores: filtros.procesadores + }); + } + + if (filtros?.sistemas?.length) { + qb.andWhere('sistemaOperativo.id_sistema_operativo IN (:...sistemas)', { + sistemas: filtros.sistemas + }); + } + + if (filtros?.usos?.length) { + qb.andWhere('tipoUso.id_uso IN (:...usos)', { + usos: filtros.usos + }); + } + + if (filtros?.antiguedad?.length) { + qb.andWhere('equipo.antiguedad IN (:...antiguedad)', { + antiguedad: filtros.antiguedad + }); + } + + const data = await qb + .addSelect('COUNT(*)', 'total') + .addGroupBy('adscripcion.adscripcion') + .orderBy(`equipo.adscripcion`, "DESC") + .getRawMany(); + + const workbook = new ExcelJS.Workbook(); + const sheet = workbook.addWorksheet('Resumen'); + + sheet.columns = [ + { header: 'Adscripción', key: 'adscripcion', width: 30 }, + { header: 'Total', key: 'total', width: 15 }, + ]; + + data.forEach(row => { + sheet.addRow(row); + }); + + sheet.getRow(1).font = { bold: true }; + + return await workbook.xlsx.writeBuffer(); + } } let estado_excluidos = ['DE BAJA'];