Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Text
Imports System.Data.SqlClient

Public Class ReworkissuanceForm
    Dim conn As New SqlConnection
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader


    Public cnstr As String = loginForm.cnstr

    Private Sub btnIssue_Click(sender As System.Object, e As System.EventArgs) Handles btnIssue.Click

        If (txtStat.Text <> "WHS") Then
            MessageBox.Show("Parts not on stock!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        ElseIf (QtyNumericUpDown.Text = "0") Then
            MessageBox.Show("Please Input Quantity!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            QtyNumericUpDown.Focus()
            QtyNumericUpDown.Select()
        Else

            Dim button2 As DialogResult
            button2 = MessageBox.Show _
            ("Are you want to issue the parts?", _
            "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            If button2 = Windows.Forms.DialogResult.Yes Then

                Dim Total As Integer

                Total = Integer.Parse(txtQuantity.Text) - Integer.Parse(QtyNumericUpDown.Text)

                conn = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba_kanban;user=cats;password=dogs;Integrated Security=false")


                Dim myQuery As String
                Dim insertQuery As String
                myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Qty = '" & Total & "' WHERE SerialNum in ( SELECT TOP 1 SerialNum FROM [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & txtSerial.Text & "' and Status = 'WHS' ORDER BY PrintedDate DESC)"
                '"UPDATE PartsRecords Set Qty = '" & Total & "' FROM (Select TOP 1 SerialNum, Status from PartsRecords ORDER BY PrintedDate desc) AS a WHERE PartsRecords.SerialNum = a.SerialNum and a.Status = 'WHS' "
                '"UPDATE PartsRecords SET Qty = '" & Total & "' WHERE SerialNum = '" & txtSerial.Text.Trim.ToUpper & "'"
               
                Dim mycommand As SqlCommand
                Dim insertCommand As SqlCommand
                mycommand = New SqlCommand(myQuery, conn)

                'start added 
                insertQuery = "INSERT INTO [Toshiba_kanban].[dbo].[ReworkIssuance] VALUES ( '' ,'" & txtSerial.Text & "', '" & QtyNumericUpDown.Text & "', '" & txtPN.Text & "', '" & txtDesc.Text & "', '" & txtLot.Text & "', '" & txtQuantity.Text & "', '" & txtStat.Text & "', '" & loginForm.username & "', '" & txtFamily.Text & "', '" & txtMC.Text & "', '" & SupplierTextBox.Text & "',GETDATE(), '" & loginForm.username & "')"
                insertCommand = New SqlCommand(insertQuery, conn)
                'MessageBox.Show("pasok sa logs ReworkIssuance" + myQuery)


                'end

                Try
                    conn.Open()
                    mycommand.ExecuteNonQuery()
                    insertCommand.ExecuteNonQuery()
                    MessageBox.Show("Successfully Issued.", "Rework Issuance", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    conn.Close()
                    conn.Dispose()
                End Try

                ' MsgBox(Total.ToString)


                reset()
            End If
        End If

    End Sub


    Private Sub ReworkissuanceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        txtSerial.Focus()
    End Sub

    Private Sub txtSerial_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSerial.KeyDown

        If e.KeyCode = Keys.Enter Then
            conn = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba_kanban;user=cats;password=dogs;Integrated Security=false")
            conn.Open()
            Try
                cmd = New SqlCommand("Select * from [Toshiba_kanban].[dbo].[PartsRecords] where SerialNum = '" & txtSerial.Text & "'", conn)
                reader = cmd.ExecuteReader
                If reader.HasRows Then
                    reader.Read()
                    txtPN.Text = reader("IonicsPN")
                    txtDesc.Text = reader("Description")
                    txtLot.Text = reader("LotNO")
                    txtQuantity.Text = reader("Qty")
                    txtStat.Text = reader("Status")
                    txtOperator.Text = reader("PrintedBy")
                    txtFamily.Text = reader("Family")
                    txtMC.Text = reader("MakerCode")
                    SupplierTextBox.Text = reader("SupplierPN")
                    btnIssue.Enabled = True
                    QtyNumericUpDown.Focus()

                End If


                conn.Close()
            Catch ex As Exception

            End Try
        End If

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
        txtSerial.Enabled = True
        txtSerial.Focus()
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        reset()
    End Sub
    Private Sub reset()

        txtPN.Text = ""
        QtyNumericUpDown.Value = "0"
        txtDesc.Text = ""
        txtLot.Text = ""
        txtQuantity.Text = ""
        txtStat.Text = ""
        txtOperator.Text = ""
        txtFamily.Text = ""
        txtMC.Text = ""
        SupplierTextBox.Text = ""
        txtSerial.Enabled = True
        btnIssue.Enabled = False
        txtSerial.Text = ""
        txtSerial.Focus()
    End Sub


End Class