Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Public Class PartsRecords
    Private _serialnum As String
    Private _ionicspn As String
    Private _toshibapn As String
    Private _supplierpn As String
    Private _description As String
    Private _lotno As String
    Private _rack As String
    Private _qty As Integer
    Private _status As String
    Private _mfgdate As DateTime
    Private _rcvdate As DateTime
    Private _printeddate As DateTime
    Private _printedby As String
    Private _exist As Boolean
    Private _remarks As String
    Private _Family As String
    Private _Makercode As String
    Private _modelCode As String
    Private cnstr As String
    Private _result As String
    Private _yesterday As String
    Private _thaw As DateTime

#Region "Properties"

    Public Property SerialNum As String
        Get
            Return _serialnum
        End Get
        Set(ByVal value As String)
            _serialnum = value
        End Set
    End Property

    Public Property IonicsPN As String
        Get
            Return _ionicspn
        End Get
        Set(ByVal value As String)
            _ionicspn = value
        End Set
    End Property


    Public Property modelCode As String
        Get
            Return _modelCode
        End Get
        Set(ByVal value As String)
            _modelCode = value
        End Set
    End Property


    Public Property ToshibaPN As String
        Get
            Return _toshibapn
        End Get
        Set(ByVal value As String)
            _toshibapn = value
        End Set
    End Property

    Public Property SupplierPN As String
        Get
            Return _supplierpn
        End Get
        Set(ByVal value As String)
            _supplierpn = 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 LotNo As String
        Get
            Return _lotno
        End Get
        Set(ByVal value As String)
            _lotno = value
        End Set
    End Property

    Public Property Rack As String
        Get
            Return _rack
        End Get
        Set(ByVal value As String)
            _rack = value
        End Set
    End Property

    Public Property Quantity As Integer
        Get
            Return _qty
        End Get
        Set(ByVal value As Integer)
            _qty = value
        End Set
    End Property

    Public Property Status As String
        Get
            Return _status
        End Get
        Set(ByVal value As String)
            _status = value
        End Set
    End Property

    Public Property MFGDate As DateTime
        Get
            Return _mfgdate
        End Get
        Set(ByVal value As DateTime)
            _mfgdate = value
        End Set
    End Property

    Public Property RCVDate As DateTime
        Get
            Return _rcvdate
        End Get
        Set(ByVal value As DateTime)
            _rcvdate = value
        End Set
    End Property

    Public Property PrintedDate As DateTime
        Get
            Return _printeddate
        End Get
        Set(ByVal value As DateTime)
            _printeddate = value
        End Set
    End Property

    Public Property PrintedBy As String
        Get
            Return _printedby
        End Get
        Set(ByVal value As String)
            _printedby = value
        End Set
    End Property

    Public Property Remarks As String
        Get
            Return _remarks
        End Get
        Set(ByVal value As String)
            _remarks = value
        End Set
    End Property

    Public Property Family As String
        Get
            Return _Family
        End Get
        Set(ByVal value As String)
            _Family = value
        End Set
    End Property

    Public Property MakerCode As String
        Get
            Return _Makercode
        End Get
        Set(ByVal value As String)
            _Makercode = value
        End Set
    End Property

    Public Property Exist As Boolean
        Get
            Return _exist
        End Get
        Set(ByVal value As Boolean)
            _exist = value
        End Set
    End Property

    Public Property Result As String
        Get
            Return _result
        End Get
        Set(ByVal value As String)
            _result = value
        End Set
    End Property
    Public Property Yesterday As String
        Get
            Return _yesterday
        End Get
        Set(ByVal value As String)
            _yesterday = value
        End Set
    End Property

    Public Property Thaw As DateTime
        Get
            Return _thaw
        End Get
        Set(ByVal value As DateTime)
            _thaw = value
        End Set
    End Property


#End Region

#Region "Methods"

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

    Public Sub New(ByVal serial As String)
        cnstr = mainForm.cnstr
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String

        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()

            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()
                SerialNum = reader("SerialNum")
                IonicsPN = reader("IonicsPN")
                ToshibaPN = reader("ToshibaPN")
                SupplierPN = reader("SupplierPN")
                Description = reader("Description")
                LotNo = reader("LotNo")
                Quantity = reader("Qty")
                Status = reader("Status")
                MFGDate = reader("MFGDate")
                RCVDate = reader("RCVDate")
                PrintedDate = reader("PrintedDate")
                PrintedBy = reader("PrintedBy")
                Remarks = reader("Remarks")
                MakerCode = reader("MakerCode")
                Family = reader("Family")
                modelCode = reader("ModelCode")
                Thaw = reader("ThawDate")
                Exist = True
            Else
                Exist = False
            End If
            reader.Close()

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

    Public Function getAllModel()
        Dim list As New List(Of String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim mycommand As SqlCommand
        conn.Open()
        myQuery = "SELECT [latestRev] FROM [Toshiba_cats].[dbo].[model] WHERE active = '1' ORDER BY [latestRev]"
        mycommand = New SqlCommand(myQuery, conn)
        Dim reader = mycommand.ExecuteReader
        While reader.Read
            list.Add(reader(0).ToString)
        End While

        conn.Close()
        Return list
    End Function

    Public Function checkThreshold(ByVal pn As String, ByVal line As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = False
        Dim existing As Integer
        Dim threshold As Integer

        Dim mycommand As SqlCommand
        myQuery = "SELECT COUNT(*) as numexist from [Toshiba_kanban].[dbo].[PartsLog] WHERE part_no = '" & pn & "' AND lastupdate BETWEEN '" & Date.Now.ToString("yyyy-MM-dd 00:00") & "' AND '" & Date.Now.AddDays(1).ToString("yyyy-MM-dd 00:00") & "' and status = 'Issuance'"
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()
                existing = reader("numexist")
                reader.Close()
                myQuery = "SELECT line" & line & " from [Toshiba_kanban].[dbo].[PartsThreshold] WHERE part_number = '" & pn & "'"
                mycommand = New SqlCommand(myQuery, conn)
                reader = mycommand.ExecuteReader
                If reader.HasRows Then
                    reader.Read()
                    threshold = reader("line" & line)
                    reader.Close()
                    If (existing + 1 > threshold) Then
                        success = False
                    Else
                        success = True
                    End If
                Else
                    reader.Close()
                    success = False
                End If
            Else
                reader.Close()
                success = False
            End If

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

        Return success
    End Function

    Public Function checkFifo(ByVal serial As String, ByVal pn As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = False
        Dim existing As Integer
        Dim last As DateTime
        Dim format As String = "yyyy-MM-dd"
        Yesterday = Date.Now.ToString("yyyy-MM-dd")

        Dim mycommand As SqlCommand
        myQuery = "SELECT * FROM [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum =  '" & serial & "'"
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Read()
                last = reader("PrintedDate")
                reader.Close()
                myQuery = "SELECT COUNT(*) as numexist from [Toshiba_kanban].[dbo].[PartsRecords] WHERE IonicsPN = '" & pn & "' AND PrintedDate < '" & last.ToString(format) & "'  and status = 'WHS' and Family = '" & issuanceForm.txtFamily.Text & "'"
                mycommand = New SqlCommand(myQuery, conn)
                reader = mycommand.ExecuteReader
                If reader.HasRows Then
                    reader.Read()
                    existing = reader("numexist")
                    reader.Close()
                End If
            Else
                reader.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try

        Result = existing
        Return Result
    End Function

    Public Function issue(ByVal serial As String, ByVal pn As String, ByVal dval As DateTime, ByVal user As String, ByVal line As String, ByVal rev As String, ByVal received As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = False
        dval = DateTime.Now.Date



        Dim mycommand As SqlCommand
        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE IonicsPN = '" & pn & "' AND Status = 'WHS' AND PrintedDate < '" & dval & "'"
        mycommand = New SqlCommand(myQuery, conn)
        Try
            conn.Open()
            Dim reader = mycommand.ExecuteReader
            If reader.HasRows Then
                reader.Close()
            Else
                reader.Close()
                myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'PDN',Remarks = '" & Remarks & "',RCVDate = GETDATE(),PrintedBy = '" & user & "' WHERE SerialNum = '" & serial & "'"
                mycommand = New SqlCommand(myQuery, conn)
                mycommand.ExecuteNonQuery()

                myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
                mycommand = New SqlCommand(myQuery, conn)
                reader = mycommand.ExecuteReader
                reader.Read()
                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[partsPrepLog] VALUES('" & reader("SerialNum") & "','Issuance','" & reader("IonicsPN") & "','" & user & "',GETDATE(),'" & reader("SupplierPN") & "','" & reader("LotNO") & "','" & reader("Qty") & "','" & rev & "','" & line & "','" & received & "')"
                reader.Close()
                mycommand = New SqlCommand(myQuery, conn)
                mycommand.ExecuteNonQuery()

                myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
                mycommand = New SqlCommand(myQuery, conn)
                reader = mycommand.ExecuteReader
                reader.Read()
                myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & reader("SerialNum").ToString & "','" & reader("IonicsPN").ToString & "','" & reader("Description").ToString & "','" & reader("SupplierPN") & "','" & reader("LotNO").ToString & "','" & reader("Qty").ToString & "','" & "Issued" & "','" & reader("Family").ToString & "','" & line & "','" & rev & "',GETDATE(),'" & user & "')"
                reader.Close()
                mycommand = New SqlCommand(myQuery, conn)
                mycommand.ExecuteNonQuery()
                success = True
            End If

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

        Return success
    End Function

    Public Function issue2(ByVal serial As String, ByVal pn As String, ByVal dval As DateTime, ByVal user As String, ByVal line As String, ByVal rev As String, ByVal pet As String, ByVal received As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = True


        Dim mycommand As SqlCommand
        Try
            conn.Open()
            myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'PDN', Remarks = '" & Remarks & "',RCVDate = GETDATE(),PrintedBy = '" & user & "' WHERE SerialNum = '" & serial & "'"
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()


            myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
            mycommand = New SqlCommand(myQuery, conn)
            Dim reader = mycommand.ExecuteReader
            reader.Read()
            myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[partsPrepLog] VALUES('" & reader("SerialNum").ToString & "','Issuance','" & reader("IonicsPN").ToString & "','" & user & "',GETDATE(),'" & reader("SupplierPN") & "','" & reader("LotNO") & "','" & reader("Qty") & "','" & rev & "','" & line & "','" & received & "')"
            reader.Close()
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()

            myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
            mycommand = New SqlCommand(myQuery, conn)
            reader = mycommand.ExecuteReader
            reader.Read()
            myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & reader("SerialNum").ToString & "','" & reader("IonicsPN").ToString & "','" & reader("Description").ToString & "','" & reader("SupplierPN") & "','" & reader("LotNO").ToString & "','" & reader("Qty").ToString & "','" & "Issued" & "','" & reader("Family").ToString & "','" & line & "','" & rev & "',GETDATE(),'" & user & "')"
            reader.Close()
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()
            success = True

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

        Return success
    End Function



    Public Function issue3(ByVal serial As String, ByVal pn As String, ByVal dval As DateTime, ByVal user As String, ByVal line As String, ByVal rev As String, ByVal received As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = True


        Dim mycommand As SqlCommand
        'Try
        conn.Open()
        myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'PDN', Remarks = 'KIT',RCVDate = GETDATE(),PrintedBy = '" & user & "' WHERE SerialNum = '" & serial & "'"
        mycommand = New SqlCommand(myQuery, conn)
        mycommand.ExecuteNonQuery()


        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
        mycommand = New SqlCommand(myQuery, conn)
        Dim reader = mycommand.ExecuteReader
        reader.Read()
        myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[partsPrepLog] VALUES('" & reader("SerialNum").ToString & "','Issuance','" & reader("IonicsPN").ToString & "','" & user & "',GETDATE(),'" & reader("SupplierPN") & "','" & reader("LotNO") & "','" & reader("Qty") & "','" & rev & "','" & line & "','" & received & "')"
        reader.Close()
        mycommand = New SqlCommand(myQuery, conn)
        mycommand.ExecuteNonQuery()

        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
        mycommand = New SqlCommand(myQuery, conn)
        reader = mycommand.ExecuteReader
        reader.Read()

        myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & reader("SerialNum").ToString & "','" & reader("IonicsPN").ToString & "','" & reader("Description").ToString & "','" & reader("SupplierPN") & "','" & reader("LotNO").ToString & "','" & reader("Qty").ToString & "','" & "Issued" & "','" & reader("Family").ToString & "','" & line & "','" & rev & "',GETDATE(),'" & user & "')"
        reader.Close()
        mycommand = New SqlCommand(myQuery, conn)
        mycommand.ExecuteNonQuery()


        myQuery = "SELECT * from [Toshiba_kanban].[dbo].[PartsRecords] WHERE SerialNum = '" & serial & "'"
        mycommand = New SqlCommand(myQuery, conn)
        reader = mycommand.ExecuteReader
        reader.Read()

        myQuery = "INSERT INTO [Toshiba_kanban].[dbo].[PartsLog] VALUES ('" & reader("SerialNum").ToString & "','" & reader("IonicsPN").ToString & "','" & reader("Description").ToString & "','" & reader("SupplierPN") & "','" & reader("LotNO").ToString & "','" & reader("Qty").ToString & "','" & "Kit" & "','" & reader("Family").ToString & "','" & line & "','" & rev & "',GETDATE(),'" & user & "')"
        reader.Close()
        mycommand = New SqlCommand(myQuery, conn)
        mycommand.ExecuteNonQuery()
        success = True

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

        Return success
    End Function

    Public Function reset()
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = True



        Dim mycommand As SqlCommand
        Try
            conn.Open()
            myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'INV-PDN' WHERE Status = 'PDN'"
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()

            myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'INV' WHERE Status = 'WHS'"
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()

            myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'INV-SMT' WHERE Status = 'SMT'"
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            success = False
        Finally
            conn.Close()
            conn.Dispose()
        End Try

        Return success
    End Function

    Public Function updateStatus(ByVal serial As String, ByVal status As String, ByVal remarks As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = False


        Dim mycommand As SqlCommand

        Try
            conn.Open()
            myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = '" & status & "', Remarks = '" & remarks & "' WHERE SerialNum = '" & serial & "'"
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()
            success = True

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

        Return success
    End Function

    Public Function mount(ByVal serial As String)
        Dim conn As New SqlConnection(cnstr)
        Dim myQuery As String
        Dim success As Boolean = False


        Dim mycommand As SqlCommand
        Try
            conn.Open()
            myQuery = "UPDATE [Toshiba_kanban].[dbo].[PartsRecords] SET Status = 'SMT' WHERE SerialNum = '" & serial & "'"
            mycommand = New SqlCommand(myQuery, conn)
            mycommand.ExecuteNonQuery()
            success = True

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

        Return success
    End Function

#End Region

End Class
