result.component.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. this.catalogService.getFinancialEntities().subscribe(res => {
  112. this.financials = res.find(
  113. e => e.id_entidad_financiera == this.general.casa
  114. );
  115. this.financials =
  116. this.financials != undefined ? this.financials.nombre : "-";
  117. });
  118. this.catalogService.getBaseTypes().subscribe(res => {
  119. this.base_types = res.find(
  120. e => e.id_tipo_base == this.general.base_anual
  121. );
  122. this.base_types =
  123. this.base_types != undefined
  124. ? ` ${this.base_types.tipo_base} / ${this.base_types.tipo_base_dias}`
  125. : "-";
  126. });
  127. this.catalogService.getCountries().subscribe(res => {
  128. this.countries = res.find(e => e.id_pais == this.complement.pais);
  129. this.countries =
  130. this.countries != undefined ? this.countries.nombre : "-";
  131. });
  132. this.catalogService.getCompanies().subscribe(res => {
  133. this.companies = res.find(e => e.id_empresa == this.complement.empresa);
  134. this.companies =
  135. this.companies != undefined ? this.companies.nombre : "-";
  136. });
  137. this.catalogService.getRateTypes().subscribe(res => {
  138. this.rates = res.find(e => e.id_tipo_tasa == this.general.tipo_tasa);
  139. this.rates = this.rates != undefined ? this.rates.nombre : "-";
  140. });
  141. this.catalogService.getRevenueTypes().subscribe(res => {
  142. this.revenues = res.find(e => e.id_tipo_renta == this.general.tipo_renta);
  143. this.revenues = this.revenues != undefined ? this.revenues.nombre : "-";
  144. });
  145. this.catalogService.getFundsOrigins().subscribe(res => {
  146. this.funds = res.find(
  147. e => e.id_origen_fondo == this.general.origenes_fondo
  148. );
  149. this.funds = this.funds != undefined ? this.funds.nombre : "-";
  150. });
  151. this.catalogService.getInstrumentTypes().subscribe(res => {
  152. this.instrumentTypes = res.find(
  153. e => e.codigo == this.general.instrumentos
  154. );
  155. this.instrumentTypes =
  156. this.instrumentTypes != undefined ? this.instrumentTypes.nombre : "-";
  157. });
  158. this.catalogService.getMarketTypes().subscribe(res => {
  159. this.markets = res.find(
  160. e => e.id_tipo_mercado == this.complement.tipo_mercado
  161. );
  162. this.markets = this.markets != undefined ? this.markets.nombre : "-";
  163. });
  164. this.catalogService.getEmitterTypes().subscribe(res => {
  165. this.emitters = res.find(
  166. e => e.id_tipo_emisor == this.complement.emisores
  167. );
  168. this.emitters = this.emitters != undefined ? this.emitters.nombre : "-";
  169. });
  170. this.catalogService.getPeriodicities().subscribe(res => {
  171. this.periodicities = res.find(
  172. e => e.id_periodicidad == this.general.periodicidad
  173. );
  174. this.periodicities =
  175. this.periodicities != undefined ? this.periodicities.nombre : "-";
  176. });
  177. this.catalogService.getPaymentTerms().subscribe(res => {
  178. this.payment_terms = res.find(e => e.id_plazo == this.complement.plazo);
  179. this.payment_terms =
  180. this.payment_terms != undefined ? this.payment_terms.nombre : "-";
  181. });
  182. this.catalogService.getOperationTypes().subscribe(res => {
  183. this.operations = res.find(
  184. e => e.id_tipo_operacion == this.complement.operaciones
  185. );
  186. this.operations =
  187. this.operations != undefined ? this.operations.nombre : "-";
  188. });
  189. //getIncomeFormat
  190. this.catalogService.getIncomeFormat().subscribe(res => {
  191. this.format_incomes = res.find(
  192. e => e.id_formato_ingreso == this.general.formato_ingreso
  193. );
  194. this.format_incomes =
  195. this.format_incomes != undefined ? this.format_incomes.nombre : "-";
  196. });
  197. this.final = {};
  198. Object.assign(this.final, this.general, this.instrument, this.complement);
  199. console.log("info instrument");
  200. console.log(this.instrument);
  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.instrument["proyecciones"] != undefined ||
  229. this.instrument["proyecciones"] != "")
  230. ) {
  231. console.log("is an required object");
  232. this.inversion["proyecciones"] = this.instrument["proyecciones"];
  233. console.log(this.inversion);
  234. }
  235. }
  236. loadComponent() {
  237. this.currentAdIndex = this.indexDynamicComponent % this.ads.length;
  238. const adItem = this.ads[this.currentAdIndex];
  239. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  240. adItem.component
  241. );
  242. const viewContainerRef = this.adHost.viewContainerRef;
  243. viewContainerRef.clear();
  244. const componentRef = viewContainerRef.createComponent(componentFactory);
  245. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  246. (<InstrumentComponent>componentRef.instance).summary = true;
  247. }
  248. // Guardar propuesta de inversion (Crear)
  249. submit() {
  250. console.log(this.inversion);
  251. if (this.investmentProposalID != undefined) {
  252. } else {
  253. this.investmentService.createProposalInvestment(this.inversion).subscribe(
  254. success => {
  255. if (success) {
  256. Swal.fire({
  257. allowOutsideClick: false,
  258. icon: "success",
  259. showCancelButton: false,
  260. title: "Exito",
  261. confirmButtonText: "El registro ha sido guardado"
  262. }).then(result => {
  263. if (result.value) {
  264. window.location.href = "#/investment-proposals";
  265. }
  266. });
  267. }
  268. },
  269. err => {
  270. Swal.fire({
  271. icon: "error",
  272. title: "Error al guardar",
  273. text: err.message
  274. });
  275. }
  276. );
  277. }
  278. }
  279. //Enviar a revision la propuesta de inversion
  280. sendToReview() {
  281. (async () => {
  282. const { value: comentario } = await Swal.fire({
  283. title: "<h3>Enviar a revisión propuesta de inversión</h3>",
  284. icon: "info",
  285. input: "textarea",
  286. showCancelButton: true,
  287. confirmButtonText: "Enviar propuesta",
  288. cancelButtonText: "Cancelar",
  289. inputValidator: value => {
  290. if (!value) {
  291. return "Debe ingresar un comentario";
  292. }
  293. }
  294. });
  295. if (comentario) {
  296. this.reviewProposal = {
  297. id_inversion: this.investmentProposalID,
  298. step: "next",
  299. comentario: comentario
  300. };
  301. this.investmentService
  302. .sendReviewProposalInvestment(this.reviewProposal)
  303. .subscribe(
  304. success => {
  305. if (success) {
  306. Swal.fire({
  307. allowOutsideClick: false,
  308. icon: "success",
  309. showCancelButton: false,
  310. title: "Exito",
  311. confirmButtonText: "La propuesta ha sido enviada a revisión"
  312. }).then(result => {
  313. Swal.close();
  314. });
  315. }
  316. },
  317. err => {
  318. Swal.fire({
  319. icon: "error",
  320. title: "Error al guardar",
  321. text: err.message
  322. });
  323. }
  324. );
  325. }
  326. })();
  327. }
  328. // Actualizar la propuesta de inversion si se han realizado cambios
  329. update_proposal() {
  330. console.log("update inversion");
  331. console.log(this.inversion);
  332. this.investmentService
  333. .updateProposalInvestment(this.investmentProposalID, this.inversion)
  334. .subscribe(
  335. success => {
  336. if (success) {
  337. Swal.fire({
  338. allowOutsideClick: false,
  339. icon: "success",
  340. showCancelButton: false,
  341. title: "Exito",
  342. confirmButtonText: "El registro ha sido actualizado"
  343. }).then(result => {
  344. Swal.close();
  345. });
  346. }
  347. },
  348. err => {
  349. Swal.fire({
  350. icon: "error",
  351. title: "Error al guardar",
  352. text: err.message
  353. });
  354. }
  355. );
  356. }
  357. goToPrevious() {
  358. this.submitted = true;
  359. if (this.investmentProposalID != undefined) {
  360. this.router.navigate(["/investment-proposal/complement-info"], {
  361. queryParams: { id: this.investmentProposalID }
  362. });
  363. } else {
  364. this.router.navigate(["/investment-proposal/complement-info"]);
  365. }
  366. }
  367. }