pper.costs.component.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-pper-costs",
  24. templateUrl: "./pper.costs.component.html"
  25. //styleUrls: ["./dap.costs.component.scss"]
  26. })
  27. export class PPERCostsComponent implements OnInit {
  28. helper = new JwtHelperService();
  29. title: string = "Proyecciones para préstamos personales";
  30. listProposals: InvestmentProposal[];
  31. dataSource = new MatTableDataSource(this.listProposals);
  32. resultsLength = 0;
  33. isLoadingResults = true;
  34. isRateLimitReached = false;
  35. userRole: any;
  36. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  37. @ViewChild(MatSort, { static: true }) sort: MatSort;
  38. role_number: any;
  39. investmentProposalID: string;
  40. form: FormArray;
  41. investmentProposalForm: FormGroup;
  42. proyecciones: any;
  43. proyeccionesProps = [];
  44. dataRetrieved: boolean = false;
  45. array1;
  46. array2;
  47. array3;
  48. investment: any;
  49. instrument_work: any;
  50. instrument_exists: boolean;
  51. constructor(
  52. private catalogService: CatalogsService,
  53. private investmentsService: InvestmentsService,
  54. private authService: AuthService,
  55. private formInvestmentProposal: FormInvestmentProposalService,
  56. private router: Router,
  57. private route: ActivatedRoute,
  58. private instrumentService: InstrumentCalculations,
  59. private formBuilder: FormBuilder
  60. ) {
  61. const decodedToken = this.helper.decodeToken(
  62. this.authService.getJwtToken()
  63. );
  64. this.userRole = decodedToken.groups;
  65. Swal.fire({
  66. allowOutsideClick: false,
  67. icon: "info",
  68. text: "Espere por favor..."
  69. });
  70. Swal.showLoading();
  71. }
  72. ngOnInit() {
  73. Swal.close();
  74. const formDataObj = {};
  75. this.route.params.subscribe(params => {
  76. this.investmentProposalID = params["id"];
  77. });
  78. if (this.investmentProposalID == undefined)
  79. this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");
  80. if (this.investmentProposalID != undefined) {
  81. this.investmentsService
  82. .getProposalInvestment(this.investmentProposalID)
  83. .subscribe(
  84. res => {
  85. this.investment = res["result"];
  86. this.instrument_work =
  87. res["result"]["id_inversion_instrumento"]["instrumento"];
  88. this.instrument_exists = true;
  89. },
  90. err => {}
  91. );
  92. this.investmentsService
  93. .getProposalInvestment(this.investmentProposalID)
  94. .subscribe(
  95. res => {
  96. this.proyecciones =
  97. res["result"]["id_inversion_instrumento"]["instrumento"][
  98. "proyecciones"
  99. ];
  100. this.proyecciones.pop();
  101. this.form = new FormArray(this.buildForm(this.proyecciones));
  102. this.dataRetrieved = true;
  103. },
  104. err => {
  105. Swal.fire({
  106. icon: "error",
  107. title: "Error en el servidor",
  108. text: err.message
  109. });
  110. }
  111. );
  112. }
  113. setTimeout(() => {
  114. Swal.close();
  115. }, 1200);
  116. }
  117. clearProyeccion(proyecciones,nombres,tipoDato){
  118. let clearPro = []
  119. proyecciones.forEach(createNewProyeccion);
  120. function createNewProyeccion(arrFila, index) {
  121. nombres.forEach(loopNombres);
  122. function loopNombres(nombre, index) {
  123. arrFila[nombre] = parseFloat(arrFila[nombre].toString().replace(",",""))
  124. }
  125. clearPro.push(arrFila)
  126. }
  127. return clearPro;
  128. }
  129. public inputValidator(event: any) {
  130. //console.log(event.target.value);
  131. const pattern = /^[0-9]*$/;
  132. //let inputChar = String.fromCharCode(event.charCode)
  133. if (!pattern.test(event.target.value)) {
  134. event.target.value = event.target.value.replace(/[^\d,.]+/g, '');
  135. // invalid character, prevent input
  136. }
  137. }
  138. recalculateProjectionChanges() {
  139. let nombresProyecciones = ["tasa_porcentaje",
  140. "amortizacion_capital",
  141. "pago_seguro_dano",
  142. "pago_seguro_vivienda"]
  143. let clearPro = this.clearProyeccion(this.form.value,nombresProyecciones,"decimal")
  144. let objProjection = { proyecciones: clearPro };
  145. Swal.fire({
  146. allowOutsideClick: false,
  147. icon: "info",
  148. text: "Espere por favor..."
  149. });
  150. Swal.showLoading();
  151. this.instrumentService
  152. .projectionCalc(this.investmentProposalID, this.form.value)
  153. .subscribe(
  154. result => {
  155. Swal.fire({
  156. allowOutsideClick: false,
  157. icon: "success",
  158. showCancelButton: false,
  159. title: "Exito"
  160. });
  161. this.proyecciones = result["result"]["proyecciones"];
  162. this.proyecciones.pop();
  163. this.form.clear();
  164. this.form = new FormArray(this.buildForm(this.proyecciones));
  165. Swal.close();
  166. //window.location.reload();
  167. },
  168. err => {
  169. Swal.fire({
  170. icon: "error",
  171. title: "Error al guardar",
  172. text: err.message
  173. });
  174. }
  175. );
  176. }
  177. saveProjection() {
  178. let objProjection = { proyecciones: this.form.value };
  179. Swal.fire({
  180. allowOutsideClick: false,
  181. icon: "info",
  182. text: "Espere por favor..."
  183. });
  184. Swal.showLoading();
  185. this.instrumentService
  186. .projectionModification(this.investmentProposalID, objProjection)
  187. .subscribe(
  188. resp => {
  189. Swal.fire({
  190. allowOutsideClick: false,
  191. icon: "success",
  192. showCancelButton: false,
  193. title: "Exito",
  194. confirmButtonText: "La información ha sido actualizada"
  195. }).then(result => {
  196. Swal.close();
  197. //window.location.reload();
  198. });
  199. },
  200. err => {
  201. Swal.fire({
  202. icon: "error",
  203. title: "Error al guardar",
  204. text: err.message
  205. });
  206. }
  207. );
  208. }
  209. buildForm(items: any[]): FormGroup[] {
  210. return items.map(x => this.buildItem(x));
  211. }
  212. //return a formGroup
  213. buildItem(item: any): FormGroup {
  214. return new FormGroup({
  215. id_proyeccion_ingreso: new FormControl(item.id_proyeccion_ingreso),
  216. posicion: new FormControl(item.posicion),
  217. plazo: new FormControl(item.plazo),
  218. fecha_pago: new FormControl(item.fecha_pago),
  219. tasa_porcentaje: new FormControl(item.tasa_porcentaje),
  220. saldo_inicial: new FormControl(item.saldo_inicial),
  221. pago_cuota: new FormControl(item.pago_cuota),
  222. ingreso_bruto: new FormControl(item.ingreso_bruto),
  223. iva_interes: new FormControl(item.iva_interes),
  224. ingreso_neto: new FormControl(item.ingreso_neto),
  225. abono_capital: new FormControl(item.abono_capital),
  226. capital_restante: new FormControl(item.capital_restante),
  227. pago_seguro_dano: new FormControl(item.pago_seguro_dano),
  228. pago_seguro_vivienda: new FormControl(item.pago_seguro_vivienda),
  229. total_cuota: new FormControl(item.total_cuota),
  230. amortizacion_capital: new FormControl(item.amortizacion_capital),
  231. total_recibir: new FormControl(item.total_recibir)
  232. });
  233. }
  234. }