Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Public Class FrmInvoicesHisto
#Region "Mov. Pantalla"
    <DllImport("user32.Dll", EntryPoint:="ReleaseCapture")>
    Private Shared Sub ReleaseCapture()

    End Sub

    <DllImport("user32.Dll", EntryPoint:="SendMessage")>
    Private Shared Sub SendMessage(hWnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer)

    End Sub

    Private Sub PanelBarraTitulo_MouseMove(sender As Object, e As MouseEventArgs) Handles PanelBarraTitulo.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub

    Private Sub lblEmpresa_MouseMove(sender As Object, e As MouseEventArgs) Handles lblEmpresa.MouseMove
        ReleaseCapture()
        SendMessage(Me.Handle, &H112&, &HF012&, 0)
    End Sub

#End Region

    Private Sub btnCerrar_Click(sender As Object, e As EventArgs) Handles btnCerrar.Click
        Me.Close()
        CerrarFormulario()
    End Sub

    Private Sub FrmInvoicesHisto_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GlobalVariables.varReporteConsulta = 0
        CargarDGV()
        LlenaBusqueda()
    End Sub

    Sub CargarDGV()
        Dim sql As String
        Dim cn As SqlConnection
        Dim cm As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        sql = "SELECT Top 250 Id, Capturo, Folio, Fecha, FechaVenc, Cliente, Estatus, Moneda, Total, Obs, Pagado, FechaPag, RefPago, Aplico FROM Invoices order by Id desc"
        cn = New SqlConnection(GlobalVariables.cstring)
        Try
            cn.Open()
            cm = New SqlCommand()
            cm.CommandText = sql
            cm.CommandType = CommandType.Text
            cm.Connection = cn
            da = New SqlDataAdapter(cm)
            ds = New DataSet()
            da.Fill(ds)
            Me.BindingSource1.DataSource = ds.Tables(0).DefaultView
            Me.BindingNavigator1.BindingSource = Me.BindingSource1
            DGVRegs.DataSource = Me.BindingSource1
            'DGVRegs.Columns(0).Visible = False
            DGVRegs.Columns.Item("Total").DefaultCellStyle.Format = "C2"
            DGVRegs.Columns.Item("Total").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            FormatoFilas()
            cn.Close()
            cm = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Information)
        End Try
    End Sub
    Sub FormatoFilas()
        With DGVRegs
            'CAMBIA LOS TITULOS DE LAS COLUMNAS a ingles
            .Columns(0).HeaderText = "Id. Reg."
            .Columns(1).HeaderText = "Capturo/Act"
            .Columns(2).HeaderText = "Folio"
            .Columns(3).HeaderText = "Fecha"
            .Columns(4).HeaderText = "Fecha Venc."
            .Columns(5).HeaderText = "Cliente"
            .Columns(6).HeaderText = "Estatus"
            .Columns(7).HeaderText = "Móneda"
            .Columns(8).HeaderText = "Total"
            .Columns(9).HeaderText = "Observaciones"
            .Columns(10).HeaderText = "Pagado"
            .Columns(11).HeaderText = "Fecha Pago"
            .Columns(12).HeaderText = "Ref. Pago"
            .Columns(13).HeaderText = "Aplicó"
        End With
    End Sub

    Sub LlenaBusqueda()
        Dim dt As DataTable = New DataTable("Tabla")

        dt.Columns.Add("Codigo")
        dt.Columns.Add("Descripcion")

        Dim dr As DataRow

        dr = dt.NewRow()
        dr("Codigo") = "Folio"
        dr("Descripcion") = "No. Folio"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("Codigo") = "TipoReg"
        dr("Descripcion") = "Tipo Reg."
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("Codigo") = "Cliente"
        dr("Descripcion") = "Cliente"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("Codigo") = "Estatus"
        dr("Descripcion") = "Estatus"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("Codigo") = "TODOS"
        dr("Descripcion") = "TODOS *.*"
        dt.Rows.Add(dr)

        bsCriterio.ComboBox.DataSource = dt
        bsCriterio.ComboBox.ValueMember = "Codigo"
        bsCriterio.ComboBox.DisplayMember = "Descripcion"
    End Sub
    Private Sub bsCriterio_KeyDown(sender As Object, e As KeyEventArgs) Handles bsCriterio.KeyDown
        If e.KeyCode = Keys.Enter Then
            bsFiltro.Text = ""
            If bsCriterio.ComboBox.Text = "TODOS *.*" Then
                LlenaDatosTODOS()
                GlobalVariables.varReporteConsulta = 3
            End If
        End If
    End Sub

    Private Sub bsFiltro_KeyDown(sender As Object, e As KeyEventArgs) Handles bsFiltro.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim CriterioBusqueda As String = ""
            Try

                If bsCriterio.ComboBox.Text = "No. Folio" Then
                    GlobalVariables.varCriterioConsulta = "Folio Like '%" & Me.bsFiltro.Text & "%'"
                ElseIf bsCriterio.ComboBox.Text = "Tipo Reg." Then
                    GlobalVariables.varCriterioConsulta = "TipoReg like '%" & Me.bsFiltro.Text & "%'"
                ElseIf bsCriterio.ComboBox.Text = "Cliente" Then
                    GlobalVariables.varCriterioConsulta = "Cliente like '%" & Me.bsFiltro.Text & "%'"
                ElseIf bsCriterio.ComboBox.Text = "Estatus" Then
                    GlobalVariables.varCriterioConsulta = "Estatus like '%" & Me.bsFiltro.Text & "%'"
                End If
                LlenaDatos()
                GlobalVariables.varReporteConsulta = 1
            Catch ex As Exception
                Exit Sub
            End Try
        End If
    End Sub
    Sub LlenaDatos()
        Dim sql As String
        Dim cn As SqlConnection
        Dim cm As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        sql = "SELECT Id, Capturo, Folio, Fecha, FechaVenc, Cliente, Estatus, Moneda, Total, Obs, Pagado, FechaPag, RefPago, Aplico FROM Invoices  where " & GlobalVariables.varCriterioConsulta & " order by Id desc"
        cn = New SqlConnection(GlobalVariables.cstring)
        Try
            cn.Open()
            cm = New SqlCommand()
            cm.CommandText = sql
            cm.CommandType = CommandType.Text
            cm.Connection = cn
            da = New SqlDataAdapter(cm)
            ds = New DataSet()
            da.Fill(ds)
            Me.BindingSource1.DataSource = ds.Tables(0).DefaultView
            Me.BindingNavigator1.BindingSource = Me.BindingSource1
            DGVRegs.DataSource = Me.BindingSource1
            'DGVRegs.Columns(0).Visible = False
            DGVRegs.Columns.Item("Total").DefaultCellStyle.Format = "C2"
            DGVRegs.Columns.Item("Total").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            FormatoFilas()
            cn.Close()
            cm = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Information)
        End Try
    End Sub
    Sub LlenaDatosTODOS()
        Dim sql As String
        Dim cn As SqlConnection
        Dim cm As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As DataSet
        sql = "SELECT Id, Capturo, Folio, Fecha, FechaVenc, Cliente, Estatus, Moneda, Total, Obs, Pagado, FechaPag, RefPago, Aplico FROM Invoices order by Id desc"
        cn = New SqlConnection(GlobalVariables.cstring)
        Try
            cn.Open()
            cm = New SqlCommand()
            cm.CommandText = sql
            cm.CommandType = CommandType.Text
            cm.Connection = cn
            da = New SqlDataAdapter(cm)
            ds = New DataSet()
            da.Fill(ds)
            Me.BindingSource1.DataSource = ds.Tables(0).DefaultView
            Me.BindingNavigator1.BindingSource = Me.BindingSource1
            DGVRegs.DataSource = Me.BindingSource1
            ' DGVRegs.Columns(0).Visible = False
            DGVRegs.Columns.Item("Total").DefaultCellStyle.Format = "C2"
            DGVRegs.Columns.Item("Total").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            FormatoFilas()
            cn.Close()
            cm = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Information)
        End Try
    End Sub
    Private Sub DGVEntList_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVRegs.CellContentDoubleClick
        AbrirFormDatos()
    End Sub
    Private Sub btnEditar_Click(sender As Object, e As EventArgs) Handles btnEditar.Click
        AbrirFormDatos()
    End Sub

    Sub AbrirFormDatos()
        GlobalVariables.VarIDReg = DGVRegs.CurrentRow.Cells("Id").Value
        GlobalVariables.VarFromHistoInv = True

        If CheckForm(FrmInvoices) = True Then
            FrmInvoices.Inicializar()
            FrmInvoices.BringToFront()
            FrmInvoices.WindowState = FormWindowState.Normal
        Else
            FrmInvoices.Inicializar()
            FrmInvoices.Show()
            FrmMain.BtnInvoices.BackColor = Color.FromArgb(245, 97, 27)
        End If
    End Sub

    Private Sub btnVerTodos_Click(sender As Object, e As EventArgs) Handles btnVerTodos.Click
        Me.bsFiltro.Text = ""
        CargarDGV()
        GlobalVariables.varReporteConsulta = 0
    End Sub
    Private Sub btnImprimir_Click(sender As Object, e As EventArgs) Handles btnImprimir.Click
        GlobalVariables.VarOrigen = False
        If (Application.OpenForms("FrmRptInvoiceList") Is Nothing) Then
            FrmRptInvoiceList.lblEncabezado.Text = "Reporte: HISTORIAL INVOICES/REMISIONES"
            FrmRptInvoiceList.Text = "Reporte: HISTORIAL INVOICES/REMISIONES"
            FrmRptInvoiceList.ShowDialog()
        Else
            FrmRptInvoiceList.Close()
            FrmRptInvoiceList.lblEncabezado.Text = "Reporte: HISTORIAL INVOICES/REMISIONES"
            FrmRptInvoiceList.Text = "Reporte: HISTORIAL INVOICES/REMISIONES"
            FrmRptInvoiceList.ShowDialog()
        End If

    End Sub

End Class