Public Class frmFOIN
    Dim oCEFOIN As FondosDeInversionCE = New FondosDeInversionCE
    Dim oDAOGeneral As New DAOGeneral
    Dim FOIN As New FondoInversionDAO
    Dim EstadoBoton As String
    Dim Operaciones As New Operaciones
    Dim IndexBase As Integer = 0
    Dim TipoTransaccion As String = String.Empty
    Private Sub frmFOIN_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CargarBase()
        CargarPeriodicidad()
        AgregarCampos()
        CargarRegistroPrincipal()
        CargarFilas()
        Rellenar()
        Calculos()
        ExisteInversion()
        ValidarExistencia()
        CargarTransladoNuevo()
    End Sub
    Sub ValidarExistencia()
        Dim General As New DAOGeneral
        Dim Codigo As String = Variables.Codigo
        Dim Tabla As String = String.Empty
        Tabla = "FINV"
        Dim Existe As Boolean = General.ExisteTitulo(Codigo, Tabla)
        If (Existe) Then
            navNuevo.Enabled = False
            navModificar.Enabled = True
            navEliminar.Enabled = True
        Else
            navNuevo.Enabled = True
            navModificar.Enabled = False
            navEliminar.Enabled = False

        End If



    End Sub
    Function ExisteInversion()
        Dim Codigo As String = Variables.Codigo
        Dim TablaINV As String = "INV0"
        Dim TablaPINV As String = "PIN0"
        Dim General As New DAOGeneral
        Dim InvPro = Variables.InvPro



        If InvPro = "I" Then

            Dim INV0 As Boolean = General.ExisteTitulo(Codigo, TablaINV)
            If INV0 Then
                navNuevo.Visible = True
                Return True
            Else
                navNuevo.Visible = False
                Return False
            End If
        End If



        If InvPro = "P" Then
            Dim PINV0 As Boolean = General.ExisteTitulo(Codigo, TablaPINV)
            If PINV0 Then
                navNuevo.Visible = True
                Return True
            Else
                navNuevo.Visible = False
                Return False
            End If
        End If
        Return False
    End Function


    Sub CargarBase()
        If Me.cboAnioBase.Items.Count = 0 Or Me.cboAnioBase.Items.Count = 1 Then


            Me.cboAnioBase.DataSource = oDAOGeneral.ListaBase
            Me.cboAnioBase.DisplayMember = "Descripcion"
            Me.cboAnioBase.ValueMember = "Codigo"
            Me.cboAnioBase.SelectedIndex = 0
        End If
    End Sub

    Sub Rellenar()

        If (String.IsNullOrEmpty(txtValorNominal.Text.ToString)) Then
            txtValorNominal.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtCuotasDeParticipacion.Text.ToString)) Then
            txtCuotasDeParticipacion.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtValorDeParticipacion.Text.ToString)) Then
            txtValorDeParticipacion.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtPorcentajeComisionCasa.Text.ToString.Trim("%"))) Then
            txtPorcentajeComisionCasa.Text = "0.0%"
        End If

        If (String.IsNullOrEmpty(txtPorcentajeComisionBolsa.Text.ToString.Trim("%"))) Then
            txtPorcentajeComisionBolsa.Text = "0.0%"
        End If

        If (String.IsNullOrEmpty(txtComisionCasa.Text.ToString)) Then
            txtComisionCasa.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtComisionBolsa.Text.ToString)) Then
            txtComisionBolsa.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtValorTransado.Text.ToString)) Then
            txtValorTransado.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtDiasLiq.Text.ToString)) Then
            txtDiasLiq.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtRendimientoOfrecido.Text.ToString.Trim("%"))) Then
            txtRendimientoOfrecido.Text = "0.0%"
        End If

        If (String.IsNullOrEmpty(txtPlazo.Text.ToString)) Then
            txtPlazo.Text = "0.0"
        End If

        If (String.IsNullOrEmpty(txtDividendo.Text.ToString.Trim("%"))) Then
            txtDividendo.Text = "0.0%"
        End If


    End Sub










    Function CalcularFecha(ByVal FechaInicial As Date, ByVal DiaFijo As Integer)
        'Declaracion de Variables'
        Dim Base As Integer = 0
        Dim Periodicidad As String = cboPeriodicidad.SelectedValue
        Dim TipoCalculo As String = String.Empty
        Dim FechaFinal As Date = Date.Today.Date

        'Seleccion de Tipo de Calculo'
        If RadioButton1.Checked Then
            TipoCalculo = "FinMes"
        Else
            If RadioButton2.Checked Then
                TipoCalculo = "Mensual"
            End If
        End If


        IndexBase = cboAnioBase.SelectedIndex



        If (IndexBase = 0 Or IndexBase = 2) Then

            Base = 360


            If TipoCalculo = "FinMes" Then
                FechaFinal = Operaciones.FechaFinMes(FechaInicial, Periodicidad, Base)

            ElseIf TipoCalculo = "Mensual" Then

                FechaFinal = Operaciones.FechaMensual(FechaInicial, Periodicidad, TipoCalculo, 0, DiaFijo, Base)
            End If


        ElseIf IndexBase = 1 Or IndexBase = 3 Then
            Base = 365

            If TipoCalculo = "FinMes" Then
                FechaFinal = Operaciones.FechaFinMes(FechaInicial, Periodicidad, Base)

            ElseIf TipoCalculo = "Mensual" Then

                FechaFinal = Operaciones.FechaMensual(FechaInicial, Periodicidad, TipoCalculo, 0, DiaFijo, Base)
            End If


        End If
        Return FechaFinal
    End Function

    Sub AgregarFecha()
        Dim Index As Integer = dgvIngresos.CurrentRow.Index
        Dim IndexAnterior As Integer = Index - 1
        Dim FechaAnterior As Date = Date.Today.Date
        Dim FechaActual As Date = Date.Today.Date
        Dim FechaLiquidacion As Date = dtpFechaLiquidacion.Value
        Dim DiaFijo As Integer = FechaLiquidacion.Day
        If Index > 0 Then
            FechaAnterior = Operaciones.ConvertirFecha(dgvIngresos.Rows(IndexAnterior).Cells("Fecha de Corte").Value.ToString)
            FechaActual = CalcularFecha(FechaAnterior, DiaFijo)
            dgvIngresos.Rows(Index).Cells("Fecha de Corte").Value = Format((FechaActual), "dd/MM/yyyy").ToString

        Else

            FechaActual = CalcularFecha(Operaciones.ConvertirFecha(FechaLiquidacion), DiaFijo)
            dgvIngresos.Rows(Index).Cells("Fecha de Corte").Value = Format(FechaActual, "dd/MM/yyyy").ToString
        End If





    End Sub


    Sub AgregarDias()
        Dim Index As Integer = dgvIngresos.CurrentRow.Index
        Dim IndexAnterior As Integer = Index - 1
        Dim FechaAnterior As String = String.Empty
        Dim FechaActual As String = String.Empty
        Dim FechaLiquidacion As Date = Format(dtpFechaLiquidacion.Value, "dd/MM/yyyy")
        Dim BaseIndex As Integer = cboAnioBase.SelectedIndex
        Dim BaseCalculo As Integer = 0
        Dim Meses As Integer = 0
        Dim TipoCalculo As String = String.Empty
        Dim DiasFinales As Integer = 0
        Dim CantidadDiasNormal360 As Integer = 0
        Dim CantidadDias360 As Integer = 0
        Dim Periodicidad As String = cboPeriodicidad.SelectedValue
        If Periodicidad = "M" Then
            Meses = 1
        ElseIf Periodicidad = "T" Then
            Meses = 3
        ElseIf Periodicidad = "S" Then
            Meses = 6
        ElseIf Periodicidad = "A" Then
            Meses = 12
        End If

        CantidadDiasNormal360 = Meses * 30
        If BaseIndex = 0 Or BaseIndex = 2 Then
            BaseCalculo = 360

        ElseIf BaseIndex = 1 Or BaseIndex = 3 Then
            BaseCalculo = 365
        End If


        If RadioButton1.Checked Then
            TipoCalculo = "FinMes"
        Else
            If RadioButton2.Checked Then
                TipoCalculo = "Mensual"
            End If
        End If

        If Index > 0 Then
            FechaAnterior = Operaciones.ConvertirFecha(dgvIngresos.Rows(IndexAnterior).Cells("Fecha de Corte").Value.ToString)
            FechaActual = Operaciones.ConvertirFecha(dgvIngresos.Rows(Index).Cells("Fecha de Corte").Value.ToString)


            If BaseCalculo = 360 Then
                    If TipoCalculo = "FinMes" Then
                        DiasFinales = Operaciones.Base360(FechaAnterior, FechaActual)
                    ElseIf TipoCalculo = "Mensual" Then
                        DiasFinales = CantidadDiasNormal360

                    End If

                ElseIf BaseCalculo = 365 Then
                    DiasFinales = Operaciones.Base365(FechaAnterior, FechaActual)


                End If



            Else
            'Primera Fecha'



            FechaActual = Operaciones.ConvertirFecha(dgvIngresos.Rows(Index).Cells("Fecha de Corte").Value.ToString)


            If BaseCalculo = 360 Then
                    If TipoCalculo = "FinMes" Then
                        DiasFinales = Operaciones.Base360(FechaLiquidacion, FechaActual)
                    ElseIf TipoCalculo = "Mensual" Then
                        DiasFinales = CantidadDiasNormal360

                    End If

                ElseIf BaseCalculo = 365 Then
                    DiasFinales = Operaciones.Base365(FechaLiquidacion, FechaActual)


                End If



            End If
        dgvIngresos.Rows(Index).Cells("Dias").Value = DiasFinales
    End Sub

    Sub AgregarDividendo()
        Dim Index As Integer = dgvIngresos.CurrentRow.Index
        Dim Dividendo As String = String.Empty
        Dim DividendoAgregar As String = String.Empty
        Dim DividendoTexto As String = txtDividendo.Text.ToString.Trim("%")
        Dim NuevaFila As Boolean = False

        If dgvIngresos.Rows(Index).Cells("Dividendo").Value Is Nothing Then
            NuevaFila = True
        Else
            Dividendo = dgvIngresos.Rows(Index).Cells("Dividendo").Value.ToString
        End If

        If String.IsNullOrEmpty(Dividendo) Then

            If String.IsNullOrEmpty(DividendoTexto) Then
                DividendoAgregar = "0"
            Else
                DividendoAgregar = DividendoTexto
            End If
            dgvIngresos.Rows(Index).Cells("Dividendo").Value = (DividendoAgregar.ToString + "%")
        End If
    End Sub
    Sub AgregarIngreso()
        Dim Index As Integer = dgvIngresos.CurrentRow.Index
        Dim CuotasParticipacion As Double = 0
        Dim Dividendo As Double = 0
        Dim Dias As Integer = 0
        Dim Calculo As Double = 0
        If Index > 0 Then

            CuotasParticipacion = Operaciones.ConvertirDecimal(txtCuotasDeParticipacion.Text.ToString)

            If Not dgvIngresos.Rows(Index).Cells("Dividendo").Value Is Nothing Then
                Dividendo = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Dividendo").Value.ToString)


            End If

            Calculo = CuotasParticipacion * (Dividendo / 100)


        Else

            CuotasParticipacion = Operaciones.ConvertirDecimal(txtCuotasDeParticipacion.Text.ToString)


            If Not dgvIngresos.Rows(Index).Cells("Dividendo").Value Is Nothing Then

                Dividendo = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Dividendo").Value.ToString)


            End If


            If Not dgvIngresos.Rows(Index).Cells("Dias").Value Is Nothing Then

                Dias = Operaciones.ConvertirEntero(dgvIngresos.Rows(Index).Cells("Dias").Value)


            End If



            If Not Dias = 0 Then
                Dim FechaTemporal As Date = dtpFechaLiquidacion.Value.AddDays(1)
                If FechaTemporal.Month = dtpFechaLiquidacion.Value.Month Then
                    Calculo = (((Dividendo / 100) * CuotasParticipacion) / Dias)
                Else
                    Calculo = CuotasParticipacion * (Dividendo / 100)
                End If

            End If

        End If
        dgvIngresos.Rows(Index).Cells("Ingresos").Value = Calculo

    End Sub


    Sub AgregarRendimientos()
        Dim Index As Integer = dgvIngresos.CurrentRow.Index
        Dim ValorNominal As Double = 0
        Dim Dividendo As Double = 0
        Dim Dias As Integer = 0
        Dim Ingreso As Double = 0
        Dim Calculo As Double = 0
        Dim IndexBase As Integer = 0
        Dim BaseCalculo As Integer = 0
        If Not String.IsNullOrEmpty(txtValorNominal.Text.ToString) Then
            ValorNominal = txtValorNominal.Text
        End If
        If Not dgvIngresos.Rows(Index).Cells("Dividendo").Value Is Nothing Then
            Dividendo = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Dividendo").Value.ToString)
        End If


        If Not dgvIngresos.Rows(Index).Cells("Ingresos").Value Is Nothing Then
            Ingreso = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Ingresos").Value)
        End If



        If Not dgvIngresos.Rows(Index).Cells("Dias").Value Is Nothing Then
            Dias = Operaciones.ConvertirEntero(dgvIngresos.Rows(Index).Cells("Dias").Value)

        End If

        IndexBase = cboAnioBase.SelectedIndex

        If IndexBase = 0 Or IndexBase = 2 Then
            BaseCalculo = 360
        ElseIf IndexBase = 1 Or IndexBase = 3 Then
            BaseCalculo = 365
        End If

        If Not Dias = 0 And Not ValorNominal = 0 And Not BaseCalculo = 0 Then
            Calculo = Ingreso / ValorNominal * BaseCalculo / Dias
        End If

        dgvIngresos.Rows(Index).Cells("Rendimiento Mensual").Value = (Calculo * 100).ToString + "%"
    End Sub
    Sub AgregarNumDiv()
        Dim Index As Integer = dgvIngresos.CurrentRow.Index
        Dim IndexAnterior As Integer = Index - 1

        Dim I_NumDivAnterior As Integer = 0
        Dim I_NumDivActual As Integer = 0
        Dim S_NumDivAnterior As String = String.Empty
        Dim S_NumDivActual As String = String.Empty




        S_NumDivActual = dgvIngresos.Rows(Index).Cells("NumDiv").Value
        I_NumDivActual = Operaciones.ConvertirEntero(S_NumDivActual)
        If Index > 0 Then
            S_NumDivAnterior = dgvIngresos.Rows(IndexAnterior).Cells("NumDiv").Value
            I_NumDivAnterior = Operaciones.ConvertirEntero(S_NumDivAnterior)


            If I_NumDivActual = 0 Then
                I_NumDivActual = I_NumDivAnterior + 1
                dgvIngresos.Rows(Index).Cells("NumDiv").Value = I_NumDivActual
            End If
        Else

            If I_NumDivActual = 0 Then
                I_NumDivActual = 1
                dgvIngresos.Rows(Index).Cells("NumDiv").Value = I_NumDivActual
            End If
        End If


    End Sub

    Sub AgregarCorrelativo()
        Dim Cantidad As Integer = dgvIngresos.Rows.Count - 2
        Dim Index As Integer = 0

        While Index <= Cantidad
            Dim Correlativo As Integer = Index + 1
            dgvIngresos.Rows(Index).Cells("correlativo").Value = Correlativo
            Index += 1
        End While
    End Sub
    Sub CalculosTabla()
        AgregarCorrelativo()
        AgregarFecha()
        AgregarDias()
        AgregarDividendo()
        AgregarIngreso()
        AgregarRendimientos()
        AgregarNumDiv()
    End Sub

    Sub CargarPeriodicidad()
        Me.cboPeriodicidad.DataSource = oDAOGeneral.ListaPeriodicidad
        Me.cboPeriodicidad.DisplayMember = "Descripcion"
        Me.cboPeriodicidad.ValueMember = "Codigo"

        '    Me.cboPeriodicidad.SelectedIndex = -1

    End Sub

    Private Sub Calculos()

        Dim SCuotasDeParticipacion As String = String.Empty
        Dim SValorParticipacion As String = String.Empty
        Dim SValorNominal As String = String.Empty
        Dim SValorTransado As String = String.Empty
        Dim SDiasPlazo As String = String.Empty
        Dim SPorcentajeComisionCasa As String = String.Empty
        Dim SPorcentajeComisionBolsa As String = String.Empty
        Dim SComisionCasa As String = String.Empty
        Dim SComisionBolsa As String = String.Empty
        Dim SDiasLiquidos As String = String.Empty



        Dim ICuotasDeParticipacion As Double = 0
        Dim IValorParticipacion As Double = 0
        Dim IValorNominal As Double = 0
        Dim IValorTransado As Double = 0
        Dim FechaLiquidacion As Date = Date.Today.Date
        Dim FechaOperacion As Date = Date.Today.Date
        Dim FechaVencimiento As Date = Date.Today.Date
        Dim IDiasPlazo As Integer = 0
        Dim IPorcentajeComisionCasa As Double = 0
        Dim IPorcentajeComisionBolsa As Double = 0
        Dim IComisionCasa As Double = 0
        Dim IComisionBolsa As Double = 0
        Dim IDiasLiquidos As Integer = 0

        SDiasPlazo = txtPlazo.Text.ToString
        IDiasPlazo = Operaciones.ConvertirEntero(SDiasPlazo)

        SDiasLiquidos = txtDiasLiq.Text.ToString
        IDiasLiquidos = Operaciones.ConvertirEntero(SDiasLiquidos)

        SPorcentajeComisionCasa = txtPorcentajeComisionCasa.Text.ToString
        IPorcentajeComisionCasa = Operaciones.ConvertirDecimal(SPorcentajeComisionCasa) / 100

        SPorcentajeComisionBolsa = txtPorcentajeComisionBolsa.Text.ToString
        IPorcentajeComisionBolsa = Operaciones.ConvertirDecimal(SPorcentajeComisionBolsa) / 100

        SCuotasDeParticipacion = txtCuotasDeParticipacion.Text.ToString
        ICuotasDeParticipacion = Operaciones.ConvertirDecimal(SCuotasDeParticipacion)
        SValorParticipacion = txtValorDeParticipacion.Text.ToString
        IValorParticipacion = Operaciones.ConvertirDecimal(SValorParticipacion)

        FechaOperacion = dtpFechaOperacion.Value

        txtValorNominal.Text = oCEFOIN.CalculoValorNominal(ICuotasDeParticipacion, IValorParticipacion)
        SValorNominal = txtValorNominal.Text.ToString
        IValorNominal = Operaciones.ConvertirDecimal(SValorNominal)

        txtValorTransado.Text = IValorNominal

        IValorTransado = IValorNominal

        dtpFechaLiquidacion.Value = FechaOperacion
        dtpFechaLiquidacion.Value = oCEFOIN.CalculoFechaLiq(FechaOperacion, IDiasLiquidos)
        FechaLiquidacion = dtpFechaLiquidacion.Value

        txtComisionCasa.Text = CalculoComision(IPorcentajeComisionCasa, IValorTransado)
        txtComisionBolsa.Text = CalculoComision(IPorcentajeComisionBolsa, IValorTransado)

        IndexBase = cboAnioBase.SelectedIndex




        If (IndexBase = 0 Or IndexBase = 2) Then



            dtpFechaVencimiento.Value = Operaciones.FechaDias360(FechaLiquidacion, IDiasPlazo)

        ElseIf IndexBase = 1 Or IndexBase = 3 Then
            dtpFechaVencimiento.Value = FechaLiquidacion.AddDays(IDiasPlazo)
        End If
    End Sub

    Function CalculoComision(ByVal PorcComision As Double, ByVal ValTra As Double)
        Return PorcComision * ValTra
    End Function
    Private Sub txtCuotasDeParticipacion_TextChanged(sender As Object, e As EventArgs) Handles txtCuotasDeParticipacion.TextChanged
        If Not Double.TryParse(txtCuotasDeParticipacion.Text, oCEFOIN.CuotasDeParticipacion) Then
            lblErrCuotasDeParticipacion.Visible = True
        Else
            lblErrCuotasDeParticipacion.Visible = False
        End If


        Calculos()
    End Sub

    Private Sub txtCuotasDeParticipacion_LostFocus(sender As Object, e As EventArgs) Handles txtCuotasDeParticipacion.LostFocus
        If Not Double.TryParse(txtCuotasDeParticipacion.Text, oCEFOIN.CuotasDeParticipacion) Then
            lblErrCuotasDeParticipacion.Visible = True
        Else
            lblErrCuotasDeParticipacion.Visible = False
        End If
        Me.txtCuotasDeParticipacion.Text = oCEFOIN.CuotasDeParticipacion.ToString()
        RefrescarValorControles()
    End Sub

    Private Sub txtValorDeParticipacion_TextChanged(sender As Object, e As EventArgs) Handles txtValorDeParticipacion.TextChanged
        If Not Double.TryParse(txtValorDeParticipacion.Text, oCEFOIN.ValorDeParticipacion) Then
            lblErrValorDeParticipacion.Visible = True
        Else
            lblErrValorDeParticipacion.Visible = False
        End If

        Calculos()
    End Sub

    Private Sub txtValorDeParticipacion_LostFocus(sender As Object, e As EventArgs) Handles txtValorDeParticipacion.LostFocus
        If Not Double.TryParse(txtValorDeParticipacion.Text, oCEFOIN.ValorDeParticipacion) Then
            lblErrValorDeParticipacion.Visible = True
        Else
            lblErrValorDeParticipacion.Visible = False
        End If
        Me.txtValorDeParticipacion.Text = oCEFOIN.ValorDeParticipacion.ToString()
        RefrescarValorControles()
    End Sub


    Sub RefrescarValorControles()
        Me.txtValorNominal.Text = oCEFOIN.ValorNominal.ToString()
        Me.txtComisionCasa.Text = oCEFOIN.ComisionCasa.ToString()
        Me.txtComisionBolsa.Text = oCEFOIN.ComisionBolsa.ToString()
        Me.txtValorTransado.Text = oCEFOIN.ValorTransado.ToString()
    End Sub

    Private Sub txtPorcentajeComisionCasa_TextChanged(sender As Object, e As EventArgs) Handles txtPorcentajeComisionCasa.TextChanged
        Dim valor As Double
        If (txtPorcentajeComisionCasa.ToString.IndexOf("%") = -1) Then
            txtPorcentajeComisionCasa.Text += "%"
        End If
        If Not Double.TryParse(Me.txtPorcentajeComisionCasa.Text.Replace("%", ""), valor) Then
            lblErrComisionCasa.Visible = True
        Else
            lblErrComisionCasa.Visible = False
        End If
        oCEFOIN.PorcentajeComisionCasa = valor / 100

        Calculos()
    End Sub

    Private Sub txtPorcentajeComisionCasa_LostFocus(sender As Object, e As EventArgs) Handles txtPorcentajeComisionCasa.LostFocus
        Me.txtPorcentajeComisionCasa.Text = oCEFOIN.PorcentajeComisionCasa.ToString(Configuraciones.CodigoTXTPorcentaje)
        RefrescarValorControles()
    End Sub

    Private Sub txtPorcentajeComisionBolsa_TextChanged(sender As Object, e As EventArgs) Handles txtPorcentajeComisionBolsa.TextChanged
        Dim valor As Double
        If (txtPorcentajeComisionBolsa.ToString.IndexOf("%") = -1) Then
            txtPorcentajeComisionBolsa.Text += "%"
        End If
        If Not Double.TryParse(Me.txtPorcentajeComisionBolsa.Text.Replace("%", ""), valor) Then
            lblErrComisionBolsa.Visible = True
        Else
            lblErrComisionBolsa.Visible = False
        End If
        oCEFOIN.PorcentajeComisionBolsa = valor / 100

        Calculos()
    End Sub

    Private Sub txtPorcentajeComisionBolsa_LostFocus(sender As Object, e As EventArgs) Handles txtPorcentajeComisionBolsa.LostFocus
        Me.txtPorcentajeComisionBolsa.Text = oCEFOIN.PorcentajeComisionBolsa.ToString(Configuraciones.CodigoTXTPorcentaje)
        RefrescarValorControles()
    End Sub

    Private Sub dtpFechaOperacion_ValueChanged(sender As Object, e As EventArgs) Handles dtpFechaOperacion.ValueChanged
        Calculos()

    End Sub

    Private Sub dtpFechaLiquidacion_ValueChanged(sender As Object, e As EventArgs) Handles dtpFechaLiquidacion.ValueChanged
        oCEFOIN.FechaLiquidacion = dtpFechaLiquidacion.Value.Date
    End Sub

    Private Sub dtpFechaVencimiento_ValueChanged(sender As Object, e As EventArgs) Handles dtpFechaVencimiento.ValueChanged
        oCEFOIN.FechaVencimiento = dtpFechaVencimiento.Value.Date
    End Sub

    Private Sub txtRendimientoOfrecido_TextChanged(sender As Object, e As EventArgs) Handles txtRendimientoOfrecido.TextChanged

        Calculos()
        If (txtRendimientoOfrecido.ToString.IndexOf("%") = -1) Then
            txtRendimientoOfrecido.Text += "%"
        End If
    End Sub

    Private Sub txtRendimientoOfrecido_LostFocus(sender As Object, e As EventArgs) Handles txtRendimientoOfrecido.LostFocus
    End Sub

    Private Sub cboPeriodicidad_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPeriodicidad.SelectedIndexChanged
        If Not TypeOf cboPeriodicidad.SelectedValue Is DataRowView Then

            If Not cboPeriodicidad.SelectedValue Is Nothing Then


                oCEFOIN.Periodicidad = cboPeriodicidad.SelectedValue.ToString
            End If
        End If
    End Sub

    Private Sub cboAnioBase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboAnioBase.SelectedIndexChanged
        If Not TypeOf cboAnioBase.SelectedItem Is DataRowView Then


            oCEFOIN.AnioBase = cboAnioBase.SelectedItem


        End If

    End Sub

    Private Sub txtPlazo_TextChanged(sender As Object, e As EventArgs) Handles txtPlazo.TextChanged

        Calculos()
    End Sub

    Private Sub btnRefrescar_Click(sender As Object, e As EventArgs) Handles btnQuitar.Click
        QuitarFila()
    End Sub




    Private Sub txtDividendo_TextChanged(sender As Object, e As EventArgs)

        Calculos()
    End Sub

    Private Sub txtDividendo_LostFocus(sender As Object, e As EventArgs)
    End Sub

    Private Sub txtCuotasDeParticipacion_KeyUp(sender As Object, e As KeyEventArgs) Handles txtCuotasDeParticipacion.KeyUp
        Calculos()
    End Sub

    Private Sub txtValorDeParticipacion_KeyUp(sender As Object, e As KeyEventArgs) Handles txtValorDeParticipacion.KeyUp
        Calculos()
    End Sub

    Private Sub txtDiasLiq_KeyUp(sender As Object, e As KeyEventArgs) Handles txtDiasLiq.KeyUp
        Calculos()
    End Sub

    Private Sub navModificar_Click(sender As Object, e As EventArgs) Handles navModificar.Click
        TipoTransaccion = "M"
        btnAccion.Text = "Modificar"

    End Sub

    Private Sub navNuevo_Click(sender As Object, e As EventArgs) Handles navNuevo.Click
        TipoTransaccion = "N"
        btnAccion.Text = "Nuevo"
    End Sub

    Private Sub navEliminar_Click(sender As Object, e As EventArgs) Handles navEliminar.Click
        TipoTransaccion = "B"
        btnAccion.Text = "Eliminar"
    End Sub

    Private Sub btnAccion_Click(sender As Object, e As EventArgs) Handles btnAccion.Click

        Dim codigo As String = Variables.Codigo
        Dim Coleccion As Collection = ColeccionDatos()
        Dim Diccionario As Dictionary(Of String, Collection) = NuevoIngresoDiccionario()

        If (TipoTransaccion = "N") Then
            FOIN.ProcesoDatos(Coleccion, codigo, Diccionario, "Nuevo")
        ElseIf (TipoTransaccion = "M") Then
            FOIN.ProcesoDatos(Coleccion, codigo, Diccionario, "Modificar")
        ElseIf (TipoTransaccion = "B") Then
            Eliminar()
        ElseIf Not ExisteValidacion() Then
            Variables.ColeccionFINV = Coleccion
            Variables.ColeccionFINVDi = Diccionario


            Me.Close()



        End If
    End Sub

    Sub Eliminar()
        Dim codigo As String = Variables.Codigo
        FOIN.Eliminar(codigo)
    End Sub
    Function ExisteValidacion()
        Dim General As New DAOGeneral
        Dim Codigo As String = Variables.Codigo
        Dim Tabla As String = String.Empty
        Tabla = "FINV"

        Dim Existe As Boolean = General.ExisteTitulo(Codigo, Tabla)
        Return Existe
    End Function
    Private Function NuevoIngresoDiccionario()
        Dim Diccionario As New Dictionary(Of String, Collection)


        Dim Cantidad As Integer = dgvIngresos.Rows.Count - 2
        Dim Index As Integer = 0


        While Index <= Cantidad
            Dim coleccion As New Collection
            Dim FechaCorte, Dias, Dividendo, Ingreso, RendMensual, NumDiv
            NumDiv = Operaciones.ConvertirEntero(dgvIngresos.Rows(Index).Cells("NumDiv").Value)
            FechaCorte = Operaciones.ConvertirFecha(dgvIngresos.Rows(Index).Cells("Fecha de Corte").Value)
            Dias = Operaciones.ConvertirEntero(dgvIngresos.Rows(Index).Cells("Dias").Value)
            Dividendo = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Dividendo").Value.ToString) / 100
            Ingreso = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Ingresos").Value)
            RendMensual = Operaciones.ConvertirDecimal(dgvIngresos.Rows(Index).Cells("Rendimiento Mensual").Value.ToString) / 100

            coleccion.Add(NumDiv)
            coleccion.Add(FechaCorte)
            coleccion.Add(Dias)
            coleccion.Add(Dividendo)
            coleccion.Add(Ingreso)
            coleccion.Add(RendMensual)
            Diccionario.Add(NumDiv, coleccion)
            Index += 1
        End While


        Return Diccionario
    End Function


    Private Function ColeccionDatos()
        Dim Coleccion As New Collection
        Dim TipoCalculo As String = Me.TipoCalculo
        Coleccion.Add(txtValorNominal.Text)
        Coleccion.Add(txtCuotasDeParticipacion.Text)
        Coleccion.Add(txtValorDeParticipacion.Text)
        Coleccion.Add((txtPorcentajeComisionCasa.Text.ToString.Trim("%")) / 100)
        Coleccion.Add((txtPorcentajeComisionBolsa.Text.ToString.Trim("%")) / 100)
        Coleccion.Add(txtComisionCasa.Text)
        Coleccion.Add(txtComisionBolsa.Text)
        Coleccion.Add(txtValorTransado.Text)
        Coleccion.Add(dtpFechaOperacion.Value)
        Coleccion.Add(dtpFechaLiquidacion.Value)
        Coleccion.Add(dtpFechaVencimiento.Value)
        Coleccion.Add(txtDiasLiq.Text)
        Coleccion.Add(CDec(txtRendimientoOfrecido.Text.ToString.Trim("%")) / 100)
        Coleccion.Add(cboPeriodicidad.SelectedValue)
        Coleccion.Add(cboAnioBase.SelectedIndex)
        Coleccion.Add(txtPlazo.Text)
        Coleccion.Add(txtDividendo.Text.ToString.Trim("%") / 100)
        Coleccion.Add(TipoCalculo)


        Return Coleccion
    End Function

    Function TipoCalculo()
        Dim TipCalculo As String = "FinMes"

        If RadioButton1.Checked Then
            TipCalculo = "FinMes"
        Else
            If RadioButton2.Checked Then
                TipCalculo = "Mensual"
            End If
        End If
        Return TipCalculo
    End Function

    Private Sub CargarRegistroPrincipal()
        Dim FONV As New FondoInversionDAO
        Dim Codigo As String = Variables.Codigo
        Dim Coleccion As Collection = FONV.Cargar(Codigo)
        Dim TipoOperacion As String = String.Empty
        If Not Coleccion.Count = 0 Then




            txtValorNominal.Text = Operaciones.ConvertirDecimal(Coleccion(1).ToString)
            txtCuotasDeParticipacion.Text = Operaciones.ConvertirDecimal(Coleccion(2).ToString)
            txtValorDeParticipacion.Text = Operaciones.ConvertirDecimal(Coleccion(3).ToString)
            txtPorcentajeComisionCasa.Text = Operaciones.ConvertirDecimal((Coleccion(4) * 100).ToString)
            txtPorcentajeComisionBolsa.Text = Operaciones.ConvertirDecimal((Coleccion(5) * 100).ToString)
            txtComisionCasa.Text = Operaciones.ConvertirDecimal(Coleccion(6).ToString)
            txtComisionBolsa.Text = Operaciones.ConvertirDecimal(Coleccion(7).ToString)
            txtValorTransado.Text = Operaciones.ConvertirDecimal(Coleccion(8).ToString)
            dtpFechaOperacion.Value = Operaciones.ConvertirFecha(Coleccion(9).ToString)
            dtpFechaLiquidacion.Value = Operaciones.ConvertirFecha(Coleccion(10).ToString)
            dtpFechaVencimiento.Value = Operaciones.ConvertirFecha(Coleccion(11))
            txtDiasLiq.Text = Operaciones.ConvertirEntero(Coleccion(12).ToString)
            txtRendimientoOfrecido.Text = Operaciones.ConvertirDecimal((Coleccion(13) * 100).ToString)
            cboPeriodicidad.SelectedValue = Coleccion(14).ToString
            cboAnioBase.SelectedIndex = Operaciones.ConvertirEntero(Coleccion(15).ToString)
            txtPlazo.Text = Operaciones.ConvertirEntero(Coleccion(16).ToString)
            txtDividendo.Text = Operaciones.ConvertirDecimal((Coleccion(17) * 100).ToString)

            TipoOperacion = Coleccion(18).ToString


            If TipoOperacion = "FinMes" Then
                RadioButton1.Checked = True
            Else
                If TipoOperacion = "Mensual" Then
                    RadioButton2.Checked = True
                End If
            End If

        End If
    End Sub

    Sub CargarFilas()
        Dim FONV As New FondoInversionDAO
        Dim Codigo As String = Variables.Codigo
        Dim Tabla As DataTable = FONV.CargarFilas(Codigo)


        For Each rows As DataRow In Tabla.Rows
            Dim NumDiv As Integer = 0
            Dim Fecha As Date = Date.Today.Date
            Dim Dias As Integer = 0
            Dim Dividendo As String = String.Empty
            Dim Ingreso As Double = 0
            Dim Rend As String = String.Empty



            NumDiv = Operaciones.ConvertirEntero(rows("NumDiv").ToString)
            Fecha = Operaciones.ConvertirFecha(rows("Fecha").ToString)
            Dias = Operaciones.ConvertirEntero(rows("Dias").ToString)
            Dividendo = (Operaciones.ConvertirDecimal(rows("Dividendo").ToString) * 100).ToString + "%"
            Ingreso = Operaciones.ConvertirDecimal(rows("Ingreso").ToString)
            Rend = (Operaciones.ConvertirDecimal(rows("Rend").ToString) * 100).ToString + "%"
            dgvIngresos.Rows.Add("", Fecha, Dias, Dividendo, Ingreso, Rend, NumDiv)







        Next
        AgregarCorrelativo()
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs)


    End Sub



    Sub AgregarCampos()
        Dim CantidadColumnas As Integer = dgvIngresos.ColumnCount
        If CantidadColumnas = 0 Then


            Dim FechaCorte, Dias, Dividendo, Ingreso, RendMensual, correlativo, NumDiv As New DataGridViewTextBoxColumn
            correlativo.Name = "correlativo"
            FechaCorte.Name = "Fecha de Corte"
            Dias.Name = "Dias"
            Dividendo.Name = "Dividendo"
            Ingreso.Name = "Ingresos"
            RendMensual.Name = "Rendimiento Mensual"
            NumDiv.Name = "NumDiv"
            dgvIngresos.Columns.Add(correlativo)
            dgvIngresos.Columns.Add(FechaCorte)
            dgvIngresos.Columns.Add(Dias)
            dgvIngresos.Columns.Add(Dividendo)
            dgvIngresos.Columns.Add(Ingreso)
            dgvIngresos.Columns.Add(RendMensual)
            dgvIngresos.Columns.Add(NumDiv)
        End If
    End Sub

    Private Sub dgvIngresos_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvIngresos.CellContentClick

    End Sub

    Private Sub dgvIngresos_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvIngresos.CellMouseClick

        CalculosTabla()
    End Sub

    Private Sub dgvIngresos_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvIngresos.CellEndEdit


    End Sub

    Private Sub txtDiasLiq_TextChanged(sender As Object, e As EventArgs) Handles txtDiasLiq.TextChanged
        Rellenar()
        Calculos()
    End Sub

    Private Sub btnModificar_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnEliminar_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub BindingNavigator1_RefreshItems(sender As Object, e As EventArgs) Handles BindingNavigator1.RefreshItems

    End Sub

    Private Sub btnRescate_Click(sender As Object, e As EventArgs)


    End Sub

    Private Sub txtCuotasDeParticipacion_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCuotasDeParticipacion.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub txtValorDeParticipacion_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtValorDeParticipacion.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub txtPorcentajeComisionCasa_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPorcentajeComisionCasa.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub txtPorcentajeComisionBolsa_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPorcentajeComisionBolsa.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub txtDiasLiq_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDiasLiq.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub txtRendimientoOfrecido_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtRendimientoOfrecido.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub txtPlazo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPlazo.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Private Sub GroupBox3_Enter(sender As Object, e As EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub GroupBox2_Enter(sender As Object, e As EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub dgvIngresos_Click(sender As Object, e As EventArgs) Handles dgvIngresos.Click

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs)

    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnCancelat_Click(sender As Object, e As EventArgs) Handles btnCancelat.Click
        Me.Close()
    End Sub


    Sub QuitarFila()
        Dim CantidadFilas As Integer = dgvIngresos.Rows.Count - 1
        If CantidadFilas > 0 Then




            Dim result As Integer = MsgBox("¿Está seguro que desea eliminar la fila?", MsgBoxStyle.OkCancel)
            ' Test result.
            Dim codigo As String = Variables.Codigo
            Dim Numero As Integer = dgvIngresos.CurrentRow.Cells("NumDiv").Value
            If result = MsgBoxResult.Ok Then
                Try
                    dgvIngresos.Rows.Remove(dgvIngresos.CurrentRow)
                    FOIN.EliminarIngreso(codigo, Numero)
                Catch ex As Exception
                    MsgBox("No ha seleccionado una fila")
                End Try



            End If

        Else
            MsgBox("Actualmente no tiene registros")

        End If
    End Sub

    Private Sub txtCuotasDeParticipacion_Leave(sender As Object, e As EventArgs) Handles txtCuotasDeParticipacion.Leave
        Rellenar()
    End Sub

    Private Sub txtValorDeParticipacion_Leave(sender As Object, e As EventArgs) Handles txtValorDeParticipacion.Leave
        Rellenar()
    End Sub

    Private Sub txtPorcentajeComisionCasa_Leave(sender As Object, e As EventArgs) Handles txtPorcentajeComisionCasa.Leave
        Rellenar()
    End Sub

    Private Sub txtPorcentajeComisionBolsa_Leave(sender As Object, e As EventArgs) Handles txtPorcentajeComisionBolsa.Leave
        Rellenar()
    End Sub

    Private Sub txtDiasLiq_Leave(sender As Object, e As EventArgs) Handles txtDiasLiq.Leave
        Rellenar()
    End Sub

    Private Sub txtRendimientoOfrecido_Leave(sender As Object, e As EventArgs) Handles txtRendimientoOfrecido.Leave
        Rellenar()
    End Sub

    Private Sub txtPlazo_Leave(sender As Object, e As EventArgs) Handles txtPlazo.Leave
        Rellenar()
    End Sub

    Private Sub txtDividendo_TextChanged_1(sender As Object, e As EventArgs) Handles txtDividendo.TextChanged
        If (txtDividendo.ToString.IndexOf("%") = -1) Then
            txtDividendo.Text += "%"
        End If

        Rellenar()
    End Sub

    Private Sub txtDividendo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDividendo.KeyPress
        If String.IsNullOrEmpty(sender.Text) Then
            If e.KeyChar = "." Then
                sender.Text = "0"
                Exit Sub
            End If
        End If

        If (Not sender.Text.ToString.IndexOf(".") = -1 And e.KeyChar = ".") Then
            Operaciones.ValidarEntrada(sender, e, True)

        Else

            Operaciones.ValidarEntrada(sender, e, False)

        End If
    End Sub

    Sub CargarTransladoNuevo()
        Dim Existe As Boolean = ExisteInversion()
        If Not RetCodigoInversionExiste And Not Existe Then
            txtRendimientoOfrecido.Text = Variables.RetRendimiento
            txtCuotasDeParticipacion.Text = Variables.RetCuotaParticipacionFINV
            txtValorDeParticipacion.Text = Variables.RetValorParticipacionFINV
        End If
    End Sub
End Class