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-dap", templateUrl: "./dap.component.html" }) export class DAP implements InstrumentComponent { title: string = "Depósitos a plazo"; @Input() data: any; @Input() summary: boolean; @Input() investmentID: string; form: any; general: GeneralInfo; displayedColumns: string[] = [ "posicion", "plazo", "fecha_pago", "ingreso_bruto", "renta", "ingreso_neto" ]; @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @ViewChild(MatSort, { static: true }) sort: MatSort; // 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" }; instrument_null: boolean; instrument_work: any = []; investmentProposalForm: FormGroup; myDpOptions: IAngularMyDpOptions = { dateRange: false, dateFormat: "dd/mm/yyyy", dayLabels: this.daysLabels, monthLabels: this.monthsLabels }; submitted: boolean; myDateInit: boolean = true; model: IMyDateModel = null; ingreso_bruto: number = 0.0; ingreso_neto: number = 0.0; valor_transado: number = 0.0; precio_porcentaje: number = 0.0; rendimiento_neto: number = 0.0; plazo: number = 0; fecha_inicio_vigencia: string; rendimiento_bruto: number = 0; monto_inversion: number = 0; proyecciones: any; dataSource = new MatTableDataSource(this.proyecciones); dataSource2 = new MatTableDataSource(this.proyecciones); dapObject: {}; renta: any; hasProjections: boolean; numero_certificado: any; fecha_vencimiento: any; consolidado_proyeccion = { total_ingreso_bruto: 0, total_renta: 0, total_ingreso_neto: 0 }; 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_null = this.instrument_work == undefined; this.general = this.formDataService.getGeneralInfo(); if ( this.instrument_work != undefined && (this.instrument_work.proyecciones != "" || this.instrument_work != undefined) ) { this.hasProjections = true; this.dataSource2.data = this.instrument_work.proyecciones.slice(0, -1); this.dataSource2.paginator = this.paginator; this.dataSource2.sort = this.sort; this.consolidado_proyeccion = this.instrument_work.proyecciones[ this.instrument_work.proyecciones.length - 1 ]; } else { this.hasProjections = false; } this.investmentProposalForm = this.formBuilder.group({ monto_inversion: [ this.instrument_null ? "" : this.instrument_work.monto_inversion, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], renta_porcentaje: [ this.instrument_null || this.instrument_work.renta_porcentaje == null ? 10 : this.instrument_work.renta_porcentaje, [Validators.required] ], tasa_porcentaje: [ this.instrument_null ? "" : this.instrument_work.tasa_porcentaje, [ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ] ], plazo: [ this.instrument_null ? "" : this.instrument_work.plazo, [Validators.required] ], numero_certificado: [ this.instrument_null ? "" : this.instrument_work.numero_certificado, [] ], fecha_operacion: [ this.instrument_null ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_operacion, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_operacion } }, Validators.required ] }); } get f() { return this.investmentProposalForm.controls; } save(form: any): boolean { if (!form.valid) { return false; } 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 .dapCalc( "DAP", // Codigo del instrumento { id_tipo_base: +this.general.base_anual, id_periodicidad: +this.general.periodicidad, id_formato_ingreso: +this.general.formato_ingreso }, { monto_inversion: +this.f.monto_inversion.value, tasa_porcentaje: +this.f.tasa_porcentaje.value, renta_porcentaje: +this.f.renta_porcentaje.value, plazo: +this.f.plazo.value, //id_formato_ingreso: this.f.formato_ingreso.value, fecha_operacion: this.f.fecha_operacion.value.singleDate.formatted } ) .subscribe( ans => { this.monto_inversion = ans["result"]["monto_inversion"]; this.ingreso_bruto = ans["result"]["ingreso_bruto"]; this.ingreso_neto = ans["result"]["ingreso_neto"]; this.rendimiento_bruto = ans["result"]["rendimiento_bruto"]; this.rendimiento_neto = ans["result"]["rendimiento_neto"]; this.plazo = ans["result"]["plazo"]; this.fecha_inicio_vigencia = ans["result"]["fecha_inicio_vigencia"]; this.renta = ans["result"]["renta"]; this.proyecciones = ans["result"]["proyecciones"]; this.numero_certificado = ans["result"]["numero_certificado"]; this.fecha_vencimiento = ans["result"]["fecha_vencimiento"]; 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.dapObject = { monto_inversion: this.investmentProposalForm.value.monto_inversion, renta_porcentaje: this.investmentProposalForm.value .renta_porcentaje, tasa_porcentaje: this.investmentProposalForm.value.tasa_porcentaje, plazo: +this.investmentProposalForm.value.plazo, ingreso_bruto: this.ingreso_bruto, ingreso_neto: this.ingreso_neto, rendimiento_bruto: this.rendimiento_bruto, rendimiento_neto: this.rendimiento_neto, fecha_inicio_vigencia: this.fecha_inicio_vigencia, renta: this.renta, numero_certificado: this.investmentProposalForm.value .numero_certificado, proyecciones: this.proyecciones.slice(0, -1), fecha_operacion: this.investmentProposalForm.value.fecha_operacion .singleDate.formatted, fecha_vencimiento: this.fecha_vencimiento }; this.formDataService.setWork(this.dapObject); 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); } }