vcn.costs.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import { Component, ViewChild, OnInit } from "@angular/core";
  2. import { MatPaginator } from "@angular/material/paginator";
  3. import { MatSort } from "@angular/material/sort";
  4. import { MatTableDataSource } from "@angular/material/table";
  5. import Swal from "sweetalert2";
  6. import { CatalogsService } from "src/app/services/catalogs.service";
  7. import { InvestmentsService } from "@app/services/investments.service";
  8. import { InstrumentCalculations } from "@app/services/instrument-calculations.service";
  9. import { AuthService } from "@app/services/auth2.service";
  10. import { JwtHelperService } from "@auth0/angular-jwt";
  11. import { InvestmentProposal } from "@app/models/investment-proposal";
  12. import { from } from "rxjs";
  13. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  14. import { Router, ActivatedRoute } from "@angular/router";
  15. import {
  16. FormBuilder,
  17. FormGroup,
  18. FormControl,
  19. FormArray,
  20. Validators
  21. } from "@angular/forms";
  22. @Component({
  23. selector: "app-vcn-costs",
  24. templateUrl: "./vcn.costs.component.html"
  25. //styleUrls: ["./dap.costs.component.scss"]
  26. })
  27. export class VCNCostsComponent implements OnInit {
  28. helper = new JwtHelperService();
  29. title: string = "Costos para valores comerciales";
  30. displayedColumns: string[] = [
  31. "codigo_inversion",
  32. "asunto",
  33. "id_tipo_mercado",
  34. "id_inversion_instrumento",
  35. "id"
  36. ];
  37. //displayedColumns: string[] = ['state'];
  38. listProposals: InvestmentProposal[];
  39. dataSource = new MatTableDataSource(this.listProposals);
  40. resultsLength = 0;
  41. isLoadingResults = true;
  42. isRateLimitReached = false;
  43. userRole: any;
  44. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  45. @ViewChild(MatSort, { static: true }) sort: MatSort;
  46. role_number: any;
  47. investmentProposalID: string;
  48. form: FormArray;
  49. investmentProposalForm: FormGroup;
  50. proyecciones: any;
  51. proyeccionesProps = [];
  52. dataRetrieved: boolean = false;
  53. array1;
  54. array2;
  55. array3;
  56. investment: any;
  57. instrument_work: any;
  58. instrument_exists: boolean;
  59. constructor(
  60. private catalogService: CatalogsService,
  61. private investmentsService: InvestmentsService,
  62. private authService: AuthService,
  63. private formInvestmentProposal: FormInvestmentProposalService,
  64. private router: Router,
  65. private route: ActivatedRoute,
  66. private instrumentService: InstrumentCalculations,
  67. private formBuilder: FormBuilder
  68. ) {
  69. const decodedToken = this.helper.decodeToken(
  70. this.authService.getJwtToken()
  71. );
  72. this.userRole = decodedToken.groups;
  73. Swal.fire({
  74. allowOutsideClick: false,
  75. icon: "info",
  76. text: "Espere por favor..."
  77. });
  78. Swal.showLoading();
  79. }
  80. ngOnInit() {
  81. Swal.close();
  82. const formDataObj = {};
  83. this.route.params.subscribe(params => {
  84. this.investmentProposalID = params["id"];
  85. });
  86. if (this.investmentProposalID == undefined)
  87. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  88. if (this.investmentProposalID != undefined) {
  89. this.investmentsService
  90. .getProposalInvestment(this.investmentProposalID)
  91. .subscribe(
  92. res => {
  93. this.investment = res["result"];
  94. this.instrument_work =
  95. res["result"]["id_inversion_instrumento"]["instrumento"];
  96. this.instrument_exists = true;
  97. this.investmentProposalForm = this.formBuilder.group({
  98. valor_par: [
  99. !this.instrument_exists ? false : this.instrument_work.valor_par
  100. ],
  101. valor_nominal: [
  102. !this.instrument_exists
  103. ? ""
  104. : this.instrument_work.valor_nominal,
  105. [
  106. Validators.required,
  107. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  108. ]
  109. ],
  110. otros_costos: [
  111. !this.instrument_exists ? 0 : this.instrument_work.otros_costos,
  112. [Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)]
  113. ],
  114. plazo: [
  115. !this.instrument_exists ? "" : this.instrument_work.plazo,
  116. [Validators.required]
  117. ],
  118. renta_porcentaje: [
  119. !this.instrument_exists
  120. ? ""
  121. : this.instrument_work.renta_porcentaje,
  122. [Validators.required]
  123. ],
  124. comision_casa_porcentaje: [
  125. !this.instrument_exists
  126. ? ""
  127. : this.instrument_work.comision_casa_porcentaje,
  128. [
  129. Validators.required,
  130. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  131. ]
  132. ],
  133. comision_bolsa_porcentaje: [
  134. !this.instrument_exists
  135. ? ""
  136. : this.instrument_work.comision_bolsa_porcentaje,
  137. [
  138. Validators.required,
  139. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  140. ]
  141. ],
  142. rendimiento_bruto: [
  143. !this.instrument_exists
  144. ? ""
  145. : this.instrument_work.rendimiento_bruto,
  146. [
  147. Validators.required,
  148. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  149. ]
  150. ],
  151. /*formato_ingreso: [
  152. !this.instrument_exists ? "" : this.instrument_work.formato_ingreso,
  153. [Validators.required]
  154. ],*/
  155. fecha_liquidacion: [
  156. !this.instrument_exists
  157. ? ""
  158. : this.instrument_work.fecha_liquidacion,
  159. [Validators.required]
  160. ],
  161. fecha_vencimiento: [
  162. !this.instrument_exists
  163. ? ""
  164. : this.instrument_work.fecha_vencimiento,
  165. [Validators.required]
  166. ],
  167. fecha_operacion: [
  168. !this.instrument_exists
  169. ? ""
  170. : this.instrument_work.fecha_operacion,
  171. [Validators.required]
  172. ],
  173. fecha_ultima_cupon: [
  174. !this.instrument_exists
  175. ? ""
  176. : this.instrument_work.fecha_ultima_cupon,
  177. [Validators.required]
  178. ]
  179. });
  180. },
  181. err => {}
  182. );
  183. this.investmentsService
  184. .getProposalInvestment(this.investmentProposalID)
  185. .subscribe(
  186. res => {
  187. this.proyecciones =
  188. res["result"]["id_inversion_instrumento"]["instrumento"][
  189. "proyecciones"
  190. ];
  191. this.form = new FormArray(this.buildForm(this.proyecciones));
  192. this.dataRetrieved = true;
  193. },
  194. err => {
  195. Swal.fire({
  196. icon: "error",
  197. title: "Error en el servidor",
  198. text: err.message
  199. });
  200. }
  201. );
  202. }
  203. setTimeout(() => {
  204. Swal.close();
  205. }, 1200);
  206. }
  207. submitInstrumentChanges() {}
  208. submitProjectionChanges() {
  209. let objProjection = { proyecciones: this.form.value };
  210. Swal.fire({
  211. allowOutsideClick: false,
  212. icon: "info",
  213. text: "Espere por favor..."
  214. });
  215. Swal.showLoading();
  216. this.instrumentService
  217. .projectionCalc(this.investmentProposalID, this.form.value)
  218. .subscribe(
  219. resp => {
  220. this.investmentsService
  221. .updateInstrumentProjection(
  222. this.investmentProposalID,
  223. objProjection
  224. )
  225. .subscribe(
  226. success => {
  227. if (success) {
  228. Swal.fire({
  229. allowOutsideClick: false,
  230. icon: "success",
  231. showCancelButton: false,
  232. title: "Exito",
  233. confirmButtonText: "La información ha sido actualizada"
  234. }).then(result => {
  235. this.proyecciones =
  236. result["result"]["id_inversion_instrumento"][
  237. "instrumento"
  238. ]["proyecciones"];
  239. this.form = new FormArray(
  240. this.buildForm(this.proyecciones)
  241. );
  242. Swal.close();
  243. //window.location.reload();
  244. });
  245. }
  246. },
  247. err => {
  248. Swal.fire({
  249. icon: "error",
  250. title: "Error al guardar",
  251. text: err.message
  252. });
  253. }
  254. );
  255. },
  256. err => {
  257. Swal.fire({
  258. icon: "error",
  259. title: "Error al guardar",
  260. text: err.message
  261. });
  262. }
  263. );
  264. }
  265. saveProjection() {
  266. let objProjection = { proyecciones: this.form.value };
  267. Swal.fire({
  268. allowOutsideClick: false,
  269. icon: "info",
  270. text: "Espere por favor..."
  271. });
  272. Swal.showLoading();
  273. this.instrumentService
  274. .projectionModification(this.investmentProposalID, this.form.value)
  275. .subscribe(
  276. resp => {
  277. this.investmentsService
  278. .updateInstrumentProjection(
  279. this.investmentProposalID,
  280. objProjection
  281. )
  282. .subscribe(
  283. success => {
  284. if (success) {
  285. Swal.fire({
  286. allowOutsideClick: false,
  287. icon: "success",
  288. showCancelButton: false,
  289. title: "Exito",
  290. confirmButtonText: "La información ha sido actualizada"
  291. }).then(result => {
  292. Swal.close();
  293. //window.location.reload();
  294. });
  295. }
  296. },
  297. err => {
  298. Swal.fire({
  299. icon: "error",
  300. title: "Error al guardar",
  301. text: err.message
  302. });
  303. }
  304. );
  305. },
  306. err => {
  307. Swal.fire({
  308. icon: "error",
  309. title: "Error al guardar",
  310. text: err.message
  311. });
  312. }
  313. );
  314. }
  315. buildForm(items: any[]): FormGroup[] {
  316. return items.map(x => this.buildItem(x));
  317. }
  318. //return a formGroup
  319. buildItem(item: any): FormGroup {
  320. return new FormGroup({
  321. id_proyeccion_ingreso: new FormControl(item.id_proyeccion_ingreso),
  322. posicion: new FormControl(item.posicion),
  323. plazo: new FormControl(item.plazo),
  324. fecha_pago: new FormControl(item.fecha_pago),
  325. ingreso_bruto: new FormControl(item.ingreso_bruto),
  326. costo_cedeval: new FormControl(item.costo_cedeval),
  327. costo_transferencia: new FormControl(item.costo_transferencia),
  328. costo_banco: new FormControl(item.costo_banco),
  329. otros_costos: new FormControl(item.otros_costos),
  330. renta: new FormControl(item.renta),
  331. ingreso_neto: new FormControl(item.ingreso_neto),
  332. id_valor_comercial: new FormControl(item.id_valor_comercial)
  333. });
  334. }
  335. }