pbur.costs.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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-pbur-costs",
  24. templateUrl: "./pbur.costs.component.html"
  25. //styleUrls: ["./dap.costs.component.scss"]
  26. })
  27. export class PBURCostsComponent implements OnInit {
  28. helper = new JwtHelperService();
  29. title: string = "Costos para papel bursátil";
  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.proyecciones.pop();
  192. this.form = new FormArray(this.buildForm(this.proyecciones));
  193. this.dataRetrieved = true;
  194. },
  195. err => {
  196. Swal.fire({
  197. icon: "error",
  198. title: "Error en el servidor",
  199. text: err.message
  200. });
  201. }
  202. );
  203. }
  204. setTimeout(() => {
  205. Swal.close();
  206. }, 1200);
  207. }
  208. clearProyeccion(proyecciones,nombres,tipoDato){
  209. let clearPro = []
  210. proyecciones.forEach(createNewProyeccion);
  211. function createNewProyeccion(arrFila, index) {
  212. nombres.forEach(loopNombres);
  213. function loopNombres(nombre, index) {
  214. arrFila[nombre] = parseFloat(arrFila[nombre].toString().replace(",",""))
  215. }
  216. clearPro.push(arrFila)
  217. }
  218. return clearPro;
  219. }
  220. public inputValidator(event: any) {
  221. //console.log(event.target.value);
  222. const pattern = /^[0-9]*$/;
  223. //let inputChar = String.fromCharCode(event.charCode)
  224. if (!pattern.test(event.target.value)) {
  225. event.target.value = event.target.value.replace(/[^\d,.]+/g, '');
  226. // invalid character, prevent input
  227. }
  228. }
  229. recalculateProjectionChanges() {
  230. let nombresProyecciones = ["costo_transferencia",
  231. "costo_cedeval",
  232. "costo_banco",
  233. "otros_costos"]
  234. let clearPro = this.clearProyeccion(this.form.value,nombresProyecciones,"decimal")
  235. let objProjection = { proyecciones: clearPro };
  236. Swal.fire({
  237. allowOutsideClick: false,
  238. icon: "info",
  239. text: "Espere por favor..."
  240. });
  241. Swal.showLoading();
  242. this.instrumentService
  243. .projectionCalc(this.investmentProposalID, this.form.value)
  244. .subscribe(
  245. result => {
  246. Swal.fire({
  247. allowOutsideClick: false,
  248. icon: "success",
  249. showCancelButton: false,
  250. title: "Exito"
  251. });
  252. this.proyecciones = result["result"]["proyecciones"];
  253. this.proyecciones.pop();
  254. this.form.clear();
  255. this.form = new FormArray(this.buildForm(this.proyecciones));
  256. Swal.close();
  257. //window.location.reload();
  258. },
  259. err => {
  260. Swal.fire({
  261. icon: "error",
  262. title: "Error al guardar",
  263. text: err.message
  264. });
  265. }
  266. );
  267. }
  268. saveProjection() {
  269. let objProjection = { proyecciones: this.form.value };
  270. Swal.fire({
  271. allowOutsideClick: false,
  272. icon: "info",
  273. text: "Espere por favor..."
  274. });
  275. Swal.showLoading();
  276. this.instrumentService
  277. .projectionModification(this.investmentProposalID, objProjection)
  278. .subscribe(
  279. resp => {
  280. Swal.fire({
  281. allowOutsideClick: false,
  282. icon: "success",
  283. showCancelButton: false,
  284. title: "Exito",
  285. confirmButtonText: "La información ha sido actualizada"
  286. }).then(result => {
  287. Swal.close();
  288. //window.location.reload();
  289. });
  290. },
  291. err => {
  292. Swal.fire({
  293. icon: "error",
  294. title: "Error al guardar",
  295. text: err.message
  296. });
  297. }
  298. );
  299. }
  300. buildForm(items: any[]): FormGroup[] {
  301. return items.map(x => this.buildItem(x));
  302. }
  303. //return a formGroup
  304. buildItem(item: any): FormGroup {
  305. return new FormGroup({
  306. id_proyeccion_ingreso: new FormControl(item.id_proyeccion_ingreso),
  307. posicion: new FormControl(item.posicion),
  308. plazo: new FormControl(item.plazo),
  309. fecha_pago: new FormControl(item.fecha_pago),
  310. ingreso_bruto: new FormControl(item.ingreso_bruto),
  311. costo_cedeval: new FormControl(item.costo_cedeval),
  312. costo_transferencia: new FormControl(item.costo_transferencia),
  313. costo_banco: new FormControl(item.costo_banco),
  314. otros_costos: new FormControl(item.otros_costos),
  315. renta: new FormControl(item.renta),
  316. ingreso_neto: new FormControl(item.ingreso_neto),
  317. id_valor_comercial: new FormControl(item.id_valor_comercial)
  318. });
  319. }
  320. }