pper.costs.component.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. recalculateProjectionChanges() {
  118. let objProjection = { proyecciones: this.form.value };
  119. Swal.fire({
  120. allowOutsideClick: false,
  121. icon: "info",
  122. text: "Espere por favor..."
  123. });
  124. Swal.showLoading();
  125. this.instrumentService
  126. .projectionCalc(this.investmentProposalID, this.form.value)
  127. .subscribe(
  128. result => {
  129. Swal.fire({
  130. allowOutsideClick: false,
  131. icon: "success",
  132. showCancelButton: false,
  133. title: "Exito"
  134. });
  135. this.proyecciones = result["result"]["proyecciones"];
  136. this.proyecciones.pop();
  137. this.form.clear();
  138. this.form = new FormArray(this.buildForm(this.proyecciones));
  139. Swal.close();
  140. //window.location.reload();
  141. },
  142. err => {
  143. Swal.fire({
  144. icon: "error",
  145. title: "Error al guardar",
  146. text: err.message
  147. });
  148. }
  149. );
  150. }
  151. saveProjection() {
  152. let objProjection = { proyecciones: this.form.value };
  153. Swal.fire({
  154. allowOutsideClick: false,
  155. icon: "info",
  156. text: "Espere por favor..."
  157. });
  158. Swal.showLoading();
  159. this.instrumentService
  160. .projectionModification(this.investmentProposalID, objProjection)
  161. .subscribe(
  162. resp => {
  163. Swal.fire({
  164. allowOutsideClick: false,
  165. icon: "success",
  166. showCancelButton: false,
  167. title: "Exito",
  168. confirmButtonText: "La información ha sido actualizada"
  169. }).then(result => {
  170. Swal.close();
  171. //window.location.reload();
  172. });
  173. },
  174. err => {
  175. Swal.fire({
  176. icon: "error",
  177. title: "Error al guardar",
  178. text: err.message
  179. });
  180. }
  181. );
  182. }
  183. buildForm(items: any[]): FormGroup[] {
  184. return items.map(x => this.buildItem(x));
  185. }
  186. //return a formGroup
  187. buildItem(item: any): FormGroup {
  188. return new FormGroup({
  189. id_proyeccion_ingreso: new FormControl(item.id_proyeccion_ingreso),
  190. posicion: new FormControl(item.posicion),
  191. plazo: new FormControl(item.plazo),
  192. fecha_pago: new FormControl(item.fecha_pago),
  193. tasa_porcentaje: new FormControl(item.tasa_porcentaje),
  194. saldo_inicial: new FormControl(item.saldo_inicial),
  195. pago_cuota: new FormControl(item.pago_cuota),
  196. ingreso_bruto: new FormControl(item.ingreso_bruto),
  197. iva_interes: new FormControl(item.iva_interes),
  198. ingreso_neto: new FormControl(item.ingreso_neto),
  199. abono_capital: new FormControl(item.abono_capital),
  200. capital_restante: new FormControl(item.capital_restante),
  201. pago_seguro_dano: new FormControl(item.pago_seguro_dano),
  202. pago_seguro_vivienda: new FormControl(item.pago_seguro_vivienda),
  203. total_cuota: new FormControl(item.total_cuota),
  204. amortizacion_capital: new FormControl(item.amortizacion_capital),
  205. total_recibir: new FormControl(item.total_recibir)
  206. });
  207. }
  208. }