result.component.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. console.log(this.instrument);
  114. this.catalogService.getFinancialEntities().subscribe(res => {
  115. this.financials = res.find(
  116. e => e.id_entidad_financiera == this.general.casa
  117. );
  118. this.financials =
  119. this.financials != undefined ? this.financials.nombre : "-";
  120. });
  121. this.catalogService.getBaseTypes().subscribe(res => {
  122. this.base_types = res.find(
  123. e => e.id_tipo_base == this.general.base_anual
  124. );
  125. this.base_types =
  126. this.base_types != undefined
  127. ? `${this.base_types.tipo_base_dias} / ${this.base_types.tipo_base}`
  128. : "-";
  129. });
  130. this.catalogService.getCountries().subscribe(res => {
  131. this.countries = res.find(e => e.id_pais == this.complement.pais);
  132. this.countries =
  133. this.countries != undefined ? this.countries.nombre : "-";
  134. });
  135. this.catalogService.getCompanies().subscribe(res => {
  136. this.companies = res.find(e => e.id_empresa == this.complement.empresa);
  137. this.companies =
  138. this.companies != undefined ? this.companies.nombre : "-";
  139. });
  140. this.catalogService.getRateTypes().subscribe(res => {
  141. this.rates = res.find(e => e.id_tipo_tasa == this.general.tipo_tasa);
  142. this.rates = this.rates != undefined ? this.rates.nombre : "-";
  143. });
  144. this.catalogService.getRevenueTypes().subscribe(res => {
  145. this.revenues = res.find(e => e.id_tipo_renta == this.general.tipo_renta);
  146. this.revenues = this.revenues != undefined ? this.revenues.nombre : "-";
  147. });
  148. this.catalogService.getFundsOrigins().subscribe(res => {
  149. this.funds = res.find(
  150. e => e.id_origen_fondo == this.general.origenes_fondo
  151. );
  152. this.funds = this.funds != undefined ? this.funds.nombre : "-";
  153. });
  154. this.catalogService.getInstrumentTypes().subscribe(res => {
  155. this.instrumentTypes = res.find(
  156. e => e.codigo == this.general.instrumentos
  157. );
  158. this.instrumentTypes =
  159. this.instrumentTypes != undefined ? this.instrumentTypes.nombre : "-";
  160. });
  161. this.catalogService.getMarketTypes().subscribe(res => {
  162. this.markets = res.find(
  163. e => e.id_tipo_mercado == this.complement.tipo_mercado
  164. );
  165. this.markets = this.markets != undefined ? this.markets.nombre : "-";
  166. });
  167. this.catalogService.getEmitterTypes().subscribe(res => {
  168. this.emitters = res.find(
  169. e => e.id_tipo_emisor == this.complement.emisores
  170. );
  171. this.emitters = this.emitters != undefined ? this.emitters.nombre : "-";
  172. });
  173. this.catalogService.getPeriodicities().subscribe(res => {
  174. this.periodicities = res.find(
  175. e => e.id_periodicidad == this.general.periodicidad
  176. );
  177. this.periodicities =
  178. this.periodicities != undefined ? this.periodicities.nombre : "-";
  179. });
  180. this.catalogService.getPaymentTerms().subscribe(res => {
  181. this.payment_terms = res.find(e => e.id_plazo == this.complement.plazo);
  182. this.payment_terms =
  183. this.payment_terms != undefined ? this.payment_terms.nombre : "-";
  184. });
  185. this.catalogService.getOperationTypes().subscribe(res => {
  186. this.operations = res.find(
  187. e => e.id_tipo_operacion == this.complement.operaciones
  188. );
  189. this.operations =
  190. this.operations != undefined ? this.operations.nombre : "-";
  191. });
  192. //getIncomeFormat
  193. this.catalogService.getIncomeFormat().subscribe(res => {
  194. this.format_incomes = res.find(
  195. e => e.id_formato_ingreso == this.general.formato_ingreso
  196. );
  197. this.format_incomes =
  198. this.format_incomes != undefined ? this.format_incomes.nombre : "-";
  199. });
  200. this.final = {};
  201. Object.assign(this.final, this.general, this.instrument, this.complement);
  202. this.inversion = {
  203. codigo_instrumento: this.general.instrumentos,
  204. info_inversion: {
  205. id_empresa: +this.complement.empresa,
  206. id_pais: +this.complement.pais,
  207. id_entidad: +this.general.casa,
  208. id_origen_fondo: +this.general.origenes_fondo,
  209. id_periodicidad: +this.general.periodicidad,
  210. id_plazo: +this.complement.plazo,
  211. id_tipo_base: +this.general.base_anual,
  212. id_tipo_emisor: +this.complement.emisores,
  213. id_tipo_mercado: this.complement.tipo_mercado,
  214. id_tipo_operacion: +this.complement.operaciones,
  215. //id_tipo_pago: 1,
  216. id_formato_ingreso: +this.general.formato_ingreso,
  217. id_tipo_renta: +this.general.tipo_renta,
  218. id_tipo_tasa: +this.general.tipo_tasa,
  219. nombre_inversion: this.general.name,
  220. asunto: this.general.asunto,
  221. comentario: this.complement.comentarios,
  222. justificacion: this.complement.justificacion
  223. },
  224. info_instrumento: this.instrument
  225. };
  226. if (
  227. (this.inversion["codigo_instrumento"] == "CETE" ||
  228. this.inversion["codigo_instrumento"] == "DAP" ||
  229. this.inversion["codigo_instrumento"] == "VCN" ||
  230. this.inversion["codigo_instrumento"] == "BONO" ||
  231. this.inversion["codigo_instrumento"] == "TIT" ||
  232. this.inversion["codigo_instrumento"] == "EURB" ||
  233. this.inversion["codigo_instrumento"] == "CINV" ||
  234. this.inversion["codigo_instrumento"] == "FINV" ||
  235. this.inversion["codigo_instrumento"] == "PBUR") &&
  236. (this.instrument["proyecciones"] != undefined ||
  237. this.instrument["proyecciones"] != "")
  238. ) {
  239. this.inversion["proyecciones"] = this.instrument["proyecciones"];
  240. }
  241. }
  242. loadComponent() {
  243. const adItem = this.ads[this.indexDynamicComponent];
  244. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  245. adItem.component
  246. );
  247. const viewContainerRef = this.adHost.viewContainerRef;
  248. viewContainerRef.clear();
  249. const componentRef = viewContainerRef.createComponent(componentFactory);
  250. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  251. (<InstrumentComponent>componentRef.instance).summary = true;
  252. }
  253. // Guardar propuesta de inversion (Crear)
  254. submit() {
  255. Swal.fire({
  256. allowOutsideClick: false,
  257. icon: "info",
  258. text: "Espere por favor..."
  259. });
  260. Swal.showLoading();
  261. if (this.investmentProposalID != undefined) {
  262. } else {
  263. this.investmentService.createProposalInvestment(this.inversion).subscribe(
  264. success => {
  265. if (success) {
  266. Swal.fire({
  267. allowOutsideClick: false,
  268. icon: "success",
  269. showCancelButton: false,
  270. title: "Exito",
  271. confirmButtonText: "El registro ha sido guardado"
  272. }).then(result => {
  273. if (result.value) {
  274. window.location.href = "#/investment-proposals";
  275. }
  276. });
  277. }
  278. },
  279. err => {
  280. Swal.fire({
  281. icon: "error",
  282. title: "Error al guardar",
  283. text: err.message
  284. });
  285. }
  286. );
  287. }
  288. }
  289. //Enviar a revision la propuesta de inversion
  290. sendToReview() {
  291. this.investmentService
  292. .getAvailableUsers(this.investmentProposalID)
  293. .subscribe(res => {
  294. this.userList = res["usuarios_next"];
  295. this.test = "<div>Notificar a:</div>";
  296. if (this.userList.length > 0) {
  297. for (let i = 0; i < this.userList.length; i++) {
  298. 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>`;
  299. }
  300. }
  301. });
  302. this.reviewProposal = undefined;
  303. (async () => {
  304. Swal.fire({
  305. title: `<h3>Enviar a revisión propuesta de inversión</h3>`,
  306. icon: "info",
  307. html: `<p style="text-align:left;">Comentario:</p>`,
  308. input: "textarea",
  309. showCancelButton: true,
  310. confirmButtonText: "Enviar propuesta",
  311. cancelButtonText: "Cancelar",
  312. showLoaderOnConfirm: true,
  313. inputValidator: value => {
  314. if (!value) {
  315. return "Debe ingresar un comentario";
  316. }
  317. },
  318. preConfirm: comentario => {
  319. this.reviewProposal = {
  320. id_inversion: this.investmentProposalID,
  321. step: "next",
  322. comentario: comentario
  323. };
  324. },
  325. allowOutsideClick: () => !Swal.isLoading()
  326. }).then(result => {
  327. Swal.fire({
  328. title: `<h3>Enviar a revisión propuesta de inversión</h3>`,
  329. icon: "info",
  330. html: `${this.test}`,
  331. confirmButtonText: "Siguiente",
  332. showCancelButton: true,
  333. cancelButtonText: "Cancelar",
  334. showLoaderOnConfirm: true,
  335. preConfirm: () => {
  336. let array = [];
  337. for (let i = 0; i < this.userList.length; i++) {
  338. let html_input: HTMLInputElement = document.getElementById(
  339. this.userList[i]
  340. ) as HTMLInputElement;
  341. let html_value: string = html_input.value;
  342. if (html_input.checked == true) {
  343. array.push(html_value);
  344. }
  345. }
  346. this.reviewProposal["notificar"] = array.toString();
  347. this.investmentService
  348. .sendReviewProposalInvestment(this.reviewProposal)
  349. .subscribe(
  350. success => {
  351. if (success) {
  352. Swal.fire({
  353. allowOutsideClick: false,
  354. icon: "success",
  355. showCancelButton: false,
  356. title: "Exito",
  357. confirmButtonText:
  358. "La propuesta ha sido enviada a revisión"
  359. }).then(result => {
  360. Swal.close();
  361. window.location.reload();
  362. });
  363. }
  364. },
  365. err => {
  366. Swal.fire({
  367. icon: "error",
  368. title: "Error al guardar",
  369. text: err.message
  370. });
  371. }
  372. );
  373. }
  374. });
  375. //window.location.reload();
  376. });
  377. })();
  378. }
  379. // Actualizar la propuesta de inversion si se han realizado cambios
  380. update_proposal() {
  381. Swal.fire({
  382. allowOutsideClick: false,
  383. icon: "info",
  384. text: "Espere por favor..."
  385. });
  386. Swal.showLoading();
  387. this.investmentService
  388. .updateProposalInvestment(this.investmentProposalID, this.inversion)
  389. .subscribe(
  390. success => {
  391. if (success) {
  392. Swal.fire({
  393. allowOutsideClick: false,
  394. icon: "success",
  395. showCancelButton: false,
  396. title: "Exito",
  397. confirmButtonText: "El registro ha sido actualizado"
  398. }).then(result => {
  399. Swal.close();
  400. });
  401. }
  402. },
  403. err => {
  404. Swal.fire({
  405. icon: "error",
  406. title: "Error al guardar",
  407. text: err.message
  408. });
  409. }
  410. );
  411. }
  412. goToPrevious() {
  413. this.submitted = true;
  414. if (this.investmentProposalID != undefined) {
  415. this.router.navigate(["/investment-proposal/complement-info"], {
  416. queryParams: { id: this.investmentProposalID }
  417. });
  418. } else {
  419. this.router.navigate(["/investment-proposal/complement-info"]);
  420. }
  421. }
  422. //Enviar a revision la propuesta de inversion
  423. finishProposal() {
  424. this.investmentService
  425. .getAvailableUsers(this.investmentProposalID)
  426. .subscribe(res => {
  427. this.userList = res["usuarios_next"];
  428. this.test = "<div>Notificar a:</div>";
  429. if (this.userList.length > 0) {
  430. for (let i = 0; i < this.userList.length; i++) {
  431. 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>`;
  432. }
  433. }
  434. });
  435. this.reviewProposal = undefined;
  436. (async () => {
  437. Swal.fire({
  438. title: `<h3>Finalizar propuesta de inversión:</h3>`,
  439. icon: "info",
  440. input: "textarea",
  441. html: `<p style="text-align:left;">Comentario:</p>`,
  442. showCancelButton: true,
  443. confirmButtonText: "Siguiente",
  444. cancelButtonText: "Cancelar",
  445. inputValidator: value => {
  446. if (!value) {
  447. return "Debe ingresar un comentario";
  448. }
  449. },
  450. preConfirm: comentario => {
  451. this.reviewProposal = {
  452. id_inversion: this.investmentProposalID,
  453. step: "next",
  454. comentario: comentario
  455. };
  456. },
  457. allowOutsideClick: () => !Swal.isLoading()
  458. }).then(result => {
  459. Swal.fire({
  460. title: `<h3>Finalizar propuesta de inversión</h3>`,
  461. icon: "info",
  462. html: `${this.test}`,
  463. confirmButtonText: "Enviar a revision",
  464. showCancelButton: true,
  465. cancelButtonText: "Cancelar",
  466. showLoaderOnConfirm: true,
  467. preConfirm: () => {
  468. let array = [];
  469. for (let i = 0; i < this.userList.length; i++) {
  470. let html_input: HTMLInputElement = document.getElementById(
  471. this.userList[i]
  472. ) as HTMLInputElement;
  473. let html_value: string = html_input.value;
  474. if (html_input.checked == true) {
  475. array.push(html_value);
  476. }
  477. }
  478. this.reviewProposal["notificar"] = array.toString();
  479. this.investmentService
  480. .sendReviewProposalInvestment(this.reviewProposal)
  481. .subscribe(
  482. success => {
  483. if (success) {
  484. Swal.fire({
  485. allowOutsideClick: false,
  486. icon: "success",
  487. showCancelButton: false,
  488. title: "Exito",
  489. confirmButtonText: "La propuesta ha sido finalizada"
  490. }).then(result => {
  491. Swal.close();
  492. window.location.reload();
  493. });
  494. }
  495. },
  496. err => {
  497. Swal.fire({
  498. icon: "error",
  499. title: "Error al guardar",
  500. text: err.message
  501. });
  502. }
  503. );
  504. }
  505. });
  506. //window.location.reload();
  507. });
  508. })();
  509. }
  510. // Verifica permisos para mostrar boton de edicion y/o envio a revision,
  511. // segun los permisos del usuario y el estado de la propuesta
  512. can_send_to_review(status: string) {
  513. if (status == "NUEVA" || status == "RECHA") {
  514. // TO DO ver que el codigo de los tipos de usuario
  515. return true;
  516. } else {
  517. return false;
  518. }
  519. }
  520. can_finish_proposal(status: string) {
  521. if (status == "LIQUI") {
  522. // TO DO ver que el codigo de los tipos de usuario
  523. return true;
  524. } else {
  525. return false;
  526. }
  527. }
  528. }