investment-proposals.component.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { Component, ViewChild, OnInit } from "@angular/core";
  2. import { MatPaginator } from "@angular/material/paginator";
  3. import { MatSort } from "@angular/material/sort";
  4. import { MatTableDataSource } from "@angular/material/table";
  5. import Swal from "sweetalert2";
  6. import { CatalogsService } from "src/app/services/catalogs.service";
  7. import { InvestmentsService } from "@app/services/investments.service";
  8. import { AuthService } from "@app/services/auth2.service";
  9. import { JwtHelperService } from "@auth0/angular-jwt";
  10. import { InvestmentProposal } from "@app/models/investment-proposal";
  11. import { from } from "rxjs";
  12. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  13. import { Router } from "@angular/router";
  14. @Component({
  15. selector: "app-investment-proposals",
  16. templateUrl: "./investment-proposals.component.html",
  17. styleUrls: ["./investment-proposals.component.scss"]
  18. })
  19. export class InvestmentProposalsComponent implements OnInit {
  20. helper = new JwtHelperService();
  21. title: string = "Propuestas de inversión";
  22. displayedColumns: string[] = [
  23. "codigo_inversion",
  24. "asunto",
  25. "id_tipo_mercado",
  26. "id_inversion_instrumento",
  27. "id_estado_inversion",
  28. "id"
  29. ];
  30. //displayedColumns: string[] = ['state'];
  31. listProposals: InvestmentProposal[];
  32. dataSource = new MatTableDataSource(this.listProposals);
  33. resultsLength = 0;
  34. isLoadingResults = true;
  35. isRateLimitReached = false;
  36. userRole: any;
  37. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  38. @ViewChild(MatSort, { static: true }) sort: MatSort;
  39. role_number: any;
  40. constructor(
  41. private catalogService: CatalogsService,
  42. private investmentsService: InvestmentsService,
  43. private authService: AuthService,
  44. private formInvestmentProposal: FormInvestmentProposalService,
  45. private router: Router
  46. ) {
  47. const decodedToken = this.helper.decodeToken(
  48. this.authService.getJwtToken()
  49. );
  50. this.userRole = decodedToken.groups;
  51. this.dataSource.filterPredicate = (data, filter) => {
  52. const dataStr =
  53. data.id_inversion_instrumento.id_tipo_instrumento.nombre.toLowerCase() +
  54. data.id_estado_inversion.nombre.toLowerCase() +
  55. data.codigo_inversion.toLowerCase() +
  56. data.nombre_inversion.toLowerCase() +
  57. data.asunto.toLowerCase() +
  58. data.comentario.toLowerCase() +
  59. data.justificacion.toLowerCase();
  60. return dataStr.indexOf(filter) != -1;
  61. };
  62. Swal.fire({
  63. allowOutsideClick: false,
  64. icon: "info",
  65. text: "Espere por favor..."
  66. });
  67. Swal.showLoading();
  68. }
  69. ngOnInit() {
  70. this.investmentsService.getProposalInvestmentsList().subscribe(
  71. ans => {
  72. this.listProposals = ans.result;
  73. if (this.userType(this.userRole, "analistas")) {
  74. this.listProposals;
  75. } else if (this.userType(this.userRole, "contabilidad")) {
  76. this.listProposals = this.listProposals.filter(proposals =>
  77. ["COMPR", "LIQUI"].includes(
  78. proposals["id_estado_inversion"]["codigo"]
  79. )
  80. );
  81. } else if (this.userType(this.userRole, "autorizadores")) {
  82. this.listProposals = this.listProposals.filter(proposals =>
  83. ["REVIS", "APROB", "FINAL"].includes(
  84. proposals["id_estado_inversion"]["codigo"]
  85. )
  86. );
  87. } else if (this.userType(this.userRole, "supervisores")) {
  88. this.listProposals = this.listProposals.filter(proposals =>
  89. ["PENDI", "REVIS", "FINAL"].includes(
  90. proposals["id_estado_inversion"]["codigo"]
  91. )
  92. );
  93. }
  94. this.dataSource.data = this.listProposals;
  95. this.dataSource.paginator = this.paginator;
  96. this.dataSource.sort = this.sort;
  97. },
  98. err => {
  99. Swal.fire({
  100. icon: "error",
  101. title: "Error en el servidor",
  102. text: err.message
  103. });
  104. }
  105. );
  106. setTimeout(() => {
  107. Swal.close();
  108. }, 1200);
  109. }
  110. applyFilter(event: Event) {
  111. const filterValue = (event.target as HTMLInputElement).value.toLowerCase();
  112. this.dataSource.filter = filterValue;
  113. if (this.dataSource.paginator) {
  114. this.dataSource.paginator.firstPage();
  115. }
  116. }
  117. view_investment_proposal(id: string) {
  118. this.formInvestmentProposal.resetFormData();
  119. Swal.fire({
  120. allowOutsideClick: false,
  121. icon: "info",
  122. text: "Espere por favor..."
  123. });
  124. Swal.showLoading();
  125. setTimeout(() => {
  126. this.router.navigate([`/investment-proposal/${id}`]);
  127. }, 1000);
  128. }
  129. modify_investment_proposal(id: string) {
  130. this.formInvestmentProposal.resetFormData();
  131. Swal.fire({
  132. allowOutsideClick: false,
  133. icon: "info",
  134. text: "Espere por favor..."
  135. });
  136. Swal.showLoading();
  137. setTimeout(() => {
  138. this.router.navigate(["/investment-proposal/general-info"], {
  139. queryParams: { id: id }
  140. });
  141. }, 1000);
  142. }
  143. create_investment_proposal() {
  144. this.formInvestmentProposal.resetFormData();
  145. Swal.fire({
  146. allowOutsideClick: false,
  147. icon: "info",
  148. text: "Espere por favor..."
  149. });
  150. Swal.showLoading();
  151. setTimeout(() => {
  152. this.router.navigate(["/investment-proposal/general-info"], {});
  153. }, 1000);
  154. }
  155. // Verifica permisos para mostrar boton de edicion y/o envio a revision,
  156. // segun los permisos del usuario y el estado de la propuesta
  157. can_modify_or_send_to_review(status: string) {
  158. if (status == "NUEVA" && (this.userRole.length == 0 || this.userRole)) {
  159. // TO DO ver que el codigo de los tipos de usuario
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. }
  165. can_review(status: string) {
  166. if (status == "PENDI" && (this.userRole.length == 0 || this.userRole)) {
  167. // TO DO ver que el codigo de los tipos de usuario
  168. return true;
  169. } else {
  170. return false;
  171. }
  172. }
  173. can_approve(status: string) {
  174. if (status == "REVIS" && (this.userRole.length == 0 || this.userRole)) {
  175. // TO DO ver que el codigo de los tipos de usuario
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. }
  181. can_write_payment_info(status: string) {
  182. if (status == "APROB" && (this.userRole.length == 0 || this.userRole)) {
  183. // TO DO ver que el codigo de los tipos de usuario
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. can_upload_payment(status: string) {
  190. if (status == "COMPR" && (this.userRole.length == 0 || this.userRole)) {
  191. // TO DO ver que el codigo de los tipos de usuario
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. can_finish_proposal(status: string) {
  198. if (status == "LIQUI" && (this.userRole.length == 0 || this.userRole)) {
  199. // TO DO ver que el codigo de los tipos de usuario
  200. return true;
  201. } else {
  202. return false;
  203. }
  204. }
  205. sendToReview(investmentProposalID: number, investmentCode: string) {
  206. async () => {
  207. const { value: comentario } = await Swal.fire({
  208. title: `<h3>Enviar a revisión propuesta de inversión ${investmentCode}</h3>`,
  209. icon: "info",
  210. html: `<p style="text-align:left;">Comentario:</p>`,
  211. input: "textarea",
  212. showCancelButton: true,
  213. confirmButtonText: "Enviar propuesta",
  214. cancelButtonText: "Cancelar"
  215. // inputValidator: value => {
  216. // if (!value) {
  217. // return "Debe ingresar un comentario";
  218. // }
  219. // }
  220. });
  221. //if (comentario) {
  222. let reviewProposal = {
  223. id_inversion: investmentProposalID,
  224. step: "next",
  225. comentario: comentario
  226. };
  227. this.investmentsService
  228. .sendReviewProposalInvestment(reviewProposal)
  229. .subscribe(
  230. success => {
  231. if (success) {
  232. Swal.fire({
  233. allowOutsideClick: false,
  234. icon: "success",
  235. showCancelButton: false,
  236. title: "Exito",
  237. confirmButtonText: "La propuesta ha sido enviada a revisión"
  238. }).then(result => {
  239. Swal.close();
  240. window.location.reload();
  241. });
  242. }
  243. },
  244. err => {
  245. Swal.fire({
  246. icon: "error",
  247. title: "Error al guardar",
  248. text: err.message
  249. });
  250. }
  251. );
  252. };
  253. }
  254. //Enviar a revision la propuesta de inversion
  255. finishProposal(investmentProposalID: number, investmentCode: string) {
  256. (async () => {
  257. const { value: comentario } = await Swal.fire({
  258. title: `<h3>Finalizar propuesta de inversión: ${investmentCode}</h3>`,
  259. icon: "info",
  260. input: "textarea",
  261. html: `<p style="text-align:left;">Comentario:</p>`,
  262. showCancelButton: true,
  263. confirmButtonText: "Finalizar",
  264. cancelButtonText: "Cancelar"
  265. });
  266. //if (comentario) {
  267. let reviewProposal = {
  268. id_inversion: investmentProposalID,
  269. step: "next",
  270. comentario: comentario
  271. };
  272. this.investmentsService
  273. .sendReviewProposalInvestment(reviewProposal)
  274. .subscribe(
  275. success => {
  276. if (success) {
  277. Swal.fire({
  278. allowOutsideClick: false,
  279. icon: "success",
  280. showCancelButton: false,
  281. title: "Exito",
  282. confirmButtonText: "La propuesta ha sido finalizada"
  283. }).then(result => {
  284. Swal.close();
  285. window.location.href = "#/investment-proposals";
  286. });
  287. }
  288. },
  289. err => {
  290. Swal.fire({
  291. icon: "error",
  292. title: "Error al guardar",
  293. text: err.message
  294. });
  295. }
  296. );
  297. //}
  298. })();
  299. }
  300. userType(userRole: any, requiredRole: any) {
  301. if (userRole.length == 0) {
  302. return true;
  303. }
  304. return userRole.includes(requiredRole);
  305. }
  306. }