vcn.costs.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. price:string;
  30. value:any;
  31. title: string = "Costos para valores comerciales";
  32. displayedColumns: string[] = [
  33. "codigo_inversion",
  34. "asunto",
  35. "id_tipo_mercado",
  36. "id_inversion_instrumento",
  37. "id"
  38. ];
  39. //displayedColumns: string[] = ['state'];
  40. listProposals: InvestmentProposal[];
  41. dataSource = new MatTableDataSource(this.listProposals);
  42. resultsLength = 0;
  43. isLoadingResults = true;
  44. isRateLimitReached = false;
  45. userRole: any;
  46. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  47. @ViewChild(MatSort, { static: true }) sort: MatSort;
  48. role_number: any;
  49. investmentProposalID: string;
  50. form: FormArray;
  51. investmentProposalForm: FormGroup;
  52. proyecciones: any;
  53. proyeccionesProps = [];
  54. dataRetrieved: boolean = false;
  55. array1;
  56. array2;
  57. array3;
  58. investment: any;
  59. instrument_work: any;
  60. instrument_exists: boolean;
  61. constructor(
  62. private catalogService: CatalogsService,
  63. private investmentsService: InvestmentsService,
  64. private authService: AuthService,
  65. private formInvestmentProposal: FormInvestmentProposalService,
  66. private router: Router,
  67. private route: ActivatedRoute,
  68. private instrumentService: InstrumentCalculations,
  69. private formBuilder: FormBuilder
  70. ) {
  71. const decodedToken = this.helper.decodeToken(
  72. this.authService.getJwtToken()
  73. );
  74. this.userRole = decodedToken.groups;
  75. Swal.fire({
  76. allowOutsideClick: false,
  77. icon: "info",
  78. text: "Espere por favor..."
  79. });
  80. Swal.showLoading();
  81. }
  82. ngOnInit() {
  83. Swal.close();
  84. const formDataObj = {};
  85. this.route.params.subscribe(params => {
  86. this.investmentProposalID = params["id"];
  87. });
  88. if (this.investmentProposalID == undefined)
  89. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  90. if (this.investmentProposalID != undefined) {
  91. this.investmentsService
  92. .getProposalInvestment(this.investmentProposalID)
  93. .subscribe(
  94. res => {
  95. this.investment = res["result"];
  96. this.instrument_work =
  97. res["result"]["id_inversion_instrumento"]["instrumento"];
  98. this.instrument_exists = true;
  99. this.investmentProposalForm = this.formBuilder.group({
  100. valor_par: [
  101. !this.instrument_exists ? false : this.instrument_work.valor_par
  102. ],
  103. valor_nominal: [
  104. !this.instrument_exists
  105. ? ""
  106. : this.instrument_work.valor_nominal,
  107. [
  108. Validators.required,
  109. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  110. ]
  111. ],
  112. otros_costos: [
  113. !this.instrument_exists ? 0 : this.instrument_work.otros_costos,
  114. [Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)]
  115. ],
  116. plazo: [
  117. !this.instrument_exists ? "" : this.instrument_work.plazo,
  118. [Validators.required]
  119. ],
  120. renta_porcentaje: [
  121. !this.instrument_exists
  122. ? ""
  123. : this.instrument_work.renta_porcentaje,
  124. [Validators.required]
  125. ],
  126. comision_casa_porcentaje: [
  127. !this.instrument_exists
  128. ? ""
  129. : this.instrument_work.comision_casa_porcentaje,
  130. [
  131. Validators.required,
  132. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  133. ]
  134. ],
  135. comision_bolsa_porcentaje: [
  136. !this.instrument_exists
  137. ? ""
  138. : this.instrument_work.comision_bolsa_porcentaje,
  139. [
  140. Validators.required,
  141. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  142. ]
  143. ],
  144. rendimiento_bruto: [
  145. !this.instrument_exists
  146. ? ""
  147. : this.instrument_work.rendimiento_bruto,
  148. [
  149. Validators.required,
  150. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  151. ]
  152. ],
  153. /*formato_ingreso: [
  154. !this.instrument_exists ? "" : this.instrument_work.formato_ingreso,
  155. [Validators.required]
  156. ],*/
  157. fecha_liquidacion: [
  158. !this.instrument_exists
  159. ? ""
  160. : this.instrument_work.fecha_liquidacion,
  161. [Validators.required]
  162. ],
  163. fecha_vencimiento: [
  164. !this.instrument_exists
  165. ? ""
  166. : this.instrument_work.fecha_vencimiento,
  167. [Validators.required]
  168. ],
  169. fecha_operacion: [
  170. !this.instrument_exists
  171. ? ""
  172. : this.instrument_work.fecha_operacion,
  173. [Validators.required]
  174. ],
  175. fecha_ultima_cupon: [
  176. !this.instrument_exists
  177. ? ""
  178. : this.instrument_work.fecha_ultima_cupon,
  179. [Validators.required]
  180. ]
  181. });
  182. },
  183. err => {}
  184. );
  185. this.investmentsService
  186. .getProposalInvestment(this.investmentProposalID)
  187. .subscribe(
  188. res => {
  189. this.proyecciones =
  190. res["result"]["id_inversion_instrumento"]["instrumento"][
  191. "proyecciones"
  192. ];
  193. this.proyecciones.pop();
  194. this.form = new FormArray(this.buildForm(this.proyecciones));
  195. this.dataRetrieved = true;
  196. },
  197. err => {
  198. Swal.fire({
  199. icon: "error",
  200. title: "Error en el servidor",
  201. text: err.message
  202. });
  203. }
  204. );
  205. }
  206. setTimeout(() => {
  207. Swal.close();
  208. }, 1200);
  209. }
  210. clearProyeccion(proyecciones,nombres,tipoDato){
  211. let clearPro = []
  212. proyecciones.forEach(createNewProyeccion);
  213. function createNewProyeccion(arrFila, index) {
  214. nombres.forEach(loopNombres);
  215. function loopNombres(nombre, index) {
  216. arrFila[nombre] = parseFloat(arrFila[nombre].toString().replace(",",""))
  217. }
  218. clearPro.push(arrFila)
  219. }
  220. return clearPro;
  221. }
  222. public inputValidator(event: any) {
  223. //console.log(event.target.value);
  224. const pattern = /^[0-9]*$/;
  225. //let inputChar = String.fromCharCode(event.charCode)
  226. if (!pattern.test(event.target.value)) {
  227. event.target.value = event.target.value.replace(/[^\d,.]+/g, '');
  228. // invalid character, prevent input
  229. }
  230. }
  231. recalculateProjectionChanges() {
  232. let nombresProyecciones = ["costo_transferencia",
  233. "costo_cedeval",
  234. "costo_banco",
  235. "otros_costos"]
  236. let clearPro = this.clearProyeccion(this.form.value,nombresProyecciones,"decimal")
  237. let objProjection = { proyecciones: clearPro };
  238. Swal.fire({
  239. allowOutsideClick: false,
  240. icon: "info",
  241. text: "Espere por favor..."
  242. });
  243. Swal.showLoading();
  244. this.instrumentService
  245. .projectionCalc(this.investmentProposalID, this.form.value)
  246. .subscribe(
  247. result => {
  248. Swal.fire({
  249. allowOutsideClick: false,
  250. icon: "success",
  251. showCancelButton: false,
  252. title: "Exito"
  253. });
  254. this.proyecciones = result["result"]["proyecciones"];
  255. this.proyecciones.pop();
  256. this.form.clear();
  257. this.form = new FormArray(this.buildForm(this.proyecciones));
  258. Swal.close();
  259. //window.location.reload();
  260. },
  261. err => {
  262. Swal.fire({
  263. icon: "error",
  264. title: "Error al guardar",
  265. text: err.message
  266. });
  267. }
  268. );
  269. }
  270. saveProjection() {
  271. let objProjection = { proyecciones: this.form.value };
  272. Swal.fire({
  273. allowOutsideClick: false,
  274. icon: "info",
  275. text: "Espere por favor..."
  276. });
  277. Swal.showLoading();
  278. this.instrumentService
  279. .projectionModification(this.investmentProposalID, objProjection)
  280. .subscribe(
  281. resp => {
  282. Swal.fire({
  283. allowOutsideClick: false,
  284. icon: "success",
  285. showCancelButton: false,
  286. title: "Exito",
  287. confirmButtonText: "La información ha sido actualizada"
  288. }).then(result => {
  289. Swal.close();
  290. //window.location.reload();
  291. });
  292. },
  293. err => {
  294. Swal.fire({
  295. icon: "error",
  296. title: "Error al guardar",
  297. text: err.message
  298. });
  299. }
  300. );
  301. }
  302. buildForm(items: any[]): FormGroup[] {
  303. return items.map(x => this.buildItem(x));
  304. }
  305. //return a formGroup
  306. buildItem(item: any): FormGroup {
  307. return new FormGroup({
  308. id_proyeccion_ingreso: new FormControl(item.id_proyeccion_ingreso),
  309. posicion: new FormControl(item.posicion),
  310. plazo: new FormControl(item.plazo),
  311. fecha_pago: new FormControl(item.fecha_pago),
  312. ingreso_bruto: new FormControl(item.ingreso_bruto),
  313. costo_cedeval: new FormControl(item.costo_cedeval),
  314. costo_transferencia: new FormControl(item.costo_transferencia),
  315. costo_banco: new FormControl(item.costo_banco),
  316. otros_costos: new FormControl(item.otros_costos),
  317. renta: new FormControl(item.renta),
  318. ingreso_neto: new FormControl(item.ingreso_neto),
  319. id_valor_comercial: new FormControl(item.id_valor_comercial)
  320. });
  321. }
  322. }