pemp.component.ts 10 KB

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