Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Public Class Mounting
    Private _date As DateTime
    Private _sequence As String
    Private _revision As String
    Private _feeder As String
    Private _partNo As String
    Private _quantity As Double
    Private _lotNumber As String
    Private _operator As String
    Private _verify As Integer
    Private _3pL As String
    Private cnstr As String
    Private _sequenceno As String
    Private _lblserial As String
    Private _description As String
    Private _petname As String

    Public Property mDate() As DateTime
        Get
            Return _date
        End Get
        Set(ByVal value As DateTime)
            _date = value
        End Set
    End Property

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

    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property




    Public Property lblserial() As String
        Get
            Return _lblserial
        End Get
        Set(ByVal value As String)
            _lblserial = value
        End Set
    End Property


    Public Property sequenceno() As String
        Get
            Return _sequenceno
        End Get
        Set(ByVal value As String)
            _sequenceno = value
        End Set
    End Property

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

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

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

    Public Property quantity() As Double
        Get
            Return _quantity
        End Get
        Set(ByVal value As Double)
            _quantity = value
        End Set
    End Property

    Public Property lotNumber As String
        Get
            Return _lotNumber
        End Get
        Set(ByVal value As String)
            _lotNumber = value
        End Set
    End Property

    Public Property mOperator() As String
        Get
            Return _operator
        End Get
        Set(ByVal value As String)
            _operator = value
        End Set
    End Property

    Public Property verify() As Integer
        Get
            Return _verify
        End Get
        Set(ByVal value As Integer)
            _verify = value
        End Set
    End Property

    Public Property threePL() As String
        Get
            Return _3pL
        End Get
        Set(ByVal value As String)
            _3pL = value
        End Set
    End Property

    Public Property petname() As Integer
        Get
            Return _petname
        End Get
        Set(ByVal value As Integer)
            _petname = value
        End Set
    End Property

    Public Sub New()
        cnstr = mainForm.cnstr
    End Sub

    Public Sub add(ByVal reload As Boolean, ByVal qacheck As Boolean, ByVal status As Boolean)
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0

        Dim myQuery As String
        Dim myStatus As String = ""

        If status = False Then
            myStatus = "fail"
        Else
            myStatus = "pass"
        End If


        If qacheck Then
            If Not reload Then

                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[KMOUNTING] VALUES (" & _
                      "'" & Me.mDate & "'," & _
                      "'" & Me.machine & "'," & _
                      "'" & Me.revision & "'," & _
                      "'" & Me.feeder & "'," & _
                      "'" & Me.partno & "'," & _
                      Me.quantity & "," & _
                      "'" & Me.lotNumber & "'," & _
                      "'" & Me.mOperator & "'," & _
                      Me.verify & "," & _
                     "'op-" & myStatus & "'," & _
                     "'" & Me.sequenceno & "'," & _
                     "'" & Me.lblserial & "') " & _
                     "INSERT INTO [Toshiba_kanban].[dbo].[KMOUNTING] VALUES (" & _
                      "'" & Me.mDate & "'," & _
                      "'" & Me.machine & "'," & _
                      "'" & Me.revision & "'," & _
                      "'" & Me.feeder & "'," & _
                      "'" & Me.partno & "',0," & _
                      "'" & Me.lotNumber & "'," & _
                      "'" & Me.mOperator & "'," & _
                      Me.verify & "," & _
                     "'qa-" & myStatus & "'," & _
                     "'" & Me.sequenceno & "'," & _
                     "'" & Me.lblserial & "') "
            Else

                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[KMOUNTING] VALUES (" & _
                      "'" & Me.mDate & "'," & _
                      "'" & Me.machine & "'," & _
                      "'" & Me.revision & "'," & _
                      "'" & Me.feeder & "'," & _
                      "'" & Me.partno & "',0," & _
                      "'" & Me.lotNumber & "'," & _
                      "'" & Me.mOperator & "'," & _
                      Me.verify & "," & _
                     "'qa-" & myStatus & "'," & _
                     "'" & Me.sequenceno & "'," & _
                     "'" & Me.lblserial & "')"

                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & Me.lblserial & "','" & Me.partno & "','" & mountingForm.txtPartDesc.Text & "','" & "" & "','" & Me.lotNumber & "','" & Me.quantity & "','" & "Mounted" & "','" & "" & "','" & mountingForm.cmbLine.Text & "','" & mountingForm.cmbRev.Text & "','" & Me.mDate & "','" & Me.mOperator & "')"

            End If
        Else
            If reload Then
                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[KMOUNTING] VALUES (" & _
                  "'" & Me.mDate & "'," & _
                  "'" & Me.machine & "'," & _
                  "'" & Me.revision & "'," & _
                  "'" & Me.feeder & "'," & _
                  "'" & Me.partno & "'," & _
                  Me.quantity & "," & _
                  "'" & Me.lotNumber & "'," & _
                  "'" & Me.mOperator & "'," & _
                  Me.verify & "," & _
                 "'reload-" & myStatus & "'," & _
                 "'" & Me.sequenceno & "'," & _
                 "'" & Me.lblserial & "')"
            Else
                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[KMOUNTING] VALUES (" & _
                  "'" & Me.mDate & "'," & _
                  "'" & Me.machine & "'," & _
                  "'" & Me.revision & "'," & _
                  "'" & Me.feeder & "'," & _
                  "'" & Me.partno & "'," & _
                  Me.quantity & "," & _
                  "'" & Me.lotNumber & "'," & _
                  "'" & Me.mOperator & "'," & _
                  Me.verify & "," & _
                 "'op-" & myStatus & "'," & _
                 "'" & Me.sequenceno & "'," & _
                 "'" & Me.lblserial & "')"

                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & Me.lblserial & "','" & Me.partno & "','" & mountingForm.txtPartDesc.Text & "','" & "" & "','" & Me.lotNumber & "','" & Me.quantity & "','" & "Mounted" & "','" & "" & "','" & mountingForm.cmbLine.Text & "','" & mountingForm.cmbRev.Text & "','" & Me.mDate & "','" & Me.mOperator & "')"
            End If

        End If


        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)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub add_hist(ByVal reload As Boolean, ByVal qacheck As Boolean, ByVal status As Boolean)
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0

        Dim myQuery As String
        Dim myStatus As String = ""

        If status = False Then
            myStatus = "-fail"
        End If

        If reload = True And qacheck = False Then
            myQuery = "INSERT INTO kmounting_hist VALUES (" & _
                  "'" & Me.mDate & "'," & _
                  "'" & Me.machine & "'," & _
                  "'" & Me.revision & "'," & _
                  "'" & Me.feeder & "'," & _
                  "'" & Me.partno & "'," & _
                  Me.quantity & "," & _
                  "'" & Me.lotNumber & "'," & _
                  "'" & Me.mOperator & "'," & _
                  Me.verify & "," & _
                  "'reload" & myStatus & "')"
            myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & Me.lblserial & "','" & Me.partno & "','" & mountingForm.txtPartDesc.Text & "','" & "" & "','" & Me.lotNumber & "','" & Me.quantity & "','" & "Mounted" & "','" & "" & "','" & mountingForm.cmbLine.Text & "','" & mountingForm.cmbRev.Text & "','" & Me.mDate & "','" & Me.mOperator & "')"
        ElseIf reload = False And qacheck = True Then
            myQuery = "INSERT INTO kmounting_hist VALUES (" & _
                  "'" & Me.mDate & "'," & _
                  "'" & Me.machine & "'," & _
                  "'" & Me.revision & "'," & _
                  "'" & Me.feeder & "'," & _
                  "'" & Me.partno & "'," & _
                  "0," & _
                  "''," & _
                  "'" & Me.mOperator & "'," & _
                  Me.verify & "," & _
                  "'check" & myStatus & "')"
            myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & Me.lblserial & "','" & Me.partno & "','" & mountingForm.txtPartDesc.Text & "','" & "" & "','" & Me.lotNumber & "','" & Me.quantity & "','" & "Mounted" & "','" & "" & "','" & mountingForm.cmbLine.Text & "','" & mountingForm.cmbRev.Text & "','" & Me.mDate & "','" & Me.mOperator & "')"
        Else
            myQuery = "INSERT INTO kmounting_hist VALUES (" & _
                  "'" & Me.mDate & "'," & _
                  "'" & Me.machine & "'," & _
                  "'" & Me.revision & "'," & _
                  "'" & Me.feeder & "'," & _
                  "'" & Me.partno & "'," & _
                  Me.quantity & "," & _
                  "'" & Me.lotNumber & "'," & _
                  "'" & Me.mOperator & "'," & _
                  Me.verify & "," & _
                  "'" & Me.threePL & myStatus & "')"

            myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & Me.lblserial & "','" & Me.partno & "','" & mountingForm.txtPartDesc.Text & "','" & "" & "','" & Me.lotNumber & "','" & Me.quantity & "','" & "Mounted" & "','" & "" & "','" & mountingForm.cmbLine.SelectedItem & "','" & mountingForm.cmbModel.SelectedItem & Me.mDate & Me.mOperator & "')"
        End If


        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)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Sub addSettings(ByVal line As String, ByVal ksequence As String)
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0

        Dim myQuery As String
        Dim myStatus As String = ""

        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSETTINGS] WHERE line = '" & line & "'"

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

        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Close()
                myQuery = "UPDATE [Toshiba_kanban].[dbo].[KSETTINGS] SET ksequence = '" & ksequence & "' WHERE line = '" & line & "'"
                mycommand = New SqlCommand(myQuery, conn)
                mycommand.ExecuteNonQuery()
            Else
                reader.Close()
                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[KSETTINGS] (line,ksequence) VALUES(" & line & ",'" & ksequence & "')"
                mycommand = New SqlCommand(myQuery, conn)
                mycommand.ExecuteNonQuery()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Public Function getSettings(ByVal line As String) As String
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0
        Dim row As String = ""
        Dim myQuery As String
        Dim myStatus As String = ""

        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSETTINGS] WHERE line = '" & line & "'"

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

        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            reader.Read()
            Dim seq As String = reader("ksequence").ToString

            reader.Close()
            myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSEQUENCE_HEADER] WHERE KSEQUENCENO = '" & seq.Trim & "'"
            mycommand = New SqlCommand(myQuery, conn)
            reader = mycommand.ExecuteReader
            reader.Read()
            row = reader("KSQREVISION").ToString

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

        Return row
    End Function
    Public Function getSettings2(ByVal line As String) As String
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0
        Dim row As String = ""
        Dim myQuery As String
        Dim myStatus As String = ""

        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSETTINGS] WHERE line = '" & line & "'"

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

        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            reader.Read()
            Dim seq As String = reader("ksequence").ToString

            reader.Close()
            myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSEQUENCE_HEADER] WHERE KSEQUENCENO = '" & seq.Trim & "'"
            mycommand = New SqlCommand(myQuery, conn)
            reader = mycommand.ExecuteReader
            reader.Read()
            row = reader("KSQMODEL").ToString

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

        Return row
    End Function
    Public Function getSequencePN(ByVal line As String) As String()
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0
        Dim row1(0) As String
        Dim myQuery As String
        Dim myStatus As String = ""
        Dim counter As Integer = 0

        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSETTINGS] WHERE line = '" & line & "'"

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

        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            reader.Read()
            Dim seq As String = reader("ksequence").ToString
            reader.Close()

            myQuery = "SELECT KSQPARTNO AS KSQPARTNO from [Toshiba_kanban].[dbo].[KSEQUENCE_DETAIL] WHERE KSEQUENCENO = '" & seq & "' order by KSQMODULE, KSQFEEDERNO ASC"
            mycommand = New SqlCommand(myQuery, conn)
            reader = mycommand.ExecuteReader

            If reader.HasRows Then
                While reader.Read()
                    ReDim Preserve row1(counter)
                    If (reader("KSQPARTNO").ToString <> "") Then
                        row1(counter) = reader("KSQPARTNO").ToString
                        counter = counter + 1
                    End If


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

        Return row1
    End Function

    Public Function getKseq(ByVal line As String) As String
        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0
        Dim row1(0) As String
        Dim myQuery As String
        Dim myStatus As String = ""
        Dim counter As Integer = 0
        Dim seq As String
        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSETTINGS] WHERE line = '" & line & "'"

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

        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            reader.Read()
            seq = reader("ksequence").ToString
            reader.Close()

            myQuery = "SELECT DISTINCT(KSQPARTNO) AS KSQPARTNO from [Toshiba_kanban].[dbo].[KSEQUENCE_DETAIL] WHERE KSEQUENCENO = '" & seq & "'"
            mycommand = New SqlCommand(myQuery, conn)
            reader = mycommand.ExecuteReader

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

        Return seq
    End Function

    Public Function checkOldLabel(ByVal part_no As String, ByVal label As String, ByVal machine As String, ByVal feeder As String, ByVal rev As String, ByVal kseqno As String) As Boolean

        Dim conn As New SqlConnection(cnstr)
        Dim active As Integer = 0
        Dim match As Boolean = False
        Dim myQuery As String
        Dim myStatus As String = ""

        myQuery = "SELECT TOP 1 * from [Toshiba_kanban].[dbo].[KMOUNTING_ARCHIVE] WHERE  KMTMACHINE = '" & machine & "' AND KMTFEEDER = '" & feeder & "' AND KMTREVISION = '" & rev & "' AND KMT3PL like '%-pass' and KSEQUENCENO = '" & kseqno & "' ORDER BY kmtdate desc"
        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()
                Dim oldPartNo As String
                oldPartNo = reader("KMTPARTNO").ToString
                reader.Close()
                If oldPartNo = part_no Then
                    match = True
                Else
                    match = False
                End If
            Else
                reader.Close()
                match = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            match = False
        Finally
            conn.Close()
            conn.Dispose()
        End Try

        Return match
    End Function

    Public Function GetLineSeq(ByVal line As String) As String
        Dim result As String = ""

        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSETTINGS] WHERE  line = '" & line & "'"
        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()

                result = reader("ksequence").ToString

            End If
            conn.Close()
        Catch ex As Exception

        Finally
            conn.Close()
            conn.Dispose()
        End Try


        Return result
    End Function


    Public Function GetModelCode(ByVal seq As String) As String
        Dim result As String = ""

        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[KSEQUENCE_HEADER] WHERE KSEQUENCENO = '" & seq & "'"
        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()

                result = reader("KSQMODEL").ToString.Trim & " " & reader("KSQREVISION").ToString.Trim

            End If
            conn.Close()
        Catch ex As Exception

        Finally
            conn.Close()
            conn.Dispose()
        End Try


        Return result
    End Function


    Public Function GetModelNo(ByVal model As String, ByVal rev As String) As String
        Dim result As String = ""

        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        myQuery = "SELECT * from [Toshiba_cats].[dbo].[model] where modelDesc1 ='" & model & "' and latestRev ='" & rev & "'"
        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()

                result = reader("modelno").ToString.Trim

            End If
            conn.Close()
        Catch ex As Exception

        Finally
            conn.Close()
            conn.Dispose()
        End Try


        Return result
    End Function


End Class
