
Public Class AccionesCE
    Private _CodigoInversion As String
    Private _TipoOperacion As String
    Private _AccionPreferente As String
    Private _OperacionEnBolsa As String
    Private _FactorDeDividendo As Double
    Private _IngresoBruto As Double
    Private _IngresoBrutoPendiente As Double
    Private _ISR As Double
    Private _ISRPendiente As Double
    Private _IngresoNeto As Double
    Dim Operaciones As New Operaciones
    Private _IngresoNetoPendiente As Double
    Private _dtImpuestosYComisiones As DataTable
    Private _dtDividendos As DataTable
    Private _TipoInstrumento As String = "ACC"
    Private _CodigoInstrumentoFinanciero As String
    Private _DescripcionTipoInstrumento As String
    Private _Registro As DataTable
    Private _ComisionCompraCasa As Double
    Private _ComisionCompraBolsa As Double
    Private _ComisionVentaCasa As Double
    Private _ComisionVentaBolsa As Double
    Private _IOF As Boolean
    Private _Renta As Boolean
    Private _Tipo As String
    Private _RetornoBruto As Double
    Private _GananciaDeCapitalTotalVentasus As Double = 0
    Private _RetornoAnualizadoBrutoVentasus As Double = 0
    Private _MontoNetoVentasus As Double = 0
    Private _PrecioSucioVentasus As Double = 0
    Private _ComisionPorcentajeCompraCasa As Double = 0
    Private _ComisionPorcentajeCompraBolsa As Double = 0
    Private _ComisionPorcentajeVentaCasa As Double = 0
    Private _ComisionPorcentajeVentaBolsa As Double = 0
    Private _MontoNetoComprasus As Double = 0






    'COMPRA
    Private _CodigoCasaCompra As String
    Private _PrecioLimpioCompra As Double
    Private _FechaOperacionCompra As Date
    Private _FechaLiquidacionCompra As Date
    Private _DiasDeTenenciaCompra As Integer
    Private _GananciaDeCapitalTotalCompra As Double
    Private _GananciaDeCapitalUnitarioCompra As Double
    Private _RetornoAnualizadoBrutoCompra As Double
    Private _RetornoAnualizadoNetoCompra As Double
    Private _CantidadCompra As Integer
    Private _ValorTransadoCompra As Double
    Private _MontoNetoCompra As Double
    Private _PrecioSucioCompra As Double
    Private _SumaImpuestosYComisionesCompra As Double


    'VENTA
    Private _CodigoCasaVenta As String
    Private _PrecioLimpioVenta As Double
    Private _FechaOperacionVenta As Date
    Private _FechaLiquidacionVenta As Date
    Private _DiasDeTenenciaVenta As Integer
    Private _GananciaDeCapitalTotalVenta As Double
    Private _GananciaDeCapitalUnitarioVenta As Double
    Private _RetornoAnualizadoBrutoVenta As Double
    Private _RetornoAnualizadoNetoVenta As Double
    Private _CantidadVenta As Integer
    Private _ValorTransadoVenta As Double
    Private _MontoNetoVenta As Double
    Private _PrecioSucioVenta As Double
    Private _SumaImpuestosYComisionesVenta As Double



    'GENERAL
    Public Property CodigoInversion
        Get
            Return _CodigoInversion
        End Get
        Set(value)
            _CodigoInversion = value
        End Set
    End Property
    Public Property AccionPreferente As String
        Get
            Return _AccionPreferente
        End Get
        Set(value As String)
            _AccionPreferente = value
        End Set
    End Property
    Public Property OperacionEnBolsa As String
        Get
            Return _OperacionEnBolsa
        End Get
        Set(value As String)
            _OperacionEnBolsa = value
        End Set
    End Property

    Public Property FactorDeDividendo As Double
        Get
            Return _FactorDeDividendo
        End Get
        Set(value As Double)
            _FactorDeDividendo = value
        End Set
    End Property

    Public Property ImpuestosYComisiones As DataTable
        Get
            Return _dtImpuestosYComisiones
        End Get
        Set(value As DataTable)
            _dtImpuestosYComisiones = value
            CalcularSumaImpuestosyComisiones()

        End Set
    End Property




    'COMPRA
    Public Property CodigoCasaCompra As String
        Get
            Return _CodigoCasaCompra
        End Get
        Set(value As String)
            _CodigoCasaCompra = value
        End Set
    End Property
    Public Property PrecioLimpioCompra As Double
        Get
            Return _PrecioLimpioCompra
        End Get
        Set(value As Double)
            _PrecioLimpioCompra = value
            CalculaValorTrasadoCompra()
        End Set
    End Property
    Public Property FechaOperacionCompra As Date
        Get
            Return _FechaOperacionCompra
        End Get
        Set(value As Date)
            _FechaOperacionCompra = value
        End Set
    End Property
    Public Property FechaVencimientoCompra As Date
        Get
            Return _FechaLiquidacionCompra
        End Get
        Set(value As Date)
            _FechaLiquidacionCompra = value
            CalcularDiasDeTenenciaVenta()
        End Set
    End Property
    Public Property DiasDeTenenciaCompra As Integer
        Get
            Return _DiasDeTenenciaCompra
        End Get
        Set(value As Integer)
            _DiasDeTenenciaCompra = value
        End Set
    End Property




    Public Property ComisionVentaBolsa As Double
        Get
            Return _ComisionVentaBolsa
        End Get
        Set(value As Double)
            _ComisionVentaBolsa = value
        End Set
    End Property


    Public Property ComisionPorcentajeVentaBolsa As Double
        Get
            Return _ComisionPorcentajeVentaBolsa
        End Get
        Set(value As Double)
            _ComisionPorcentajeVentaBolsa = value
        End Set
    End Property

    Public Property ComisionVentaCasa As Double
        Get
            Return _ComisionVentaCasa
        End Get
        Set(value As Double)
            _ComisionVentaCasa = value
        End Set
    End Property


    Public Property ComisionPorcentajeVentaCasa As Double
        Get
            Return _ComisionPorcentajeVentaCasa
        End Get
        Set(value As Double)
            _ComisionPorcentajeVentaCasa = value
        End Set
    End Property




    Public Property ComisionCompraBolsa As Double
        Get
            Return _ComisionCompraBolsa
        End Get
        Set(value As Double)
            _ComisionCompraBolsa = value
        End Set
    End Property


    Public Property ComisionPorcentajeCompraBolsa As Double
        Get
            Return _ComisionPorcentajeCompraBolsa
        End Get
        Set(value As Double)
            _ComisionPorcentajeCompraBolsa = value
        End Set
    End Property





    Public Property ComisionCompraCasa As Double
        Get
            Return _ComisionCompraCasa
        End Get
        Set(value As Double)
            _ComisionCompraCasa = value
        End Set
    End Property


    Public Property ComisionPorcentajeCompraCasa As Double
        Get
            Return _ComisionPorcentajeCompraCasa
        End Get
        Set(value As Double)
            _ComisionPorcentajeCompraCasa = value
        End Set
    End Property

    Public Property IOF As Boolean
        Get
            Return _IOF
        End Get
        Set(value As Boolean)
            _IOF = value
        End Set
    End Property


    Public Property Renta As Boolean
        Get
            Return _Renta
        End Get
        Set(value As Boolean)
            _Renta = value
        End Set
    End Property












    Public Property GananciaDeCapitalTotalCompra As Double
        Get
            Return _GananciaDeCapitalTotalCompra
        End Get
        Set(value As Double)
            _GananciaDeCapitalTotalCompra = value
        End Set
    End Property
    Public Property GananciaDeCapitalUnitarioCompra As Double
        Get
            Return _GananciaDeCapitalUnitarioCompra
        End Get
        Set(value As Double)
            _GananciaDeCapitalUnitarioCompra = value
        End Set
    End Property
    Public Property RetornoAnualizadoBrutoCompra As Double
        Get
            Return _RetornoAnualizadoBrutoCompra
        End Get
        Set(value As Double)
            _RetornoAnualizadoBrutoCompra = value
        End Set
    End Property
    Public Property RetornoAnualizadoNetoCompra As Double
        Get
            Return _RetornoAnualizadoNetoCompra
        End Get
        Set(value As Double)
            _RetornoAnualizadoNetoCompra = value
        End Set
    End Property
    Public Property CantidadCompra As Integer
        Get
            Return _CantidadCompra
        End Get
        Set(value As Integer)

            _CantidadCompra = value
            CalculaValorTrasadoCompra()
            CalcularPrecioSucioCompra()

        End Set
    End Property
    Public Property ValorTransadoCompra As Double
        Get
            Return _ValorTransadoCompra
        End Get
        Set(value As Double)
            _ValorTransadoCompra = value
        End Set
    End Property

    Public Property MontoNetoCompra As Double
        Get

            Return _MontoNetoCompra
        End Get
        Set(value As Double)

            _MontoNetoCompra = value
            CalcularMontoNetoCompra()
        End Set
    End Property

    Public Property MontoNetoComprasus As Double
        Get

            Return _MontoNetoComprasus
        End Get
        Set(value As Double)

            _MontoNetoComprasus = value

        End Set
    End Property
    Public Property PrecioSucioCompra As Double
        Get
            Return _PrecioSucioCompra
        End Get
        Set(value As Double)
            _PrecioSucioCompra = value
        End Set
    End Property
    Public Property SumaImpuestosYComisionesCompra As Double
        Get
            Return _SumaImpuestosYComisionesCompra
        End Get
        Set(value As Double)
            _SumaImpuestosYComisionesCompra = value
            CalcularMontoNetoCompra()
        End Set
    End Property

    'VENTA
    Public Property CodigoCasaVenta As String
        Get
            Return _CodigoCasaVenta
        End Get
        Set(value As String)
            _CodigoCasaVenta = value
        End Set
    End Property
    Public Property PrecioLimpioVenta As Double
        Get
            Return _PrecioLimpioVenta
        End Get
        Set(value As Double)
            _PrecioLimpioVenta = value
            CalcularValorTransadoVenta()
        End Set
    End Property
    Public Property FechaOperacionVenta As Date
        Get
            Return _FechaOperacionVenta
        End Get
        Set(value As Date)
            _FechaOperacionVenta = value
        End Set
    End Property
    Public Property FechaVencimientoVenta As Date
        Get
            Return _FechaLiquidacionVenta
        End Get
        Set(value As Date)
            _FechaLiquidacionVenta = value
            CalcularDiasDeTenenciaVenta()
        End Set
    End Property
    Public Property DiasDeTenenciaVenta As Integer
        Get
            Return _DiasDeTenenciaVenta
        End Get
        Set(value As Integer)
            _DiasDeTenenciaVenta = value
        End Set
    End Property
    Public Property GananciaDeCapitalTotalVenta As Double
        Get
            Return _GananciaDeCapitalTotalVenta
        End Get
        Set(value As Double)
            _GananciaDeCapitalTotalVenta = value
        End Set
    End Property


    Public Property GananciaDeCapitalTotalVentasus As Double
        Get
            Return _GananciaDeCapitalTotalVentasus
        End Get
        Set(value As Double)
            _GananciaDeCapitalTotalVentasus = value
        End Set
    End Property
    Public Property GananciaDeCapitalUnitarioVenta As Double
        Get
            Return _GananciaDeCapitalUnitarioVenta
        End Get
        Set(value As Double)
            _GananciaDeCapitalUnitarioVenta = value
        End Set
    End Property
    Public Property RetornoAnualizadoBrutoVenta As Double
        Get
            CalcularRetornoAnualBrutoVenta()
            Return _RetornoAnualizadoBrutoVenta
        End Get
        Set(value As Double)
            _RetornoAnualizadoBrutoVenta = value
        End Set
    End Property

    Public Property RetornoAnualizadoBrutoVentasus As Double
        Get

            Return _RetornoAnualizadoBrutoVentasus
        End Get
        Set(value As Double)
            _RetornoAnualizadoBrutoVentasus = value
        End Set
    End Property
    Public Property RetornoAnualizadoNetoVenta As Double
        Get
            CalcularRetornoAnualNetoVenta()
            Return _RetornoAnualizadoNetoVenta
        End Get
        Set(value As Double)
            _RetornoAnualizadoNetoVenta = value
        End Set
    End Property
    Public Property CantidadVenta As Integer
        Get
            Return _CantidadVenta
        End Get
        Set(value As Integer)
            _CantidadVenta = value
            CalcularValorTransadoVenta()
            CalcularGananciaDeCapitalUnitarioVenta()
            CalcularPrecioSucioVenta()

        End Set
    End Property

    Public Property Tipo As String
        Get
            Return _Tipo
        End Get
        Set(value As String)
            _Tipo = value


        End Set
    End Property
    Public Property RetornoBruto As Double
        Get
            Return _RetornoBruto
        End Get
        Set(value As Double)
            _RetornoBruto = value


        End Set
    End Property
    Public Property ValorTransadoVenta As Double
        Get
            Return _ValorTransadoVenta
        End Get
        Set(value As Double)
            _ValorTransadoVenta = value
        End Set
    End Property

    Public Property MontoNetoVenta As Double
        Get
            Return _MontoNetoVenta
        End Get
        Set(value As Double)
            _MontoNetoVenta = value
        End Set
    End Property


    Public Property MontoNetoVentasus As Double
        Get
            Return _MontoNetoVentasus
        End Get
        Set(value As Double)
            _MontoNetoVentasus = value
        End Set
    End Property

    Public Property PrecioSucioVenta As Double
        Get
            Return _PrecioSucioVenta
        End Get
        Set(value As Double)
            _PrecioSucioVenta = value
        End Set
    End Property

    Public Property PrecioSucioVentasus As Double
        Get
            Return _PrecioSucioVentasus
        End Get
        Set(value As Double)
            _PrecioSucioVentasus = value
        End Set
    End Property
    Public Property SumaImpuestosYComisionesVenta As Double
        Get
            Return _SumaImpuestosYComisionesVenta
        End Get
        Set(value As Double)
            _SumaImpuestosYComisionesVenta = value
            CalcularMontoNetoVenta()
        End Set
    End Property
    Public Sub New()
        'GENERAL
        _IngresoBruto = 0
        _IngresoBrutoPendiente = 0
        _ISR = 0
        _ISRPendiente = 0
        _IngresoNeto = 0
        _IngresoNetoPendiente = 0
        _dtImpuestosYComisiones = New DataTable
        _dtDividendos = New DataTable
        _Registro = New DataTable

        'COMPRA
        _PrecioLimpioCompra = 0
        _FechaOperacionCompra = Date.Now.Date
        _FechaLiquidacionCompra = Date.Now.Date
        _CantidadCompra = 1

        _DiasDeTenenciaCompra = 0
        _GananciaDeCapitalTotalCompra = 0
        _GananciaDeCapitalUnitarioCompra = 0
        _RetornoAnualizadoBrutoCompra = 0
        _RetornoAnualizadoNetoCompra = 0
        _ValorTransadoCompra = 0
        _MontoNetoCompra = 0
        _PrecioSucioCompra = 0


        'VENTA
        _PrecioLimpioVenta = 0
        _FechaOperacionVenta = Date.Now.Date
        _FechaLiquidacionVenta = Date.Now.Date
        _CantidadVenta = 1

        _DiasDeTenenciaVenta = 0
        _GananciaDeCapitalTotalVenta = 0
        _GananciaDeCapitalUnitarioVenta = 0
        _RetornoAnualizadoBrutoVenta = 0
        _RetornoAnualizadoNetoVenta = 0
        _CantidadVenta = 0
        _ValorTransadoVenta = 0
        _MontoNetoVenta = 0
        _PrecioSucioVenta = 0

    End Sub


    'CALCULOS COMPRA
    Private Sub CalculaValorTrasadoCompra()

        _ValorTransadoCompra = _CantidadCompra * _PrecioLimpioCompra
        CalcularMontoNetoCompra()
    End Sub
    Private Sub CalcularMontoNetoCompra()
        _MontoNetoCompra = _ValorTransadoCompra + _SumaImpuestosYComisionesCompra
        CalcularPrecioSucioCompra()

        CalcularGananciaDeCapitalTotalVenta()
        CalcularRetornoAnualBrutoVenta()
        CalcularRetornoAnualNetoVenta()


    End Sub
    Private Sub CalcularPrecioSucioCompra()
        If (String.IsNullOrEmpty(_CantidadCompra.ToString) Or _CantidadCompra.ToString = "0") Then
            _PrecioSucioCompra = "0.0"
        Else
            _PrecioSucioCompra = _MontoNetoCompra / _CantidadCompra
        End If

    End Sub


    'CALCULOS VENTA
    Private Sub CalcularDiasDeTenenciaVenta()
        Dim valor As Integer
        If _FechaLiquidacionVenta > Date.Today.Date Then
            valor = DateDiff(DateInterval.Day, _FechaLiquidacionCompra, Date.Today.Date)
        Else
            valor = DateDiff(DateInterval.Day, _FechaLiquidacionCompra, _FechaLiquidacionVenta)
        End If

        _DiasDeTenenciaVenta = valor
        CalcularRetornoAnualBrutoVenta()
        CalcularRetornoAnualNetoVenta()

    End Sub
    Private Sub CalcularValorTransadoVenta()
        _ValorTransadoVenta = _PrecioLimpioVenta * _CantidadVenta
        CalcularMontoNetoVenta()
    End Sub
    Private Sub CalcularMontoNetoVenta()
        _MontoNetoVenta = _ValorTransadoVenta - _SumaImpuestosYComisionesVenta
        CalcularGananciaDeCapitalTotalVenta()
        CalcularPrecioSucioVenta()

    End Sub
    Private Sub CalcularGananciaDeCapitalTotalVenta()
        _GananciaDeCapitalTotalVenta = _MontoNetoVenta - _MontoNetoCompra
        CalcularGananciaDeCapitalUnitarioVenta()
        CalcularRetornoAnualBrutoVenta()
        CalcularRetornoAnualNetoVenta()


    End Sub
    Private Sub CalcularGananciaDeCapitalUnitarioVenta()

        _GananciaDeCapitalUnitarioVenta = _GananciaDeCapitalTotalVenta / _CantidadVenta
        _GananciaDeCapitalUnitarioVenta = Format(_GananciaDeCapitalUnitarioVenta, "0.0000")


    End Sub
    Private Sub CalcularRetornoAnualBrutoVenta()
        If _DiasDeTenenciaVenta = 0 Or _MontoNetoCompra = 0 Then
            _RetornoAnualizadoBrutoVenta = 0

        Else
            _RetornoAnualizadoBrutoVenta = ((_GananciaDeCapitalTotalVenta + Variables.IngresoBruto) / _MontoNetoCompra) * 365 / _DiasDeTenenciaVenta


        End If



    End Sub
    Private Sub CalcularRetornoAnualNetoVenta()


        If _DiasDeTenenciaVenta = 0 Or _MontoNetoCompra Then

            _RetornoAnualizadoNetoVenta = 0
        Else
            _RetornoAnualizadoNetoVenta = ((_GananciaDeCapitalTotalVenta + Variables.IngresoNeto) / _MontoNetoCompra) * 365 / _DiasDeTenenciaVenta


        End If


    End Sub
    Private Sub CalcularPrecioSucioVenta()

        _PrecioSucioVenta = _MontoNetoVenta / _CantidadVenta


    End Sub
    Private Sub CalcularSumaImpuestosyComisiones()
        If Not Double.TryParse(_dtImpuestosYComisiones.Compute("SUM(MontoImp)", "TipoOper='C'").ToString, _SumaImpuestosYComisionesCompra) Then
            _SumaImpuestosYComisionesCompra = 0
        End If

        If Not Double.TryParse(_dtImpuestosYComisiones.Compute("SUM(MontoImp)", "TipoOper='V'").ToString, _SumaImpuestosYComisionesVenta) Then
            _SumaImpuestosYComisionesVenta = 0
        End If

        CalcularMontoNetoCompra()
        CalcularMontoNetoVenta()
    End Sub





End Class
