pper.component.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import { Component, OnInit, Input, ViewChild } from "@angular/core";
  2. import { InstrumentComponent } from "@app/components/investment-proposals/instrument/instrument.component";
  3. import { FormBuilder, FormGroup, Validators } from "@angular/forms";
  4. import { IAngularMyDpOptions, IMyDateModel } from "angular-mydatepicker";
  5. import { formatDate, DatePipe } from "@angular/common";
  6. import { Router } from "@angular/router";
  7. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  8. import { CatalogsService } from "@app/services/catalogs.service";
  9. import { InstrumentCalculations } from "@app/services/instrument-calculations.service";
  10. import Swal from "sweetalert2";
  11. import { GeneralInfo } from "@app/models/investment-proposal-form";
  12. import { parse } from "date-fns";
  13. import { MatPaginator } from "@angular/material/paginator";
  14. import { MatSort } from "@angular/material/sort";
  15. import { MatTableDataSource } from "@angular/material/table";
  16. @Component({
  17. selector: "app-pper",
  18. templateUrl: "./pper.component.html"
  19. })
  20. export class PPER implements InstrumentComponent {
  21. title: string = "Préstamo personal";
  22. @Input() data: any;
  23. @Input() summary: boolean;
  24. @Input() investmentID: string;
  25. displayedColumns: string[] = [
  26. "posicion",
  27. "plazo",
  28. "fecha_pago",
  29. "tasa_porcentaje",
  30. "saldo_inicial",
  31. "pago_cuota",
  32. "ingreso_bruto",
  33. "iva_interes",
  34. "ingreso_neto",
  35. "abono_capital",
  36. "capital_restante",
  37. "pago_seguro_dano",
  38. "pago_seguro_vivienda",
  39. "total_cuota",
  40. "amortizacion_capital",
  41. "total_recibir"
  42. ];
  43. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  44. @ViewChild(MatSort, { static: true }) sort: MatSort;
  45. form: any;
  46. general: GeneralInfo;
  47. // For daterange
  48. daysLabels: any = {
  49. su: "Dom",
  50. mo: "Lun",
  51. tu: "Mar",
  52. we: "Mie",
  53. th: "Jue",
  54. fr: "Vie",
  55. sa: "Sab"
  56. };
  57. monthsLabels: any = {
  58. 1: "Ene",
  59. 2: "Feb",
  60. 3: "Mar",
  61. 4: "Abr",
  62. 5: "May",
  63. 6: "Jun",
  64. 7: "Jul",
  65. 8: "Ago",
  66. 9: "Sep",
  67. 10: "Oct",
  68. 11: "Nov",
  69. 12: "Dic"
  70. };
  71. investmentProposalForm: FormGroup;
  72. myDpOptions: IAngularMyDpOptions = {
  73. dateRange: false,
  74. dateFormat: "dd/mm/yyyy",
  75. dayLabels: this.daysLabels,
  76. monthLabels: this.monthsLabels
  77. };
  78. myDateInit: boolean = true;
  79. m_fecha_otorgamiento: IMyDateModel;
  80. m_fecha_liquidacion: IMyDateModel;
  81. m_fecha_rendencion: IMyDateModel;
  82. submitted: boolean = false;
  83. instrument_exists: boolean;
  84. instrument_work: any = [];
  85. financials: any;
  86. base_types: any;
  87. ingreso_bruto: number = 0.0;
  88. ingreso_neto: number = 0.0;
  89. monto_prestamo: number = 0.0;
  90. tasa_porcentaje: number = 0.0;
  91. plazo: number = 0.0;
  92. iva_porcentaje: number = 0.0;
  93. plazo_gracia: number = 0.0;
  94. iva: number = 0.0;
  95. proyecciones: any;
  96. pperObject: {};
  97. dataSource = new MatTableDataSource(this.proyecciones);
  98. dataSource2 = new MatTableDataSource(this.proyecciones);
  99. hasProjections: boolean;
  100. fecha_vencimiento: any;
  101. consolidado_proyeccion: any;
  102. fecha_otorgamiento: any;
  103. comision_desembolso: any;
  104. fecha_gracia: any;
  105. fecha_fin_otorgamiento: any;
  106. pago_seguro_dano: any;
  107. pago_seguro_vivienda: any;
  108. total_cuotas_mensuales: any;
  109. cuota_sin_seguros: any;
  110. constructor(
  111. private formBuilder: FormBuilder,
  112. private router: Router,
  113. private formDataService: FormInvestmentProposalService,
  114. private catalogService: CatalogsService,
  115. private instrumentCalcService: InstrumentCalculations,
  116. public datepipe: DatePipe
  117. ) {
  118. this.instrument_work = this.formDataService.getWork();
  119. this.instrument_exists = this.instrument_work == undefined;
  120. this.general = this.formDataService.getGeneralInfo();
  121. if (
  122. this.instrument_work != undefined &&
  123. this.instrument_work.proyecciones != ""
  124. ) {
  125. this.consolidado_proyeccion = this.instrument_work.proyecciones[
  126. this.instrument_work.proyecciones.length - 1
  127. ];
  128. this.hasProjections = true;
  129. this.dataSource2.data = this.instrument_work.proyecciones.slice(0, -1);
  130. this.dataSource2.paginator = this.paginator;
  131. this.dataSource2.sort = this.sort;
  132. } else {
  133. this.hasProjections = false;
  134. }
  135. this.investmentProposalForm = this.formBuilder.group({
  136. monto_prestamo: [
  137. this.instrument_exists ? "" : this.instrument_work.monto_prestamo,
  138. [
  139. Validators.required,
  140. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  141. ]
  142. ],
  143. tasa_porcentaje: [
  144. this.instrument_exists ? "" : this.instrument_work.tasa_porcentaje,
  145. [
  146. Validators.required,
  147. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  148. ]
  149. ],
  150. iva_porcentaje: [
  151. this.instrument_exists ? "" : this.instrument_work.iva_porcentaje,
  152. [
  153. Validators.required,
  154. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  155. ]
  156. ],
  157. plazo_gracia: [
  158. this.instrument_exists ? "" : this.instrument_work.plazo_gracia,
  159. [
  160. Validators.required,
  161. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  162. ]
  163. ],
  164. pago_seguro_dano: [
  165. this.instrument_exists ? "" : this.instrument_work.pago_seguro_dano,
  166. [
  167. Validators.required,
  168. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  169. ]
  170. ],
  171. pago_seguro_vivienda: [
  172. this.instrument_exists ? "" : this.instrument_work.pago_seguro_vivienda,
  173. [
  174. Validators.required,
  175. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  176. ]
  177. ],
  178. total_cuotas_mensuales: [
  179. this.instrument_exists
  180. ? ""
  181. : this.instrument_work.total_cuotas_mensuales,
  182. [
  183. Validators.required,
  184. Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/)
  185. ]
  186. ],
  187. nombre_deudor: [
  188. this.instrument_exists ? "" : this.instrument_work.nombre_deudor
  189. ],
  190. numero_deudor: [
  191. this.instrument_exists ? "" : this.instrument_work.numero_deudor
  192. ],
  193. fecha_otorgamiento: [
  194. this.instrument_exists
  195. ? ""
  196. : {
  197. isRange: false,
  198. singleDate: {
  199. jsDate: parse(
  200. this.instrument_work.fecha_otorgamiento,
  201. "dd/MM/yyyy",
  202. new Date()
  203. ),
  204. formatted: this.instrument_work.fecha_otorgamiento
  205. }
  206. },
  207. Validators.required
  208. ]
  209. });
  210. }
  211. get f() {
  212. return this.investmentProposalForm.controls;
  213. }
  214. save(form: any): boolean {
  215. if (!form.valid) {
  216. return false;
  217. }
  218. this.formDataService.setWork(this.pperObject);
  219. return true;
  220. }
  221. getCalculations(form: any, saveForm: boolean) {
  222. this.submitted = true;
  223. if (!form.valid) {
  224. return false;
  225. }
  226. Swal.fire({
  227. allowOutsideClick: false,
  228. icon: "info",
  229. text: "Espere por favor..."
  230. });
  231. Swal.showLoading();
  232. this.instrumentCalcService
  233. .pperCalc(
  234. "PPER", // Codigo del instrumento
  235. {
  236. id_tipo_base: +this.general.base_anual,
  237. id_formato_ingreso: +this.general.formato_ingreso,
  238. id_periodicidad: +this.general.periodicidad
  239. },
  240. {
  241. monto_prestamo: this.f.monto_prestamo.value,
  242. fecha_otorgamiento: this.f.fecha_otorgamiento.value.singleDate
  243. .formatted,
  244. plazo_gracia: this.f.plazo_gracia.value,
  245. tasa_porcentaje: this.f.tasa_porcentaje.value,
  246. iva_porcentaje: this.f.iva_porcentaje.value,
  247. pago_seguro_dano: this.f.pago_seguro_dano.value,
  248. pago_seguro_vivienda: this.f.pago_seguro_vivienda.value,
  249. total_cuotas_mensuales: this.f.total_cuotas_mensuales.value
  250. }
  251. )
  252. .subscribe(
  253. ans => {
  254. this.monto_prestamo = ans["result"]["instrumento"]["monto_prestamo"];
  255. this.fecha_otorgamiento =
  256. ans["result"]["instrumento"]["fecha_otorgamiento"];
  257. this.fecha_gracia = ans["result"]["instrumento"]["fecha_gracia"];
  258. this.fecha_fin_otorgamiento =
  259. ans["result"]["instrumento"]["fecha_fin_otorgamiento"];
  260. this.plazo_gracia = ans["result"]["instrumento"]["plazo_gracia"];
  261. this.tasa_porcentaje =
  262. ans["result"]["instrumento"]["tasa_porcentaje"];
  263. this.iva_porcentaje = ans["result"]["instrumento"]["iva_porcentaje"];
  264. this.pago_seguro_dano =
  265. ans["result"]["instrumento"]["pago_seguro_dano"];
  266. this.pago_seguro_vivienda =
  267. ans["result"]["instrumento"]["pago_seguro_vivienda"];
  268. this.total_cuotas_mensuales =
  269. ans["result"]["instrumento"]["total_cuotas_mensuales"];
  270. this.cuota_sin_seguros =
  271. ans["result"]["instrumento"]["cuota_sin_seguros"];
  272. this.proyecciones = ans["result"]["proyecciones"];
  273. if (this.proyecciones != undefined && this.proyecciones.length > 0) {
  274. this.hasProjections = true;
  275. let proyecciones_temp = this.proyecciones;
  276. this.consolidado_proyeccion =
  277. proyecciones_temp[proyecciones_temp.length - 1];
  278. this.proyecciones = ans["result"]["proyecciones"];
  279. }
  280. this.dataSource.data = this.proyecciones.slice(0, -1);
  281. this.dataSource.paginator = this.paginator;
  282. this.dataSource.sort = this.sort;
  283. this.pperObject = {
  284. monto_prestamo: this.investmentProposalForm.value.monto_prestamo,
  285. plazo_gracia: this.investmentProposalForm.value.plazo_gracia,
  286. tasa_porcentaje: this.investmentProposalForm.value.tasa_porcentaje,
  287. iva_porcentaje: this.investmentProposalForm.value.iva_porcentaje,
  288. pago_seguro_dano: this.investmentProposalForm.value
  289. .pago_seguro_dano,
  290. pago_seguro_vivienda: this.investmentProposalForm.value
  291. .pago_seguro_vivienda,
  292. total_cuotas_mensuales: this.investmentProposalForm.value
  293. .total_cuotas_mensuales,
  294. numero_deudor: this.investmentProposalForm.value.numero_deudor,
  295. nombre_deudor: this.investmentProposalForm.value.nombre_deudor,
  296. fecha_otorgamiento: this.f.fecha_otorgamiento.value.singleDate
  297. .formatted,
  298. fecha_fin_otorgamiento: this.fecha_fin_otorgamiento,
  299. fecha_gracia: this.fecha_gracia,
  300. cuota_sin_seguros: this.cuota_sin_seguros,
  301. proyecciones: this.proyecciones
  302. };
  303. this.formDataService.setWork(this.pperObject);
  304. Swal.close();
  305. if (saveForm == true) {
  306. if (this.investmentID != undefined) {
  307. this.router.navigate(["/investment-proposal/complement-info"], {
  308. queryParams: { id: this.investmentID }
  309. });
  310. } else {
  311. this.router.navigate(["/investment-proposal/complement-info"]);
  312. }
  313. }
  314. },
  315. err => {
  316. Swal.fire({
  317. icon: "error",
  318. title: "Error en el servidor",
  319. text: "No su pudo obtener la informacion"
  320. });
  321. return false;
  322. }
  323. );
  324. }
  325. goToPrevious() {
  326. this.submitted = true;
  327. if (this.investmentID != undefined) {
  328. this.router.navigate(["/investment-proposal/general-info"], {
  329. queryParams: { id: this.investmentID }
  330. });
  331. } else {
  332. this.router.navigate(["/investment-proposal/general-info"]);
  333. }
  334. }
  335. goToNext(form: any) {
  336. this.getCalculations(form, true);
  337. }
  338. }