approve.component.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 Swal from "sweetalert2";
  17. import { CatalogsService } from "@app/services/catalogs.service";
  18. import { InvestmentsService } from "@app/services/investments.service";
  19. @Component({
  20. selector: "app-investment-proposal-approve",
  21. templateUrl: "./approve.component.html",
  22. styleUrls: ["./approve.component.scss"]
  23. })
  24. export class InvestmentProposalApproveComponent implements OnInit {
  25. title = "Aprobación de propuesta";
  26. @Input() ads: Instrument[];
  27. @Input() formData: InvestmentProposalForm;
  28. @ViewChild(InstrumentDirective, { static: true })
  29. adHost: InstrumentDirective;
  30. isFormValid: boolean = false;
  31. general: any;
  32. instrument: any;
  33. complement: any;
  34. final: any;
  35. workType: string;
  36. form: any;
  37. currentAdIndex = -1;
  38. interval: any;
  39. indexDynamicComponent: number;
  40. investmentProposalID: string;
  41. investmentExists;
  42. state;
  43. financials;
  44. base_types;
  45. countries;
  46. companies;
  47. rates;
  48. revenues;
  49. funds;
  50. instrumentTypes;
  51. markets;
  52. emitters;
  53. periodicities;
  54. format_incomes;
  55. payment_terms;
  56. operations;
  57. gInfo: any;
  58. gInstrument: any;
  59. gComplement: any;
  60. subject: any;
  61. investmentName: any;
  62. comment: any;
  63. justification: any;
  64. reviewProposal: {};
  65. constructor(
  66. private router: Router,
  67. private formDataService: FormInvestmentProposalService,
  68. private componentFactoryResolver: ComponentFactoryResolver,
  69. private instrumentService: InvestmentProposalWorkflowService,
  70. private loadInstrumentsService: InstrumentsService,
  71. private catalogService: CatalogsService,
  72. private route: ActivatedRoute,
  73. private investmentService: InvestmentsService
  74. ) {}
  75. ngOnInit() {
  76. //this.formDataService
  77. this.ads = this.loadInstrumentsService.getInstruments();
  78. this.route.params.subscribe(params => {
  79. this.investmentProposalID = params["id"];
  80. });
  81. if (this.investmentProposalID == undefined)
  82. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  83. if (this.investmentProposalID != undefined) {
  84. this.investmentExists = true;
  85. this.investmentService
  86. .getProposalInvestment(this.investmentProposalID)
  87. .subscribe(res => {
  88. this.state = res["result"]["id_estado_inversion"]["codigo"];
  89. this.gInfo = {
  90. asunto: res["result"]["asunto"],
  91. origenes_fondo:
  92. res["result"]["id_origen_fondo"] == null
  93. ? ""
  94. : res["result"]["id_origen_fondo"]["id_origen_fondo"],
  95. name: res["result"]["nombre_inversion"],
  96. //date: "",
  97. tipo_tasa: res["result"]["id_tipo_tasa"] || "",
  98. tipo_renta:
  99. res["result"]["id_tipo_renta"] == null
  100. ? ""
  101. : res["result"]["id_tipo_renta"]["id_tipo_renta"],
  102. periodicidad:
  103. res["result"]["id_periodicidad"] == null
  104. ? ""
  105. : res["result"]["id_periodicidad"]["id_periodicidad"],
  106. instrumentos:
  107. res["result"]["id_inversion_instrumento"]["id_tipo_instrumento"][
  108. "codigo"
  109. ],
  110. base_anual:
  111. res["result"]["id_tipo_base"] == null
  112. ? ""
  113. : res["result"]["id_tipo_base"]["id_tipo_base"],
  114. casa:
  115. res["result"]["id_entidad"] == null
  116. ? ""
  117. : res["result"]["id_entidad"]["id_entidad_financiera"],
  118. formato_ingreso:
  119. res["result"]["id_formato_ingreso"] == null
  120. ? ""
  121. : res["result"]["id_formato_ingreso"]["id_formato_ingreso"]
  122. };
  123. this.gInstrument =
  124. res["result"]["id_inversion_instrumento"]["instrumento"];
  125. this.gInstrument["id_inversion_instrumento"] =
  126. res["result"]["id_inversion_instrumento"][
  127. "id_inversion_instrumento"
  128. ];
  129. this.gComplement = {
  130. tipo_mercado:
  131. res["result"]["id_tipo_mercado"] == null
  132. ? ""
  133. : res["result"]["id_tipo_mercado"]["id_tipo_mercado"],
  134. emisores:
  135. res["result"]["id_tipo_emisor"] == null
  136. ? ""
  137. : res["result"]["id_tipo_emisor"]["id_tipo_emisor"],
  138. empresa:
  139. res["result"]["id_empresa"] == null
  140. ? ""
  141. : res["result"]["id_empresa"]["id_empresa"],
  142. pais:
  143. res["result"]["id_pais"] == null
  144. ? ""
  145. : res["result"]["id_pais"]["id_pais"],
  146. plazo:
  147. res["result"]["id_plazo"] == null
  148. ? ""
  149. : res["result"]["id_plazo"]["id_plazo"],
  150. operaciones:
  151. res["result"]["id_tipo_operacion"] == null
  152. ? ""
  153. : res["result"]["id_tipo_operacion"]["id_tipo_operacion"],
  154. comentarios: res["result"]["comentario"],
  155. justificacion: res["result"]["justificacion"]
  156. };
  157. this.formDataService.setGeneralInfo(this.gInfo);
  158. this.formDataService.setWork(this.gInstrument);
  159. this.formDataService.setComplementInfo(this.gComplement);
  160. this.general = this.formDataService.getGeneralInfo();
  161. this.instrument = this.formDataService.getWork();
  162. this.complement = this.formDataService.getComplementInfo();
  163. this.formData = this.formDataService.getFormData();
  164. this.formData.instrumentos;
  165. this.indexDynamicComponent = this.ads.findIndex(
  166. x => x.data.key == this.formData.instrumentos
  167. );
  168. if (this.indexDynamicComponent >= 0) {
  169. this.loadComponent();
  170. } else {
  171. console.log("No existe el componente");
  172. }
  173. if (this.general != undefined) {
  174. this.subject = this.general.asunto;
  175. this.investmentName = this.general.name;
  176. this.comment = this.complement.comment;
  177. this.justification = this.complement.comment;
  178. this.catalogService.getFinancialEntities().subscribe(res => {
  179. this.financials = res.find(
  180. e => e.id_entidad_financiera == this.general.casa
  181. );
  182. this.financials =
  183. this.financials != undefined ? this.financials.nombre : "-";
  184. });
  185. this.catalogService.getBaseTypes().subscribe(res => {
  186. this.base_types = res.find(
  187. e => e.id_tipo_base == this.general.base_anual
  188. );
  189. this.base_types =
  190. this.base_types != undefined
  191. ? ` ${this.base_types.tipo_base} / ${this.base_types.tipo_base_dias}`
  192. : "-";
  193. });
  194. this.catalogService.getCountries().subscribe(res => {
  195. this.countries = res.find(e => e.id_pais == this.complement.pais);
  196. this.countries =
  197. this.countries != undefined ? this.countries.nombre : "-";
  198. });
  199. this.catalogService.getCompanies().subscribe(res => {
  200. this.companies = res.find(
  201. e => e.id_empresa == this.complement.empresa
  202. );
  203. this.companies =
  204. this.companies != undefined ? this.companies.nombre : "-";
  205. });
  206. this.catalogService.getRateTypes().subscribe(res => {
  207. this.rates = res.find(
  208. e => e.id_tipo_tasa == this.general.tipo_tasa
  209. );
  210. this.rates = this.rates != undefined ? this.rates.nombre : "-";
  211. });
  212. this.catalogService.getRevenueTypes().subscribe(res => {
  213. this.revenues = res.find(
  214. e => e.id_tipo_renta == this.general.tipo_renta
  215. );
  216. this.revenues =
  217. this.revenues != undefined ? this.revenues.nombre : "-";
  218. });
  219. this.catalogService.getFundsOrigins().subscribe(res => {
  220. this.funds = res.find(
  221. e => e.id_origen_fondo == this.general.origenes_fondo
  222. );
  223. this.funds = this.funds != undefined ? this.funds.nombre : "-";
  224. });
  225. this.catalogService.getInstrumentTypes().subscribe(res => {
  226. this.instrumentTypes = res.find(
  227. e => e.codigo == this.general.instrumentos
  228. );
  229. this.instrumentTypes =
  230. this.instrumentTypes != undefined
  231. ? this.instrumentTypes.nombre
  232. : "-";
  233. });
  234. this.catalogService.getMarketTypes().subscribe(res => {
  235. this.markets = res.find(
  236. e => e.id_tipo_mercado == this.complement.tipo_mercado
  237. );
  238. this.markets =
  239. this.markets != undefined ? this.markets.nombre : "-";
  240. });
  241. this.catalogService.getEmitterTypes().subscribe(res => {
  242. this.emitters = res.find(
  243. e => e.id_tipo_emisor == this.complement.emisores
  244. );
  245. this.emitters =
  246. this.emitters != undefined ? this.emitters.nombre : "-";
  247. });
  248. this.catalogService.getPeriodicities().subscribe(res => {
  249. this.periodicities = res.find(
  250. e => e.id_periodicidad == this.general.periodicidad
  251. );
  252. this.periodicities =
  253. this.periodicities != undefined
  254. ? this.periodicities.nombre
  255. : "-";
  256. });
  257. this.catalogService.getPaymentTerms().subscribe(res => {
  258. this.payment_terms = res.find(
  259. e => e.codigo == this.complement.calificadora_riesgo
  260. );
  261. this.payment_terms =
  262. this.payment_terms != undefined
  263. ? this.payment_terms.nombre
  264. : "-";
  265. });
  266. this.catalogService.getOperationTypes().subscribe(res => {
  267. this.operations = res.find(
  268. e => e.codigo == this.complement.operaciones
  269. );
  270. this.operations =
  271. this.operations != undefined ? this.operations.nombre : "-";
  272. });
  273. //getIncomeFormat
  274. this.catalogService.getIncomeFormat().subscribe(res => {
  275. this.format_incomes = res.find(
  276. e => e.id_formato_ingreso == this.general.formato_ingreso
  277. );
  278. this.format_incomes =
  279. this.format_incomes != undefined
  280. ? this.format_incomes.nombre
  281. : "-";
  282. });
  283. }
  284. });
  285. } else {
  286. this.investmentExists = false;
  287. }
  288. }
  289. loadComponent() {
  290. const adItem = this.ads[this.indexDynamicComponent];
  291. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  292. adItem.component
  293. );
  294. const viewContainerRef = this.adHost.viewContainerRef;
  295. viewContainerRef.clear();
  296. const componentRef = viewContainerRef.createComponent(componentFactory);
  297. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  298. (<InstrumentComponent>componentRef.instance).summary = true;
  299. }
  300. approve_proposal() {
  301. (async () => {
  302. const { value: comentario } = await Swal.fire({
  303. title: "<h3>Aprobación de propuesta de inversión</h3>",
  304. icon: "info",
  305. html: `<p style="text-align:left;">Comentario:</p>`,
  306. input: "textarea",
  307. showCancelButton: true,
  308. confirmButtonText: "Enviar propuesta",
  309. cancelButtonText: "Cancelar",
  310. // inputValidator: value => {
  311. // if (!value) {
  312. // return "Debe ingresar un comentario";
  313. // }
  314. // }
  315. });
  316. // if (comentario) {
  317. this.reviewProposal = {
  318. id_inversion: this.investmentProposalID,
  319. step: "next",
  320. comentario: comentario
  321. };
  322. this.investmentService
  323. .sendProposalInvestmentToNextStep(this.reviewProposal)
  324. .subscribe(
  325. success => {
  326. if (success) {
  327. Swal.fire({
  328. allowOutsideClick: false,
  329. icon: "success",
  330. showCancelButton: false,
  331. title: "Exito",
  332. confirmButtonText: "La propuesta ha sido aprobada"
  333. }).then(result => {
  334. Swal.close();
  335. window.location.href = "#/investment-proposals";
  336. });
  337. }
  338. },
  339. err => {
  340. Swal.fire({
  341. icon: "error",
  342. title: "Error al guardar",
  343. text: err.message
  344. });
  345. }
  346. );
  347. // }
  348. })();
  349. }
  350. dismiss_proposal() {
  351. (async () => {
  352. const { value: comentario } = await Swal.fire({
  353. title: "<h3>Aprobación de propuesta de inversión</h3>",
  354. icon: "info",
  355. html: `<p style="text-align:left;">Comentario:</p>`,
  356. input: "textarea",
  357. showCancelButton: true,
  358. confirmButtonText: "Rechazar propuesta",
  359. confirmButtonColor: "#C82333",
  360. cancelButtonText: "Cancelar",
  361. // inputValidator: value => {
  362. // if (!value) {
  363. // return "Debe ingresar un comentario";
  364. // }
  365. // }
  366. });
  367. // if (comentario) {
  368. this.reviewProposal = {
  369. id_inversion: this.investmentProposalID,
  370. step: "previous",
  371. comentario: comentario
  372. };
  373. this.investmentService
  374. .sendProposalInvestmentToNextStep(this.reviewProposal)
  375. .subscribe(
  376. success => {
  377. if (success) {
  378. Swal.fire({
  379. allowOutsideClick: false,
  380. icon: "warning",
  381. showCancelButton: false,
  382. title: "Exito",
  383. confirmButtonText: "La propuesta ha sido rechazada"
  384. }).then(result => {
  385. window.location.href = "#/investment-proposals";
  386. Swal.close();
  387. });
  388. }
  389. },
  390. err => {
  391. Swal.fire({
  392. icon: "error",
  393. title: "Error en el servidor",
  394. text: err.message
  395. });
  396. }
  397. );
  398. // }
  399. })();
  400. }
  401. }