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-futuros", templateUrl: "./futuros.component.html" }) export class FUTU implements InstrumentComponent { title: string = "Futuros"; @Input() data: any; @Input() summary: boolean; @Input() investmentID: string; 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_vencimiento_compra: IMyDateModel; m_fecha_operacion: IMyDateModel; m_fecha_rendencion: IMyDateModel; m_fecha_liquidacion_compra: IMyDateModel; submitted: boolean = false; instrument_exists: boolean; instrument_work: any = []; financials: any; base_types: any; futurosObject: {}; hasProjections: boolean; tipoOperacion: any; tipoEjecucion: any; fecha_vencimiento: any; operation_result: boolean = false; operation_results_work: any = []; instrument_work_summary: any = []; fecha_operacion_compra: any; fecha_liquidacion_compra: any; precio_unitario_compra: any; comision_broker_compra: any; cantidad_contratos_compra: any; unidades_contratos_compra: any; valor_transado_compra: any; valor_pagado: any; ganancia_perdida: any; rendimiento: any; corto: any; fecha_inicio_vigencia: any; fecha_vencimiento_compra: any; fecha_operacion_venta: any; fecha_liquidacion_venta: any; precio_unitario_venta: any; comision_broker_venta: any; cantidad_contratos_venta: any; unidades_contratos_venta: any; valor_transado_venta: any; valor_recibido: any; fecha_vencimiento_venta: any; valor_nominal: any; valor_transado: any; comision_casa: any; comision_bolsa: any; fecha_liquidacion: any; operacion: any; ejecucion: any; operaciones = [ { codigo: 1, nombre: "Corto" }, { codigo: 0, nombre: "Largo" } ]; ejecuciones = [ { codigo: 1, nombre: "Completa" }, { codigo: 0, nombre: "Parcial" } ]; 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(); console.log("init"); console.log(this.operaciones); console.log(this.instrument_work); if ( this.instrument_work != undefined && this.instrument_work.completo == undefined ) { if ( this.instrument_work.valor_pagado > 0 && this.instrument_work.valor_recibido > 0 ) { this.ejecucion = 1; } else { this.ejecucion = +this.instrument_work_summary.completo; } } this.investmentProposalForm = this.formBuilder.group({ operacion: [this.instrument_exists ? "" : this.instrument_work.corto], ejecucion: [this.instrument_exists ? "" : this.ejecucion], ticker: [this.instrument_exists ? "" : this.instrument_work.ticker], precio_unitario_compra: [ this.instrument_exists ? "" : this.instrument_work.precio_unitario_compra ], comision_broker_compra: [ this.instrument_exists ? "" : this.instrument_work.comision_broker_compra ], cantidad_contratos_compra: [ this.instrument_exists ? "" : this.instrument_work.cantidad_contratos_compra ], unidades_contratos_compra: [ this.instrument_exists ? "" : this.instrument_work.unidades_contratos_compra ], fecha_operacion_compra: [ this.instrument_exists ? "" : this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_operacion_compra, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_operacion_compra } } ], fecha_liquidacion_compra: [ this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_liquidacion_compra, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_liquidacion_compra } } ], fecha_vencimiento_compra: [ this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_vencimiento_compra, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_vencimiento_compra } } ], precio_unitario_venta: [ this.instrument_exists ? "" : this.instrument_work.precio_unitario_venta ], comision_broker_venta: [ this.instrument_exists ? "" : this.instrument_work.comision_broker_venta ], cantidad_contratos_venta: [ this.instrument_exists ? "" : this.instrument_work.cantidad_contratos_venta ], unidades_contratos_venta: [ this.instrument_exists ? "" : this.instrument_work.unidades_contratos_venta ], fecha_operacion_venta: [ this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_operacion_venta, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_operacion_venta } } ], fecha_liquidacion_venta: [ this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_liquidacion_venta, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_liquidacion_venta } } ], fecha_vencimiento_venta: [ this.instrument_exists ? "" : { isRange: false, singleDate: { jsDate: parse( this.instrument_work.fecha_vencimiento_venta, "dd/MM/yyyy", new Date() ), formatted: this.instrument_work.fecha_vencimiento_venta } } ] }); if (this.instrument_work != undefined) { this.instrument_work_summary = this.instrument_work; this.operation_result = true; console.log("datos"); console.log(this.instrument_work_summary); this.operacion = +this.instrument_work_summary.corto; if (this.instrument_work_summary.completo == undefined) { if ( this.instrument_work_summary.valor_pagado > 0 && this.instrument_work_summary.valor_recibido > 0 ) { this.ejecucion = 1; } } else { this.ejecucion = +this.instrument_work_summary.completo; } this.toggle_operacion(this.operacion); this.toggle_ejecucion(this.ejecucion); } } toggle_operacion(input: any) { this.tipoOperacion = +input; this.setValidators(); } toggle_ejecucion(input: any) { this.tipoEjecucion = +input; this.setValidators(); } setValidators() { console.log("tipo de operacion"); console.log(this.tipoOperacion); console.log("tipo de ejecucion"); console.log(this.tipoEjecucion); const ticker = this.investmentProposalForm.get("ticker"); const precio_unitario_compra = this.investmentProposalForm.get( "precio_unitario_compra" ); const comision_broker_compra = this.investmentProposalForm.get( "comision_broker_compra" ); const cantidad_contratos_compra = this.investmentProposalForm.get( "cantidad_contratos_compra" ); const unidades_contratos_compra = this.investmentProposalForm.get( "unidades_contratos_compra" ); const fecha_operacion_compra = this.investmentProposalForm.get( "fecha_operacion_compra" ); const fecha_liquidacion_compra = this.investmentProposalForm.get( "fecha_liquidacion_compra" ); const fecha_vencimiento_compra = this.investmentProposalForm.get( "fecha_vencimiento_compra" ); const precio_unitario_venta = this.investmentProposalForm.get( "precio_unitario_venta" ); const comision_broker_venta = this.investmentProposalForm.get( "comision_broker_venta" ); const cantidad_contratos_venta = this.investmentProposalForm.get( "cantidad_contratos_venta" ); const unidades_contratos_venta = this.investmentProposalForm.get( "unidades_contratos_venta" ); const fecha_operacion_venta = this.investmentProposalForm.get( "fecha_operacion_venta" ); const fecha_liquidacion_venta = this.investmentProposalForm.get( "fecha_liquidacion_venta" ); const fecha_vencimiento_venta = this.investmentProposalForm.get( "fecha_vencimiento_venta" ); ticker.setValidators([Validators.required]); if (this.tipoEjecucion == 1) { precio_unitario_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); comision_broker_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); cantidad_contratos_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); unidades_contratos_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); fecha_operacion_compra.setValidators([Validators.required]); fecha_liquidacion_compra.setValidators([Validators.required]); if (this.tipoOperacion == 0) { fecha_vencimiento_compra.setValidators([Validators.required]); fecha_vencimiento_venta.setValidators(null); } precio_unitario_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); comision_broker_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); cantidad_contratos_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); unidades_contratos_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); fecha_operacion_venta.setValidators([Validators.required]); fecha_liquidacion_venta.setValidators([Validators.required]); if (this.tipoOperacion == 1) { fecha_vencimiento_venta.setValidators([Validators.required]); fecha_vencimiento_compra.setValidators(null); } } else { if (this.tipoOperacion == 1) { precio_unitario_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); comision_broker_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); cantidad_contratos_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); unidades_contratos_venta.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); fecha_operacion_venta.setValidators([Validators.required]); fecha_liquidacion_venta.setValidators([Validators.required]); fecha_vencimiento_venta.setValidators(null); //.setValidators([Validators.required]); precio_unitario_compra.setValidators(null); comision_broker_compra.setValidators(null); cantidad_contratos_compra.setValidators(null); unidades_contratos_compra.setValidators(null); fecha_operacion_compra.setValidators(null); fecha_liquidacion_compra.setValidators(null); fecha_vencimiento_compra.setValidators(null); } if (this.tipoOperacion == 0) { precio_unitario_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); comision_broker_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); cantidad_contratos_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); unidades_contratos_compra.setValidators([ Validators.required, Validators.pattern(/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/) ]); fecha_operacion_compra.setValidators([Validators.required]); fecha_liquidacion_compra.setValidators([Validators.required]); fecha_vencimiento_compra.setValidators(null); //.setValidators([Validators.required]); precio_unitario_venta.setValidators(null); comision_broker_venta.setValidators(null); cantidad_contratos_venta.setValidators(null); unidades_contratos_venta.setValidators(null); fecha_operacion_venta.setValidators(null); fecha_liquidacion_venta.setValidators(null); fecha_vencimiento_venta.setValidators(null); } } precio_unitario_compra.updateValueAndValidity(); comision_broker_compra.updateValueAndValidity(); cantidad_contratos_compra.updateValueAndValidity(); unidades_contratos_compra.updateValueAndValidity(); fecha_operacion_compra.updateValueAndValidity(); fecha_liquidacion_compra.updateValueAndValidity(); fecha_vencimiento_compra.updateValueAndValidity(); precio_unitario_venta.updateValueAndValidity(); comision_broker_venta.updateValueAndValidity(); cantidad_contratos_venta.updateValueAndValidity(); unidades_contratos_venta.updateValueAndValidity(); fecha_operacion_venta.updateValueAndValidity(); fecha_liquidacion_venta.updateValueAndValidity(); fecha_vencimiento_venta.updateValueAndValidity(); } save(form: any): boolean { if (!form.valid) { return false; } this.formDataService.setWork(this.futurosObject); return true; } get f() { return this.investmentProposalForm.controls; } getCalculations(form: any, saveForm: boolean) { this.submitted = true; console.log(this.investmentProposalForm); if (!form.valid) { return false; } Swal.fire({ allowOutsideClick: false, icon: "info", text: "Espere por favor..." }); Swal.showLoading(); this.instrumentCalcService .opcionesFuturosCalc( "OPC", // Codigo del instrumento { id_tipo_base: +this.general.base_anual, id_formato_ingreso: +this.general.formato_ingreso, id_periodicidad: +this.general.periodicidad }, { completo: this.tipoEjecucion, corto: this.tipoOperacion, precio_unitario_compra: this.f.precio_unitario_compra.value, comision_broker_compra: this.f.comision_broker_compra.value, cantidad_contratos_compra: this.f.cantidad_contratos_compra.value, unidades_contratos_compra: this.f.unidades_contratos_compra.value, fecha_operacion_compra: this.f.fecha_operacion_compra.value == "" ? "" : this.f.fecha_operacion_compra.value.singleDate.formatted, fecha_liquidacion_compra: this.f.fecha_liquidacion_compra.value == "" ? "" : this.f.fecha_liquidacion_compra.value.singleDate.formatted, fecha_vencimiento_compra: this.f.fecha_vencimiento_compra.value == "" ? "" : this.f.fecha_vencimiento_compra.value.singleDate.formatted, precio_unitario_venta: this.f.precio_unitario_venta.value, comision_broker_venta: this.f.comision_broker_venta.value, cantidad_contratos_venta: this.f.cantidad_contratos_venta.value, unidades_contratos_venta: this.f.unidades_contratos_venta.value, fecha_operacion_venta: this.f.fecha_operacion_venta.value == "" ? "" : this.f.fecha_operacion_venta.value.singleDate.formatted, fecha_liquidacion_venta: this.f.fecha_liquidacion_venta.value == "" ? "" : this.f.fecha_liquidacion_venta.value.singleDate.formatted, fecha_vencimiento_venta: this.f.fecha_vencimiento_venta.value == "" ? "" : this.f.fecha_vencimiento_venta.value.singleDate.formatted } ) .subscribe( ans => { // Instrumento compra if (this.tipoOperacion == 0 || this.tipoEjecucion == 1) { this.fecha_operacion_compra = ans["result"]["instrumento_compra"]["fecha_operacion_compra"]; this.fecha_liquidacion_compra = ans["result"]["instrumento_compra"]["fecha_liquidacion_compra"]; this.precio_unitario_compra = ans["result"]["instrumento_compra"]["precio_unitario_compra"]; this.comision_broker_compra = ans["result"]["instrumento_compra"]["comision_broker_compra"]; this.cantidad_contratos_compra = ans["result"]["instrumento_compra"]["cantidad_contratos_compra"]; this.unidades_contratos_compra = ans["result"]["instrumento_compra"]["unidades_contratos_compra"]; this.valor_transado_compra = ans["result"]["instrumento_compra"]["valor_transado_compra"]; this.valor_pagado = ans["result"]["instrumento_compra"]["valor_pagado"]; this.ganancia_perdida = ans["result"]["instrumento_compra"]["ganancia_perdida"]; this.rendimiento = ans["result"]["instrumento_compra"]["rendimiento"]; } // Si es operacion largo y ejecucion completa if (this.tipoOperacion == 0) { this.corto = ans["result"]["instrumento_compra"]["corto"]; this.fecha_inicio_vigencia = ans["result"]["instrumento_compra"]["fecha_inicio_vigencia"]; this.fecha_vencimiento_compra = ans["result"]["instrumento_compra"]["fecha_vencimiento_compra"]; } // Instrumento venta if (this.tipoOperacion == 1 || this.tipoEjecucion == 1) { this.fecha_operacion_venta = ans["result"]["instrumento_venta"]["fecha_operacion_venta"]; this.fecha_liquidacion_venta = ans["result"]["instrumento_venta"]["fecha_liquidacion_venta"]; this.precio_unitario_venta = ans["result"]["instrumento_venta"]["precio_unitario_venta"]; this.comision_broker_venta = ans["result"]["instrumento_venta"]["comision_broker_venta"]; this.cantidad_contratos_venta = ans["result"]["instrumento_venta"]["cantidad_contratos_venta"]; this.unidades_contratos_venta = ans["result"]["instrumento_venta"]["unidades_contratos_venta"]; this.valor_transado_venta = ans["result"]["instrumento_venta"]["valor_transado_venta"]; this.valor_recibido = ans["result"]["instrumento_venta"]["valor_recibido "]; } // Si es operacion corto if (this.tipoOperacion == 1) { this.corto = ans["result"]["instrumento_venta"]["corto"]; this.fecha_inicio_vigencia = ans["result"]["instrumento_venta"]["fecha_inicio_vigencia"]; this.fecha_vencimiento_venta = ans["result"]["instrumento_venta"]["fecha_vencimiento_venta"]; } this.operation_result = true; this.futurosObject = { completo: this.tipoEjecucion, corto: this.tipoOperacion, ticker: this.investmentProposalForm.value.ticker, fecha_inicio_vigencia: this.fecha_inicio_vigencia }; if (this.tipoOperacion == 0 || this.tipoEjecucion == 1) { (this.futurosObject[ "precio_unitario_compra" ] = this.investmentProposalForm.value.precio_unitario_compra), (this.futurosObject[ "comision_broker_compra" ] = this.investmentProposalForm.value.comision_broker_compra), (this.futurosObject[ "cantidad_contratos_compra" ] = this.investmentProposalForm.value.cantidad_contratos_compra), (this.futurosObject[ "unidades_contratos_compra" ] = this.investmentProposalForm.value.unidades_contratos_compra), (this.futurosObject["fecha_operacion_compra"] = this.investmentProposalForm.value.fecha_operacion_compra == "" ? "" : this.investmentProposalForm.value.fecha_operacion_compra .singleDate.formatted), (this.futurosObject["fecha_liquidacion_compra"] = this.investmentProposalForm.value.fecha_liquidacion_compra == "" ? "" : this.investmentProposalForm.value.fecha_liquidacion_compra .singleDate.formatted), (this.futurosObject[ "valor_transado_compra" ] = this.valor_transado_compra), (this.futurosObject["valor_pagado"] = this.valor_pagado), (this.futurosObject["ganancia_perdida"] = this.ganancia_perdida), (this.futurosObject["rendimiento"] = this.rendimiento); } if (this.tipoOperacion == 1 || this.tipoEjecucion == 1) { (this.futurosObject[ "precio_unitario_venta" ] = this.investmentProposalForm.value.precio_unitario_venta), (this.futurosObject[ "comision_broker_venta" ] = this.investmentProposalForm.value.comision_broker_venta), (this.futurosObject[ "cantidad_contratos_venta" ] = this.investmentProposalForm.value.cantidad_contratos_venta), (this.futurosObject[ "unidades_contratos_venta" ] = this.investmentProposalForm.value.unidades_contratos_venta), (this.futurosObject["fecha_operacion_venta"] = this.investmentProposalForm.value.fecha_operacion_venta == "" ? "" : this.investmentProposalForm.value.fecha_operacion_venta .singleDate.formatted), (this.futurosObject["fecha_liquidacion_venta"] = this.investmentProposalForm.value.fecha_liquidacion_venta == "" ? "" : this.investmentProposalForm.value.fecha_liquidacion_venta .singleDate.formatted), (this.futurosObject[ "valor_transado_venta" ] = this.valor_transado_venta), (this.futurosObject["valor_recibido"] = this.valor_recibido); } if (this.tipoOperacion == 0) { this.futurosObject[ "fecha_vencimiento_compra" ] = this.fecha_vencimiento_compra; } if (this.tipoOperacion == 1) { this.futurosObject[ "fecha_vencimiento_venta" ] = this.fecha_vencimiento_venta; } if (this.tipoEjecucion == 1) { this.futurosObject["fecha_cancelado"] = this.datepipe.transform( new Date(), "dd/MM/yyyy" ); //.toISOString().slice(0, 10); } this.formDataService.setWork(this.futurosObject); 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); } }