result.component.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. import {
  2. Component,
  3. OnInit,
  4. Input,
  5. ViewChild,
  6. ComponentFactoryResolver
  7. } from "@angular/core";
  8. import { Router, ActivatedRoute } from "@angular/router";
  9. import { InvestmentProposalForm } from "@app/models/investment-proposal-form";
  10. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  11. import { Instrument } from "@app/models/instrument";
  12. import { InvestmentProposalWorkflowService } from "@app/services/investment-proposal-workflow.service";
  13. import { InstrumentDirective } from "../instrument/instrument.directive";
  14. import { InstrumentComponent } from "../instrument/instrument.component";
  15. import { InstrumentsService } from "@app/services/instruments.service";
  16. import { CatalogsService } from "@app/services/catalogs.service";
  17. import { InvestmentsService } from "@app/services/investments.service";
  18. import { AuthService } from "@app/services/auth2.service";
  19. import { JwtHelperService } from "@auth0/angular-jwt";
  20. import Swal from "sweetalert2";
  21. @Component({
  22. selector: "mt-wizard-result",
  23. templateUrl: "./result.component.html",
  24. styleUrls: ["./result.component.scss"]
  25. })
  26. export class ResultComponent implements OnInit {
  27. helper = new JwtHelperService();
  28. title = "Resumen de la propuesta";
  29. @Input() ads: Instrument[];
  30. @Input() formData: InvestmentProposalForm;
  31. @ViewChild(InstrumentDirective, { static: true })
  32. adHost: InstrumentDirective;
  33. isFormValid: boolean = false;
  34. general: any;
  35. instrument: any;
  36. complement: any;
  37. final: any;
  38. workType: string;
  39. form: any;
  40. currentAdIndex = -1;
  41. interval: any;
  42. indexDynamicComponent: number;
  43. countries: any;
  44. companies: any;
  45. rates: any;
  46. revenues: any;
  47. funds: any;
  48. instrumentTypes: any;
  49. markets: any;
  50. emitters: any;
  51. periodicities: any;
  52. operations: any;
  53. format_incomes: any;
  54. base_types: any;
  55. payment_terms: any;
  56. financials: any;
  57. inversion: {};
  58. investmentProposalID: string;
  59. investmentExists: boolean = false;
  60. reviewProposal: {};
  61. userRole: any;
  62. state: string = "";
  63. submitted: boolean = false;
  64. userList: any;
  65. test: any;
  66. constructor(
  67. private router: Router,
  68. private formDataService: FormInvestmentProposalService,
  69. private componentFactoryResolver: ComponentFactoryResolver,
  70. private instrumentService: InvestmentProposalWorkflowService,
  71. private loadInstrumentsService: InstrumentsService,
  72. private catalogService: CatalogsService,
  73. private investmentService: InvestmentsService,
  74. private route: ActivatedRoute,
  75. private authService: AuthService
  76. ) {
  77. this.formData = this.formDataService.getFormData();
  78. this.isFormValid = this.formDataService.isFormValid();
  79. this.ads = this.loadInstrumentsService.getInstruments();
  80. const decodedToken = this.helper.decodeToken(
  81. this.authService.getJwtToken()
  82. );
  83. this.userRole = decodedToken.groups;
  84. }
  85. ngOnInit() {
  86. this.route.params.subscribe(params => {
  87. this.investmentProposalID = params["id"];
  88. });
  89. if (this.investmentProposalID == undefined)
  90. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  91. if (this.investmentProposalID != undefined) {
  92. this.investmentExists = true;
  93. this.investmentService
  94. .getProposalInvestment(this.investmentProposalID)
  95. .subscribe(res => {
  96. this.state = res["result"]["id_estado_inversion"]["codigo"];
  97. });
  98. } else {
  99. this.investmentExists = false;
  100. }
  101. this.formData.instrumentos;
  102. this.indexDynamicComponent = this.ads.findIndex(
  103. x => x.data.key == this.formData.instrumentos
  104. );
  105. if (this.indexDynamicComponent >= 0) {
  106. this.loadComponent();
  107. } else {
  108. console.log("No existe el componente");
  109. }
  110. this.general = this.formDataService.getGeneralInfo();
  111. this.instrument = this.formDataService.getWork();
  112. this.complement = this.formDataService.getComplementInfo();
  113. this.catalogService.getFinancialEntities().subscribe(res => {
  114. this.financials = res.find(
  115. e => e.id_entidad_financiera == this.general.casa
  116. );
  117. this.financials =
  118. this.financials != undefined ? this.financials.nombre : "-";
  119. });
  120. this.catalogService.getBaseTypes().subscribe(res => {
  121. this.base_types = res.find(
  122. e => e.id_tipo_base == this.general.base_anual
  123. );
  124. this.base_types =
  125. this.base_types != undefined
  126. ? `${this.base_types.tipo_base_dias} / ${this.base_types.tipo_base}`
  127. : "-";
  128. });
  129. this.catalogService.getCountries().subscribe(res => {
  130. this.countries = res.find(e => e.id_pais == this.complement.pais);
  131. this.countries =
  132. this.countries != undefined ? this.countries.nombre : "-";
  133. });
  134. this.catalogService.getCompanies().subscribe(res => {
  135. this.companies = res.find(e => e.id_empresa == this.complement.empresa);
  136. this.companies =
  137. this.companies != undefined ? this.companies.nombre : "-";
  138. });
  139. this.catalogService.getRateTypes().subscribe(res => {
  140. this.rates = res.find(e => e.id_tipo_tasa == this.general.tipo_tasa);
  141. this.rates = this.rates != undefined ? this.rates.nombre : "-";
  142. });
  143. this.catalogService.getRevenueTypes().subscribe(res => {
  144. this.revenues = res.find(e => e.id_tipo_renta == this.general.tipo_renta);
  145. this.revenues = this.revenues != undefined ? this.revenues.nombre : "-";
  146. });
  147. this.catalogService.getFundsOrigins().subscribe(res => {
  148. this.funds = res.find(
  149. e => e.id_origen_fondo == this.general.origenes_fondo
  150. );
  151. this.funds = this.funds != undefined ? this.funds.nombre : "-";
  152. });
  153. this.catalogService.getInstrumentTypes().subscribe(res => {
  154. this.instrumentTypes = res.find(
  155. e => e.codigo == this.general.instrumentos
  156. );
  157. this.instrumentTypes =
  158. this.instrumentTypes != undefined ? this.instrumentTypes.nombre : "-";
  159. });
  160. this.catalogService.getMarketTypes().subscribe(res => {
  161. this.markets = res.find(
  162. e => e.id_tipo_mercado == this.complement.tipo_mercado
  163. );
  164. this.markets = this.markets != undefined ? this.markets.nombre : "-";
  165. });
  166. this.catalogService.getEmitterTypes().subscribe(res => {
  167. this.emitters = res.find(
  168. e => e.id_tipo_emisor == this.complement.emisores
  169. );
  170. this.emitters = this.emitters != undefined ? this.emitters.nombre : "-";
  171. });
  172. this.catalogService.getPeriodicities().subscribe(res => {
  173. this.periodicities = res.find(
  174. e => e.id_periodicidad == this.general.periodicidad
  175. );
  176. this.periodicities =
  177. this.periodicities != undefined ? this.periodicities.nombre : "-";
  178. });
  179. this.catalogService.getPaymentTerms().subscribe(res => {
  180. this.payment_terms = res.find(e => e.id_plazo == this.complement.plazo);
  181. this.payment_terms =
  182. this.payment_terms != undefined ? this.payment_terms.nombre : "-";
  183. });
  184. this.catalogService.getOperationTypes().subscribe(res => {
  185. this.operations = res.find(
  186. e => e.id_tipo_operacion == this.complement.operaciones
  187. );
  188. this.operations =
  189. this.operations != undefined ? this.operations.nombre : "-";
  190. });
  191. //getIncomeFormat
  192. this.catalogService.getIncomeFormat().subscribe(res => {
  193. this.format_incomes = res.find(
  194. e => e.id_formato_ingreso == this.general.formato_ingreso
  195. );
  196. this.format_incomes =
  197. this.format_incomes != undefined ? this.format_incomes.nombre : "-";
  198. });
  199. this.final = {};
  200. Object.assign(this.final, this.general, this.instrument, this.complement);
  201. this.inversion = {
  202. codigo_instrumento: this.general.instrumentos,
  203. info_inversion: {
  204. id_empresa: +this.complement.empresa,
  205. id_pais: +this.complement.pais,
  206. id_entidad: +this.general.casa,
  207. id_origen_fondo: +this.general.origenes_fondo,
  208. id_periodicidad: +this.general.periodicidad,
  209. id_plazo: +this.complement.plazo,
  210. id_tipo_base: +this.general.base_anual,
  211. id_tipo_emisor: +this.complement.emisores,
  212. id_tipo_mercado: this.complement.tipo_mercado,
  213. id_tipo_operacion: +this.complement.operaciones,
  214. //id_tipo_pago: 1,
  215. id_formato_ingreso: +this.general.formato_ingreso,
  216. id_tipo_renta: +this.general.tipo_renta,
  217. id_tipo_tasa: +this.general.tipo_tasa,
  218. nombre_inversion: this.general.name,
  219. asunto: this.general.asunto,
  220. comentario: this.complement.comentarios,
  221. justificacion: this.complement.justificacion
  222. },
  223. info_instrumento: this.instrument
  224. };
  225. if (
  226. (this.inversion["codigo_instrumento"] == "CETE" ||
  227. this.inversion["codigo_instrumento"] == "DAP" ||
  228. this.inversion["codigo_instrumento"] == "VCN" ||
  229. this.inversion["codigo_instrumento"] == "BONO" ||
  230. this.inversion["codigo_instrumento"] == "TIT" ||
  231. this.inversion["codigo_instrumento"] == "EURB" ||
  232. this.inversion["codigo_instrumento"] == "CINV" ||
  233. this.inversion["codigo_instrumento"] == "FINV" ||
  234. this.inversion["codigo_instrumento"] == "PBUR" ||
  235. this.inversion["codigo_instrumento"] == "PPER" ||
  236. this.inversion["codigo_instrumento"] == "PEMP") &&
  237. (this.instrument["proyecciones"] != undefined ||
  238. this.instrument["proyecciones"] != "")
  239. ) {
  240. this.inversion["proyecciones"] = this.instrument["proyecciones"].slice(
  241. 0,
  242. -1
  243. );
  244. }
  245. }
  246. loadComponent() {
  247. const adItem = this.ads[this.indexDynamicComponent];
  248. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  249. adItem.component
  250. );
  251. const viewContainerRef = this.adHost.viewContainerRef;
  252. viewContainerRef.clear();
  253. const componentRef = viewContainerRef.createComponent(componentFactory);
  254. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  255. (<InstrumentComponent>componentRef.instance).summary = true;
  256. }
  257. // Guardar propuesta de inversion (Crear)
  258. submit() {
  259. Swal.fire({
  260. allowOutsideClick: false,
  261. icon: "info",
  262. text: "Espere por favor..."
  263. });
  264. Swal.showLoading();
  265. if (this.investmentProposalID != undefined) {
  266. } else {
  267. this.investmentService.createProposalInvestment(this.inversion).subscribe(
  268. success => {
  269. if (success) {
  270. Swal.fire({
  271. allowOutsideClick: false,
  272. icon: "success",
  273. showCancelButton: false,
  274. title: "Exito",
  275. confirmButtonText: "El registro ha sido guardado"
  276. }).then(result => {
  277. if (result.value) {
  278. window.location.href = "#/investment-proposals";
  279. }
  280. });
  281. }
  282. },
  283. err => {
  284. Swal.fire({
  285. icon: "error",
  286. title: "Error al guardar",
  287. text: err.message
  288. });
  289. }
  290. );
  291. }
  292. }
  293. //Enviar a revision la propuesta de inversion
  294. sendToReview() {
  295. this.investmentService
  296. .getAvailableUsers(this.investmentProposalID)
  297. .subscribe(res => {
  298. this.userList = res["usuarios_next"];
  299. this.test = "<div>Notificar a:</div>";
  300. if (this.userList.length > 0) {
  301. for (let i = 0; i < this.userList.length; i++) {
  302. this.test += `<div class='form-check form-check-inline'><input type='checkbox' class='form-check-input' id='${this.userList[i]}' name='users' value='${this.userList[i]}'><label class='form-check-label' for='${this.userList[i]}'>${this.userList[i]}</label></div>`;
  303. }
  304. }
  305. });
  306. this.reviewProposal = undefined;
  307. (async () => {
  308. Swal.fire({
  309. title: `<h3>Enviar a revisión propuesta de inversión</h3>`,
  310. icon: "info",
  311. html: `<p style="text-align:left;">Comentario:</p>`,
  312. input: "textarea",
  313. showCancelButton: true,
  314. confirmButtonText: "Enviar propuesta",
  315. cancelButtonText: "Cancelar",
  316. showLoaderOnConfirm: true,
  317. inputValidator: value => {
  318. if (!value) {
  319. return "Debe ingresar un comentario";
  320. }
  321. },
  322. preConfirm: comentario => {
  323. this.reviewProposal = {
  324. id_inversion: this.investmentProposalID,
  325. step: "next",
  326. comentario: comentario
  327. };
  328. },
  329. allowOutsideClick: () => !Swal.isLoading()
  330. }).then(result => {
  331. if (result.dismiss) {
  332. return false;
  333. }
  334. Swal.fire({
  335. title: `<h3>Enviar a revisión propuesta de inversión</h3>`,
  336. icon: "info",
  337. html: `${this.test}`,
  338. confirmButtonText: "Siguiente",
  339. showCancelButton: true,
  340. cancelButtonText: "Cancelar",
  341. showLoaderOnConfirm: true,
  342. preConfirm: () => {
  343. let array = [];
  344. for (let i = 0; i < this.userList.length; i++) {
  345. let html_input: HTMLInputElement = document.getElementById(
  346. this.userList[i]
  347. ) as HTMLInputElement;
  348. let html_value: string = html_input.value;
  349. if (html_input.checked == true) {
  350. array.push(html_value);
  351. }
  352. }
  353. this.reviewProposal["notificar"] = array.toString();
  354. }
  355. }).then(result1 => {
  356. if (result1.dismiss) {
  357. return false;
  358. }
  359. Swal.fire({
  360. allowOutsideClick: false,
  361. title: "Espere por favor...",
  362. icon: "info"
  363. });
  364. Swal.showLoading();
  365. this.investmentService
  366. .sendReviewProposalInvestment(this.reviewProposal)
  367. .subscribe(
  368. success => {
  369. if (success) {
  370. Swal.fire({
  371. allowOutsideClick: false,
  372. icon: "success",
  373. showCancelButton: false,
  374. title: "Exito",
  375. confirmButtonText: "La propuesta ha sido enviada a revisión"
  376. }).then(result => {
  377. Swal.close();
  378. window.location.reload();
  379. });
  380. }
  381. },
  382. err => {
  383. if (err.code == 405) {
  384. Swal.fire({
  385. icon: "error",
  386. title: "Operacion no permitida",
  387. text: err.message
  388. }).then(result => {
  389. Swal.close();
  390. window.location.href = "#/investment-proposals";
  391. });
  392. } else {
  393. Swal.fire({
  394. icon: "error",
  395. title: "Error al guardar",
  396. text: err.message
  397. });
  398. }
  399. }
  400. );
  401. });
  402. //window.location.reload();
  403. });
  404. })();
  405. }
  406. // Actualizar la propuesta de inversion si se han realizado cambios
  407. update_proposal() {
  408. Swal.fire({
  409. allowOutsideClick: false,
  410. icon: "info",
  411. text: "Espere por favor..."
  412. });
  413. Swal.showLoading();
  414. this.investmentService
  415. .updateProposalInvestment(this.investmentProposalID, this.inversion)
  416. .subscribe(
  417. success => {
  418. if (success) {
  419. Swal.fire({
  420. allowOutsideClick: false,
  421. icon: "success",
  422. showCancelButton: false,
  423. title: "Exito",
  424. confirmButtonText: "El registro ha sido actualizado"
  425. }).then(result => {
  426. Swal.close();
  427. });
  428. }
  429. },
  430. err => {
  431. Swal.fire({
  432. icon: "error",
  433. title: "Error al guardar",
  434. text: err.message
  435. });
  436. }
  437. );
  438. }
  439. goToPrevious() {
  440. this.submitted = true;
  441. if (this.investmentProposalID != undefined) {
  442. this.router.navigate(["/investment-proposal/complement-info"], {
  443. queryParams: { id: this.investmentProposalID }
  444. });
  445. } else {
  446. this.router.navigate(["/investment-proposal/complement-info"]);
  447. }
  448. }
  449. //Enviar a revision la propuesta de inversion
  450. finishProposal() {
  451. this.investmentService
  452. .getAvailableUsers(this.investmentProposalID)
  453. .subscribe(res => {
  454. this.userList = res["usuarios_next"];
  455. this.test = "<div>Notificar a:</div>";
  456. if (this.userList.length > 0) {
  457. for (let i = 0; i < this.userList.length; i++) {
  458. this.test += `<div class='form-check form-check-inline'><input type='checkbox' class='form-check-input' id='${this.userList[i]}' name='users' value='${this.userList[i]}'><label class='form-check-label' for='${this.userList[i]}'>${this.userList[i]}</label></div>`;
  459. }
  460. }
  461. });
  462. this.reviewProposal = undefined;
  463. (async () => {
  464. Swal.fire({
  465. title: `<h3>Finalizar propuesta de inversión:</h3>`,
  466. icon: "info",
  467. input: "textarea",
  468. html: `<p style="text-align:left;">Comentario:</p>`,
  469. showCancelButton: true,
  470. confirmButtonText: "Siguiente",
  471. cancelButtonText: "Cancelar",
  472. inputValidator: value => {
  473. if (!value) {
  474. return "Debe ingresar un comentario";
  475. }
  476. },
  477. preConfirm: comentario => {
  478. this.reviewProposal = {
  479. id_inversion: this.investmentProposalID,
  480. step: "next",
  481. comentario: comentario
  482. };
  483. },
  484. allowOutsideClick: () => !Swal.isLoading()
  485. }).then(result => {
  486. if (result.dismiss) {
  487. return false;
  488. }
  489. Swal.fire({
  490. title: `<h3>Finalizar propuesta de inversión</h3>`,
  491. icon: "info",
  492. html: `${this.test}`,
  493. confirmButtonText: "Enviar a revision",
  494. showCancelButton: true,
  495. cancelButtonText: "Cancelar",
  496. showLoaderOnConfirm: true,
  497. preConfirm: () => {
  498. let array = [];
  499. for (let i = 0; i < this.userList.length; i++) {
  500. let html_input: HTMLInputElement = document.getElementById(
  501. this.userList[i]
  502. ) as HTMLInputElement;
  503. let html_value: string = html_input.value;
  504. if (html_input.checked == true) {
  505. array.push(html_value);
  506. }
  507. }
  508. this.reviewProposal["notificar"] = array.toString();
  509. }
  510. }).then(result1 => {
  511. if (result1.dismiss) {
  512. return false;
  513. }
  514. Swal.fire({
  515. allowOutsideClick: false,
  516. title: "Espere por favor...",
  517. icon: "info"
  518. });
  519. Swal.showLoading();
  520. this.investmentService
  521. .sendReviewProposalInvestment(this.reviewProposal)
  522. .subscribe(
  523. success => {
  524. if (success) {
  525. Swal.fire({
  526. allowOutsideClick: false,
  527. icon: "success",
  528. showCancelButton: false,
  529. title: "Exito",
  530. confirmButtonText: "La propuesta ha sido finalizada"
  531. }).then(result => {
  532. Swal.close();
  533. window.location.reload();
  534. });
  535. }
  536. },
  537. err => {
  538. if (err.code == 405) {
  539. Swal.fire({
  540. icon: "error",
  541. title: "Operacion no permitida",
  542. text: err.message
  543. }).then(result => {
  544. Swal.close();
  545. window.location.href = "#/investment-proposals";
  546. });
  547. } else {
  548. Swal.fire({
  549. icon: "error",
  550. title: "Error al guardar",
  551. text: err.message
  552. });
  553. }
  554. }
  555. );
  556. });
  557. //window.location.reload();
  558. });
  559. })();
  560. }
  561. // Verifica permisos para mostrar boton de edicion y/o envio a revision,
  562. // segun los permisos del usuario y el estado de la propuesta
  563. can_send_to_review(status: string) {
  564. if (status == "NUEVA" || status == "RECHA") {
  565. // TO DO ver que el codigo de los tipos de usuario
  566. return true;
  567. } else {
  568. return false;
  569. }
  570. }
  571. can_finish_proposal(status: string) {
  572. if (status == "LIQUI") {
  573. // TO DO ver que el codigo de los tipos de usuario
  574. return true;
  575. } else {
  576. return false;
  577. }
  578. }
  579. }