mesmre.component.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. import { SelectionModel } from '@angular/cdk/collections';
  2. import { Component, ViewChild, AfterViewInit } from '@angular/core';
  3. import { MatPaginator } from '@angular/material/paginator';
  4. import { MatTableDataSource } from '@angular/material/table';
  5. import { MatDialog } from '@angular/material/dialog';
  6. import { MatSnackBar } from '@angular/material/snack-bar';
  7. import { FormGroup, FormControl } from '@angular/forms';
  8. import { ExportExcelService } from 'src/app/services/excel/export-excel.service';
  9. import { MESMREService } from 'src/app/services/mes/mesmre/mesmre.service';
  10. import { MESMREInterface } from 'src/app/interfaces/mes/mesmre/mesmre-interface';
  11. import { MESMPRService } from 'src/app/services/mes/mesmpr/mesmpr.service';
  12. import { MesmprInterface } from 'src/app/interfaces/mes/mesmpr/mesmpr-interface';
  13. import { ENCService } from 'src/app/services/enc/enc.service';
  14. import { IAMService } from 'src/app/services/iam/iam.service';
  15. import { USERInterface } from 'src/app/interfaces/user-interface';
  16. import { ValidationsService } from 'src/app/services/validations.service';
  17. import { lastValueFrom } from 'rxjs';
  18. @Component({
  19. selector: 'app-mesmre',
  20. templateUrl: './mesmre.component.html',
  21. styleUrls: ['./mesmre.component.css'],
  22. })
  23. export class MESMREComponent implements AfterViewInit {
  24. isLoading: Boolean = true;
  25. isReloading: Boolean = false;
  26. isFilter: Boolean = false;
  27. isFilterNull: Boolean = true;
  28. isUser: Boolean = false;
  29. role = '';
  30. fecha_inicio = '';
  31. fecha_fin = '';
  32. //DATES
  33. options: any = {
  34. year: 'numeric',
  35. month: 'long',
  36. day: 'numeric',
  37. };
  38. rango = new FormGroup({
  39. fecha_inicio: new FormControl(),
  40. fecha_fin: new FormControl(),
  41. });
  42. //Historial
  43. solicitudes: MESMREInterface[] = [];
  44. //Todos
  45. solicitudes_todos: MESMREInterface[] = [];
  46. empleados: MESMREInterface[] = [];
  47. empleado: MESMREInterface = {} as MESMREInterface;
  48. //Periodos
  49. periodos: MesmprInterface[] = [];
  50. nombre_usuario: string = '';
  51. opcionesSolicitudes = [
  52. { valor: 0, tipo: 'Mi historial' },
  53. { valor: 1, tipo: 'Todos' },
  54. ];
  55. opcionSolicitud = this.opcionesSolicitudes[0].valor;
  56. tipoSolicitud = this.opcionesSolicitudes[0].tipo;
  57. opcionesEstatus = [
  58. { estatus: 'Aprobadas' },
  59. { estatus: 'Rechazadas' },
  60. { estatus: 'Pendientes' },
  61. { estatus: 'Canceladas' },
  62. ];
  63. opcionEstatus = null;
  64. opcionPeriodo = null;
  65. displayedColumns: string[] = [
  66. 'select',
  67. 'numero_solicitud',
  68. 'nombre_empleado',
  69. 'fecha_inicio',
  70. 'fecha_fin',
  71. 'numero_dias',
  72. 'estatus',
  73. 'periodo_vacacional',
  74. 'vacaciones_antes',
  75. 'vacaciones_despues',
  76. ];
  77. dataSource = new MatTableDataSource<any>(this.solicitudes);
  78. selection = new SelectionModel<MESMREInterface>(true, []);
  79. @ViewChild(MatPaginator) paginator!: MatPaginator;
  80. constructor(
  81. public dialog: MatDialog,
  82. private _snackBar: MatSnackBar,
  83. private _iamService: IAMService,
  84. private __mesmreService: MESMREService,
  85. private __mesmprService: MESMPRService,
  86. public excel: ExportExcelService,
  87. private _encService: ENCService,
  88. private _validationService: ValidationsService
  89. ) {
  90. let usuario: USERInterface = JSON.parse(
  91. localStorage.getItem('TIMUSERENC')!
  92. );
  93. this.nombre_usuario = usuario.NOMBRE;
  94. if (this._encService.desencriptar(JSON.parse(localStorage.getItem('TIMUSERENC')!).PERFIL) === '1') {
  95. this.role = 'Administrador';
  96. } else if (this._encService.desencriptar(JSON.parse(localStorage.getItem('TIMUSERENC')!).PERFIL) !== '1') {
  97. this.opcionesSolicitudes = this.opcionesSolicitudes.slice(0, 1);
  98. this.isUser = true;
  99. this.role = 'Usuario';
  100. }
  101. }
  102. ngAfterViewInit(): void {
  103. this.obtener();
  104. this.obtenerEmpleados();
  105. this.obtenerPeriodos(
  106. this._encService.desencriptar(
  107. this.__mesmreService.usuario_session.IDUSUARIO
  108. )
  109. );
  110. }
  111. async obtener() {
  112. await lastValueFrom(this.__mesmreService.consultar()).then(
  113. (res: any) => {
  114. if (res.status == 'Token is Expired') {
  115. this._iamService.logout();
  116. this._validationService.openSnackBar(
  117. 'Sesión expirada. Vuelva a iniciar sesión'
  118. );
  119. } else if (!res.status) {
  120. this.solicitudes = res.response;
  121. } else {
  122. this._validationService.openSnackBar(
  123. res.response.length > 0 ? res.msg : 'No hay datos para mostrar'
  124. );
  125. }
  126. this.isLoading = false;
  127. },
  128. (error) => {
  129. if (!error.ok) {
  130. this._validationService.openSnackBar('Ocurrió un error inesperado');
  131. }
  132. if (error.error.msg != undefined) {
  133. this._validationService.openSnackBar(error.error.msg);
  134. }
  135. if (error.status == 408) {
  136. this._validationService.openSnackBar('Conexion lenta');
  137. }
  138. }
  139. );
  140. this.cargarTabla(this.solicitudes);
  141. this.filtroEstatus();
  142. }
  143. async obtenerEmpleados() {
  144. if (this._encService.desencriptar(JSON.parse(localStorage.getItem('TIMUSERENC')!).PERFIL) === '1') {
  145. await lastValueFrom(this.__mesmreService.consultarTodos()).then(
  146. (res: any) => {
  147. if (res.error) {
  148. this._validationService.openSnackBar(
  149. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  150. );
  151. } else {
  152. this.empleados = res.response;
  153. }
  154. },
  155. (error: any) => {
  156. if (!error.ok) {
  157. this._validationService.openSnackBar('Ocurrió un error inesperado');
  158. }
  159. if (error.error.msg != undefined) {
  160. this._validationService.openSnackBar(error.error.msg);
  161. }
  162. if (error.status == 408) {
  163. this._validationService.openSnackBar('Conexion lenta');
  164. }
  165. }
  166. );
  167. this.empleados = [
  168. ...new Map(
  169. this.empleados.map((empleado) => [empleado.NOMBREEMPLEADO, empleado])
  170. ).values(),
  171. ];
  172. this.empleados = this.empleados.filter(
  173. (solicitud) =>
  174. !solicitud.NOMBREEMPLEADO.includes(
  175. this._encService.desencriptar(
  176. this.__mesmreService.usuario_session.IDUSUARIO
  177. )
  178. )
  179. );
  180. } else {
  181. await lastValueFrom(this.__mesmreService.consultarEmpleados()).then(
  182. (res: any) => {
  183. if (res.error) {
  184. this._validationService.openSnackBar(
  185. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  186. );
  187. } else {
  188. this.empleados = res.response;
  189. if (this.empleados.length > 0) {
  190. this.role = "Jefe Directo"
  191. }
  192. }
  193. },
  194. (error: any) => {
  195. if (!error.ok) {
  196. this._validationService.openSnackBar('Ocurrió un error inesperado');
  197. }
  198. if (error.error.msg != undefined) {
  199. this._validationService.openSnackBar(error.error.msg);
  200. }
  201. if (error.status == 408) {
  202. this._validationService.openSnackBar('Conexion lenta');
  203. }
  204. }
  205. );
  206. this.empleados = this.empleados.map(function(empleado) {
  207. return {NOMBREEMPLEADO: empleado.USUARIO}
  208. }) as MESMREInterface[];
  209. }
  210. }
  211. async obtenerPeriodos(usuario: any) {
  212. await lastValueFrom(this.__mesmprService.obtenerPeriodos(usuario)).then(
  213. (res: any) => {
  214. if (res.status == 'Token is Expired') {
  215. this._iamService.logout();
  216. this._validationService.openSnackBar(
  217. 'Sesión expirada. Vuelva a iniciar sesión'
  218. );
  219. } else if (!res.status && res.response.length > 0) {
  220. this.periodos = res.response;
  221. } else {
  222. if (res.msg !== 'EXITO') {
  223. this._validationService.openSnackBar(res.msg);
  224. }
  225. }
  226. this.isLoading = false;
  227. },
  228. (error) => {
  229. if (!error.ok) {
  230. this._validationService.openSnackBar('Ocurrió un error inesperado');
  231. }
  232. if (error.error.msg != undefined) {
  233. this._validationService.openSnackBar(error.error.msg);
  234. }
  235. if (error.status == 408) {
  236. this._validationService.openSnackBar('Conexion lenta');
  237. }
  238. }
  239. );
  240. this.periodos.map((periodo) => {
  241. periodo.PERIODO = this.formatoFechaPeriodo(periodo.PERIODO).join();
  242. });
  243. }
  244. async filtroSolicitudes(filtro: string) {
  245. if (this.role === 'Administrador' || this.role === 'Jefe Directo') {
  246. let aux_response: any;
  247. this.isReloading = true;
  248. this.tipoSolicitud = filtro;
  249. this.eliminarFiltros();
  250. switch (filtro) {
  251. case 'Mi historial':
  252. await lastValueFrom(this.__mesmreService.consultar()).then(
  253. (res: any) => {
  254. if (res.error) {
  255. this._validationService.openSnackBar(
  256. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  257. );
  258. } else {
  259. aux_response = res.response;
  260. }
  261. },
  262. (error) => {
  263. if (!error.ok) {
  264. this._validationService.openSnackBar(
  265. 'Ocurrió un error inesperado'
  266. );
  267. }
  268. if (error.error.msg != undefined) {
  269. this._validationService.openSnackBar(error.error.msg);
  270. }
  271. if (error.status == 408) {
  272. this._validationService.openSnackBar('Conexion lenta');
  273. }
  274. }
  275. );
  276. this.obtenerPeriodos(
  277. this._encService.desencriptar(
  278. this.__mesmreService.usuario_session.IDUSUARIO
  279. )
  280. );
  281. this.cargarTabla(aux_response);
  282. this.filtroEstatus();
  283. break;
  284. case 'Todos':
  285. await lastValueFrom(this.__mesmreService.consultarTodos()).then(
  286. (res: any) => {
  287. if (res.error) {
  288. this._validationService.openSnackBar(
  289. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  290. );
  291. } else {
  292. aux_response = res.response;
  293. }
  294. },
  295. (error) => {
  296. if (!error.ok) {
  297. this._validationService.openSnackBar(
  298. 'Ocurrió un error inesperado'
  299. );
  300. }
  301. if (error.error.msg != undefined) {
  302. this._validationService.openSnackBar(error.error.msg);
  303. }
  304. if (error.status == 408) {
  305. this._validationService.openSnackBar('Conexion lenta');
  306. }
  307. }
  308. );
  309. this.cargarTabla(aux_response);
  310. this.filtroEstatus();
  311. break;
  312. default:
  313. this._validationService.openSnackBar(
  314. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  315. );
  316. this.isReloading = false;
  317. break;
  318. }
  319. }
  320. }
  321. async filtroEmpleados(empleado: MESMREInterface) {
  322. //Se hace un split al primer parentesis, se obtiene la segunda posicion y se elimna el segundo parentesis
  323. if (
  324. empleado.NOMBREEMPLEADO.includes('(') &&
  325. empleado.NOMBREEMPLEADO.includes(')')
  326. ) {
  327. let usuario = empleado.NOMBREEMPLEADO.split('(')[1].replace(')', '');
  328. let aux_response: any;
  329. this.eliminarFiltros();
  330. await lastValueFrom(this.__mesmreService.consultarUsuario(usuario)).then(
  331. (res: any) => {
  332. if (res.error) {
  333. this._validationService.openSnackBar(
  334. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  335. );
  336. } else {
  337. this.tipoSolicitud = usuario;
  338. aux_response = res.response;
  339. }
  340. },
  341. (error) => {
  342. if (!error.ok) {
  343. this._validationService.openSnackBar('Ocurrió un error inesperado');
  344. }
  345. if (error.error.msg != undefined) {
  346. this._validationService.openSnackBar(error.error.msg);
  347. }
  348. if (error.status == 408) {
  349. this._validationService.openSnackBar('Conexion lenta');
  350. }
  351. }
  352. );
  353. this.obtenerPeriodos(usuario);
  354. this.cargarTabla(aux_response);
  355. this.filtroEstatus();
  356. } else {
  357. //No cargo los datos del id
  358. this._validationService.openSnackBar(
  359. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  360. );
  361. }
  362. }
  363. filtroEstatus() {
  364. let filtro_estatus = [
  365. ...new Map(
  366. this.solicitudes.map((solicitud) => [solicitud.ESTATUS, solicitud])
  367. ).values(),
  368. ];
  369. this.opcionesEstatus = [];
  370. filtro_estatus.forEach((estatus) => {
  371. this.opcionesEstatus.push({ estatus: estatus.ESTATUS });
  372. });
  373. }
  374. filtrosBusqueda() {
  375. if (this.solicitudes.length > 0) {
  376. let filtro_solicitudes: MESMREInterface[] = [];
  377. this.isFilter = true;
  378. //El filtro aplica a los rangos ?
  379. if (
  380. this.rango.value.fecha_inicio != null &&
  381. this.rango.value.fecha_fin != null
  382. ) {
  383. //Obtengo el tiempo transcurrido
  384. let rango_fecha_inicio = new Date(
  385. this.rango.value.fecha_inicio
  386. ).getTime();
  387. let rango_fecha_fin = new Date(this.rango.value.fecha_fin).getTime();
  388. //Las 2 fechas del rango son menores a la fecha inicio ? true
  389. //Las 2 fechas del rango son mayores a la fecha fin ? true
  390. //Si las fechas del rango no son menores ni mayores entonces son *false* las condiciones,
  391. //Se debe de enviar verdadero si no excede este rango !false
  392. filtro_solicitudes = this.solicitudes.filter(
  393. (solicitud) =>
  394. !(
  395. rango_fecha_inicio <
  396. Date.parse(
  397. this.formatoFecha(solicitud.FECHAINICIO).split('-0').join('-')
  398. ) &&
  399. rango_fecha_fin <
  400. Date.parse(
  401. this.formatoFecha(solicitud.FECHAINICIO).split('-0').join('-')
  402. )
  403. ) &&
  404. !(
  405. rango_fecha_inicio >
  406. Date.parse(
  407. this.formatoFecha(solicitud.FECHAFIN).split('-0').join('-')
  408. ) &&
  409. rango_fecha_fin >
  410. Date.parse(
  411. this.formatoFecha(solicitud.FECHAFIN).split('-0').join('-')
  412. )
  413. )
  414. );
  415. }
  416. //FILTROS
  417. //TODOS LOS FILTROS FUERON SOLICITADOS
  418. if (
  419. this.rango.value.fecha_inicio != null &&
  420. this.rango.value.fecha_fin != null &&
  421. this.opcionEstatus != null &&
  422. this.opcionPeriodo != null
  423. ) {
  424. filtro_solicitudes = filtro_solicitudes.filter(
  425. (solicitud) =>
  426. solicitud.ESTATUS == this.opcionEstatus &&
  427. solicitud.PERIODOVACACIONAL == this.opcionPeriodo
  428. );
  429. //RANGO FECHAS Y ESTATUS
  430. } else if (
  431. this.rango.value.fecha_inicio != null &&
  432. this.rango.value.fecha_fin != null &&
  433. this.opcionEstatus != null
  434. ) {
  435. filtro_solicitudes = filtro_solicitudes.filter(
  436. (solicitud) => solicitud.ESTATUS == this.opcionEstatus
  437. );
  438. //RANGO FECHAS Y PERIODO
  439. } else if (
  440. this.rango.value.fecha_inicio != null &&
  441. this.rango.value.fecha_fin != null &&
  442. this.opcionPeriodo != null
  443. ) {
  444. filtro_solicitudes = filtro_solicitudes.filter(
  445. (solicitud) => solicitud.PERIODOVACACIONAL == this.opcionPeriodo
  446. );
  447. //ESTATUS Y PERIODO
  448. } else if (this.opcionEstatus != null && this.opcionPeriodo != null) {
  449. filtro_solicitudes = this.solicitudes.filter(
  450. (solicitud) =>
  451. solicitud.ESTATUS == this.opcionEstatus &&
  452. solicitud.PERIODOVACACIONAL == this.opcionPeriodo
  453. );
  454. //ESTATUS
  455. } else if (this.opcionEstatus != null) {
  456. filtro_solicitudes = this.solicitudes.filter(
  457. (solicitud) => solicitud.ESTATUS == this.opcionEstatus
  458. );
  459. //PERIODO
  460. } else if (this.opcionPeriodo != null) {
  461. filtro_solicitudes = this.solicitudes.filter(
  462. (solicitud) => solicitud.PERIODOVACACIONAL == this.opcionPeriodo
  463. );
  464. }
  465. this.dataSource.data = filtro_solicitudes;
  466. this.selection = new SelectionModel<MESMREInterface>(
  467. true,
  468. filtro_solicitudes
  469. );
  470. this.isReloading = false;
  471. }
  472. }
  473. eliminarFiltrosGetData() {
  474. this.isFilter = false;
  475. this.isFilterNull = true;
  476. if (
  477. this.rango.value.fecha_inicio != null ||
  478. this.rango.value.fecha_fin != null ||
  479. this.opcionesEstatus != null ||
  480. this.opcionPeriodo != null
  481. ) {
  482. if (
  483. this.rango.value.fecha_inicio != null ||
  484. this.rango.value.fecha_fin != null
  485. ) {
  486. this.rango = new FormGroup({
  487. fecha_inicio: new FormControl(),
  488. fecha_fin: new FormControl(),
  489. });
  490. }
  491. if (this.opcionEstatus != null) {
  492. this.opcionEstatus = null;
  493. }
  494. if (this.opcionPeriodo != null) {
  495. this.opcionPeriodo = null;
  496. }
  497. switch (this.opcionSolicitud) {
  498. case 0:
  499. this.__mesmreService.consultar().subscribe(
  500. (res: any) => {
  501. if (res.error) {
  502. this._validationService.openSnackBar(
  503. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  504. );
  505. } else {
  506. this.cargarTabla(res.response);
  507. }
  508. },
  509. (error) => {
  510. if (!error.ok) {
  511. this._validationService.openSnackBar(
  512. 'Ocurrió un error inesperado'
  513. );
  514. }
  515. if (error.error.msg != undefined) {
  516. this._validationService.openSnackBar(error.error.msg);
  517. }
  518. if (error.status == 408) {
  519. this._validationService.openSnackBar('Conexion lenta');
  520. }
  521. }
  522. );
  523. break;
  524. case 1:
  525. this.__mesmreService.consultarTodos().subscribe(
  526. (res: any) => {
  527. if (res.error) {
  528. this._validationService.openSnackBar(
  529. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  530. );
  531. } else {
  532. this.cargarTabla(res.response);
  533. }
  534. },
  535. (error: any) => {
  536. if (!error.ok) {
  537. this._validationService.openSnackBar(
  538. 'Ocurrió un error inesperado'
  539. );
  540. }
  541. if (error.error.msg != undefined) {
  542. this._validationService.openSnackBar(error.error.msg);
  543. }
  544. if (error.status == 408) {
  545. this._validationService.openSnackBar('Conexion lenta');
  546. }
  547. }
  548. );
  549. break;
  550. case 2:
  551. this.__mesmreService.consultarUsuario(this.tipoSolicitud).subscribe(
  552. (res: any) => {
  553. if (res.error) {
  554. this._validationService.openSnackBar(
  555. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  556. );
  557. } else {
  558. this.cargarTabla(res.response);
  559. }
  560. },
  561. (error) => {
  562. if (!error.ok) {
  563. this._validationService.openSnackBar(
  564. 'Ocurrió un error inesperado'
  565. );
  566. }
  567. if (error.error.msg != undefined) {
  568. this._validationService.openSnackBar(error.error.msg);
  569. }
  570. if (error.status == 408) {
  571. this._validationService.openSnackBar('Conexion lenta');
  572. }
  573. }
  574. );
  575. break;
  576. default:
  577. this._validationService.openSnackBar(
  578. 'Ocurrió un error inesperado, favor de intentarlo más tarde'
  579. );
  580. break;
  581. }
  582. }
  583. }
  584. eliminarFiltros() {
  585. this.isFilter = false;
  586. this.isFilterNull = true;
  587. if (
  588. this.rango.value.fecha_inicio != null ||
  589. this.rango.value.fecha_fin != null ||
  590. this.opcionesEstatus != null ||
  591. this.opcionPeriodo != null
  592. ) {
  593. if (
  594. this.rango.value.fecha_inicio != null ||
  595. this.rango.value.fecha_fin != null
  596. ) {
  597. this.rango = new FormGroup({
  598. fecha_inicio: new FormControl(),
  599. fecha_fin: new FormControl(),
  600. });
  601. }
  602. if (this.opcionEstatus != null) {
  603. this.opcionEstatus = null;
  604. }
  605. if (this.opcionPeriodo != null) {
  606. this.opcionPeriodo = null;
  607. }
  608. }
  609. }
  610. validarOpcionesFiltro() {
  611. //Saber si los filtros estan nulos
  612. if (
  613. (this.rango.value.fecha_inicio != null &&
  614. this.rango.value.fecha_fin != null) ||
  615. this.opcionEstatus != null ||
  616. this.opcionPeriodo != null
  617. ) {
  618. this.isFilterNull = false;
  619. } else {
  620. this.isFilterNull = true;
  621. }
  622. }
  623. cargarTabla(solicitudes: MESMREInterface[]) {
  624. this.isLoading = false;
  625. this.solicitudes = solicitudes;
  626. this.dataSource = new MatTableDataSource<MESMREInterface>(this.solicitudes);
  627. this.selection = new SelectionModel<MESMREInterface>(true, solicitudes);
  628. this.dataSource.filterPredicate = function (
  629. data: MESMREInterface,
  630. filter: string
  631. ): boolean {
  632. return (
  633. data.IDSOLICITUD.toString().toLowerCase().includes(filter) ||
  634. data.NOMBREEMPLEADO.toLowerCase().includes(filter) ||
  635. data.FECHAINICIO.toString().toLowerCase().includes(filter) ||
  636. data.FECHAFIN.toString().toLowerCase().includes(filter) ||
  637. data.NUMERODIAS.toString().toLowerCase().includes(filter) ||
  638. data.ESTATUS.toLowerCase().includes(filter) ||
  639. data.PERIODOVACACIONAL.toString().toLowerCase().includes(filter) ||
  640. data.VACACIONESANTES.toString().toLowerCase().includes(filter) ||
  641. data.VACACIONESDESPUES.toString().toLowerCase().includes(filter)
  642. );
  643. };
  644. this.dataSource.paginator = this.paginator;
  645. this.paginator._intl.itemsPerPageLabel = 'Datos por página';
  646. this.paginator._intl.firstPageLabel = 'Primera Página';
  647. this.paginator._intl.lastPageLabel = 'Últmina Página';
  648. this.paginator._intl.nextPageLabel = 'Siguiente Página';
  649. this.paginator._intl.previousPageLabel = 'Anterior Página';
  650. this.mapearTabla();
  651. }
  652. mapearTabla() {
  653. this.solicitudes.map((solicitud: MESMREInterface) => {
  654. if (solicitud.PERIODOVACACIONAL == null) {
  655. solicitud.PERIODOVACACIONAL = 'N/A';
  656. } else {
  657. solicitud.PERIODOVACACIONAL = this.formatoFechaPeriodo(
  658. solicitud.PERIODOVACACIONAL
  659. ).join();
  660. }
  661. if (solicitud.VACACIONESANTES == null) {
  662. solicitud.VACACIONESANTES = 0;
  663. }
  664. if (solicitud.VACACIONESDESPUES == null) {
  665. solicitud.VACACIONESDESPUES = 0;
  666. }
  667. solicitud.FECHAINICIO = this.formatoFecha(solicitud.FECHAINICIO);
  668. solicitud.FECHAFIN = this.formatoFecha(solicitud.FECHAFIN);
  669. solicitud.NUMERODIAS =
  670. solicitud.VACACIONESANTES - solicitud.VACACIONESDESPUES;
  671. return {
  672. IDSOLICITUD: solicitud.IDSOLICITUD,
  673. NOMBREEMPLEADO: solicitud.NOMBREEMPLEADO,
  674. FECHAINICIO: solicitud.FECHAINICIO,
  675. FECHAFIN: solicitud.FECHAFIN,
  676. NUMERODIAS: solicitud.NUMERODIAS,
  677. ESTATUS: solicitud.ESTATUS,
  678. PERIODOVACACIONAL: solicitud.PERIODOVACACIONAL,
  679. VACACIONESANTES: solicitud.VACACIONESANTES,
  680. VACACIONESDESPUES: solicitud.VACACIONESDESPUES,
  681. };
  682. });
  683. }
  684. applyFilter(filterValue: any) {
  685. this.dataSource.filter = filterValue.target.value.trim().toLowerCase();
  686. }
  687. isAllSelected() {
  688. const numSelected = this.selection.selected.length;
  689. const numRows = this.dataSource.data.length;
  690. return numSelected === numRows;
  691. }
  692. masterToggle() {
  693. if (this.isAllSelected()) {
  694. this.selection.clear();
  695. return;
  696. }
  697. this.selection.select(...this.dataSource.data);
  698. }
  699. checkboxLabel(row?: MESMREInterface): string {
  700. if (!row) {
  701. return `${this.isAllSelected() ? 'deselect' : 'select'} all`;
  702. }
  703. return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row`;
  704. }
  705. generarExcel(): void {
  706. if (this.selection.selected.length > 0) {
  707. //let hoy = new Date(2022,1,1,1,1,1);
  708. let hoy = new Date();
  709. let anio = hoy.getFullYear();
  710. let mes = hoy.getMonth() + 1;
  711. let dia = hoy.getDate();
  712. let hora = hoy.getHours();
  713. let minuto = hoy.getMinutes();
  714. let segundos = hoy.getSeconds();
  715. let titulo = this.tipoSolicitud;
  716. let filename =
  717. 'reporte_' +
  718. this.tipoSolicitud.toLowerCase() +
  719. '_' +
  720. anio +
  721. (mes < 10 ? '0' : '') +
  722. mes +
  723. (dia < 10 ? '0' : '') +
  724. dia +
  725. '_' +
  726. (hora < 10 ? '0' : '') +
  727. hora +
  728. (minuto < 10 ? '0' : '') +
  729. minuto +
  730. (segundos < 10 ? '0' : '') +
  731. segundos;
  732. this.excel.generarExcel(this.selection.selected, filename, titulo);
  733. } else {
  734. this._validationService.openSnackBar(
  735. 'Para generar el Excel se debe seleccionar al menos una fila'
  736. );
  737. }
  738. }
  739. formatoFecha(fecha: string): string {
  740. let split = fecha.split('-');
  741. return `${split[2]}-${split[1]}-${split[0]}`;
  742. }
  743. formatoFechaPeriodo(item: any) {
  744. let arrPeriodoVacacional = [];
  745. let fechaPeriodo = item.split('|');
  746. arrPeriodoVacacional.push(
  747. 'De ' +
  748. this.formatoFecha(fechaPeriodo[0]) +
  749. ' a ' +
  750. this.formatoFecha(fechaPeriodo[1])
  751. );
  752. return arrPeriodoVacacional;
  753. }
  754. }