import { Component, OnInit, Input, ViewChild } from "@angular/core"; import { InstrumentComponent } from "@app/components/investment-proposals/instrument/instrument.component"; import { FormBuilder, FormGroup, Validators } from "@angular/forms"; import { IAngularMyDpOptions, IMyDateModel } from "angular-mydatepicker"; import { formatDate, DatePipe } from "@angular/common"; import { Router } from "@angular/router"; import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service"; import { CatalogsService } from "@app/services/catalogs.service"; import { InstrumentCalculations } from "@app/services/instrument-calculations.service"; import Swal from "sweetalert2"; import { GeneralInfo } from "@app/models/investment-proposal-form"; import { parse } from "date-fns"; import { MatPaginator } from "@angular/material/paginator"; import { MatSort } from "@angular/material/sort"; import { MatTableDataSource } from "@angular/material/table"; @Component({ selector: "app-pper", templateUrl: "./pper.component.html" }) export class PPER implements InstrumentComponent { title: string = "Préstamo personal"; @Input() data: any; @Input() summary: boolean; @Input() investmentID: string; displayedColumns: string[] = [ "posicion", "plazo", "fecha_pago", "tasa_porcentaje", "saldo_inicial", "pago_cuota", "ingreso_bruto", "iva_interes", "ingreso_neto", "abono_capital", "capital_restante", "pago_seguro_dano", "pago_seguro_vivienda", "total_cuota", "amortizacion_capital", "total_recibir" ]; @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @ViewChild(MatSort, { static: true }) sort: MatSort; form: any; general: GeneralInfo; // For daterange daysLabels: any = { su: "Dom", mo: "Lun", tu: "Mar", we: "Mie", th: "Jue", fr: "Vie", sa: "Sab" }; monthsLabels: any = { 1: "Ene", 2: "Feb", 3: "Mar", 4: "Abr", 5: "May", 6: "Jun", 7: "Jul", 8: "Ago", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dic" }; investmentProposalForm: FormGroup; myDpOptions: IAngularMyDpOptions = { dateRange: false, dateFormat: "dd/mm/yyyy", dayLabels: this.daysLabels, monthLabels: this.monthsLabels }; myDateInit: boolean = true; m_fecha_otorgamiento: IMyDateModel; m_fecha_liquidacion: IMyDateModel; m_fecha_rendencion: IMyDateModel; submitted: boolean = false; instrument_exists: boolean; instrument_work: any = []; financials: any; base_types: any; ingreso_bruto: number = 0.0; ingreso_neto: number = 0.0; monto_prestamo: number = 0.0; tasa_porcentaje: number = 0.0; plazo: number = 0.0; iva_porcentaje: number = 0.0; plazo_gracia: number = 0.0; iva: number = 0.0; proyecciones: any; pperObject: {}; dataSource = new MatTableDataSource(this.proyecciones); dataSource2 = new MatTableDataSource(this.proyecciones); hasProjections: boolean; fecha_vencimiento: any; consolidado_proyeccion: any; fecha_otorgamiento: any; comision_desembolso: any; fecha_gracia: any; fecha_fin_otorgamiento: any; pago_seguro_dano: any; pago_seguro_vivienda: any; total_cuotas_mensuales: any; cuota_sin_seguros: any; constructor( private formBuilder: FormBuilder, private router: Router, private formDataService: FormInvestmentProposalService, private catalogService: CatalogsService, private instrumentCalcService: InstrumentCalculations, public datepipe: DatePipe ) { this.instrument_work = this.formDataService.getWork(); this.instrument_exists = this.instrument_work == undefined; this.general = this.formDataService.getGeneralInfo(); if ( this.instrument_work != undefined && this.instrument_work.proyecciones != "" ) { this.consolidado_proyeccion = this.instrument_work.proyecciones[ this.instrument_work.proyecciones.length - 1 ]; this.hasProjections = true; this.dataSource2.data = this.instrument_work.proyecciones.slice(0, -1); this.dataSource2.paginator = this.paginator; this.dataSource2.sort = this.sort; } else { this.hasProjections = false; } this.investmentProposalForm = this.formBuilder.group({ monto_prestamo: [ this.instrument_exists ? "" : this.instrument_work.monto_prestamo, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], tasa_porcentaje: [ this.instrument_exists ? "" : this.instrument_work.tasa_porcentaje, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], iva_porcentaje: [ this.instrument_exists ? "" : this.instrument_work.iva_porcentaje, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], plazo_gracia: [ this.instrument_exists ? "" : this.instrument_work.plazo_gracia, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], pago_seguro_dano: [ this.instrument_exists ? "" : this.instrument_work.pago_seguro_dano, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], pago_seguro_vivienda: [ this.instrument_exists ? "" : this.instrument_work.pago_seguro_vivienda, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], total_cuotas_mensuales: [ this.instrument_exists ? "" : this.instrument_work.total_cuotas_mensuales, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], nombre_deudor: [ this.instrument_exists ? "" : this.instrument_work.nombre_deudor ], numero_deudor: [ this.instrument_exists ? "" : this.instrument_work.numero_deudor ], fecha_otorgamiento: [ this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_otorgamiento, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_otorgamiento } }, Validators.required ] }); } get f() { return this.investmentProposalForm.controls; } save(form: any): boolean { if (!form.valid) { return false; } this.formDataService.setWork(this.pperObject); return true; } getCalculations(form: any, saveForm: boolean) { this.submitted = true; if (!form.valid) { return false; } Swal.fire({ allowOutsideClick: false, icon: "info", text: "Espere por favor..." }); Swal.showLoading(); this.instrumentCalcService .pperCalc( "PPER", // Codigo del instrumento { id_tipo_base: +this.general.base_anual, id_formato_ingreso: +this.general.formato_ingreso, id_periodicidad: +this.general.periodicidad }, { monto_prestamo: this.f.monto_prestamo.value, fecha_otorgamiento: this.f.fecha_otorgamiento.value.singleDate .formatted, plazo_gracia: this.f.plazo_gracia.value, tasa_porcentaje: this.f.tasa_porcentaje.value, iva_porcentaje: this.f.iva_porcentaje.value, pago_seguro_dano: this.f.pago_seguro_dano.value, pago_seguro_vivienda: this.f.pago_seguro_vivienda.value, total_cuotas_mensuales: this.f.total_cuotas_mensuales.value } ) .subscribe( ans => { this.monto_prestamo = ans["result"]["instrumento"]["monto_prestamo"]; this.fecha_otorgamiento = ans["result"]["instrumento"]["fecha_otorgamiento"]; this.fecha_gracia = ans["result"]["instrumento"]["fecha_gracia"]; this.fecha_fin_otorgamiento = ans["result"]["instrumento"]["fecha_fin_otorgamiento"]; this.plazo_gracia = ans["result"]["instrumento"]["plazo_gracia"]; this.tasa_porcentaje = ans["result"]["instrumento"]["tasa_porcentaje"]; this.iva_porcentaje = ans["result"]["instrumento"]["iva_porcentaje"]; this.pago_seguro_dano = ans["result"]["instrumento"]["pago_seguro_dano"]; this.pago_seguro_vivienda = ans["result"]["instrumento"]["pago_seguro_vivienda"]; this.total_cuotas_mensuales = ans["result"]["instrumento"]["total_cuotas_mensuales"]; this.cuota_sin_seguros = ans["result"]["instrumento"]["cuota_sin_seguros"]; this.proyecciones = ans["result"]["proyecciones"]; if (this.proyecciones != undefined && this.proyecciones.length > 0) { this.hasProjections = true; let proyecciones_temp = this.proyecciones; this.consolidado_proyeccion = proyecciones_temp[proyecciones_temp.length - 1]; this.proyecciones = ans["result"]["proyecciones"]; } this.dataSource.data = this.proyecciones.slice(0, -1); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; this.pperObject = { monto_prestamo: this.investmentProposalForm.value.monto_prestamo, plazo_gracia: this.investmentProposalForm.value.plazo_gracia, tasa_porcentaje: this.investmentProposalForm.value.tasa_porcentaje, iva_porcentaje: this.investmentProposalForm.value.iva_porcentaje, pago_seguro_dano: this.investmentProposalForm.value .pago_seguro_dano, pago_seguro_vivienda: this.investmentProposalForm.value .pago_seguro_vivienda, total_cuotas_mensuales: this.investmentProposalForm.value .total_cuotas_mensuales, numero_deudor: this.investmentProposalForm.value.numero_deudor, nombre_deudor: this.investmentProposalForm.value.nombre_deudor, fecha_otorgamiento: this.f.fecha_otorgamiento.value.singleDate .formatted, fecha_fin_otorgamiento: this.fecha_fin_otorgamiento, fecha_gracia: this.fecha_gracia, cuota_sin_seguros: this.cuota_sin_seguros, proyecciones: this.proyecciones }; this.formDataService.setWork(this.pperObject); Swal.close(); if (saveForm == true) { if (this.investmentID != undefined) { this.router.navigate(["/investment-proposal/complement-info"], { queryParams: { id: this.investmentID } }); } else { this.router.navigate(["/investment-proposal/complement-info"]); } } }, err => { Swal.fire({ icon: "error", title: "Error en el servidor", text: "No su pudo obtener la informacion" }); return false; } ); } goToPrevious() { this.submitted = true; if (this.investmentID != undefined) { this.router.navigate(["/investment-proposal/general-info"], { queryParams: { id: this.investmentID } }); } else { this.router.navigate(["/investment-proposal/general-info"]); } } goToNext(form: any) { this.getCalculations(form, true); } }