Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Public Class PPReturnForm

    Private cnstr = mainForm.cnstr
    Private conn As New SqlConnection(cnstr)
    Private myQuery As String = String.Empty
    Private myCommand As SqlCommand
    Private adapter As SqlDataAdapter = New SqlDataAdapter()
    Private ds As New DataSet("Machines")
    Private i, limit As Integer
    Private oldMchCode As String = String.Empty
    Private addMode, editMode As Integer
    Private row As Integer

    Private Sub PPReceiveForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        txtStart.Focus()
    End Sub

    Private Sub machineForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        Control.CheckForIllegalCrossThreadCalls = False
        Me.Top = 0
        Me.Left = 0

        dtNow.Value = DateTime.Now
        txtOperator.Text = loginForm.username
        Timer1.Enabled = True
        txtStart.Focus()
        initializeDG_inventory()
    End Sub

    Private Sub ts_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub clear()
        txtPartNo.Text = String.Empty
        txtPartDesc.Text = String.Empty
        txtQuantity.Text = String.Empty
        txtLotNum.Text = String.Empty
        txtStart.Text = String.Empty
    End Sub

    Private Sub initializeDG_inventory()
        Dim partno As New DataGridViewTextBoxColumn
        Dim quantity As New DataGridViewTextBoxColumn
        Dim lotNumber As New DataGridViewTextBoxColumn
        Dim inputDate As New DataGridViewTextBoxColumn
        Dim user As New DataGridViewTextBoxColumn

        partno.HeaderText = "Part Number"
        quantity.HeaderText = "Quantity"
        lotNumber.HeaderText = "Lot Number"
        inputDate.HeaderText = "Input Date"
        user.HeaderText = "User"

        dg_inventory.Columns.Add(partno)
        dg_inventory.Columns.Add(quantity)
        dg_inventory.Columns.Add(lotNumber)
        dg_inventory.Columns.Add(inputDate)
        dg_inventory.Columns.Add(user)

        dg_inventory.Columns(0).ReadOnly = True
        dg_inventory.Columns(1).ReadOnly = True
        dg_inventory.Columns(2).ReadOnly = True
        dg_inventory.Columns(3).ReadOnly = True
        dg_inventory.Columns(4).ReadOnly = True

        dg_inventory.Columns(0).Width = 150
        dg_inventory.Columns(1).Width = 150
        dg_inventory.Columns(2).Width = 120
        dg_inventory.Columns(3).Width = 150
        dg_inventory.Columns(4).Width = 120

        dg_inventory.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        dg_inventory.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(1).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(2).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(3).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(4).SortMode = DataGridViewColumnSortMode.Programmatic
    End Sub

    Private Sub fillDataSet_inventory(ByVal partNumber As String)
        Dim conn As New SqlConnection(cnstr)

        myQuery = "SELECT * FROM kparts_pp WHERE KMTPARTNO = '" & partNumber & "' " & _
                  "AND KMTQTY < 0 " & _
                  "ORDER BY KMTDATE"

        myCommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            adapter.SelectCommand = myCommand
            ds.Clear()
            adapter.Fill(ds)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Private Sub fillDataGridView_inventory()
        Dim i As Integer = 0

        dg_inventory.Rows.Clear()
        For i = 0 To ds.Tables(0).Rows.Count - 1
            row = dg_inventory.Rows.Add()
            dg_inventory.Rows(row).Cells(0).Value = ds.Tables(0).Rows(row).Item("KMTPARTNO")
            dg_inventory.Rows(row).Cells(1).Value = ds.Tables(0).Rows(row).Item("KMTQTY") * -1
            dg_inventory.Rows(row).Cells(2).Value = ds.Tables(0).Rows(row).Item("KMTLOT")
            dg_inventory.Rows(row).Cells(3).Value = ds.Tables(0).Rows(row).Item("KMTDATE")
            dg_inventory.Rows(row).Cells(4).Value = ds.Tables(0).Rows(row).Item("KMTOPERATOR")
        Next
    End Sub

    Private Function isComplete() As Boolean
        If txtPartNo.Text <> String.Empty And txtPartDesc.Text <> String.Empty And txtQuantity.Text <> String.Empty And txtLotNum.Text <> String.Empty Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub txtMulQty_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        'txtMulQty.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        dtNow.Value = DateTime.Now
    End Sub

    Private Sub txtStart_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtStart.KeyUp
        If e.KeyCode = Keys.Enter Then
            Dim fChar As String = String.Empty
            Dim entry As String = txtStart.Text

            fChar = Mid(entry, 1, 1)
            If IsNumeric(entry) Then
                txtQuantity.Text = entry
                txtStart.Text = String.Empty
                txtStart.Focus()
            ElseIf mountingForm.IsSerialNumber(entry, False, "") Then
                txtPartNo.Text = entry
                txtPartDesc.Text = mountingForm.getDesc(entry)
                txtStart.Text = String.Empty
                txtStart.Focus()

                fillDataSet_inventory(txtPartNo.Text)
                fillDataGridView_inventory()
            Else
                txtLotNum.Text = entry
                txtStart.Text = String.Empty
                txtStart.Focus()
            End If

            If isComplete() Then
                Dim pp As New PartsPrep

                pp.partNo = txtPartNo.Text
                pp.quantity = CDbl(txtQuantity.Text) * -1
                pp.lotNumber = txtLotNum.Text
                pp.mDate = dtNow.Value
                pp.mOperator = txtOperator.Text

                pp.add()
                fillDataSet_inventory(txtPartNo.Text)
                fillDataGridView_inventory()
                clear()
                txtStart.Focus()
            End If
        End If
    End Sub

    Private Sub txtQuantity_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtQuantity.KeyUp
        If e.KeyCode = Keys.Enter Then
            If isComplete() Then
                Dim pp As New PartsPrep

                pp.partNo = txtPartNo.Text
                pp.quantity = CDbl(txtQuantity.Text) * -1
                pp.lotNumber = txtLotNum.Text
                pp.mDate = dtNow.Value
                pp.mOperator = txtOperator.Text

                pp.add()
                fillDataSet_inventory(txtPartNo.Text)
                fillDataGridView_inventory()
                clear()
                txtStart.Focus()
            End If
            txtStart.Focus()
        End If
    End Sub
End Class