approve.component.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. userListPrevious: any;
  66. generated_inputs_previous: string;
  67. userListNext: any;
  68. generated_inputs_next: string;
  69. constructor(
  70. private router: Router,
  71. private formDataService: FormInvestmentProposalService,
  72. private componentFactoryResolver: ComponentFactoryResolver,
  73. private instrumentService: InvestmentProposalWorkflowService,
  74. private loadInstrumentsService: InstrumentsService,
  75. private catalogService: CatalogsService,
  76. private route: ActivatedRoute,
  77. private investmentService: InvestmentsService
  78. ) {}
  79. ngOnInit() {
  80. //this.formDataService
  81. this.ads = this.loadInstrumentsService.getInstruments();
  82. this.route.params.subscribe(params => {
  83. this.investmentProposalID = params["id"];
  84. });
  85. if (this.investmentProposalID == undefined)
  86. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  87. if (this.investmentProposalID != undefined) {
  88. this.investmentExists = true;
  89. this.investmentService
  90. .getAvailableUsers(this.investmentProposalID)
  91. .subscribe(res => {
  92. this.userListPrevious = res["usuarios_previous"];
  93. this.generated_inputs_previous = "";
  94. this.userListNext = res["usuarios_next"];
  95. this.generated_inputs_next = "";
  96. if (this.userListPrevious.length > 0) {
  97. for (let i = 0; i < this.userListPrevious.length; i++) {
  98. if (this.userListPrevious[i].default_email == true) {
  99. this.generated_inputs_previous += `<div class='form-check form-check-inline'><input type='checkbox' class='form-check-input' id='previous-${
  100. this.userListPrevious[i].name
  101. }' name='users' value='${
  102. this.userListPrevious[i].name
  103. }' checked='${
  104. this.userListPrevious[i].default_email == "true"
  105. ? true
  106. : false
  107. }' disabled='disabled'><label class='form-check-label' for='previous-${
  108. this.userListPrevious[i].name
  109. }'>${this.userListPrevious[i].name}</label></div>`;
  110. } else {
  111. this.generated_inputs_previous += `<div class='form-check form-check-inline'><input type='checkbox' class='form-check-input' id='previous-${this.userListPrevious[i].name}' name='users' value='${this.userListPrevious[i].name}'><label class='form-check-label' for='previous-${this.userListPrevious[i].name}'>${this.userListPrevious[i].name}</label></div>`;
  112. }
  113. }
  114. }
  115. if (this.userListNext.length > 0) {
  116. for (let i = 0; i < this.userListNext.length; i++) {
  117. if (this.userListNext[i].default_email == true) {
  118. this.generated_inputs_next += `<div class='form-check form-check-inline'><input type='checkbox' class='form-check-input' id='next-${
  119. this.userListNext[i].name
  120. }' name='users' value='${this.userListNext[i].name}' checked='${
  121. this.userListNext[i].default_email == "true" ? true : false
  122. }' disabled='disabled'><label class='form-check-label' for='next-${
  123. this.userListNext[i].name
  124. }'>${this.userListNext[i].name}</label></div>`;
  125. } else {
  126. this.generated_inputs_next += `<div class='form-check form-check-inline'><input type='checkbox' class='form-check-input' id='next-${this.userListNext[i].name}' name='users' value='${this.userListNext[i].name}'><label class='form-check-label' for='next-${this.userListNext[i].name}'>${this.userListNext[i].name}</label></div>`;
  127. }
  128. }
  129. }
  130. });
  131. this.investmentService
  132. .getProposalInvestment(this.investmentProposalID)
  133. .subscribe(res => {
  134. this.state = res["result"]["id_estado_inversion"]["codigo"];
  135. this.gInfo = {
  136. asunto: res["result"]["asunto"],
  137. origenes_fondo:
  138. res["result"]["id_origen_fondo"] == null
  139. ? ""
  140. : res["result"]["id_origen_fondo"]["id_origen_fondo"],
  141. name: res["result"]["nombre_inversion"],
  142. //date: "",
  143. tipo_tasa: res["result"]["id_tipo_tasa"] || "",
  144. tipo_renta:
  145. res["result"]["id_tipo_renta"] == null
  146. ? ""
  147. : res["result"]["id_tipo_renta"]["id_tipo_renta"],
  148. periodicidad:
  149. res["result"]["id_periodicidad"] == null
  150. ? ""
  151. : res["result"]["id_periodicidad"]["id_periodicidad"],
  152. instrumentos:
  153. res["result"]["id_inversion_instrumento"]["id_tipo_instrumento"][
  154. "codigo"
  155. ],
  156. base_anual:
  157. res["result"]["id_tipo_base"] == null
  158. ? ""
  159. : res["result"]["id_tipo_base"]["id_tipo_base"],
  160. casa:
  161. res["result"]["id_entidad"] == null
  162. ? ""
  163. : res["result"]["id_entidad"]["id_entidad_financiera"],
  164. formato_ingreso:
  165. res["result"]["id_formato_ingreso"] == null
  166. ? ""
  167. : res["result"]["id_formato_ingreso"]["id_formato_ingreso"]
  168. };
  169. this.gInstrument =
  170. res["result"]["id_inversion_instrumento"]["instrumento"];
  171. this.gInstrument["id_inversion_instrumento"] =
  172. res["result"]["id_inversion_instrumento"][
  173. "id_inversion_instrumento"
  174. ];
  175. this.gComplement = {
  176. tipo_mercado:
  177. res["result"]["id_tipo_mercado"] == null
  178. ? ""
  179. : res["result"]["id_tipo_mercado"]["id_tipo_mercado"],
  180. emisores:
  181. res["result"]["id_tipo_emisor"] == null
  182. ? ""
  183. : res["result"]["id_tipo_emisor"]["id_tipo_emisor"],
  184. empresa:
  185. res["result"]["id_empresa"] == null
  186. ? ""
  187. : res["result"]["id_empresa"]["id_empresa"],
  188. pais:
  189. res["result"]["id_pais"] == null
  190. ? ""
  191. : res["result"]["id_pais"]["id_pais"],
  192. plazo:
  193. res["result"]["id_plazo"] == null
  194. ? ""
  195. : res["result"]["id_plazo"]["id_plazo"],
  196. operaciones:
  197. res["result"]["id_tipo_operacion"] == null
  198. ? ""
  199. : res["result"]["id_tipo_operacion"]["id_tipo_operacion"],
  200. comentarios: res["result"]["comentario"],
  201. justificacion: res["result"]["justificacion"]
  202. };
  203. this.formDataService.setGeneralInfo(this.gInfo);
  204. this.formDataService.setWork(this.gInstrument);
  205. this.formDataService.setComplementInfo(this.gComplement);
  206. this.general = this.formDataService.getGeneralInfo();
  207. this.instrument = this.formDataService.getWork();
  208. this.complement = this.formDataService.getComplementInfo();
  209. this.formData = this.formDataService.getFormData();
  210. this.formData.instrumentos;
  211. this.indexDynamicComponent = this.ads.findIndex(
  212. x => x.data.key == this.formData.instrumentos
  213. );
  214. if (this.indexDynamicComponent >= 0) {
  215. this.loadComponent();
  216. } else {
  217. console.log("No existe el componente");
  218. }
  219. if (this.general != undefined) {
  220. this.subject = this.general.asunto;
  221. this.investmentName = this.general.name;
  222. this.comment = this.complement.comentarios;
  223. this.justification = this.complement.justificacion;
  224. this.catalogService.getFinancialEntities().subscribe(res => {
  225. this.financials = res.find(
  226. e => e.id_entidad_financiera == this.general.casa
  227. );
  228. this.financials =
  229. this.financials != undefined ? this.financials.nombre : "-";
  230. });
  231. this.catalogService.getBaseTypes().subscribe(res => {
  232. this.base_types = res.find(
  233. e => e.id_tipo_base == this.general.base_anual
  234. );
  235. this.base_types =
  236. this.base_types != undefined
  237. ? ` ${this.base_types.tipo_base} / ${this.base_types.tipo_base_dias}`
  238. : "-";
  239. });
  240. this.catalogService.getCountries().subscribe(res => {
  241. this.countries = res.find(e => e.id_pais == this.complement.pais);
  242. this.countries =
  243. this.countries != undefined ? this.countries.nombre : "-";
  244. });
  245. this.catalogService.getCompanies().subscribe(res => {
  246. this.companies = res.find(
  247. e => e.id_empresa == this.complement.empresa
  248. );
  249. this.companies =
  250. this.companies != undefined ? this.companies.nombre : "-";
  251. });
  252. this.catalogService.getRateTypes().subscribe(res => {
  253. this.rates = res.find(
  254. e => e.id_tipo_tasa == this.general.tipo_tasa
  255. );
  256. this.rates = this.rates != undefined ? this.rates.nombre : "-";
  257. });
  258. this.catalogService.getRevenueTypes().subscribe(res => {
  259. this.revenues = res.find(
  260. e => e.id_tipo_renta == this.general.tipo_renta
  261. );
  262. this.revenues =
  263. this.revenues != undefined ? this.revenues.nombre : "-";
  264. });
  265. this.catalogService.getFundsOrigins().subscribe(res => {
  266. this.funds = res.find(
  267. e => e.id_origen_fondo == this.general.origenes_fondo
  268. );
  269. this.funds = this.funds != undefined ? this.funds.nombre : "-";
  270. });
  271. this.catalogService.getInstrumentTypes().subscribe(res => {
  272. this.instrumentTypes = res.find(
  273. e => e.codigo == this.general.instrumentos
  274. );
  275. this.instrumentTypes =
  276. this.instrumentTypes != undefined
  277. ? this.instrumentTypes.nombre
  278. : "-";
  279. });
  280. this.catalogService.getMarketTypes().subscribe(res => {
  281. this.markets = res.find(
  282. e => e.id_tipo_mercado == this.complement.tipo_mercado
  283. );
  284. this.markets =
  285. this.markets != undefined ? this.markets.nombre : "-";
  286. });
  287. this.catalogService.getEmitterTypes().subscribe(res => {
  288. this.emitters = res.find(
  289. e => e.id_tipo_emisor == this.complement.emisores
  290. );
  291. this.emitters =
  292. this.emitters != undefined ? this.emitters.nombre : "-";
  293. });
  294. this.catalogService.getPeriodicities().subscribe(res => {
  295. this.periodicities = res.find(
  296. e => e.id_periodicidad == this.general.periodicidad
  297. );
  298. this.periodicities =
  299. this.periodicities != undefined
  300. ? this.periodicities.nombre
  301. : "-";
  302. });
  303. this.catalogService.getPaymentTerms().subscribe(res => {
  304. this.payment_terms = res.find(
  305. e => e.id_plazo == this.complement.plazo
  306. );
  307. this.payment_terms =
  308. this.payment_terms != undefined
  309. ? this.payment_terms.nombre
  310. : "-";
  311. });
  312. this.catalogService.getOperationTypes().subscribe(res => {
  313. this.operations = res.find(
  314. e => e.id_tipo_operacion == this.complement.operaciones
  315. );
  316. this.operations =
  317. this.operations != undefined ? this.operations.nombre : "-";
  318. });
  319. //getIncomeFormat
  320. this.catalogService.getIncomeFormat().subscribe(res => {
  321. this.format_incomes = res.find(
  322. e => e.id_formato_ingreso == this.general.formato_ingreso
  323. );
  324. this.format_incomes =
  325. this.format_incomes != undefined
  326. ? this.format_incomes.nombre
  327. : "-";
  328. });
  329. }
  330. });
  331. } else {
  332. this.investmentExists = false;
  333. }
  334. }
  335. loadComponent() {
  336. const adItem = this.ads[this.indexDynamicComponent];
  337. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  338. adItem.component
  339. );
  340. const viewContainerRef = this.adHost.viewContainerRef;
  341. viewContainerRef.clear();
  342. const componentRef = viewContainerRef.createComponent(componentFactory);
  343. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  344. (<InstrumentComponent>componentRef.instance).summary = true;
  345. }
  346. approve_proposal() {
  347. this.reviewProposal = undefined;
  348. Swal.fire({
  349. title: `<h3>Aprobación de propuesta de inversión</h3>`,
  350. icon: "info",
  351. html: `<p style="text-align:left;">Comentario:</p>`,
  352. input: "textarea",
  353. showCancelButton: true,
  354. confirmButtonText: "Siguiente",
  355. cancelButtonText: "Cancelar",
  356. showLoaderOnConfirm: true,
  357. preConfirm: comentario => {
  358. this.reviewProposal = {
  359. id_inversion: this.investmentProposalID,
  360. step: "next",
  361. comentario: comentario
  362. };
  363. },
  364. allowOutsideClick: () => !Swal.isLoading()
  365. }).then(result => {
  366. if (result.dismiss) {
  367. return false;
  368. }
  369. Swal.fire({
  370. title: `<h3>Aprobación de propuesta de inversión</h3>`,
  371. icon: "info",
  372. html: `<div>Notificar a:</div>${this.generated_inputs_next}`,
  373. confirmButtonText: "Aprobar propuesta",
  374. showCancelButton: true,
  375. cancelButtonText: "Cancelar",
  376. showLoaderOnConfirm: true,
  377. preConfirm: () => {
  378. let array = [];
  379. for (let i = 0; i < this.userListNext.length; i++) {
  380. let html_input: HTMLInputElement = document.getElementById(
  381. "next-" + this.userListNext[i].name
  382. ) as HTMLInputElement;
  383. let html_value: string = html_input.value;
  384. if (html_input.checked == true) {
  385. array.push(html_value);
  386. }
  387. }
  388. this.reviewProposal["notificar"] = array.toString();
  389. }
  390. }).then(result1 => {
  391. if (result1.dismiss) {
  392. return false;
  393. }
  394. Swal.fire({
  395. allowOutsideClick: false,
  396. title: "Espere por favor...",
  397. icon: "info"
  398. });
  399. Swal.showLoading();
  400. this.investmentService
  401. .sendReviewProposalInvestment(this.reviewProposal)
  402. .subscribe(
  403. success => {
  404. if (success) {
  405. Swal.fire({
  406. allowOutsideClick: false,
  407. icon: "success",
  408. showCancelButton: false,
  409. title: "Exito",
  410. confirmButtonText: "La propuesta ha sido aprobada"
  411. }).then(result => {
  412. Swal.close();
  413. window.location.href = "#/investment-proposals";
  414. });
  415. }
  416. },
  417. err => {
  418. debugger;
  419. if (err.code == 405) {
  420. Swal.fire({
  421. icon: "error",
  422. title: "Operacion no permitida",
  423. text: err.message
  424. }).then(result => {
  425. Swal.close();
  426. window.location.href = "#/investment-proposals";
  427. });
  428. } else {
  429. Swal.fire({
  430. icon: "error",
  431. title: "Error al guardar",
  432. text: err.message
  433. });
  434. }
  435. }
  436. );
  437. });
  438. });
  439. }
  440. dismiss_proposal() {
  441. this.reviewProposal = undefined;
  442. Swal.fire({
  443. title: `<h3>Rechazar propuesta de inversión</h3>`,
  444. icon: "info",
  445. html: `<p style="text-align:left;">Comentario:</p>`,
  446. input: "textarea",
  447. showCancelButton: true,
  448. confirmButtonColor: "#C82333",
  449. confirmButtonText: "Siguiente",
  450. cancelButtonText: "Cancelar",
  451. showLoaderOnConfirm: true,
  452. preConfirm: comentario => {
  453. this.reviewProposal = {
  454. id_inversion: this.investmentProposalID,
  455. step: "previous",
  456. comentario: comentario
  457. };
  458. },
  459. allowOutsideClick: () => !Swal.isLoading()
  460. }).then(result => {
  461. if (result.dismiss) {
  462. return false;
  463. }
  464. Swal.fire({
  465. title: `<h3>Rechazar propuesta de inversión</h3>`,
  466. icon: "info",
  467. html: `<div>Notificar a:</div>${this.generated_inputs_previous}`,
  468. showCancelButton: true,
  469. confirmButtonColor: "#C82333",
  470. confirmButtonText: "Rechazar propuesta",
  471. cancelButtonText: "Cancelar",
  472. showLoaderOnConfirm: true,
  473. preConfirm: () => {
  474. let array = [];
  475. for (let i = 0; i < this.userListPrevious.length; i++) {
  476. let html_input: HTMLInputElement = document.getElementById(
  477. "previous-" + this.userListPrevious[i].name
  478. ) as HTMLInputElement;
  479. let html_value: string = html_input.value;
  480. if (html_input.checked == true) {
  481. array.push(html_value);
  482. }
  483. }
  484. this.reviewProposal["notificar"] = array.toString();
  485. }
  486. }).then(result1 => {
  487. if (result1.dismiss) {
  488. return false;
  489. }
  490. Swal.fire({
  491. allowOutsideClick: false,
  492. title: "Espere por favor...",
  493. icon: "info"
  494. });
  495. Swal.showLoading();
  496. this.investmentService
  497. .sendReviewProposalInvestment(this.reviewProposal)
  498. .subscribe(
  499. success => {
  500. if (success) {
  501. Swal.fire({
  502. allowOutsideClick: false,
  503. icon: "warning",
  504. showCancelButton: false,
  505. title: "Exito",
  506. confirmButtonText: "La propuesta ha sido rechazada"
  507. }).then(result => {
  508. Swal.close();
  509. window.location.href = "#/investment-proposals";
  510. });
  511. }
  512. },
  513. err => {
  514. if (err.code == 405) {
  515. Swal.fire({
  516. icon: "error",
  517. title: "Operacion no permitida",
  518. text: err.message
  519. }).then(result => {
  520. Swal.close();
  521. window.location.href = "#/investment-proposals";
  522. });
  523. } else {
  524. Swal.fire({
  525. icon: "error",
  526. title: "Error al guardar",
  527. text: err.message
  528. });
  529. }
  530. this.router.navigate(["/investment-proposals"]);
  531. }
  532. );
  533. });
  534. });
  535. }
  536. }