Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Public Class Sequence
    Private _ksqsequence As String
    Private _ksqmodel As String
    Private _ksqpcbside As String
    Private _ksqpartno As String
    Private _ksqmachine As String
    Private _ksqrevision As String
    Private _ksqtopsdloc As String
    Private _ksqfsbsqty As String
    Private _ksqdesc1 As String
    Private _ksqdesc2 As String
    Private _ksqtopmark As String
    Private _ksqmodule As String
    Private _ksqfeeder As String
    Private _ksqfeederType As String
    Private _ksqprepby As String
    Private _ksqupdtime As String
    Private _ksqcheckby As String
    Private _ksqchkdate As String
    Private _ksqremarks As String
    Private _ksqStatus As Integer
    Private cnstr As String

#Region "Properties"
    Public Property sequence() As String
        Get
            Return _ksqsequence
        End Get
        Set(ByVal value As String)
            _ksqsequence = value
        End Set
    End Property

    Public Property model() As String
        Get
            Return _ksqmodel
        End Get
        Set(ByVal value As String)
            _ksqmodel = value
        End Set
    End Property

    Public Property pcbside() As String
        Get
            Return _ksqpcbside
        End Get
        Set(ByVal value As String)
            _ksqpcbside = value
        End Set
    End Property

    Public Property partno() As String
        Get
            Return _ksqpartno
        End Get
        Set(ByVal value As String)
            _ksqpartno = value
        End Set
    End Property

    Public Property machine() As String
        Get
            Return _ksqmachine
        End Get
        Set(ByVal value As String)
            _ksqmachine = value
        End Set
    End Property

    Public Property revision() As String
        Get
            Return _ksqrevision
        End Get
        Set(ByVal value As String)
            _ksqrevision = value
        End Set
    End Property

    Public Property topsdloc() As String
        Get
            Return _ksqtopsdloc
        End Get
        Set(ByVal value As String)
            _ksqtopsdloc = value
        End Set
    End Property

    Public Property fsbsqty() As String
        Get
            Return _ksqfsbsqty
        End Get
        Set(ByVal value As String)
            _ksqfsbsqty = value
        End Set
    End Property

    Public Property desc1() As String
        Get
            Return _ksqdesc1
        End Get
        Set(ByVal value As String)
            _ksqdesc1 = value
        End Set
    End Property

    Public Property desc2() As String
        Get
            Return _ksqdesc2
        End Get
        Set(ByVal value As String)
            _ksqdesc2 = value
        End Set
    End Property

    Public Property topmark() As String
        Get
            Return _ksqtopmark
        End Get
        Set(ByVal value As String)
            _ksqtopmark = value
        End Set
    End Property

    Public Property modules() As String
        Get
            Return _ksqmodule
        End Get
        Set(ByVal value As String)
            _ksqmodule = value
        End Set
    End Property

    Public Property feeder() As String
        Get
            Return _ksqfeeder
        End Get
        Set(ByVal value As String)
            _ksqfeeder = value
        End Set
    End Property

    Public Property feederType() As String
        Get
            Return _ksqfeederType
        End Get
        Set(ByVal value As String)
            _ksqfeederType = value
        End Set
    End Property

    Public Property prepby() As String
        Get
            Return _ksqprepby
        End Get
        Set(ByVal value As String)
            _ksqprepby = value
        End Set
    End Property

    Public Property updtime() As String
        Get
            Return _ksqupdtime
        End Get
        Set(ByVal value As String)
            _ksqupdtime = value
        End Set
    End Property

    Public Property checkby() As String
        Get
            Return _ksqcheckby
        End Get
        Set(ByVal value As String)
            _ksqcheckby = value
        End Set
    End Property

    Public Property chkdate() As String
        Get
            Return _ksqchkdate
        End Get
        Set(ByVal value As String)
            _ksqchkdate = value
        End Set
    End Property

    Public Property remarks() As String
        Get
            Return _ksqremarks
        End Get
        Set(ByVal value As String)
            _ksqremarks = value
        End Set
    End Property

    Public Property status() As Integer
        Get
            Return _ksqStatus
        End Get
        Set(ByVal value As Integer)
            _ksqStatus = value
        End Set
    End Property

#End Region

#Region "Methods"
    Public Sub New()
        cnstr = mainForm.cnstr
    End Sub

    Public Function addHeader() As Boolean
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "INSERT INTO KSEQUENCE_HEADER " & _
                  "VALUES(" & "'" & _ksqsequence & "'," & _
                  "'" & _ksqrevision & "'," & _
                  "'" & _ksqmachine & "'," & _
                  "'" & _ksqpcbside & "'," & _
                  "'" & _ksqprepby & "'," & _
                  "'" & _ksqcheckby & "'," & _
                  "'" & _ksqchkdate & "'," & _
                  "'" & _ksqmodel & "'," & _
                  "'" & DateTime.Now & "'," & _
                  "0" & ")"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            addHeader = True
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            addHeader = False
            Exit Function
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Function

    Public Function addDetail() As Boolean
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "INSERT INTO KSEQUENCE_DETAIL " & _
                  "VALUES(" & "'" & _ksqsequence & "'," & _
                  "'" & _ksqrevision & "'," & _
                  "'" & _ksqmodule & "'," & _
                  "'" & _ksqfeeder & "'," & _
                  "'" & _ksqfeederType & "'," & _
                  "'" & _ksqpartno & "'," & _
                  "'" & _ksqtopsdloc & "'," & _
                  "'" & _ksqfsbsqty & "'," & _
                  "'" & _ksqdesc1 & "'," & _
                  "'" & _ksqdesc2 & "'," & _
                  "'" & _ksqtopmark & "'," & _
                  "'" & _ksqremarks & "')"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            addDetail = True
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            addDetail = False
            Exit Function
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Function

    Public Sub deleteHeader()
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "DELETE FROM KSEQUENCE_HEADER" & _
                  " WHERE KSEQUENCE = '" & Me.sequence & "'" & _
                  " AND KSQREVISION = '" & Me.revision & "'"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub deleteDetail()
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "DELETE FROM KSEQUENCE_DETAIL" & _
                  " WHERE KSEQUENCE = '" & Me.sequence & "'" & _
                  " AND KSQREVISION = '" & Me.revision & "'"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Function verify(ByVal partNo As String, ByVal sqModule As String, ByVal feeder As String) As Boolean
        Dim conn As New SqlConnection(cnstr)
        Dim status As Boolean = False

        Dim myQuery As String
        myQuery = "SELECT * FROM KSEQUENCE_DETAIL" & _
                  " WHERE KSEQUENCE = '" & Me.sequence & "'" & _
                  " AND KSQREVISION = '" & Me.revision & "'" & _
                  " AND KSQPARTNO = '" & partNo & "'" & _
                  " AND KSQMODULE = '" & sqModule & "'" & _
                  " AND KSQFEEDERNO = '" & feeder & "'"


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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader
            If reader.HasRows Then
                status = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
        Return status
    End Function

    Public Function approve() As Boolean
        Dim conn As New SqlConnection(cnstr)
        Dim approved As Boolean = False

        Dim myQuery As String
        myQuery = "UPDATE KSEQUENCE_HEADER SET KSQSTATUS = 1," & _
                  "KSQCHECKBY = '" & Me.checkby & "'," & _
                  "KSQCHKDATE = '" & Me.chkdate & "'" & _
                  " WHERE KSEQUENCE = '" & Me.sequence & "'" & _
                  " AND KSQREVISION = '" & Me.revision & "'"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            approved = 1
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
        Return approved
    End Function

    Public Function disApprove() As Boolean
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String

        myQuery = "UPDATE KSEQUENCE_HEADER SET KSQSTATUS = 0," & _
                  "KSQCHECKBY = '" & Me.checkby & "'," & _
                  "KSQCHKDATE = '" & Me.chkdate & "'" & _
                  " WHERE KSEQUENCE = '" & Me.sequence & "'" & _
                  " AND KSQREVISION = '" & Me.revision & "'"

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

        Try
            conn.Open()
            mycommand.ExecuteNonQuery()
            disApprove = 1
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Function

    Public Function getValidSequence() As List(Of String)
        Dim sequenceList As New List(Of String)
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM KSEQUENCE_HEADER" & _
                  " WHERE KSQSTATUS = 1" & _
                  " ORDER BY KSEQUENCE"

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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            While reader.Read
                sequenceList.Add(reader("KSEQUENCE"))
            End While
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return sequenceList
    End Function

    Public Function getAllSequence() As List(Of String)
        Dim sequenceList As New List(Of String)
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM KSEQUENCE_HEADER"

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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            While reader.Read
                sequenceList.Add(reader("KSEQUENCE"))
            End While
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return sequenceList
    End Function

    Public Function getRevisions(ByVal sequence As String) As List(Of String)
        Dim revList As New List(Of String)
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT DISTINCT KSQREVISION FROM KSEQUENCE_HEADER " & _
                  "WHERE KSEQUENCE = '" & sequence & "' " & _
                  "ORDER BY KSQREVISION"

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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            While reader.Read
                revList.Add(reader("KSQREVISION"))
            End While
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return revList
    End Function

    Public Function isVerified(ByVal sequence As String, ByVal revision As String) As Boolean
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT KSQSTATUS FROM KSEQUENCE_HEADER" & _
                  " WHERE KSEQUENCE = '" & sequence & "'" & _
                  " AND KSQREVISION = '" & revision & "'"

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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            reader.Read()
            If reader("KSQSTATUS") = 1 Then
                isVerified = True
            Else
                isVerified = False
            End If
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
    End Function

    Public Function verificationDetails(ByVal sequence As String, ByVal revision As String) As List(Of String)
        Dim verDetails As New List(Of String)
        Dim conn As New SqlConnection(cnstr)

        Dim myQuery As String
        myQuery = "SELECT * FROM KSEQUENCE_HEADER" & _
                  " WHERE KSEQUENCE = '" & sequence & "'" & _
                  " AND KSQREVISION = '" & revision & "'"

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

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            While reader.Read
                verDetails.Add(reader("KSQCHECKBY"))
                verDetails.Add(reader("KSQCHKDATE"))
            End While
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return verDetails
    End Function
#End Region
End Class
