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

Public Class issuanceForm
    Private isUpdate = False
    Dim family As String
    Dim pass As Boolean = False
    Dim proceed As Boolean = False
    Dim tuloy As Boolean = False
    Dim conn As SqlConnection
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader
    Dim conn2 As SqlConnection
    Dim cmd2 As SqlCommand
    Dim reader2 As SqlDataReader


    Private Sub txtSerial_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSerial.KeyPress
        Dim proceed As Boolean = False
        If Asc(e.KeyChar) = 13 Then
            If txtSerial.Text.Length >= 10 Then
                If Not txtSerial.Text.StartsWith("LA") Then
                    txtSerial.Text = txtSerial.Text.Substring(0, 10)
                End If

                Try
                    conn = New SqlConnection("Initial Catalog=Toshiba_kanban; Data Source=192.168.1.41,1433; Persist Security Info = False;User ID=cats;Password=dogs;")
                    conn.Open()
                    cmd = New SqlCommand("Select * from [Toshiba_kanban].[dbo].[PartsRecords] where SerialNum = '" & txtSerial.Text & "'", conn)
                    reader = cmd.ExecuteReader()
                    If reader.HasRows Then
                        tuloy = True

                    Else
                        MsgBox("DID is not yet registered!")
                        reset()
                    End If

                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error1", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    conn.Close()
                End Try

                If tuloy = True Then

                    Dim parts = New PartsRecords(txtSerial.Text)
                    If parts.Exist Then
                        txtPN.Text = parts.IonicsPN
                        txtDesc.Text = parts.Description
                        txtLot.Text = parts.LotNo
                        txtBox.Text = parts.Rack
                        txtQuantity.Text = parts.Quantity
                        SupplierTextBox.Text = parts.SupplierPN
                        lblStatus.Text = parts.Status
                        lblMFGDate.Text = parts.MFGDate
                        lblRCVDate.Text = parts.RCVDate
                        lblPDate.Text = parts.PrintedDate
                        lblBy.Text = parts.PrintedBy
                        txtFamily.Text = parts.Family
                        txtMC.Text = parts.MakerCode
                        lblthaw.Text = parts.Thaw
                        cbopetname.Text = parts.modelCode
                        txtOperator.Text = parts.PrintedBy




                        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                        If txtPN.Text = "M705PCIS4000" And lblStatus.Text = "THAW" Or txtPN.Text = "GY4CS000P110" And lblStatus.Text = "THAW" Or parts.Status = "Thaw" Then

                            Dim cnstr = mainForm.cnstr
                            Dim conn As New SqlConnection(cnstr)
                            Dim myQuery As String
                            myQuery = "SELECT * FROM [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & txtSerial.Text & "' and Status = 'THAW'"

                            Dim mycommand As SqlCommand
                            mycommand = New SqlCommand(myQuery, conn)
                            Dim spdate As Date

                            Try
                                conn.Open()
                                Dim reader As SqlDataReader = mycommand.ExecuteReader()
                                reader.Read()
                                Dim NextTime As Date
                                NextTime = NextTime.AddHours(2)
                                spdid.Text = reader("SerialNum")
                                lbldo.Text = reader("ThawDate")
                                conn.Close()
                                spdate = lbldo.Text
                                lbled.Text = spdate.AddHours(49)
                                'MessageBox.Show(lbled.Text)

                                If lbled.Text <= Date.Now Then
                                    proceed = True
                                    NotificationForm.text = "SOLDERPASTE NEED TO BE RETURNED TO REFRIGERATOR"
                                    NotificationForm.ShowDialog()
                                    btnReset.Enabled = True

                                Else
                                    proceed = False
                                End If
                            Catch ex As Exception
                                'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            End Try
                            conn.Close()
                            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


                        ElseIf txtPN.Text = "M705PCIS4000" And lblStatus.Text = "INV-PDN" Or txtPN.Text = "GY4CS000P110" And lblStatus.Text = "PDN" Or parts.Status = "PDN" Then

                            Dim cnstr = mainForm.cnstr
                            Dim conn As New SqlConnection(cnstr)
                            Dim myQuery As String
                            myQuery = "SELECT * FROM [Toshiba_kanban].[dbo].[PartsLog] WHERE MaterialID = '" & txtSerial.Text & "' and Status = 'Issued' order by lastupdated desc"

                            Dim mycommand As SqlCommand
                            mycommand = New SqlCommand(myQuery, conn)
                            Dim spdate As Date

                            Try
                                conn.Open()
                                Dim reader As SqlDataReader = mycommand.ExecuteReader()
                                reader.Read()
                                Dim NextTime As Date
                                NextTime = NextTime.AddDays(3)
                                spdid.Text = reader("MaterialID")
                                splotno.Text = reader("LotNo")
                                lbldo.Text = reader("lastupdated")
                                txtOperator.Text = reader("lastupdatedby")
                                cmbLine.Text = reader("line")
                                lblFam.Text = reader("family")



                                conn.Close()
                                spdate = lbldo.Text
                                lbled.Text = spdate.AddDays(3)

                                If lbled.Text <= Date.Now Then
                                    proceed = True
                                    NotificationForm.text = "72 HOURS EXPIRED SOLDER PASTE: FOR SCRAP"
                                    NotificationForm.ShowDialog()
                                    btnReset.Enabled = True

                                Else
                                    proceed = False
                                End If
                            Catch ex As Exception
                                'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            End Try
                            conn.Close()


                        ElseIf txtPN.Text = "M705PCIS4000" Or txtPN.Text = "GY4CS000P110" AndAlso (lblStatus.Text = "INV-SMT" Or lblStatus.Text = "SMT" Or parts.Status = "SMT") Then



                            Dim cnstr = mainForm.cnstr
                            Dim conn As New SqlConnection(cnstr)
                            Dim myQuery As String
                            myQuery = "SELECT * FROM [Toshiba_kanban].[dbo].[PartsLog] WHERE MaterialID = '" & txtSerial.Text & "' and Status = 'Mounted' order by lastupdated desc"

                            Dim mycommand As SqlCommand
                            mycommand = New SqlCommand(myQuery, conn)
                            Dim spdate As Date

                            Try
                                conn.Open()
                                Dim reader As SqlDataReader = mycommand.ExecuteReader()
                                reader.Read()
                                Dim NextTime As Date
                                NextTime = NextTime.AddDays(2)
                                spdid.Text = reader("MaterialID")
                                splotno.Text = reader("LotNo")
                                lbldo.Text = reader("lastupdated")
                                txtOperator.Text = reader("lastupdatedby")
                                cmbLine.Text = reader("line")
                                Label15.Text = reader("family")
                                conn.Close()
                                spdate = lbldo.Text
                                lbled.Text = spdate.AddDays(2)

                                If lbled.Text <= Date.Now Then
                                    proceed = True
                                    NotificationForm.text = "48 HOURS EXPIRED SOLDER PASTE : FOR SCRAP"
                                    NotificationForm.ShowDialog()
                                    btnReset.Enabled = True

                                Else
                                    proceed = False
                                End If
                            Catch ex As Exception
                                'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            End Try
                            conn.Close()
                        Else
                            proceed = False

                        End If


                        If (proceed = False) Then
                            Dim remarks() = parts.Remarks.Split("-")
                            cmbStatus.Text = parts.Status
                            enable(parts)

                            If remarks.Length = 3 Then
                                cmbLine.Text = remarks(0)
                                cmbRev.Text = remarks(1)
                                'cbopetname.Text = remarks(3)
                                txtOperator.Text = remarks(2)

                            End If

                            If parts.Status <> "WHS" And parts.Status <> "SMT" And parts.Status <> "INV" And parts.Status <> "INV-PDN" And parts.Status <> "INV-SMT" And parts.Status <> "THAW" Then
                                btnIssue.Enabled = False
                                If loginForm.userType <> "Administrator" And loginForm.userType <> "Super Administrator" Then
                                    btnUpdate.Enabled = False
                                End If
                            ElseIf parts.Status = "RTN" Or parts.Status = "SCR" Or parts.Status = "PDN" Then
                                btnIssue.Enabled = False
                            End If


                            If parts.Remarks <> "" Then
                                txtRemarks.Text = parts.Remarks
                            Else
                                txtRemarks.Text = "N/A"
                            End If

                        Else
                            'MessageBox.Show("Item not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End If
                    End If
                End If
            Else
                MsgBox("Wrong DID format!")
                reset()
            End If

        End If
    End Sub

    Public Sub enable(ByVal parts As PartsRecords)
        btnIssue.Enabled = True
        btnReset.Enabled = True
        btnUpdate.Enabled = True
        cmbLine.Enabled = True
        ' cmbRev.Enabled = True
        txtOperator.ReadOnly = False

    End Sub

    Public Sub reset()
        txtSerial.Text = Nothing
        txtPN.Text = Nothing
        txtDesc.Text = Nothing
        txtLot.Text = Nothing
        txtBox.Text = Nothing
        txtQuantity.Text = Nothing
        SupplierTextBox.Text = Nothing
        txtRemarks.ReadOnly = False
        txtRemarks.Text = Nothing
        txtRemarks.Visible = False
        lblRemarks.Visible = False
        lblLine.Visible = True
        cmbLine.Visible = True
        cmbLine.Text = Nothing
        cmbLine.Enabled = False
        lblRev.Visible = True
        cmbRev.Visible = True
        cmbRev.Text = Nothing
        'cmbRev.Enabled = False
        cbopetname.Enabled = True
        cbopetname.Text = Nothing
        lblOperator.Visible = True
        txtOperator.Visible = True
        txtOperator.Text = Nothing
        txtOperator.ReadOnly = True
        cmbStatus.SelectedText = Nothing
        cmbStatus.Text = Nothing
        cmbStatus.Enabled = False
        lblStatus.Text = Nothing
        lblMFGDate.Text = Nothing
        lblRCVDate.Text = Nothing
        lblPDate.Text = Nothing
        lblBy.Text = Nothing
        lblupdateddate.Text = Nothing
        lblupdatedby.Text = Nothing
        btnIssue.Enabled = False
        btnReset.Enabled = False
        isUpdate = False
        btnUpdate.Text = "Update"
        btnUpdate.Enabled = False
        txtFamily.Text = Nothing
        lblFam.Text = Nothing
        txtMC.Text = Nothing
        dgPN.Rows.Clear()
        spdid.Text = Nothing
        splotno.Text = Nothing
        lbldo.Text = Nothing
        lbled.Text = Nothing
        lblthaw.Text = Nothing
        txtSerial.Focus()
    End Sub

    Public Sub updatemode()
        txtRemarks.ReadOnly = False
        cmbStatus.Enabled = True
        btnUpdate.Text = "Save"
        btnIssue.Enabled = False
        isUpdate = True
    End Sub

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

        Dim p = New PartsRecords()
        Dim items = p.getAllModel()

        For Each i As String In items
            cmbRev.Items.Add(i)
        Next

        btnUpdate.Visible = True
        txtSerial.Focus()



        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        myQuery = "SELECT * FROM [Toshiba2].[dbo].[kanban_line]"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        cmbLine.Items.Clear()
        Try
            conn.Open()

            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            While reader.Read
                cmbLine.Items.Add(reader("line"))
            End While
            reader.Close()
            conn.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error2", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

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

    Private Function checkMC(ByVal mc As System.Object)
        Dim go As Boolean = False

        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        myQuery = "SELECT * From [Toshiba_kanban].[dbo].[makerCode] where Ionics_PN = '" & txtPN.Text & "' and MakerCode like '%" & txtMC.Text & "%' "

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            If reader.HasRows Then
                reader.Read()
                go = True

            Else
                go = False

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error3", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        conn.Close()

        Return go

    End Function

    Private Function checkMC2(ByVal mc As System.Object)
        Dim go As Boolean = False

        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        myQuery = "SELECT * From [Toshiba_kanban].[dbo].[partsprep] where Ionics_PN = '" & txtPN.Text & "' and MakerCode like '%" & txtMC.Text & "%' "

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            If reader.HasRows Then
                reader.Read()
                go = True

            Else
                go = False

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error4", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        conn.Close()

        Return go

    End Function

    Private Sub btnIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIssue.Click
        If txtSerial.Text = "" Then
            MsgBox("please input DID")
            reset()
            Exit Sub
        End If
        Dim Mounting As New Mounting
        Dim i As Integer = 0
        Dim found As Boolean = False
        Dim sunod As Boolean = False
        Dim pwbfound As Boolean = False

        If (txtDesc.Text.Trim() = "PWB") Then



            'Dim verifyPWB As String
            'verifyPWB = Microsoft.VisualBasic.InputBox("Enter Serial Number:", "Entry Form")

            'Dim SerialChar() As Char = verifyPWB.ToCharArray
            'Dim SerialNo() As String
            'Dim modelno() As String
            'Dim SerialLine As String
            'Dim ModelTemp As String

            'If (SerialChar(2) = "A") Then
            '    SerialLine = "10"
            'ElseIf (SerialChar(2) = "B") Then
            '    SerialLine = "11"
            'Else
            '    SerialLine = SerialChar(2)
            'End If

            'ModelTemp = Mounting.GetModelCode(Mounting.GetLineSeq(SerialLine))
            'modelno = ModelTemp.Split(" ")
            'SerialNo = verifyPWB.Split(" ")

            'If SerialChar(2) = cmbLine.Text.Trim And Mounting.GetModelNo(modelno(0), modelno(1)) = SerialNo(1) Then
            pwbfound = True

            '        Else
            '            pwbfound = False

            '            MessageBox.Show("Please check the Line and Assycode!", _
            '"Important Note", _
            'MessageBoxButtons.OK, _
            'MessageBoxIcon.Error, _
            'MessageBoxDefaultButton.Button1)
            '        End If
        Else
            pwbfound = True

        End If


        If (pwbfound = True) Then


            If (txtPN.Text.Trim = "M705PCIS4000" Or txtPN.Text.Trim = "GY4CS000P110") And lblStatus.Text.Trim.ToUpper <> "THAW" Then
                MsgBox("Please thaw the solder paste first!")
                reset()

            ElseIf (txtPN.Text.Trim = "M705PCIS4000" Or txtPN.Text.Trim = "GY4CS000P110") And lblStatus.Text.Trim.ToUpper = "THAW" Then

                Dim NextTime As Date = lblthaw.Text
                NextTime = NextTime.AddHours(2)

                If NextTime > Date.Now Then
                    MsgBox("Thawing time is not yet complete!")
                    reset()
                Else
                    sunod = True
                End If
                'Else
                '    MsgBox("Please thaw the solder paste first!")
            Else
                sunod = True
            End If


            If sunod = True Then

                While i < dgPN.Rows.Count - 1
                    If cmbLine.Text.ToString() = "7" Or cmbLine.Text.ToString() = "9" Or cmbLine.Text.ToString() = "10" Or cmbLine.Text.ToString() = "11" Or cmbLine.Text.ToString() = "12" Or cmbLine.Text.ToString() = "13" Then
                        found = True

                    ElseIf (dgPN.Rows(i).Cells(0).Value = txtPN.Text) Then

                        found = True
                        Exit While
                    Else

                        found = False

                    End If


                    i = i + 1
                End While


                If found = True Then

                    Dim cnstr = mainForm.cnstr
                    Dim conn As New SqlConnection(cnstr)
                    Dim myQuery As String
                    myQuery = "SELECT * FROM [Toshiba_cats].[dbo].[model] WHERE minRev like '%" & cmbRev.Text.Trim & "%' AND modelDesc1 like '%" & cbopetname.Text.Trim & "%'"

                    Dim mycommand As SqlCommand
                    mycommand = New SqlCommand(myQuery, conn)

                    Try
                        conn.Open()
                        Dim reader As SqlDataReader = mycommand.ExecuteReader()
                        reader.Read()
                        lblFam.Text = reader("modelFamily")
                        conn.Close()
                        'MessageBox.Show("txtFamily " + txtFamily.Text, "Error!")
                        'MessageBox.Show("lblFam " + lblFam.Text, "Error!")

                        If txtFamily.Text = lblFam.Text Then
                            pass = True
                        Else
                            MessageBox.Show("DID is not for issuance: " + lblFam.Text, "Error!")
                            pass = False
                        End If
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "Error5", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try

                    Dim parts = New PartsRecords(txtSerial.Text)
                    Dim success As Boolean = False
                    Dim success2 As Boolean = False
                    Dim go As Boolean = False
                    Dim val As String
                    Dim maker As String
                    Dim mcode = New makerCode

                    If pass = True Then
                        'MessageBox.Show(txtPN.Text.ToString())
                        'MessageBox.Show(Environment.NewLine)

                        'Dim arrayseq() = New Mounting().getSequencePN(cmbLine.SelectedItem.ToString)
                        'If txtPN.Text.ToString() <> String.Join(Environment.NewLine, arrayseq) Then
                        '    NotificationForm.text = "PARTS IS NOT INCLUDED TO THIS LINE!"
                        '    NotificationForm.ShowDialog()
                        '    'btnIssue.Enabled = False
                        'Else
                        verifypn.Show()
                        verifypn.txtpn.Text = Trim(parts.ToshibaPN)

                        verifymc.txtmc.Text = Trim(parts.MakerCode)
                        parts.Remarks = cmbLine.SelectedItem.ToString & "-" & cmbRev.Text.ToString & "-" & cbopetname.Text.ToString & "-" & txtOperator.Text

                        'If (parts.checkThreshold(txtPN.Text, cmbLine.SelectedItem.ToString)) Then
                        Do

                            Dim Partno() As Char = txtPN.Text

                            val = InputBox("Enter part number : ", "Message", "")
                            If (val.Contains("GDM")) Then
                                val = (Trim(val).Substring(Trim(val).IndexOf("GDM"), 12))
                                If (Trim(val).Substring(Trim(val).IndexOf("GDM"), 12) = Trim(parts.ToshibaPN)) Then
                                    '  If checkMC(parts.ToshibaPN) = True Then

                                    If Partno(5) = "4" And Partno(6) = "6" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "4" And Partno(6) = "7" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "1" And Partno(6) = "0" And txtFamily.Text = "Enterprise" Then
                                        success2 = True

                                    Else
                                        success = True
                                    End If
                                Else
                                    NotificationForm.text = "Part Number Mismatch"
                                    NotificationForm.ShowDialog()
                                End If
                            ElseIf (val.Contains("G5B")) Then
                                val = (Trim(val).Substring(Trim(val).IndexOf("G5B"), 12))
                                If (Trim(val).Substring(Trim(val).IndexOf("G5B"), 12) = Trim(parts.ToshibaPN)) Then

                                    '  If checkMC(parts.ToshibaPN) = True Then
                                    If Partno(5) = "4" And Partno(6) = "6" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "4" And Partno(6) = "7" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "1" And Partno(6) = "0" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "9" And Partno(6) = "0" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    Else
                                        success = True
                                    End If

                                Else
                                    NotificationForm.text = "Part Number Mismatch"
                                    NotificationForm.ShowDialog()
                                End If
                            ElseIf (val.Contains("G5D")) Then
                                val = (Trim(val).Substring(Trim(val).IndexOf("G5D"), 12))
                                If (Trim(val).Substring(Trim(val).IndexOf("G5D"), 12) = Trim(parts.ToshibaPN)) Then

                                    ' If checkMC(parts.ToshibaPN) = True Then
                                    If Partno(5) = "4" And Partno(6) = "6" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "4" And Partno(6) = "7" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "1" And Partno(6) = "0" And txtFamily.Text = "Enterprise" Then
                                        success2 = True

                                    Else
                                        success = True
                                    End If

                                Else
                                    NotificationForm.text = "Part Number Mismatch"
                                    NotificationForm.ShowDialog()
                                End If




                            Else
                                val = Trim(val).Substring(0, 12)
                                If (Trim(val) = Trim(parts.ToshibaPN)) Then


                                    If Partno(5) = "4" And Partno(6) = "6" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "4" And Partno(6) = "7" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    ElseIf Partno(5) = "1" And Partno(6) = "0" And txtFamily.Text = "Enterprise" Then
                                        success2 = True
                                    Else
                                        success = True
                                    End If

                                Else
                                    NotificationForm.text = "Part Number Mismatch"
                                    NotificationForm.ShowDialog()
                                End If
                            End If
                        Loop While (val <> Trim(parts.ToshibaPN)) And (val <> "")

                        If success2 Then

                            ' Do
                            maker = InputBox("Enter maker code : ", "Message", "")

                            Dim trymaker As String
                            trymaker = mcode.getmakercode(txtPN.Text, maker).ToString
                            'MsgBox(parts.MakerCode.ToUpper)
                            If (trymaker.ToUpper = parts.MakerCode.ToUpper) Then

                                '(parts.MakerCode.ToUpper)
                                success = True

                            Else
                                NotificationForm.text = "Maker Code Mismatch"
                                NotificationForm.ShowDialog()
                                reset()
                                txtSerial.Focus()

                            End If

                            ' Loop While (maker <> Trim(parts.MakerCode)) And (maker <> "")

                        End If

                        If success Then

                            verifypn.Close()
                            If cmbLine.Text.ToString() = "7" Or cmbLine.Text.ToString() = "9" Or cmbLine.Text.ToString() = "10" Or cmbLine.Text.ToString() = "11" Or cmbLine.Text.ToString() = "12" Or cmbLine.Text.ToString() = "13" Then

                                Dim Partno() As Char = txtPN.Text
                                If Partno(0) = "M" Or Partno(0) = "G" Then

                                    Dim Description1 As String = txtDesc.Text
                                    Description1 = Description1.Trim()

                                    'Dim csvFilePath1 As String = "\\192.168.1.49\Monitor\" & txtSerial.Text & ".csv"
                                    'Dim outFile1 As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFilePath1, False, Encoding.ASCII)
                                    'outFile1.WriteLine(txtSerial.Text + "," + txtPN.Text + "," + SupplierTextBox.Text + "," + txtLot.Text + "," + Description1 + "," + txtMC.Text + "," + " " + "," + " " + "," + " " + "," + txtQuantity.Text)
                                    'outFile1.Close()

                                    Dim csvFilePath3 As String = "\\192.168.1.81\Shared\MaterialID\" & txtSerial.Text & ".csv"
                                    Dim outFile3 As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFilePath3, False, Encoding.ASCII)
                                    outFile3.WriteLine(txtSerial.Text + "," + txtPN.Text + "," + SupplierTextBox.Text + "," + txtLot.Text + "," + Description1 + "," + txtMC.Text + "," + " " + "," + " " + "," + " " + "," + txtQuantity.Text)
                                    outFile3.Close()

                                    Dim Result As String
                                    'parts.checkFifo(txtSerial.Text, txtPN.Text)
                                    Result = parts.checkFifo(txtSerial.Text, txtPN.Text)
                                    If Result > 0 Then
                                        proceed = True
                                        If MessageBox.Show("There are older " & Result & " items that you need issuE21C103416e!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) = Windows.Forms.DialogResult.Yes Then
                                            reset()
                                            'fifoverify.Show()
                                        End If
                                    Else
                                        parts.issue2(txtSerial.Text, txtPN.Text, DateTime.Parse(lblPDate.Text), loginForm.username, cmbLine.SelectedItem.ToString, cmbRev.Text.ToString, cbopetname.Text.ToString, txtOperator.Text)
                                        MessageBox.Show("Parts Successfully Issued.", "Success")
                                        reset()
                                        txtSerial.Focus()
                                    End If

                                Else

                                    Dim finalPN As String = Partno(2) + Partno(3) + Partno(4) + Partno(5) + Partno(6) + Partno(7) + Partno(8) + Partno(9) + Partno(10) + Partno(11) + Partno(12) + Partno(13)
                                    Dim Description As String = txtDesc.Text
                                    Description = Description.Trim()

                                    Dim csvFilePath As String = "\\192.168.1.49\Monitor\" & txtSerial.Text & ".csv"
                                    Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFilePath, False, Encoding.ASCII)
                                    outFile.WriteLine(txtSerial.Text + "," + finalPN + "," + SupplierTextBox.Text + "," + txtLot.Text + "," + Description + "," + txtMC.Text + "," + " " + "," + " " + "," + " " + "," + txtQuantity.Text)
                                    outFile.Close()

                                    Dim csvFilePath2 As String = "\\192.168.1.81\Shared\MaterialID\" & txtSerial.Text & ".csv"
                                    Dim outFile2 As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFilePath2, False, Encoding.ASCII)
                                    outFile2.WriteLine(txtSerial.Text + "," + finalPN + "," + SupplierTextBox.Text + "," + txtLot.Text + "," + Description + "," + txtMC.Text + "," + " " + "," + " " + "," + " " + "," + txtQuantity.Text)
                                    outFile2.Close()

                                    Dim Result As String
                                    'parts.checkFifo(txtSerial.Text, txtPN.Text)
                                    Result = parts.checkFifo(txtSerial.Text, txtPN.Text)
                                    If Result > 0 Then
                                        proceed = True
                                        If MessageBox.Show("There are older " & Result & " items that you need issue!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) = Windows.Forms.DialogResult.Yes Then
                                            reset()
                                            'fifoverify.Show()
                                            'fifoverify.Show()
                                        End If
                                    Else
                                        parts.issue2(txtSerial.Text, txtPN.Text, DateTime.Parse(lblPDate.Text), loginForm.username, cmbLine.SelectedItem.ToString, cmbRev.Text.ToString, cbopetname.Text.ToString, txtOperator.Text)
                                        MessageBox.Show("Parts Successfully Issued.", "Success")
                                        reset()
                                        txtSerial.Focus()
                                    End If

                                End If

                            Else
                                If Not parts.issue(txtSerial.Text, txtPN.Text, DateTime.Parse(lblPDate.Text), loginForm.username, cmbLine.SelectedItem.ToString, cmbRev.Text.ToString, txtOperator.Text) Then
                                    Dim Result As String
                                    'parts.checkFifo(txtSerial.Text, txtPN.Text)
                                    Result = parts.checkFifo(txtSerial.Text, txtPN.Text)
                                    If Result > 0 Then
                                        proceed = True
                                        If MessageBox.Show("There are older " & Result & " items that you need issue!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) = Windows.Forms.DialogResult.Yes Then
                                            reset()
                                        End If
                                    Else
                                        parts.issue2(txtSerial.Text, txtPN.Text, DateTime.Parse(lblPDate.Text), loginForm.username, cmbLine.SelectedItem.ToString, cmbRev.Text.ToString, cbopetname.Text.ToString, txtOperator.Text)
                                        MessageBox.Show("Parts Successfully Issued.", "Success")
                                        reset()
                                        txtSerial.Focus()

                                    End If

                                    'If proceed = False Then
                                    '    parts.issue2(txtSerial.Text, txtPN.Text, DateTime.Parse(lblPDate.Text), loginForm.username, cmbLine.SelectedItem.ToString, cmbRev.Text.ToString, cbopetname.Text.ToString, txtOperator.Text)
                                    '    MessageBox.Show("Parts Successfully Issued.", "Success")
                                    '    reset()
                                    '    txtSerial.Focus()
                                    'End If
                                End If
                            End If
                        End If
                        'Else
                        '    MessageBox.Show("This part number is exceeding the allowed threshold per day or not exist on threshold maintenance. Please try again.")
                        '    reset()
                        '    txtSerial.Focus()
                        'End If
                        '  End If

                        pass = False
                        'End If
                    End If
                End If
            End If
        End If
    End Sub


    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        Dim parts As New PartsRecords(txtSerial.Text)
        If parts.Status = "INV" Or parts.Status = "INV-PDN" Or parts.Status = "INV-SMT" Then
            If parts.updateStatus(txtSerial.Text, "WHS", cmbLine.SelectedItem.ToString & "-" & cmbRev.Text.ToString & "-" & txtOperator.Text) Then
                MessageBox.Show("Successfully Saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

            reset()
        Else
            If isUpdate Then
                parts = New PartsRecords
                If cmbStatus.SelectedItem = "LST" And (txtRemarks.Text = "N/A" Or txtRemarks.Text = "") Then
                    MessageBox.Show("Please provide proper remarks.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

                Else
                    If parts.updateStatus(txtSerial.Text, cmbStatus.SelectedItem, cmbLine.SelectedItem.ToString & "-" & cmbRev.Text.ToString & "-" & txtOperator.Text) Then
                        MessageBox.Show("Successfully Saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    End If
                    reset()

                End If
            Else
                updatemode()
            End If

        End If
    End Sub

    Private Sub cmbRev_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbRev.SelectedIndexChanged

        'Try

        '    conn2 = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba2;Integrated Security=True")
        '    conn2.Open()

        '    cmd2 = New SqlCommand("SELECT * From model where minRev = '" & cmbRev.Text & "'", conn2)
        '    reader2 = cmd.ExecuteReader()
        '    If reader2.HasRows Then
        '        reader2.Read()
        '        lblFam.Text = reader("modelFamily")
        '    Else
        '        MsgBox("Model not yet enrolled!")
        '    End If

        'Catch ex As Exception

        'End Try
        'conn2.Close()

        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        'myQuery = "SELECT * FROM model WHERE minRev like '%" & cmbRev.Text.Trim & "%' AND modelDesc1 = '%" & cbopetname.Text.Trim & "%'"
        myQuery = "SELECT * FROM [Toshiba_cats].[dbo].[model] WHERE modelDesc1 like '%" & cbopetname.Text.Trim & "%' and minRev like '%" & cmbRev.Text.Trim & "%'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            reader.Read()
            lblFam.Text = reader("modelFamily")
            conn.Close()





        Catch ex As Exception
            'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


        txtOperator.Focus()

    End Sub

    Private Sub cmbLine_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbLine.SelectedIndexChanged
        If cmbLine.Text <> Nothing Then
            dgPN.Rows.Clear()
            cmbRev.Text = New Mounting().getSettings(cmbLine.SelectedItem.ToString)
            cbopetname.Text = New Mounting().getSettings2(cmbLine.SelectedItem.ToString)

            getSeq.Text = New Mounting().getKseq(cmbLine.SelectedItem.ToString)
            Dim arrayseq() = New Mounting().getSequencePN(cmbLine.SelectedItem.ToString)
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'MessageBox.Show("here")
            ''cj
            'MessageBox.Show(String.Join(Environment.NewLine, arrayseq))


            Try
                conn2 = New SqlConnection("Initial Catalog=Toshiba_kanban; Data Source=192.168.1.41,1433; Persist Security Info = False;User ID=cats;Password=dogs;")
                conn2.Open()
                cmd2 = New SqlCommand("SELECT * FROM [Toshiba_cats].[dbo].[Issuance_Restrictions] WHERE Petname='" & cbopetname.Text & "'AND rev='" & cmbRev.Text & "' AND Partnumber='" & txtPN.Text & "'AND Maker ='" & SupplierTextBox.Text & "'AND Makercode='" & txtMC.Text & "'AND lotnumber='" & txtLot.Text & "'", conn2)
                reader2 = cmd2.ExecuteReader()
                If reader2.HasRows Then

                    NotificationForm.text = "PARTS IS RESTRICTED!"
                    NotificationForm.ShowDialog()
                    reset()
                    '    'Dim arrayseq() = New Mounting().getSequencePN(cmbLine.SelectedItem.ToString)
                    'ElseIf txtPN.Text.ToString() <> String.Join(Environment.NewLine, arrayseq) Then
                    '    NotificationForm.text = "PARTS IS NOT INCLUDED TO THIS LINE!"
                    '    NotificationForm.ShowDialog()
                    '    btnIssue.Enabled = False

                Else

                    Try

                        For x = 0 To arrayseq.Length - 1
                            If (arrayseq(x) <> "") Then
                                dgPN.Rows.Add(arrayseq(x))
                            End If
                        Next
                    Catch ex As Exception

                    End Try
                End If

            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error1", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                conn2.Close()
            End Try







            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        End If

        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        'myQuery = "SELECT * FROM model WHERE minRev like '%" & cmbRev.Text.Trim & "%' AND modelDesc1='%" & cbopetname.Text.Trim & "%'"
        myQuery = "SELECT * FROM [Toshiba_cats].[dbo].[model] WHERE modelDesc1 like '%" & cbopetname.Text.Trim & "%' and minRev like '%" & cmbRev.Text.Trim & "%'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            reader.Read()
            lblFam.Text = reader("modelFamily")

            reader.Close()
            conn.Close()

        Catch ex As Exception
            'MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        If (txtPN.Text = "M705PCIS4000" Or txtPN.Text = "GY4CS000P110") Then
            txtFamily.Text = lblFam.Text
        End If
        'MessageBox.Show("here 2")

        updateDG()
    End Sub

    Private Sub getExpirationDate()

        Dim cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM [Toshiba_kanban].[dbo].[PartsLog] WHERE (IonicsPN = 'M705PCIS4000' or IonicsPN = 'GY4CS000P110') AND  line = '" & cmbLine.Text & "' AND Status  = 'Issued' ORDER BY lastupdated DESC"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)
        Dim spdate As Date

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            reader.Read()
            Dim NextTime As Date
            NextTime = NextTime.AddDays(4)
            spdid.Text = reader("MaterialID")
            splotno.Text = reader("LotNo")
            lbldo.Text = reader("lastupdated")
            conn.Close()

            spdate = lbldo.Text
            lbled.Text = spdate.AddDays(3)

            If (txtPN.Text = "M705PCIS4000" Or txtPN.Text = "GY4CS000P110") And lbled.Text <= Date.Now Then
                NotificationForm.text = "EXPIRED SOLDER PASTE"
                NotificationForm.ShowDialog()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error6", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        conn.Close()


    End Sub

    Private Sub updateDG()

        Dim i As Integer = 0

        While i < dgPN.Rows.Count - 1

            If (dgPN.Rows(i).Cells(0).Value <> "") Then

                If (dgPN.Rows(i).Cells(0).Value = txtPN.Text) Then
                    dgPN.Rows(i).Cells(0).Style.BackColor = Color.Green

                    dgPN.CurrentCell = dgPN(0, i)

                Else
                    dgPN.Rows(i).Cells(0).Style.BackColor = Color.White
                End If
            End If


            i = i + 1
        End While
    End Sub



End Class