employee.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { HttpErrorResponse } from '@angular/common/http';
  2. import { Component, OnInit, ViewChild } from '@angular/core';
  3. import { MatDialog } from '@angular/material/dialog';
  4. import { MatPaginator } from '@angular/material/paginator';
  5. import { MatSort } from '@angular/material/sort';
  6. import { MatTableDataSource } from '@angular/material/table';
  7. import { MatTabGroup } from '@angular/material/tabs';
  8. import { Router } from '@angular/router';
  9. import { lastValueFrom } from 'rxjs';
  10. import { EncService } from 'src/app/services/enc/enc.service';
  11. import { EmployeeService } from 'src/app/services/personal-management/employee.service';
  12. import { ResourcesService } from 'src/app/services/resources/resources.service';
  13. import { AlertComponent } from '../../resources/dialogs/alert/alert.component';
  14. import { EmployeesListItem, EmployeesListResponse } from 'src/app/interfaces/personal-managment/employee.interface';
  15. import { EmployeeDetailsDialogComponent } from './employee-details-dialog/employee-details-dialog.component';
  16. import { DocumentsEmployeeComponent } from './documents-employee/documents-employee.component';
  17. import { ContractsBySubcontratists, ContractsBySubcontratistsResponse } from 'src/app/interfaces/personal-managment/subcontratists.interface';
  18. import { ContractsHistoryEmployeeComponent } from './contracts-history-employee/contracts-history-employee.component';
  19. @Component({
  20. selector: 'app-employee',
  21. templateUrl: './employee.component.html',
  22. styleUrls: ['./employee.component.css']
  23. })
  24. export class EmployeeComponent implements OnInit {
  25. public screenMode: string;
  26. public isLoading: boolean;
  27. public hasError: boolean;
  28. public errorStr: string;
  29. public isLoadingForm: boolean;
  30. public changeOnTab: boolean = false;
  31. public btnSmall: boolean;
  32. public cardSmall: boolean;
  33. // Para Empleados
  34. public txtBuscadorEmployees: string;
  35. public displayedColumnsEmployees: Array<string>;
  36. public dataSourceEmployees: MatTableDataSource<EmployeesListItem>;
  37. // Para Contratos
  38. public txtBuscadorContracts: string;
  39. public displayedColumnsContracts: Array<string>;
  40. public dataSourceContracts: MatTableDataSource<ContractsBySubcontratists>;
  41. @ViewChild(MatPaginator) paginator!: MatPaginator;
  42. @ViewChild(MatSort) sort!: MatSort;
  43. @ViewChild('MatPaginator2') paginator2!: MatPaginator;
  44. @ViewChild('MatSort2') sort2!: MatSort;
  45. @ViewChild('TabGroup') tabGroup!: MatTabGroup;
  46. constructor(
  47. private _employeeService: EmployeeService,
  48. public resourcesService: ResourcesService,
  49. private _dialog: MatDialog,
  50. private _router: Router,
  51. private _encService: EncService
  52. ) {
  53. this.screenMode = 'employees';
  54. this.isLoading = true;
  55. this.hasError = false;
  56. this.errorStr = "";
  57. this.isLoadingForm = false;
  58. this.btnSmall = false;
  59. this.cardSmall = false;
  60. this.txtBuscadorEmployees = '';
  61. this.displayedColumnsEmployees = [
  62. 'NAME',
  63. 'CONTRACT_TYPE',
  64. 'SPECIALITY',
  65. 'WORKTEAMS',
  66. 'STATUS',
  67. 'ACCIONES']
  68. this.dataSourceEmployees = new MatTableDataSource<any>();
  69. this.txtBuscadorContracts = '';
  70. this.displayedColumnsContracts = [
  71. 'NAME',
  72. 'CONTRACTS_COUNT',
  73. 'ACCIONES']
  74. this.dataSourceContracts = new MatTableDataSource<any>();
  75. }
  76. ngAfterViewInit() {
  77. this.dataSourceEmployees.paginator = this.paginator;
  78. this.dataSourceEmployees.sort = this.sort;
  79. this.dataSourceContracts.paginator = this.paginator2;
  80. this.dataSourceContracts.sort = this.sort2;
  81. }
  82. ngOnInit(): void {
  83. let routeStr = this._router.url;
  84. let routeArr = routeStr.split('/').reverse();
  85. if(routeArr[0] == 'GEPE'){
  86. this.screenMode = 'employees';
  87. this.getEmployees();
  88. }else{
  89. this.screenMode = 'contracts';
  90. this.getContractsByEmployees();
  91. }
  92. this.onResize();
  93. }
  94. public tabChange(tab: any) {
  95. if (tab.index == 0) {
  96. this.changeOnTab = false;
  97. } else {
  98. this.changeOnTab = true;
  99. }
  100. }
  101. public async openDialog(typeForm: string, element?: any) {
  102. if (typeForm === 'REG') {
  103. const id = await this._encService.encrypt("0");
  104. this._router.navigate(['sam/GPRS/GEPE/GEPE/employee/form/' + id + '/2']);
  105. } else if (typeForm === 'UPD') {
  106. this._router.navigate(['sam/GPRS/GEPE/GEPE/employee/form/' + element!.ID_EMPLOYEE + '/1']);
  107. } else if (typeForm === 'HIS') {
  108. this._dialog.open(ContractsHistoryEmployeeComponent, {
  109. width: '640px',
  110. data: element
  111. });
  112. /*const id = await this._encService.encrypt(element.ID_EMPLOYEE);
  113. this._router.navigate(['sam/GPRS/GEPE/GEPE/employee/contracts-history/' + id]);*/
  114. } else if (typeForm === 'TAH') {
  115. this._router.navigate([`/sam/GPRS/GEPE/GEPE/tasa-horaria/${element.ID_EMPLOYEE}`]);
  116. } else if (typeForm === 'CRA') {
  117. this._router.navigate([`/sam/GPRS/GEPE/GEPE/cronograma-actividades/${element.ID_EMPLOYEE}`]);
  118. } else if (typeForm === 'DOC') {
  119. let employeeNameEnc = await this._encService.encrypt(element!.NAME);
  120. this._dialog.open(DocumentsEmployeeComponent, {
  121. width: '580px',
  122. data: {
  123. idEmployee: element!.ID_EMPLOYEE,
  124. employeeName: employeeNameEnc
  125. }
  126. });
  127. } else if (typeForm === 'ORD') {
  128. let nameEnc = await this._encService.encrypt(element.NAME);
  129. this._router.navigate([`sam/GPRS/GEPE/GEPE/employee/orders/${element.ID_EMPLOYEE}/${nameEnc}`]);
  130. } else if (typeForm === 'DET') {
  131. let employeeNameEnc = await this._encService.encrypt(element!.NAME);
  132. this._dialog.open(EmployeeDetailsDialogComponent, {
  133. width: '640px',
  134. data: {
  135. idEmployee: element!.ID_EMPLOYEE,
  136. employeeName: employeeNameEnc
  137. }
  138. });
  139. } else if (typeForm === 'DEL') {
  140. this._dialog.open(AlertComponent, {
  141. data: {
  142. title: 'Eliminación de Empleado',
  143. description: '¿Está seguro que desea eliminar al empleado?',
  144. icon: 'warning',
  145. },
  146. disableClose: true,
  147. }).afterClosed().subscribe((result) => {
  148. if (result) {
  149. this.deleteEmployee(`${element!.ID_EMPLOYEE}`);
  150. }
  151. });
  152. } else if (typeForm === 'ACT') {
  153. this._dialog.open(AlertComponent, {
  154. data: {
  155. title: 'Activación de Empleado',
  156. description: '¿Está seguro que desea activar al empleado?',
  157. icon: 'warning',
  158. },
  159. disableClose: true,
  160. }).afterClosed().subscribe((result) => {
  161. if (result) {
  162. //this.activateEmployee(element!.ID_EMPLOYEE);
  163. }
  164. });
  165. }
  166. }
  167. public async getEmployees() {
  168. try{
  169. this.isLoading = true;
  170. this.hasError = false;
  171. this.errorStr = "";
  172. let idUser = localStorage.getItem('idusuario')!;
  173. let employees: EmployeesListResponse = await lastValueFrom(this._employeeService.getConsultOfEmployees(idUser, 1));
  174. this.hasError = employees.error;
  175. this.errorStr = employees.msg;
  176. if(!this.hasError){
  177. let employeesArr: EmployeesListItem[] = [];
  178. for(const employee of employees.response){
  179. let idEmployeeDec = await this._encService.decrypt(employee.ID_EMPLOYEE);
  180. if(employee.TEAM_ID != null){
  181. console.log(employee.TEAM_ID);
  182. let idTeamDec = await this._encService.decrypt(employee.TEAM_ID);
  183. employee.TEAM_NAME = `${employee.TEAM_NAME} (${idTeamDec})`;
  184. }
  185. employee.NAME = `${employee.NAME} (${idEmployeeDec})`;
  186. employeesArr.push(employee);
  187. }
  188. this.dataSourceEmployees = new MatTableDataSource(employeesArr);
  189. this.dataSourceEmployees.paginator = this.paginator!;
  190. this.dataSourceEmployees.sort = this.sort!;
  191. }
  192. this.isLoading = false;
  193. }catch(error: any){
  194. if(error.error == undefined){
  195. this.errorStr = 'Ocurrió un error inesperado (1).';
  196. }else if(error.error.msg == undefined){
  197. this.errorStr = 'Ocurrió un error inesperado (2).';
  198. }else{
  199. this.errorStr = error.error.msg;
  200. }
  201. this.hasError = true;
  202. this.isLoading = false;
  203. }
  204. }
  205. private async deleteEmployee(employeeId: string) {
  206. try{
  207. let idUser = localStorage.getItem('idusuario')!;
  208. let formData = new FormData();
  209. formData.append('id_user', idUser);
  210. formData.append('linea', '1');
  211. formData.append('id_employee', employeeId);
  212. await lastValueFrom(this._employeeService.updateToInactiveStatus(formData));
  213. this.resourcesService.openSnackBar('El usuario se eliminó correctamente.');
  214. this.getEmployees();
  215. }catch(error: any){
  216. if(error.error == undefined){
  217. this.resourcesService.openSnackBar('Ocurrió un error inesperado.');
  218. }else if(error.error.msg == undefined){
  219. this.resourcesService.openSnackBar('Ocurrió un error inesperado.');
  220. }else{
  221. this.resourcesService.openSnackBar(error.error.msg);
  222. }
  223. }
  224. }
  225. public async getContractsByEmployees() {
  226. try{
  227. let idUser = localStorage.getItem('idusuario')!;
  228. const line: number = this.resourcesService.getLineNumber();
  229. let contracts: ContractsBySubcontratistsResponse = await lastValueFrom(this._employeeService.getContractsOfEveryEmployee(idUser, line));
  230. this.hasError = contracts.error;
  231. this.errorStr = contracts.msg;
  232. if(!this.hasError){
  233. this.dataSourceContracts = new MatTableDataSource(contracts.response);
  234. this.dataSourceContracts.paginator = this.paginator2;
  235. this.dataSourceContracts.sort = this.sort2;
  236. }
  237. this.isLoading = false;
  238. }catch(error: any){
  239. if(error.error == undefined){
  240. this.errorStr = 'Ocurrió un error inesperado.';
  241. }else if(error.error.msg == undefined){
  242. this.errorStr = 'Ocurrió un error inesperado.';
  243. }else{
  244. this.errorStr = error.error.msg;
  245. }
  246. this.hasError = true;
  247. this.isLoading = false;
  248. }
  249. /*this.isLoading = true;
  250. await lastValueFrom(this._employeeService.getContractsOfEveryEmployee()).then(
  251. (responseData: any) => {
  252. this.dataSourceContracts.data = responseData.response;
  253. }, (error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
  254. );
  255. this.isLoading = false;*/
  256. }
  257. // Método de filtrado de datos
  258. public applyFilterEmployees(event: any, type: string): void {
  259. let filterValue: string;
  260. if (type == 'INP') {
  261. filterValue = (event.target as HTMLInputElement).value;
  262. } else {
  263. this.txtBuscadorEmployees = event;
  264. filterValue = event;
  265. }
  266. this.dataSourceEmployees.filter = filterValue.trim().toLowerCase();
  267. if (this.dataSourceEmployees.paginator) {
  268. this.dataSourceEmployees.paginator.firstPage();
  269. }
  270. }
  271. // Método de filtrado de datos
  272. public applyFilterContracts(event: any, type: string): void {
  273. let filterValue: string;
  274. if (type == 'INP') {
  275. filterValue = (event.target as HTMLInputElement).value;
  276. } else {
  277. this.txtBuscadorContracts = event;
  278. filterValue = event;
  279. }
  280. this.dataSourceContracts.filter = filterValue.trim().toLowerCase();
  281. if (this.dataSourceContracts.paginator) {
  282. this.dataSourceContracts.paginator.firstPage();
  283. }
  284. }
  285. // Método para reajustar los botones según sus dimensiones
  286. public onResize(): void {
  287. this.btnSmall = window.innerWidth <= 1825;
  288. this.cardSmall = window.innerWidth >= 722;
  289. }
  290. }