mesmhs.component.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core';
  2. import { MatPaginator } from '@angular/material/paginator';
  3. import { MatTableDataSource } from '@angular/material/table';
  4. import { Router } from '@angular/router';
  5. import { MatDialog } from '@angular/material/dialog';
  6. import { MatSnackBar } from '@angular/material/snack-bar';
  7. import { AlertaComponent } from '../../resources/dialogos/alerta/alerta.component';
  8. import { MESMHSService } from '../../../services/mes/mesmhs/mesmhs.service';
  9. import { MESMHSFORMComponent } from './mesmhs-form/mesmhs-form.component';
  10. import { MESMHSFORMDAYSComponent } from './mesmhs-form-days/mesmhs-form-days.component';
  11. import { IAMService } from '../../../services/iam/iam.service';
  12. import { USERInterface } from '../../../interfaces/user-interface';
  13. import { ENCService } from 'src/app/services/enc/enc.service';
  14. import { MCOMAUService } from 'src/app/services/mco/mcomau/mcomau.service';
  15. @Component({
  16. selector: 'app-mesmhs',
  17. templateUrl: './mesmhs.component.html',
  18. styleUrls: ['./mesmhs.component.css'],
  19. })
  20. export class MESMHSComponent implements AfterViewInit {
  21. loading: Boolean = true;
  22. disabled = false;
  23. usuario_session: USERInterface = {} as USERInterface;
  24. hide: boolean = false;
  25. solicitudes: any[] = [];
  26. historial_admin: any[] = [];
  27. solicitud: any;
  28. select_value: string = '';
  29. data_empty = false;
  30. displayedColumns: string[] = [];
  31. dataSource = new MatTableDataSource<any>(this.solicitudes);
  32. subordinados: any;
  33. isAdmin = false;
  34. today = new Date();
  35. @ViewChild(MatPaginator) paginator!: MatPaginator;
  36. constructor(
  37. public dialog: MatDialog,
  38. private _mesmhsService: MESMHSService,
  39. private _encService: ENCService,
  40. private _snackBar: MatSnackBar,
  41. private _mcomauService: MCOMAUService,
  42. private _iamService: IAMService
  43. ) {
  44. this.usuario_session = JSON.parse(localStorage.getItem('TIMUSERENC')!);
  45. this.isAdmin = this._encService.desencriptar(JSON.parse(localStorage.getItem('TIMUSERENC')!).PERFIL) == 'Administrador';
  46. if (this.isAdmin) {
  47. this.displayedColumns = [
  48. 'no',
  49. 'fecha_inicial',
  50. 'fecha_final',
  51. 'estatus',
  52. 'periodovacacional',
  53. 'id_usuario',
  54. 'agregarFecha',
  55. 'detalles'
  56. ]
  57. } else {
  58. this.displayedColumns = [
  59. 'no',
  60. 'fecha_inicial',
  61. 'fecha_final',
  62. 'estatus',
  63. 'periodovacacional',
  64. 'id_usuario',
  65. 'detalles'
  66. ]
  67. }
  68. }
  69. ngAfterViewInit(): void {
  70. this.obtenerHistorialPorNumero(
  71. this._encService.desencriptar(this.usuario_session.IDUSUARIO)
  72. );
  73. if (this.isAdmin) {
  74. this.cargarSubordinadosAdmin();
  75. } else {
  76. this.cargarSubordiandos();
  77. }
  78. }
  79. OnInit() {
  80. this.dataSource.data = [];
  81. }
  82. applyFilter(filterValue: any) {
  83. this.dataSource.filter = filterValue.target.value.trim().toLowerCase();
  84. }
  85. obtenerHistorialPorNumero(numero_empleado: string) {
  86. this.loading = true;
  87. this._mesmhsService.obtenerHistorial(numero_empleado).subscribe(
  88. (res) => {
  89. if (res.status == 'Token is Expired') {
  90. this._iamService.logout();
  91. this.snackAlert('Sesión expirada. Vuelva a iniciar sesión');
  92. } else if (!res.status && res.response.length > 0) {
  93. this.loading = false;
  94. this.dataSource.data = res.response;
  95. this.historial_admin = res.response;
  96. this.cargarTabla(res.response);
  97. } else {
  98. this.solicitudes = [];
  99. this.snackAlert( res.response.length > 0 ? res.msg : 'No hay datos para mostrar');
  100. this.data_empty = true;
  101. }
  102. this.loading = false;
  103. },
  104. (error) => {
  105. if (!error.ok) {
  106. this.snackAlert('Ocurrió un error inesperado');
  107. }
  108. if (error.error.msg != undefined) {
  109. this.snackAlert(error.error.msg);
  110. }
  111. if (error.status == 408) {
  112. this.snackAlert('Conexion lenta');
  113. }
  114. }
  115. );
  116. }
  117. obtenerHistorialSubordinado(numero_empleado: string) {
  118. this._mesmhsService.obtenerHistorial(numero_empleado).subscribe(
  119. (res) => {
  120. if (res.status == 'Token is Expired') {
  121. this._iamService.logout();
  122. this.snackAlert('Sesión expirada. Vuelva a iniciar sesión');
  123. } else if (!res.status && res.response.length > 0) {
  124. let usuarios_repetidos_removidos = res.response.filter(
  125. (element: any) => element.IDUSUARIO == numero_empleado
  126. );
  127. this.loading = false;
  128. this.dataSource.data = usuarios_repetidos_removidos;
  129. this.data_empty = false;
  130. this.cargarTabla(usuarios_repetidos_removidos);
  131. } else {
  132. this.solicitudes = [];
  133. this.snackAlert( res.response.length > 0 ? res.msg : 'No hay datos para mostrar');
  134. this.data_empty = true;
  135. }
  136. this.loading = false;
  137. },
  138. (error) => {
  139. if (!error.ok) {
  140. this.snackAlert('Ocurrió un error inesperado');
  141. }
  142. if (error.error.msg != undefined) {
  143. this.snackAlert(error.error.msg);
  144. }
  145. if (error.status == 408) {
  146. this.snackAlert('Conexion lenta');
  147. }
  148. }
  149. );
  150. }
  151. obtenerMiHistorial() {
  152. this.loading = true;
  153. this._mesmhsService.obtenerHistorial(this._encService.desencriptar(this.usuario_session.IDUSUARIO)).subscribe(
  154. (res) => {
  155. if (res.status == 'Token is Expired') {
  156. this._iamService.logout();
  157. this.snackAlert('Sesión expirada. Vuelva a iniciar sesión');
  158. } else if (!res.status && res.response.length > 0) {
  159. let mi_historial = res.response.filter(
  160. (element: any) =>
  161. element.IDUSUARIO ==
  162. this._encService.desencriptar(this.usuario_session.IDUSUARIO)
  163. );
  164. this.dataSource.data = mi_historial;
  165. this.cargarTabla(mi_historial);
  166. } else {
  167. this.snackAlert( res.response.length > 0 ? res.msg : 'No hay datos para mostrar');
  168. this.data_empty = true;
  169. }
  170. this.loading = false;
  171. },
  172. (error) => {
  173. if (!error.ok) {
  174. this.snackAlert('Ocurrió un error inesperado');
  175. }
  176. if (error.error.msg != undefined) {
  177. this.snackAlert(error.error.msg);
  178. }
  179. if (error.status == 408) {
  180. this.snackAlert('Conexion lenta');
  181. }
  182. }
  183. );
  184. }
  185. obtenerTodosHistorial() {
  186. this.loading = true;
  187. this._mesmhsService.obtenerHistorial( this._encService.desencriptar(this.usuario_session.IDUSUARIO)
  188. )
  189. .subscribe(
  190. (res) => {
  191. if (res.status == 'Token is Expired') {
  192. this._iamService.logout();
  193. this.snackAlert('Sesión expirada. Vuelva a iniciar sesión');
  194. } else if (!res.status && res.response.length > 0) {
  195. this.dataSource.data = res.response;
  196. this.data_empty = false;
  197. console.log(this.dataSource.data);
  198. this.cargarTabla(res.response);
  199. } else {
  200. this.snackAlert( res.response.length > 0 ? res.msg : 'No hay datos para mostrar');
  201. this.data_empty = true;
  202. }
  203. this.loading = false;
  204. },
  205. (error) => {
  206. if (!error.ok) {
  207. this.snackAlert('Ocurrió un error inesperado');
  208. }
  209. if (error.error.msg != undefined) {
  210. this.snackAlert(error.error.msg);
  211. }
  212. if (error.status == 408) {
  213. this.snackAlert('Conexion lenta');
  214. }
  215. }
  216. );
  217. }
  218. private async cargarSubordinadosAdmin() {
  219. const sleep = (ms: number) =>
  220. new Promise((resolve) => setTimeout(resolve, ms));
  221. await sleep(1000);
  222. let usuarios = this.historial_admin;
  223. var hash: any = {};
  224. usuarios = usuarios.filter(function (current) {
  225. var exists = !hash[current.IDUSUARIO];
  226. hash[current.IDUSUARIO] = true;
  227. return exists;
  228. });
  229. let arr_user: any[] = [];
  230. usuarios.forEach((element) => {
  231. if (
  232. element.IDUSUARIO !=
  233. this._encService.desencriptar(this.usuario_session.IDUSUARIO)
  234. ) {
  235. arr_user.push(element);
  236. }
  237. });
  238. this.subordinados = arr_user;
  239. }
  240. private cargarSubordiandos() {
  241. this._mesmhsService.obtenerSubordinados( this._encService.desencriptar(this.usuario_session.IDUSUARIO) ).subscribe(
  242. (res) => {
  243. if (res.status == 'Token is Expired') {
  244. this._iamService.logout();
  245. this.snackAlert('Sesión expirada. Vuelva a iniciar sesión');
  246. } else if (!res.status) {
  247. this.subordinados = res;
  248. } else {
  249. this.snackAlert( res.response.length > 0 ? res.msg : 'No hay datos para mostrar');
  250. this.data_empty = true;
  251. }
  252. },
  253. (error) => {
  254. if (!error.ok) {
  255. this.snackAlert('Ocurrió un error inesperado');
  256. }
  257. if (error.error.msg != undefined) {
  258. this.snackAlert(error.error.msg);
  259. }
  260. if (error.status == 408) {
  261. this.snackAlert('Conexion lenta');
  262. }
  263. }
  264. );
  265. }
  266. private formato(fecha: string) {
  267. let fechaAux = fecha.split('-');
  268. return `${fechaAux[2]}-${fechaAux[1]}-${fechaAux[0]}`;
  269. }
  270. private formatDate(date: Date) {
  271. var d = new Date(date),
  272. month = '' + (d.getMonth() + 1),
  273. day = '' + d.getDate(),
  274. year = d.getFullYear();
  275. if (month.length < 2) month = '0' + month;
  276. if (day.length < 2) day = '0' + day;
  277. return [year, month, day].join('-');
  278. }
  279. private cargarTabla(solicitudes:any) {
  280. this.solicitudes = solicitudes;
  281. this.dataSource = new MatTableDataSource<any>(this.solicitudes);
  282. this.dataSource.filterPredicate = function (data, filter: string): boolean {
  283. return (
  284. data.FECHAINICIAL.toString().toLowerCase().includes(filter) ||
  285. data.FECHAFINAL.toString().toLowerCase().includes(filter) ||
  286. data.ESTATUS.toLowerCase().includes(filter) ||
  287. data.PERIODOVACACIONAL.toLowerCase().includes(filter) ||
  288. data.IDUSUARIO.toLowerCase().includes(filter)
  289. );
  290. };
  291. this.dataSource.paginator = this.paginator;
  292. this.paginator._intl.itemsPerPageLabel = 'Datos por página';
  293. this.paginator._intl.firstPageLabel = 'Primera Página';
  294. this.paginator._intl.lastPageLabel = 'Últmina Página';
  295. this.paginator._intl.nextPageLabel = 'Siguiente Página';
  296. this.paginator._intl.previousPageLabel = 'Anterior Página';
  297. this.mapearTabla();
  298. }
  299. private mapearTabla() {
  300. this.solicitudes.map( (
  301. solicitud: any
  302. ) => {
  303. if (solicitud.PERIODOVACACIONAL == null) {
  304. solicitud.PERIODOVACACIONAL = 'N/A';
  305. } else {
  306. solicitud.PERIODOVACACIONAL = this.formatoFechaPeriodo(solicitud.PERIODOVACACIONAL);
  307. }
  308. solicitud.FECHAINICIAL = this.formato(solicitud.FECHAINICIAL);
  309. solicitud.FECHAFINAL = this.formato(solicitud.FECHAFINAL);
  310. return solicitud;
  311. });
  312. }
  313. validatedDateNow(element:any) {
  314. let finalDate = this.getDateWithString(this.dateFormat(element.FECHAFINAL));
  315. if (element.ESTATUS !== 'Aprobado' && finalDate.getTime() > this.today.getTime()) {
  316. return false;
  317. }
  318. return true;
  319. }
  320. openDialogForm(item: any) {
  321. let dataAction = {
  322. action: 'Detalles solicitud',
  323. item: item,
  324. };
  325. this.dialog.open(MESMHSFORMComponent, {
  326. data: dataAction,
  327. });
  328. }
  329. openDialogAddDays(item: any) {
  330. let dataAction = {
  331. action: 'Detalles solicitud',
  332. item: item,
  333. };
  334. this.dialog.open(MESMHSFORMDAYSComponent, {
  335. data: dataAction,
  336. });
  337. }
  338. private getDateWithString(fecha: string) {
  339. let fechaAux = fecha.split('-');
  340. return new Date(parseInt(fechaAux[0]), parseInt(fechaAux[1]) - 1, parseInt(fechaAux[2]));
  341. }
  342. private dateFormat(fecha: string) {
  343. let fechaAux = fecha.split('-');
  344. return `${fechaAux[2]}-${fechaAux[1]}-${fechaAux[0]}`;
  345. }
  346. obtenerIDUsuario(item: any) {
  347. let arr1 = item.split('(');
  348. let IDUsuario1 = arr1[1].split(')');
  349. return IDUsuario1[0];
  350. }
  351. formatoFechaPeriodo(item: any) {
  352. let arrPeriodoVacacional = [];
  353. let fechaPeriodo = item.split('|');
  354. arrPeriodoVacacional.push(
  355. 'De ' +
  356. this.formato(fechaPeriodo[0]) +
  357. ' a ' +
  358. this.formato(fechaPeriodo[1])
  359. );
  360. return arrPeriodoVacacional;
  361. }
  362. obtenerDetalleSolicitud(item: any) {
  363. this._mesmhsService.obtenerDetalleSolicitud(item.IDSOLICITUD).subscribe((res) => {
  364. this.solicitud = res;
  365. this.openDialogForm(this.solicitud);
  366. }, error => {
  367. if (!error.ok) {
  368. this.snackAlert('Ocurrió un error inesperado');
  369. }
  370. if (error.error.msg != undefined) {
  371. this.snackAlert(error.error.msg);
  372. }
  373. if (error.status == 408) {
  374. this.snackAlert('Conexion lenta');
  375. }
  376. });
  377. }
  378. private snackAlert(mensaje: string) {
  379. this._snackBar.open(mensaje, 'Cerrar', {
  380. duration: 4000,
  381. });
  382. }
  383. }