Public Class frmIngresosConsulta
    Private _Desde As Boolean
    Private _Hasta As Boolean
    Private _FechaDesde As Date
    Private _FechaHasta As Date
    Private _SoloPendientes As Boolean
    Dim oDAOGeneral As New DAOGeneral
    Dim dtIngresos As DataTable

    Private Sub frmIngresos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListaIngresos()
        CargarEmpresas()
        CargarTiposInstrumentos()

    End Sub

    Private Sub Controles()
        If Me.chkDesde.Checked = True Then
            _Desde = True
        Else
            _Desde = False
        End If
        If Me.chkHasta.Checked = True Then
            _Hasta = True
        Else
            _Hasta = False
        End If


        If _Desde Then
            _FechaDesde = Me.dtpDesde.Value
        Else
            _FechaDesde = dtpDesde.MinDate
        End If

        If _Hasta Then
            _FechaHasta = Me.dtpHasta.Value
        Else
            _FechaHasta = dtpHasta.MaxDate
        End If



        If Me.chkSoloPendientes.Checked Then
            _SoloPendientes = True
        Else
            _SoloPendientes = False
        End If

    End Sub

    Private Sub ListaIngresos()
        Controles()
        dtIngresos = IngresosDAO.ListaIngresos(_FechaDesde, _FechaHasta, _SoloPendientes)
        Me.dgvIngresos.DataSource = dtIngresos
        'ColorEstadosGrid()

    End Sub
    Private Sub Refrescar()
        ListaIngresos()
    End Sub


    Private Sub chkDesde_CheckedChanged(sender As Object, e As EventArgs) Handles chkDesde.CheckedChanged
        Refrescar()

    End Sub

    Private Sub chkHasta_CheckedChanged(sender As Object, e As EventArgs) Handles chkHasta.CheckedChanged
        Refrescar()

    End Sub

    Private Sub chkSoloPendientes_CheckedChanged(sender As Object, e As EventArgs) Handles chkSoloPendientes.CheckedChanged
        Refrescar()

    End Sub

    Private Sub dtpDesde_ValueChanged(sender As Object, e As EventArgs) Handles dtpDesde.ValueChanged
        If _Desde Then
            _FechaDesde = Me.dtpDesde.Value.Date
            Refrescar()
        End If
    End Sub

    Private Sub dtpHasta_ValueChanged(sender As Object, e As EventArgs) Handles dtpHasta.ValueChanged
        If _Hasta Then
            _FechaHasta = Me.dtpHasta.Value.Date
            Refrescar()
        End If
    End Sub
    Private Sub dgvIngresos_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvIngresos.CellDoubleClick
        LeDiDobleClic(e.RowIndex)
    End Sub
    Private Sub LeDiDobleClic(vIdFila As Integer)
        Dim vFila As DataGridViewRow = dgvIngresos.Rows(vIdFila)
        Dim vCodigoInversion As String
        Dim vIngresoID As Integer
        Dim vEstado As String
        Dim vValidado As String
        Dim vConciliado As String

        Dim vNivel0Instrumeto As String
        Dim vNivel1Instrumeto As String

        vCodigoInversion = vFila.Cells("CodInv").Value.ToString
        vIngresoID = vFila.Cells("IngresoID").Value.ToString
        vEstado = vFila.Cells("Estado").Value.ToString
        vValidado = vFila.Cells("Validado").Value.ToString
        vConciliado = vFila.Cells("Conciliado").Value.ToString
        'vNivel0Instrumeto = vFila.Cells("INS0").Value.ToString
        'vNivel1Instrumeto = vFila.Cells("INS1").Value.ToString

        DetalleIngreso(vCodigoInversion, vIngresoID)

    End Sub
    Private Sub DetalleIngreso(ByVal vCodigoInversion As String, ByVal vIngresoID As Integer)
        Dim f As New frmValidarIngreso
        f.CodigoInversion = vCodigoInversion
        f.IngresoID = vIngresoID
        'f.Nivel0Instrumeto = vNivel0Instrumeto
        'f.Nivel1Instrumeto = vNivel1Instrumeto
        f.ShowDialog()
    End Sub
    Private Sub CargarEmpresas()
        Me.lstEmpresas.DataSource = oDAOGeneral.ListaEmpresas
        Me.lstEmpresas.DisplayMember = "Descripcion"
        Me.lstEmpresas.ValueMember = "Codigo"

        SeleccionarTodasEmpresas()

    End Sub
    Private Sub CargarTiposInstrumentos()
        Me.lstInstrumentos.DataSource = oDAOGeneral.ListaInstrumentos("Propuesta")
        Me.lstInstrumentos.DisplayMember = "Descripcion"
        Me.lstInstrumentos.ValueMember = "Codigo"

        SeleccionarTodosInstrumentos()
    End Sub

    Private Sub lstEmpresas_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles lstEmpresas.ItemCheck
        'MsgBox("ItemCheck")
        'Dim drv As DataRowView
        If e.CurrentValue = CheckState.Unchecked Then
            'MsgBox("Checked")
            'MsgBox(lstEmpresas.Items(e.Index).ToString)
            'drv = lstEmpresas.Items(e.Index)
            'MsgBox(drv(0).ToString)
            'ActualizarPorEmpresa(drv(0).ToString)

        Else
            'MsgBox("No Checked")


        End If

        'ActualizarPorEmpresa()
    End Sub
    Public Sub SeleccionarTodasEmpresas()
        For i = 0 To lstEmpresas.Items.Count - 1
            lstEmpresas.SetItemChecked(i, True)
        Next
        'ActualizarPorEmpresa()
    End Sub
    Private Sub btnSeleccionarTodasEmpresas_Click(sender As Object, e As EventArgs) Handles btnSeleccionarTodasEmpresas.Click
        SeleccionarTodasEmpresas()
    End Sub

    Private Sub btnLimpiarEmpresas_Click(sender As Object, e As EventArgs) Handles btnLimpiarEmpresas.Click
        For i = 0 To lstEmpresas.Items.Count - 1
            lstEmpresas.SetItemChecked(i, False)
        Next
        'ActualizarPorEmpresa()

    End Sub
    Private Sub ActualizarPorEmpresa()
        Dim drv As DataRowView
        Dim vFiltro As String = String.Empty

        vFiltro = "CodEmpr IN("

        For Each itemChecked In lstEmpresas.CheckedItems
            drv = itemChecked
            'MessageBox.Show(drv(0).ToString)
            vFiltro = vFiltro & "'" & drv(0).ToString & "',"
        Next
        vFiltro = vFiltro & "'')"

        Dim custDV As DataView = New DataView(dtIngresos, vFiltro, "CodEmpr", DataViewRowState.CurrentRows)
        Me.dgvIngresos.DataSource = custDV
    End Sub
    Private Sub ActualizarPorEmpresa(ByVal vCodEmpr As String)
        Dim drv As DataRowView
        Dim vFiltro As String = String.Empty

        vFiltro = "CodEmpr IN('" & vCodEmpr & "',"

        For Each itemChecked In lstEmpresas.CheckedItems
            drv = itemChecked
            'MessageBox.Show(drv(0).ToString)
            vFiltro = vFiltro & "'" & drv(0).ToString & "',"
        Next
        vFiltro = vFiltro & "'')"

        Dim custDV As DataView = New DataView(dtIngresos, vFiltro, "CodEmpr", DataViewRowState.CurrentRows)
        Me.dgvIngresos.DataSource = custDV
    End Sub
    Private Sub btnActualizarPorEmpresa_Click(sender As Object, e As EventArgs) Handles btnActualizarPorEmpresa.Click
        ActualizarPorEmpresa()
    End Sub

    Private Sub btnActualizarPorInstrumentos_Click(sender As Object, e As EventArgs) Handles btnActualizarPorInstrumentos.Click
        Dim drv As DataRowView
        Dim vFiltro As String = String.Empty

        vFiltro = "CodIF IN("

        For Each itemChecked In lstInstrumentos.CheckedItems
            drv = itemChecked
            'MessageBox.Show(drv(0).ToString)
            vFiltro = vFiltro & "'" & drv(0).ToString & "',"
        Next
        vFiltro = vFiltro & "'')"

        Dim custDV As DataView = New DataView(dtIngresos, vFiltro, "CodEmpr", DataViewRowState.CurrentRows)
        Me.dgvIngresos.DataSource = custDV
    End Sub
    Public Sub SeleccionarTodosInstrumentos()
        For i = 0 To lstInstrumentos.Items.Count - 1
            lstInstrumentos.SetItemChecked(i, True)
        Next
    End Sub
    Private Sub btnSeleccionarTodosInstrumentos_Click(sender As Object, e As EventArgs) Handles btnSeleccionarTodosInstrumentos.Click
        SeleccionarTodosInstrumentos()
    End Sub

    Private Sub btnLimpiarInstrumentos_Click(sender As Object, e As EventArgs) Handles btnLimpiarInstrumentos.Click
        For i = 0 To lstInstrumentos.Items.Count - 1
            lstInstrumentos.SetItemChecked(i, False)
        Next
    End Sub

    Private Sub lstEmpresas_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstEmpresas.SelectedValueChanged
        'MsgBox("SelectedValueChanged")
    End Sub

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

    End Sub
End Class