result.component.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. constructor(
  65. private router: Router,
  66. private formDataService: FormInvestmentProposalService,
  67. private componentFactoryResolver: ComponentFactoryResolver,
  68. private instrumentService: InvestmentProposalWorkflowService,
  69. private loadInstrumentsService: InstrumentsService,
  70. private catalogService: CatalogsService,
  71. private investmentService: InvestmentsService,
  72. private route: ActivatedRoute,
  73. private authService: AuthService
  74. ) {
  75. this.formData = this.formDataService.getFormData();
  76. this.isFormValid = this.formDataService.isFormValid();
  77. this.ads = this.loadInstrumentsService.getInstruments();
  78. const decodedToken = this.helper.decodeToken(
  79. this.authService.getJwtToken()
  80. );
  81. this.userRole = decodedToken.groups;
  82. }
  83. ngOnInit() {
  84. this.route.params.subscribe(params => {
  85. this.investmentProposalID = params["id"];
  86. });
  87. if (this.investmentProposalID == undefined)
  88. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  89. if (this.investmentProposalID != undefined) {
  90. this.investmentExists = true;
  91. this.investmentService
  92. .getProposalInvestment(this.investmentProposalID)
  93. .subscribe(res => {
  94. this.state = res["result"]["id_estado_inversion"]["codigo"];
  95. });
  96. } else {
  97. this.investmentExists = false;
  98. }
  99. this.formData.instrumentos;
  100. this.indexDynamicComponent = this.ads.findIndex(
  101. x => x.data.key == this.formData.instrumentos
  102. );
  103. if (this.indexDynamicComponent >= 0) {
  104. this.loadComponent();
  105. } else {
  106. console.log("No existe el componente");
  107. }
  108. this.general = this.formDataService.getGeneralInfo();
  109. this.instrument = this.formDataService.getWork();
  110. this.complement = this.formDataService.getComplementInfo();
  111. console.log(this.instrument);
  112. this.catalogService.getFinancialEntities().subscribe(res => {
  113. this.financials = res.find(
  114. e => e.id_entidad_financiera == this.general.casa
  115. );
  116. this.financials =
  117. this.financials != undefined ? this.financials.nombre : "-";
  118. });
  119. this.catalogService.getBaseTypes().subscribe(res => {
  120. this.base_types = res.find(
  121. e => e.id_tipo_base == this.general.base_anual
  122. );
  123. this.base_types =
  124. this.base_types != undefined
  125. ? `${this.base_types.tipo_base_dias} / ${this.base_types.tipo_base}`
  126. : "-";
  127. });
  128. this.catalogService.getCountries().subscribe(res => {
  129. this.countries = res.find(e => e.id_pais == this.complement.pais);
  130. this.countries =
  131. this.countries != undefined ? this.countries.nombre : "-";
  132. });
  133. this.catalogService.getCompanies().subscribe(res => {
  134. this.companies = res.find(e => e.id_empresa == this.complement.empresa);
  135. this.companies =
  136. this.companies != undefined ? this.companies.nombre : "-";
  137. });
  138. this.catalogService.getRateTypes().subscribe(res => {
  139. this.rates = res.find(e => e.id_tipo_tasa == this.general.tipo_tasa);
  140. this.rates = this.rates != undefined ? this.rates.nombre : "-";
  141. });
  142. this.catalogService.getRevenueTypes().subscribe(res => {
  143. this.revenues = res.find(e => e.id_tipo_renta == this.general.tipo_renta);
  144. this.revenues = this.revenues != undefined ? this.revenues.nombre : "-";
  145. });
  146. this.catalogService.getFundsOrigins().subscribe(res => {
  147. this.funds = res.find(
  148. e => e.id_origen_fondo == this.general.origenes_fondo
  149. );
  150. this.funds = this.funds != undefined ? this.funds.nombre : "-";
  151. });
  152. this.catalogService.getInstrumentTypes().subscribe(res => {
  153. this.instrumentTypes = res.find(
  154. e => e.codigo == this.general.instrumentos
  155. );
  156. this.instrumentTypes =
  157. this.instrumentTypes != undefined ? this.instrumentTypes.nombre : "-";
  158. });
  159. this.catalogService.getMarketTypes().subscribe(res => {
  160. this.markets = res.find(
  161. e => e.id_tipo_mercado == this.complement.tipo_mercado
  162. );
  163. this.markets = this.markets != undefined ? this.markets.nombre : "-";
  164. });
  165. this.catalogService.getEmitterTypes().subscribe(res => {
  166. this.emitters = res.find(
  167. e => e.id_tipo_emisor == this.complement.emisores
  168. );
  169. this.emitters = this.emitters != undefined ? this.emitters.nombre : "-";
  170. });
  171. this.catalogService.getPeriodicities().subscribe(res => {
  172. this.periodicities = res.find(
  173. e => e.id_periodicidad == this.general.periodicidad
  174. );
  175. this.periodicities =
  176. this.periodicities != undefined ? this.periodicities.nombre : "-";
  177. });
  178. this.catalogService.getPaymentTerms().subscribe(res => {
  179. this.payment_terms = res.find(e => e.id_plazo == this.complement.plazo);
  180. this.payment_terms =
  181. this.payment_terms != undefined ? this.payment_terms.nombre : "-";
  182. });
  183. this.catalogService.getOperationTypes().subscribe(res => {
  184. this.operations = res.find(
  185. e => e.id_tipo_operacion == this.complement.operaciones
  186. );
  187. this.operations =
  188. this.operations != undefined ? this.operations.nombre : "-";
  189. });
  190. //getIncomeFormat
  191. this.catalogService.getIncomeFormat().subscribe(res => {
  192. this.format_incomes = res.find(
  193. e => e.id_formato_ingreso == this.general.formato_ingreso
  194. );
  195. this.format_incomes =
  196. this.format_incomes != undefined ? this.format_incomes.nombre : "-";
  197. });
  198. this.final = {};
  199. Object.assign(this.final, this.general, this.instrument, this.complement);
  200. this.inversion = {
  201. codigo_instrumento: this.general.instrumentos,
  202. info_inversion: {
  203. id_empresa: +this.complement.empresa,
  204. id_pais: +this.complement.pais,
  205. id_entidad: +this.general.casa,
  206. id_origen_fondo: +this.general.origenes_fondo,
  207. id_periodicidad: +this.general.periodicidad,
  208. id_plazo: +this.complement.plazo,
  209. id_tipo_base: +this.general.base_anual,
  210. id_tipo_emisor: +this.complement.emisores,
  211. id_tipo_mercado: this.complement.tipo_mercado,
  212. id_tipo_operacion: +this.complement.operaciones,
  213. //id_tipo_pago: 1,
  214. id_formato_ingreso: +this.general.formato_ingreso,
  215. id_tipo_renta: +this.general.tipo_renta,
  216. id_tipo_tasa: +this.general.tipo_tasa,
  217. nombre_inversion: this.general.name,
  218. asunto: this.general.asunto,
  219. comentario: this.complement.comentarios,
  220. justificacion: this.complement.justificacion
  221. },
  222. info_instrumento: this.instrument
  223. };
  224. if (
  225. (this.inversion["codigo_instrumento"] == "CETE" ||
  226. this.inversion["codigo_instrumento"] == "DAP" ||
  227. this.inversion["codigo_instrumento"] == "VCN" ||
  228. this.inversion["codigo_instrumento"] == "PBUR") &&
  229. (this.instrument["proyecciones"] != undefined ||
  230. this.instrument["proyecciones"] != "")
  231. ) {
  232. this.inversion["proyecciones"] = this.instrument["proyecciones"];
  233. }
  234. }
  235. loadComponent() {
  236. const adItem = this.ads[this.indexDynamicComponent];
  237. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  238. adItem.component
  239. );
  240. const viewContainerRef = this.adHost.viewContainerRef;
  241. viewContainerRef.clear();
  242. const componentRef = viewContainerRef.createComponent(componentFactory);
  243. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  244. (<InstrumentComponent>componentRef.instance).summary = true;
  245. }
  246. // Guardar propuesta de inversion (Crear)
  247. submit() {
  248. Swal.fire({
  249. allowOutsideClick: false,
  250. icon: "info",
  251. text: "Espere por favor..."
  252. });
  253. Swal.showLoading();
  254. if (this.investmentProposalID != undefined) {
  255. } else {
  256. this.investmentService.createProposalInvestment(this.inversion).subscribe(
  257. success => {
  258. if (success) {
  259. Swal.fire({
  260. allowOutsideClick: false,
  261. icon: "success",
  262. showCancelButton: false,
  263. title: "Exito",
  264. confirmButtonText: "El registro ha sido guardado"
  265. }).then(result => {
  266. if (result.value) {
  267. window.location.href = "#/investment-proposals";
  268. }
  269. });
  270. }
  271. },
  272. err => {
  273. Swal.fire({
  274. icon: "error",
  275. title: "Error al guardar",
  276. text: err.message
  277. });
  278. }
  279. );
  280. }
  281. }
  282. //Enviar a revision la propuesta de inversion
  283. sendToReview() {
  284. (async () => {
  285. const { value: comentario } = await Swal.fire({
  286. title: "<h3>Enviar a revisión propuesta de inversión</h3>",
  287. icon: "info",
  288. html: `<p style="text-align:left;">Comentario:</p>`,
  289. input: "textarea",
  290. showCancelButton: true,
  291. confirmButtonText: "Enviar propuesta",
  292. cancelButtonText: "Cancelar",
  293. // inputValidator: value => {
  294. // if (!value) {
  295. // return "Debe ingresar un comentario";
  296. // }
  297. // }
  298. preConfirm: comentario => {
  299. this.reviewProposal = {
  300. id_inversion: this.investmentProposalID,
  301. step: "next",
  302. comentario: comentario
  303. };
  304. this.investmentService
  305. .sendReviewProposalInvestment(this.reviewProposal)
  306. .subscribe(
  307. success => {
  308. if (success) {
  309. Swal.fire({
  310. allowOutsideClick: false,
  311. icon: "success",
  312. showCancelButton: false,
  313. title: "Exito",
  314. confirmButtonText: "La propuesta ha sido enviada a revisión"
  315. }).then(result => {
  316. Swal.close();
  317. window.location.href = "#/investment-proposals";
  318. });
  319. }
  320. },
  321. err => {
  322. Swal.fire({
  323. icon: "error",
  324. title: "Error al guardar",
  325. text: err.message
  326. });
  327. }
  328. );
  329. }
  330. });
  331. })();
  332. }
  333. // Actualizar la propuesta de inversion si se han realizado cambios
  334. update_proposal() {
  335. Swal.fire({
  336. allowOutsideClick: false,
  337. icon: "info",
  338. text: "Espere por favor..."
  339. });
  340. Swal.showLoading();
  341. this.investmentService
  342. .updateProposalInvestment(this.investmentProposalID, this.inversion)
  343. .subscribe(
  344. success => {
  345. if (success) {
  346. Swal.fire({
  347. allowOutsideClick: false,
  348. icon: "success",
  349. showCancelButton: false,
  350. title: "Exito",
  351. confirmButtonText: "El registro ha sido actualizado"
  352. }).then(result => {
  353. Swal.close();
  354. });
  355. }
  356. },
  357. err => {
  358. Swal.fire({
  359. icon: "error",
  360. title: "Error al guardar",
  361. text: err.message
  362. });
  363. }
  364. );
  365. }
  366. goToPrevious() {
  367. this.submitted = true;
  368. if (this.investmentProposalID != undefined) {
  369. this.router.navigate(["/investment-proposal/complement-info"], {
  370. queryParams: { id: this.investmentProposalID }
  371. });
  372. } else {
  373. this.router.navigate(["/investment-proposal/complement-info"]);
  374. }
  375. }
  376. //Enviar a revision la propuesta de inversion
  377. finishProposal() {
  378. (async () => {
  379. const { value: comentario } = await Swal.fire({
  380. title: "<h3>Finalizar propuesta de inversión</h3>",
  381. icon: "info",
  382. html: `<p style="text-align:left;">Comentario:</p>`,
  383. input: "textarea",
  384. showCancelButton: true,
  385. confirmButtonText: "Finalizar",
  386. cancelButtonText: "Cancelar",
  387. // inputValidator: value => {
  388. // if (!value) {
  389. // return "Debe ingresar un comentario";
  390. // }
  391. // }
  392. preConfirm: comentario => {
  393. this.reviewProposal = {
  394. id_inversion: this.investmentProposalID,
  395. step: "next",
  396. comentario: comentario
  397. };
  398. this.investmentService
  399. .sendReviewProposalInvestment(this.reviewProposal)
  400. .subscribe(
  401. success => {
  402. if (success) {
  403. Swal.fire({
  404. allowOutsideClick: false,
  405. icon: "success",
  406. showCancelButton: false,
  407. title: "Exito",
  408. confirmButtonText: "La propuesta ha sido finalizada"
  409. }).then(result => {
  410. Swal.close();
  411. window.location.href = "#/investment-proposals";
  412. });
  413. }
  414. },
  415. err => {
  416. Swal.fire({
  417. icon: "error",
  418. title: "Error al guardar",
  419. text: err.message
  420. });
  421. }
  422. );
  423. }
  424. });
  425. })();
  426. }
  427. // Verifica permisos para mostrar boton de edicion y/o envio a revision,
  428. // segun los permisos del usuario y el estado de la propuesta
  429. can_send_to_review(status: string) {
  430. if (status == "NUEVA" || status == "RECHA") {
  431. // TO DO ver que el codigo de los tipos de usuario
  432. return true;
  433. } else {
  434. return false;
  435. }
  436. }
  437. can_finish_proposal(status: string) {
  438. if (status == "LIQUI") {
  439. // TO DO ver que el codigo de los tipos de usuario
  440. return true;
  441. } else {
  442. return false;
  443. }
  444. }
  445. }