﻿Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports BTClientLib
Imports System.IO
Imports System.Reflection

Public Class frm7E45
    Inherits System.Windows.Forms.Form

    'Private dbClient As BtrieveDbClient
    Private tmpStr As List(Of Dictionary(Of String, String))
    Private affected_rows As Integer

#Region " Windows tH[€ fUCiลถฌณ๊ฝR[h "

    Public Sub New()
        MyBase.New()

        ' ฑฬฤัoตอ Windows tH[€ fUCiล•K—vลทB
        InitializeComponent()

        ' InitializeComponent() ฤัoตฬใษ๚ป๐’วมตทB

    End Sub

    ' Form อAR“|[l“g๊——ษใ—๐ภsท้ฝ฿ษ dispose ๐I[o[ChตทB
    'Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    '    If disposing Then
    '        If Not (components Is Nothing) Then
    '            components.Dispose()
    '        End If
    '    End If
    '    MyBase.Dispose(disposing)
    'End Sub

    ' Windows tH[€ fUCiล•K—vลทB
    'Private components As System.ComponentModel.IContainer

    '  : ศบฬvV[WอAWindows tH[€ fUCiล•K—vลทB
    'Windows tH[€ fUCi๐gมฤ•ฯXตฤญพณขB  
    ' R[h GfB^๐gมฤ•ฯXตศขลญพณขB
    'Friend WithEvents WarpCtrl1 As WarpControl.WarpCtrl
    'Friend WithEvents WarpFrame1 As WarpControl.WarpFrame
    'Friend WithEvents WarpHWBcr1 As WarpControl.WarpHWBcr
    'Friend WithEvents WarpBox1 As WarpControl.WarpBox
    'Friend WithEvents wfName As WarpControl.WarpField
    'Friend WithEvents wfInput As WarpControl.WarpField

    '<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    '    Me.WarpFrame1 = New WarpControl.WarpFrame()
    '    Me.wfInput = New WarpControl.WarpField()
    '    Me.WarpBox1 = New WarpControl.WarpBox()
    '    Me.wfName = New WarpControl.WarpField()
    '    Me.WarpHWBcr1 = New WarpControl.WarpHWBcr()
    '    Me.WarpFrame1.SuspendLayout()
    '    Me.SuspendLayout()
    '    '
    '    'WarpFrame1
    '    '
    '    Me.WarpFrame1.Controls.Add(Me.wfInput)
    '    Me.WarpFrame1.Controls.Add(Me.WarpBox1)
    '    Me.WarpFrame1.Controls.Add(Me.wfName)
    '    Me.WarpFrame1.Font = New System.Drawing.Font("MS UI Gothic", 9.0!)
    '    Me.WarpFrame1.Location = New System.Drawing.Point(0, 0)
    '    Me.WarpFrame1.Name = "WarpFrame1"
    '    Me.WarpFrame1.Size = New System.Drawing.Size(240, 296)
    '    Me.WarpFrame1.TabIndex = 1
    '    Me.WarpFrame1.Text = "WarpFrame1"
    '    Me.WarpFrame1.UseMacro = False
    '    Me.WarpFrame1.UseMacroFileName = ""
    '    '
    '    'wfInput
    '    '
    '    Me.wfInput.BcrFlag = True
    '    Me.wfInput.DecimalPointPosition = CType(0, Short)
    '    Me.wfInput.Font = New System.Drawing.Font("lr SVbN", 33.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.wfInput.Format = ""
    '    Me.wfInput.InputMode.Secret = True
    '    Me.wfInput.Length = CType(1, Short)
    '    Me.wfInput.Location = New System.Drawing.Point(200, 161)
    '    Me.wfInput.MacroBcr = ""
    '    Me.wfInput.MacroIn = ""
    '    Me.wfInput.MacroKey = ""
    '    Me.wfInput.MacroOut = ""
    '    Me.wfInput.Name = "wfInput"
    '    Me.wfInput.Size = New System.Drawing.Size(18, 36)
    '    Me.wfInput.TabIndex = CType(4, Short)
    '    '
    '    'WarpBox1
    '    '
    '    Me.WarpBox1.BoxType = WarpControl.BoxTypes.Box
    '    Me.WarpBox1.DrawMode = WarpControl.DrawModes.Paint
    '    Me.WarpBox1.Font = New System.Drawing.Font("lr SVbN", 33.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.WarpBox1.ForeColor = CType(15, Short)
    '    Me.WarpBox1.Image = ""
    '    Me.WarpBox1.Location = New System.Drawing.Point(0, 48)
    '    Me.WarpBox1.Name = "WarpBox1"
    '    Me.WarpBox1.Size = New System.Drawing.Size(240, 96)
    '    Me.WarpBox1.TabIndex = 3
    '    '
    '    'wfName
    '    '
    '    Me.wfName.DecimalPointPosition = CType(0, Short)
    '    Me.wfName.Font = New System.Drawing.Font("lr SVbN", 22.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    '    Me.wfName.FontSize = WarpControl.FontSizes.Small2
    '    Me.wfName.Format = ""
    '    Me.wfName.Length = CType(20, Short)
    '    Me.wfName.Location = New System.Drawing.Point(0, 216)
    '    Me.wfName.MacroBcr = ""
    '    Me.wfName.MacroIn = ""
    '    Me.wfName.MacroKey = ""
    '    Me.wfName.MacroOut = ""
    '    Me.wfName.Name = "wfName"
    '    Me.wfName.Size = New System.Drawing.Size(240, 24)
    '    Me.wfName.TabIndex = CType(2, Short)
    '    '
    '    'WarpHWBcr1
    '    '
    '    Me.WarpHWBcr1.Decode.UPC_EAN_JAN = True
    '    Me.WarpHWBcr1.DiscreteFirstLen = CType(0, Short)
    '    Me.WarpHWBcr1.DiscreteSecondLen = CType(0, Short)
    '    Me.WarpHWBcr1.InterleavedFirstLen = CType(0, Short)
    '    Me.WarpHWBcr1.InterleavedSecondLen = CType(0, Short)
    '    '
    '    'frm7E45
    '    '
    '    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
    '    Me.ClientSize = New System.Drawing.Size(240, 296)
    '    Me.Controls.Add(Me.WarpFrame1)
    '    Me.Font = New System.Drawing.Font("MS UI Gothic", 9.0!)
    '    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
    '    Me.Name = "frm7E45"
    '    Me.Text = "Form1"
    '    Me.WarpFrame1.ResumeLayout(False)
    '    Me.ResumeLayout(False)

    'End Sub

#End Region


    Private Sub frm2E45_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '// Case QVGA -> resolution to 1/2
        If System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width = 240 Then
            Call resolution.VGAtoQVGA(Me)
        End If

        Dim wstrImage As String
        Dim rtn As Integer

        Try

            ' t[€ฬI[v“
            'rtn = WarpFrame1.Open()
            'If (rtn <> 0) Then
            '    Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            'End If
#If Not Debug Then
            'sSetImgFName(G_CONST_MID_E7E45, WarpBox1, 1)
#End If
            'sDisp2E45()

            ' tH[€EC“ ภs
            sMain()

            ' t[€ฬN[Y
            'rtn = WarpFrame1.Close()
            'If (rtn <> 0) Then
            '    Err.Raise(vbObjectError + 1, gAppName, "WarpError")
            'End If

            ' tH[€EN[Y
            'Me.Close()

            Exit Sub

        Catch ex As Exception
            gintStatus = OTR_STATUS.QUIT
            sErrLog(gAppName, "frm7E45_Load", ex.ToString)
            Me.Close()
        End Try

    End Sub

    ' t[€EC“
    Public Sub sMain()

        'Dim WKey As Short
        'Dim buf As String
        'Dim rtn As Integer
        'Dim wintCnt As Integer

        'Try

        '    ' t[€ฬ—๐ฑฑษLqตทB
        '    Do While (True)

        '        ' R[h“—อ
        '        WKey = WarpFrame1.Input(wfInput)

        '        If WKey = WKY_ENT Then

        '            rtn = fSearchStock()
        '            If rtn = -1 Then
        '                'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)
        '                f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(gclsDB.BtriveStatus, gclsDB.ErrFile), MWA_DB_WARNING, MWA_DB_OK)
        '            ElseIf rtn = -2 Then
        '                gintStatus = OTR_STATUS.NOTFOUND
        '                Exit Do
        '            Else
        '                gintStatus = OTR_STATUS.PART
        '                Exit Do
        '            End If

        '        ElseIf WKey = WKY_BCR Then

        '        ElseIf WKey = WKY_F1 Then

        '            gintStatus = OTR_STATUS.PART
        '            Exit Do

        '        ElseIf WKey = WKY_F2 Then

        '        ElseIf WKey = WKY_ERROR Then

        '            f0.WarpCtrl1.MessageBox(G_CONST_EMT_WARP, G_CONST_EM_WARP, MWA_DB_WARNING, MWA_DB_OK)

        '        End If

        '    Loop

        '    Exit Sub

        'Catch ex As Exception
        '    sErrLog(gAppName, "sMain", ex.ToString)
        'End Try

    End Sub

    ''/*
    ''|| ึ”–ผFsDisp7E45
    ''|| T—v  F7E45ๆ–ส•\ฆ
    ''*/
    'Private Sub sDisp7E45()

    '    wfName.Puts(gstrctKDHT.strUserName)

    'End Sub


    '/*
    '|| ึ”–ผFfSearchStock
    '|| T—v  F
    '*/
    Public Function fSearchStock() As Integer

        Dim wintRet As Integer
        Dim wstrLocation As String
        Dim wstrSerial As String

        fSearchStock = -1

        '// ฤืปธผฎJn
        'BtLib.Transaction.BeginConCurrent()

        wintRet = fResetStock(wstrLocation, wstrSerial)
        If wintRet = -1 Then
            'BtLib.Transaction.Abort()
            Exit Function
        ElseIf wintRet = -2 Then
            'BtLib.Transaction.End()
            fSearchStock = -2
            Exit Function
        End If

        wintRet = fSetPKCP(wstrLocation, wstrSerial)
        If wintRet = -1 Then
            'BtLib.Transaction.Abort()
            Exit Function
        Else
            fSearchStock = 0
        End If

        '// ฤืปธผฎฎ—น
        'BtLib.Transaction.End()

        gstrctPPDispPart.strLocation = wstrLocation
        gstrctPPDispPart.strLocation1 = Mid(wstrLocation, 1, 2)
        gstrctPPDispPart.strLocation2 = Mid(wstrLocation, 3, 3)
        gstrctPPDispPart.strSerial = wstrSerial
        gstrctPPDispPart.bLocked = True

    End Function


    '/*
    '|| ึ”–ผFfResetStock
    '|| T—v  F
    '*/
    Public Function fResetStock(ByRef pstrLocation As String, ByRef pstrSerial As String) As Integer

        Dim rtn As Integer
        Dim bFound As Boolean
        Dim wstrKey(2) As String
        Dim wstrKeyValue(2) As String
        Dim wstrKey2(9) As String
        Dim wstrKeyValue2(9) As String
        Dim wstrKey3(12) As String
        Dim wstrKeyValue3(12) As String
        Dim wstrLocation As String
        Dim wstrQty As String
        Dim wstrNewLocation As String
        Dim wstrNewSerial As String
        Dim wstrNewStockDate As String
        Dim wstrNewStockTime As String
        Dim sSQL As String
        Dim wstrWhere As String

        fResetStock = -1
        Try

            '// PKCPฉ็๕๐๐ๆ“พ
            With gstrctPPDispPart
                wstrKey(0) = "ItemCode" : wstrKeyValue(0) = .strItemCode
                wstrKey(1) = "ItemInCode" : wstrKeyValue(1) = .strItemInCode
                wstrKey(2) = "PlanSerial" : wstrKeyValue(2) = .strSerial
            End With

            wstrWhere = ""
            wstrWhere = wstrKey(0) & " = '" & wstrKeyValue(0) & "'"
            wstrWhere &= " and " & wstrKey(1) & " = '" & wstrKeyValue(1) & "'"
            wstrWhere &= " and " & wstrKey(2) & " = '" & wstrKeyValue(2) & "'"

            tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_PACKAP + " WHERE " + wstrWhere)

            Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)
            Dim sItemCode As String = ""
            Dim sItemInCode As String = ""
            Dim sPlanSerial As String = ""
            Dim sPicsType As String = ""
            Dim sCstmid As String = ""
            Dim sCstmCode As String = ""
            Dim sCenterCode As String = ""
            Dim sBhk As String = ""

            Dim sStockLctn As String = ""
            Dim sItemInID As String = ""
            Dim sBunkatuJ As String = ""
            Dim sItemMCode As String = ""

            If tmpStr.Count > 0 Then

                values = tmpStr(0)

                Dim result As String = Nothing
                If values.TryGetValue("ItemCode", result) Then
                    sItemCode = RTrim(result)
                End If
                If values.TryGetValue("ItemInCode", result) Then
                    sItemInCode = RTrim(result)
                End If
                If values.TryGetValue("PlanSerial", result) Then
                    sPlanSerial = RTrim(result)
                End If
                If values.TryGetValue("PicsType", result) Then
                    sPicsType = RTrim(result)
                End If
                If values.TryGetValue("Cstmid", result) Then
                    sCstmid = RTrim(result)
                End If
                If values.TryGetValue("CstmCode", result) Then
                    sCstmCode = RTrim(result)
                End If
                If values.TryGetValue("CenterCode", result) Then
                    sCenterCode = RTrim(result)
                End If
                If values.TryGetValue("Bhk", result) Then
                    sBhk = RTrim(result)
                End If

                If values.TryGetValue("StockLctn", result) Then
                    sStockLctn = RTrim(result)
                End If
                If values.TryGetValue("ItemInID", result) Then
                    sItemInID = RTrim(result)
                End If
                If values.TryGetValue("ItemInCode", result) Then
                    sItemInCode = RTrim(result)
                End If
                If values.TryGetValue("StockLctn", result) Then
                    sStockLctn = RTrim(result)
                End If
                If values.TryGetValue("BunkatuJ", result) Then
                    sBunkatuJ = RTrim(result)
                End If
                If values.TryGetValue("ItemMCode", result) Then
                    sItemMCode = RTrim(result)
                End If


                If wstrKeyValue(0) = RTrim(sItemCode) And _
                   wstrKeyValue(1) = RTrim(sItemInCode) And _
                   wstrKeyValue(2) = RTrim(sPlanSerial) Then

                    wstrKeyValue2(0) = RTrim(sPicsType)
                    wstrKeyValue2(1) = RTrim(sCstmid)
                    wstrKeyValue2(2) = RTrim(sCstmCode)
                    wstrKeyValue2(3) = RTrim(sCenterCode)
                    wstrKeyValue2(4) = RTrim(sBhk)
                    wstrKeyValue2(5) = RTrim(sStockLctn)
                    wstrKeyValue2(6) = RTrim(sItemInID)
                    wstrKeyValue2(7) = RTrim(sItemInCode)
                    wstrKeyValue2(8) = Space(5)
                    wstrKeyValue2(9) = Space(6)
                    wstrLocation = RTrim(sStockLctn)
                    wstrQty = RTrim(sBunkatuJ)

                    wstrKeyValue3(0) = RTrim(sPicsType)
                    wstrKeyValue3(1) = RTrim(sCstmid)
                    wstrKeyValue3(2) = RTrim(sCstmCode)
                    wstrKeyValue3(3) = RTrim(sCenterCode)
                    wstrKeyValue3(4) = RTrim(sBhk)
                    wstrKeyValue3(5) = RTrim(sItemInID)
                    wstrKeyValue3(6) = RTrim(sItemInCode)
                    wstrKeyValue3(7) = "3"
                    wstrKeyValue3(8) = RTrim(sBunkatuJ)
                    wstrKeyValue3(9) = RTrim(sItemMCode)
                    wstrKeyValue3(10) = Space(8)
                    wstrKeyValue3(11) = Space(4)
                    wstrKeyValue3(12) = Space(6)

                Else
                    'gclsDB.fCloseRecord("PKCP01")
                    Exit Function
                End If


            Else

                Exit Function

            End If


            wstrKey2(0) = "PicsType"
            wstrKey2(1) = "Cstmid"
            wstrKey2(2) = "CstmCode"
            wstrKey2(3) = "CenterCode"
            wstrKey2(4) = "Bhk"
            wstrKey2(5) = "Location"
            wstrKey2(6) = "Itemid"
            wstrKey2(7) = "ItemCode"
            wstrKey2(8) = "Qty"
            wstrKey2(9) = "SerialNo"

            wstrWhere = ""
            wstrWhere = wstrKey2(0) & " = '" & wstrKeyValue(0) & "'"
            wstrWhere &= " and " & wstrKey2(1) & " = '" & wstrKeyValue2(1) & "'"
            wstrWhere &= " and " & wstrKey2(2) & " = '" & wstrKeyValue2(2) & "'"
            wstrWhere &= " and " & wstrKey2(3) & " = '" & wstrKeyValue2(3) & "'"
            wstrWhere &= " and " & wstrKey2(4) & " = '" & wstrKeyValue2(4) & "'"
            wstrWhere &= " and " & wstrKey2(5) & " = '" & wstrKeyValue2(5) & "'"
            wstrWhere &= " and " & wstrKey2(6) & " = '" & wstrKeyValue2(6) & "'"
            wstrWhere &= " and " & wstrKey2(7) & " = '" & wstrKeyValue2(7) & "'"
            wstrWhere &= " and " & wstrKey2(8) & " = '" & wstrKeyValue2(8) & "'"
            wstrWhere &= " and " & wstrKey2(9) & " = '" & wstrKeyValue2(9) & "'"

            tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_STOCK + " WHERE " + wstrWhere)


            values = New Dictionary(Of String, String)
            sPicsType = ""
            sCstmid = ""
            sCstmCode = ""
            sCenterCode = ""
            sBhk = ""

            Dim sLocation As String = ""
            Dim sItemid As String = ""
            sItemCode = ""
            Dim sQty As String = ""
            Dim ssQty As String = ""
            Dim sSerialNo As String = ""
            Dim sZaikoStatus As String = ""
            Dim i As Integer = 0

            bFound = False

            If tmpStr.Count > 0 Then

                Do

                    values = tmpStr(i)

                    Dim result As String = Nothing

                    If values.TryGetValue("PicsType", result) Then
                        sPicsType = RTrim(result)
                    End If
                    If values.TryGetValue("Cstmid", result) Then
                        sCstmid = RTrim(result)
                    End If
                    If values.TryGetValue("CstmCode", result) Then
                        sCstmCode = RTrim(result)
                    End If
                    If values.TryGetValue("CenterCode", result) Then
                        sCenterCode = RTrim(result)
                    End If
                    If values.TryGetValue("Bhk", result) Then
                        sBhk = RTrim(result)
                    End If

                    If values.TryGetValue("Location", result) Then
                        sLocation = RTrim(result)
                    End If
                    If values.TryGetValue("Itemid", result) Then
                        sItemid = RTrim(result)
                    End If
                    If values.TryGetValue("ItemCode", result) Then
                        sItemCode = RTrim(result)
                    End If
                    If values.TryGetValue("Qty", result) Then
                        ssQty = RTrim(result)
                    End If
                    If values.TryGetValue("SerialNo", result) Then
                        sSerialNo = RTrim(result)
                    End If
                    If values.TryGetValue("ZaikoStatus", result) Then
                        sZaikoStatus = RTrim(result)
                    End If


                    If wstrKeyValue2(0) = RTrim(sPicsType) And _
                           wstrKeyValue2(1) = RTrim(sCstmid) And _
                           wstrKeyValue2(2) = RTrim(sCstmCode) And _
                           wstrKeyValue2(3) = RTrim(sCenterCode) And _
                           wstrKeyValue2(4) = RTrim(sBhk) And _
                           wstrKeyValue2(5) = RTrim(sLocation) And _
                           wstrKeyValue2(6) = RTrim(sItemid) And _
                           wstrKeyValue2(7) = RTrim(sItemCode) Then

                        wstrKeyValue2(8) = RTrim(sQty)
                        wstrKeyValue2(9) = RTrim(sSerialNo)

                        If G_CONST_ST_STOCK_INSTOCK = RTrim(sZaikoStatus) Or G_CONST_ST_STOCK_RESERVED1 = RTrim(sZaikoStatus) Or G_CONST_ST_STOCK_RESERVED2 = RTrim(sZaikoStatus) Then

                            wstrWhere = ""
                            wstrWhere = wstrKey2(0) & " = '" & wstrKeyValue(0) & "'"
                            wstrWhere &= " and " & wstrKey2(1) & " = '" & wstrKeyValue2(1) & "'"
                            wstrWhere &= " and " & wstrKey2(2) & " = '" & wstrKeyValue2(2) & "'"
                            wstrWhere &= " and " & wstrKey2(3) & " = '" & wstrKeyValue2(3) & "'"
                            wstrWhere &= " and " & wstrKey2(4) & " = '" & wstrKeyValue2(4) & "'"
                            wstrWhere &= " and " & wstrKey2(5) & " = '" & wstrKeyValue2(5) & "'"
                            wstrWhere &= " and " & wstrKey2(6) & " = '" & wstrKeyValue2(6) & "'"
                            wstrWhere &= " and " & wstrKey2(7) & " = '" & wstrKeyValue2(7) & "'"
                            wstrWhere &= " and " & wstrKey2(8) & " = '" & wstrKeyValue2(8) & "'"
                            wstrWhere &= " and " & wstrKey2(9) & " = '" & wstrKeyValue2(9) & "'"

                            sSQL = "UPDATE " + G_CONST_TBL_STOCK + " SET ZaikoStatus='" _
                                                                                     + G_CONST_ST_STOCK_MISSING _
                                                                                     + "' WHERE " + wstrWhere

                            affected_rows = dbClient.Execute(sSQL)


                            If affected_rows = 0 Then
                                Exit Function
                            End If


                        End If

                    Else
                        Exit Do
                    End If


                    i = i + 1

                    If tmpStr.Count = i Then
                        Exit Do
                    End If


                Loop


            Else

                Exit Function


            End If

            wstrKey3(0) = "PicsType"
            wstrKey3(1) = "Cstmid"
            wstrKey3(2) = "CstmCode"
            wstrKey3(3) = "CenterCode"
            wstrKey3(4) = "Bhk"
            wstrKey3(5) = "Itemid"
            wstrKey3(6) = "ItemCode"
            wstrKey3(7) = "ZaikoStatus"
            wstrKey3(8) = "Qty"
            wstrKey3(9) = "CartonType"
            wstrKey3(10) = "StockDate"
            wstrKey3(11) = "StockTime"
            wstrKey3(12) = "SerialNo"

            wstrWhere = ""
            wstrWhere = wstrKey3(0) & " = '" & wstrKeyValue3(0) & "'"
            wstrWhere &= " and " & wstrKey3(1) & " = '" & wstrKeyValue3(1) & "'"
            wstrWhere &= " and " & wstrKey3(2) & " = '" & wstrKeyValue3(2) & "'"
            wstrWhere &= " and " & wstrKey3(3) & " = '" & wstrKeyValue3(3) & "'"
            wstrWhere &= " and " & wstrKey3(4) & " = '" & wstrKeyValue3(4) & "'"
            wstrWhere &= " and " & wstrKey3(5) & " = '" & wstrKeyValue3(5) & "'"
            wstrWhere &= " and " & wstrKey3(6) & " = '" & wstrKeyValue3(6) & "'"
            wstrWhere &= " and " & wstrKey3(7) & " = '" & wstrKeyValue3(7) & "'"
            wstrWhere &= " and " & wstrKey3(8) & " = '" & wstrKeyValue3(8) & "'"
            wstrWhere &= " and " & wstrKey3(9) & " = '" & wstrKeyValue3(9) & "'"
            wstrWhere &= " and " & wstrKey3(10) & " = '" & wstrKeyValue3(10) & "'"
            wstrWhere &= " and " & wstrKey3(11) & " = '" & wstrKeyValue3(11) & "'"
            wstrWhere &= " and " & wstrKey3(12) & " = '" & wstrKeyValue3(12) & "'"

            tmpStr = dbClient.Query("SELECT * FROM " + G_CONST_TBL_STOCK + " WHERE " + wstrWhere)


            values = New Dictionary(Of String, String)
            sPicsType = ""
            sCstmid = ""
            sCstmCode = ""
            sCenterCode = ""
            sBhk = ""

            sLocation = ""
            sItemid = ""
            sItemCode = ""
            sQty = ""
            sSerialNo = ""
            sZaikoStatus = ""

            Dim sCartonType As String = ""
            Dim sStockDate As String = ""
            Dim sStockTime As String = ""

            i = 0

            bFound = False

            If tmpStr.Count > 0 Then

                Do



                    values = tmpStr(i)

                    Dim result As String = Nothing

                    If values.TryGetValue("PicsType", result) Then
                        sPicsType = RTrim(result)
                    End If
                    If values.TryGetValue("Cstmid", result) Then
                        sCstmid = RTrim(result)
                    End If
                    If values.TryGetValue("CstmCode", result) Then
                        sCstmCode = RTrim(result)
                    End If
                    If values.TryGetValue("CenterCode", result) Then
                        sCenterCode = RTrim(result)
                    End If
                    If values.TryGetValue("Bhk", result) Then
                        sBhk = RTrim(result)
                    End If

                    If values.TryGetValue("Location", result) Then
                        sLocation = RTrim(result)
                    End If
                    If values.TryGetValue("Itemid", result) Then
                        sItemid = RTrim(result)
                    End If
                    If values.TryGetValue("ItemCode", result) Then
                        sItemCode = RTrim(result)
                    End If
                    If values.TryGetValue("Qty", result) Then
                        ssQty = RTrim(result)
                    End If
                    If values.TryGetValue("SerialNo", result) Then
                        sSerialNo = RTrim(result)
                    End If
                    If values.TryGetValue("ZaikoStatus", result) Then
                        sZaikoStatus = RTrim(result)
                    End If

                    If values.TryGetValue("CartonType", result) Then
                        sCartonType = RTrim(result)
                    End If
                    If values.TryGetValue("StockDate", result) Then
                        sStockDate = RTrim(result)
                    End If
                    If values.TryGetValue("StockTime", result) Then
                        sStockTime = RTrim(result)
                    End If


                    If wstrKeyValue3(0) = RTrim(sPicsType) And _
                               wstrKeyValue3(1) = RTrim(sCstmid) And _
                               wstrKeyValue3(2) = RTrim(sCstmCode) And _
                               wstrKeyValue3(3) = RTrim(sCenterCode) And _
                               wstrKeyValue3(4) = RTrim(sBhk) And _
                               wstrKeyValue3(5) = RTrim(sItemid) And _
                               wstrKeyValue3(6) = RTrim(sItemCode) And _
                               wstrKeyValue3(7) = RTrim(sZaikoStatus) And _
                               wstrKeyValue3(8) = RTrim(sQty) And _
                               wstrKeyValue3(9) = RTrim(sCartonType) Then

                        wstrKeyValue3(10) = RTrim(sStockDate)
                        wstrKeyValue3(11) = RTrim(sStockTime)
                        wstrKeyValue3(12) = RTrim(sSerialNo)

                        If wstrLocation <> RTrim(sLocation) And G_CONST_ST_STOCK_INSTOCK = RTrim(sZaikoStatus) Then

                            wstrNewLocation = RTrim(sLocation)
                            wstrNewSerial = RTrim(sSerialNo)
                            wstrNewStockDate = RTrim(sStockDate)
                            wstrNewStockTime = RTrim(sStockTime)
                            bFound = True
                            Exit Do

                        End If

                    Else
                        Exit Do
                    End If

                    i = i + 1

                    If tmpStr.Count = i Then
                        Exit Do
                    End If

                Loop

            End If

            If bFound Then

                wstrKeyValue3(10) = wstrNewStockDate
                wstrKeyValue3(11) = wstrNewStockTime
                wstrKeyValue3(12) = wstrNewSerial

                wstrWhere = ""
                wstrWhere = wstrKey3(0) & " = '" & wstrKeyValue3(0) & "'"
                wstrWhere &= " and " & wstrKey3(1) & " = '" & wstrKeyValue3(1) & "'"
                wstrWhere &= " and " & wstrKey3(2) & " = '" & wstrKeyValue3(2) & "'"
                wstrWhere &= " and " & wstrKey3(3) & " = '" & wstrKeyValue3(3) & "'"
                wstrWhere &= " and " & wstrKey3(4) & " = '" & wstrKeyValue3(4) & "'"
                wstrWhere &= " and " & wstrKey3(5) & " = '" & wstrKeyValue3(5) & "'"
                wstrWhere &= " and " & wstrKey3(6) & " = '" & wstrKeyValue3(6) & "'"
                wstrWhere &= " and " & wstrKey3(7) & " = '" & wstrKeyValue3(7) & "'"
                wstrWhere &= " and " & wstrKey3(8) & " = '" & wstrKeyValue3(8) & "'"
                wstrWhere &= " and " & wstrKey3(9) & " = '" & wstrKeyValue3(9) & "'"
                wstrWhere &= " and " & wstrKey3(10) & " = '" & wstrKeyValue3(10) & "'"
                wstrWhere &= " and " & wstrKey3(11) & " = '" & wstrKeyValue3(11) & "'"
                wstrWhere &= " and " & wstrKey3(12) & " = '" & wstrKeyValue3(12) & "'"

                sSQL = "UPDATE " + G_CONST_TBL_STOCK + " SET ZaikoStatus='" _
                                                         + G_CONST_ST_STOCK_RESERVED2 _
                                                         + "' WHERE " + wstrWhere

                affected_rows = dbClient.Execute(sSQL)


                If affected_rows = 0 Then
                    Exit Function
                End If


                pstrLocation = wstrNewLocation
                pstrSerial = wstrNewSerial

                fResetStock = 0
            Else
                fResetStock = -2
            End If

            Exit Function

        Catch ex As Exception
            sErrLog(gAppName, "fResetStock", ex.ToString)
        End Try






        'Dim rtn As Integer
        'Dim bFound As Boolean
        'Dim wstrKey(2) As String
        'Dim wstrKeyValue(2) As String
        'Dim wstrKey2(9) As String
        'Dim wstrKeyValue2(9) As String
        'Dim wstrKey3(12) As String
        'Dim wstrKeyValue3(12) As String
        'Dim wstrLocation As String
        'Dim wstrQty As String
        'Dim wstrNewLocation As String
        'Dim wstrNewSerial As String
        'Dim wstrNewStockDate As String
        'Dim wstrNewStockTime As String

        'fResetStock = -1
        'Try

        '    '// PKCPฉ็๕๐๐ๆ“พ
        '    With gstrctPPDispPart
        '        wstrKey(0) = "ItemCode" : wstrKeyValue(0) = .strItemCode
        '        wstrKey(1) = "ItemInCode" : wstrKeyValue(1) = .strItemInCode
        '        wstrKey(2) = "PlanSerial" : wstrKeyValue(2) = .strSerial
        '    End With


        '    rtn = gclsDB.fOpenRecord(G_CONST_TBL_PACKCP, G_CONST_KEY_PACKCPKEY06, "PKCP01", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)

        '    If rtn = 9 Then
        '        gclsDB.fCloseRecord("PKCP01")
        '        Exit Function
        '    ElseIf rtn <> 0 Then
        '        gclsDB.fCloseRecord("PKCP01")
        '        Exit Function
        '    End If

        '    With gclsDB.Records("PKCP01")

        '        If wstrKeyValue(0) = RTrim(.Item("ItemCode")) And _
        '           wstrKeyValue(1) = RTrim(.Item("ItemInCode")) And _
        '           wstrKeyValue(2) = RTrim(.Item("PlanSerial")) Then

        '            wstrKeyValue2(0) = RTrim(.Item("PicsType"))
        '            wstrKeyValue2(1) = RTrim(.Item("Cstmid"))
        '            wstrKeyValue2(2) = RTrim(.Item("CstmCode"))
        '            wstrKeyValue2(3) = RTrim(.Item("CenterCode"))
        '            wstrKeyValue2(4) = RTrim(.Item("Bhk"))
        '            wstrKeyValue2(5) = RTrim(.Item("StockLctn"))
        '            wstrKeyValue2(6) = RTrim(.Item("ItemInID"))
        '            wstrKeyValue2(7) = RTrim(.Item("ItemInCode"))
        '            wstrKeyValue2(8) = Space(5)
        '            wstrKeyValue2(9) = Space(6)
        '            wstrLocation = RTrim(.Item("StockLctn"))
        '            wstrQty = RTrim(.Item("BunkatuJ"))

        '            wstrKeyValue3(0) = RTrim(.Item("PicsType"))
        '            wstrKeyValue3(1) = RTrim(.Item("Cstmid"))
        '            wstrKeyValue3(2) = RTrim(.Item("CstmCode"))
        '            wstrKeyValue3(3) = RTrim(.Item("CenterCode"))
        '            wstrKeyValue3(4) = RTrim(.Item("Bhk"))
        '            wstrKeyValue3(5) = RTrim(.Item("ItemInID"))
        '            wstrKeyValue3(6) = RTrim(.Item("ItemInCode"))
        '            wstrKeyValue3(7) = "3"
        '            wstrKeyValue3(8) = RTrim(.Item("BunkatuJ"))
        '            wstrKeyValue3(9) = RTrim(.Item("ItemMCode"))
        '            wstrKeyValue3(10) = Space(8)
        '            wstrKeyValue3(11) = Space(4)
        '            wstrKeyValue3(12) = Space(6)

        '        Else
        '            gclsDB.fCloseRecord("PKCP01")
        '            Exit Function
        '        End If

        '    End With

        '    gclsDB.fCloseRecord("PKCP01")

        '    '// ปฬP[V“ฬษฬXe[^XXV
        '    wstrKey2(0) = "PicsType"
        '    wstrKey2(1) = "Cstmid"
        '    wstrKey2(2) = "CstmCode"
        '    wstrKey2(3) = "CenterCode"
        '    wstrKey2(4) = "Bhk"
        '    wstrKey2(5) = "Location"
        '    wstrKey2(6) = "Itemid"
        '    wstrKey2(7) = "ItemCode"
        '    wstrKey2(8) = "Qty"
        '    wstrKey2(9) = "SerialNo"

        '    rtn = gclsDB.fOpenRecord(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY01, "STOCK1", wstrKey2, wstrKeyValue2, BtLib.Operation.GetGreaterOrEqual)

        '    If rtn = 9 Then
        '        gclsDB.fCloseRecord("STOCK1")
        '        Exit Function
        '    ElseIf rtn <> 0 Then
        '        gclsDB.fCloseRecord("STOCK1")
        '        Exit Function
        '    End If

        '    bFound = False
        '    If rtn = 0 Then
        '        Do
        '            With gclsDB.Records("STOCK1")
        '                If wstrKeyValue2(0) = RTrim(.Item("PicsType")) And _
        '                   wstrKeyValue2(1) = RTrim(.Item("Cstmid")) And _
        '                   wstrKeyValue2(2) = RTrim(.Item("CstmCode")) And _
        '                   wstrKeyValue2(3) = RTrim(.Item("CenterCode")) And _
        '                   wstrKeyValue2(4) = RTrim(.Item("Bhk")) And _
        '                   wstrKeyValue2(5) = RTrim(.Item("Location")) And _
        '                   wstrKeyValue2(6) = RTrim(.Item("Itemid")) And _
        '                   wstrKeyValue2(7) = RTrim(.Item("ItemCode")) Then

        '                    wstrKeyValue2(8) = RTrim(.Item("Qty"))
        '                    wstrKeyValue2(9) = RTrim(.Item("SerialNo"))

        '                    If G_CONST_ST_STOCK_INSTOCK = RTrim(.Item("ZaikoStatus")) Or G_CONST_ST_STOCK_RESERVED1 = RTrim(.Item("ZaikoStatus")) Or G_CONST_ST_STOCK_RESERVED2 = RTrim(.Item("ZaikoStatus")) Then
        '                        '// DBXV
        '                        With gclsDB
        '                            .Setcolumn = "ZaikoStatus"
        '                            .SetData = G_CONST_ST_STOCK_MISSING

        '                            If .fUpdRecords(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY00, wstrKey2, wstrKeyValue2) = -1 Then
        '                                gclsDB.fCloseRecord("STOCK1")
        '                                Exit Function
        '                            End If
        '                        End With
        '                    End If

        '                Else
        '                    Exit Do
        '                End If

        '                'R[h๐ๆ“พท้
        '                If gclsDB.Records("STOCK1").Read(BtLib.Operation.GetNext) <> 0 Then
        '                    'R[hชศญศ๊ฮI—นท้
        '                    Exit Do
        '                End If

        '            End With
        '        Loop
        '    End If

        '    gclsDB.fCloseRecord("STOCK1")

        '    '// ‘ใ‘ึฬXgbNฬ๕
        '    wstrKey3(0) = "PicsType"
        '    wstrKey3(1) = "Cstmid"
        '    wstrKey3(2) = "CstmCode"
        '    wstrKey3(3) = "CenterCode"
        '    wstrKey3(4) = "Bhk"
        '    wstrKey3(5) = "Itemid"
        '    wstrKey3(6) = "ItemCode"
        '    wstrKey3(7) = "ZaikoStatus"
        '    wstrKey3(8) = "Qty"
        '    wstrKey3(9) = "CartonType"
        '    wstrKey3(10) = "StockDate"
        '    wstrKey3(11) = "StockTime"
        '    wstrKey3(12) = "SerialNo"

        '    rtn = gclsDB.fOpenRecord(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY06, "STOCK1", wstrKey3, wstrKeyValue3, BtLib.Operation.GetGreaterOrEqual)

        '    If rtn = 9 Then
        '        gclsDB.fCloseRecord("STOCK1")
        '        Exit Function
        '    ElseIf rtn <> 0 Then
        '        gclsDB.fCloseRecord("STOCK1")
        '        Exit Function
        '    End If

        '    bFound = False
        '    If rtn = 0 Then
        '        Do
        '            With gclsDB.Records("STOCK1")
        '                If wstrKeyValue3(0) = RTrim(.Item("PicsType")) And _
        '                   wstrKeyValue3(1) = RTrim(.Item("Cstmid")) And _
        '                   wstrKeyValue3(2) = RTrim(.Item("CstmCode")) And _
        '                   wstrKeyValue3(3) = RTrim(.Item("CenterCode")) And _
        '                   wstrKeyValue3(4) = RTrim(.Item("Bhk")) And _
        '                   wstrKeyValue3(5) = RTrim(.Item("Itemid")) And _
        '                   wstrKeyValue3(6) = RTrim(.Item("ItemCode")) And _
        '                   wstrKeyValue3(7) = RTrim(.Item("ZaikoStatus")) And _
        '                   wstrKeyValue3(8) = RTrim(.Item("Qty")) And _
        '                   wstrKeyValue3(9) = RTrim(.Item("CartonType")) Then

        '                    wstrKeyValue3(10) = RTrim(.Item("StockDate"))
        '                    wstrKeyValue3(11) = RTrim(.Item("StockTime"))
        '                    wstrKeyValue3(12) = RTrim(.Item("SerialNo"))

        '                    If wstrLocation <> RTrim(.Item("Location")) And G_CONST_ST_STOCK_INSTOCK = RTrim(.Item("ZaikoStatus")) Then

        '                        wstrNewLocation = RTrim(.Item("Location"))
        '                        wstrNewSerial = RTrim(.Item("SerialNo"))
        '                        wstrNewStockDate = RTrim(.Item("StockDate"))
        '                        wstrNewStockTime = RTrim(.Item("StockTime"))
        '                        bFound = True
        '                        Exit Do

        '                    End If

        '                Else
        '                    Exit Do
        '                End If

        '                'R[h๐ๆ“พท้
        '                If gclsDB.Records("STOCK1").Read(BtLib.Operation.GetNext) <> 0 Then
        '                    'R[hชศญศ๊ฮI—นท้
        '                    Exit Do
        '                End If

        '            End With
        '        Loop
        '    End If

        '    gclsDB.fCloseRecord("STOCK1")

        '    '// ‘ใ‘ึฬXgbNฬXV
        '    If bFound Then

        '        wstrKeyValue3(10) = wstrNewStockDate
        '        wstrKeyValue3(11) = wstrNewStockTime
        '        wstrKeyValue3(12) = wstrNewSerial
        '        With gclsDB
        '            .Setcolumn = "ZaikoStatus"
        '            .SetData = G_CONST_ST_STOCK_RESERVED2

        '            If .fUpdRecords(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY06, wstrKey3, wstrKeyValue3) = -1 Then
        '                Exit Function
        '            End If
        '        End With

        '        pstrLocation = wstrNewLocation
        '        pstrSerial = wstrNewSerial

        '        fResetStock = 0
        '    Else
        '        fResetStock = -2
        '    End If

        '    Exit Function

        'Catch ex As Exception
        '    sErrLog(gAppName, "fResetStock", ex.ToString)
        'End Try

    End Function

    '/*
    '|| ึ”–ผFfSetPKCP
    '|| T—v  F
    '*/
    Public Function fSetPKCP(ByVal pstrLocation As String, ByVal pstrSerial As String) As Integer

        Dim rtn As Integer
        Dim wstrKey(2) As String
        Dim wstrKeyValue(2) As String
        Dim wstrLocation As String
        Dim wstrWhere As String
        Dim sSQL As String

        Try

            fSetPKCP = -1

            '// PKCPฉ็๕๐๐ๆ“พ
            With gstrctPPDispPart
                wstrKey(0) = "ItemCode" : wstrKeyValue(0) = .strItemCode
                wstrKey(1) = "ItemInCode" : wstrKeyValue(1) = .strItemInCode
                wstrKey(2) = "PlanSerial" : wstrKeyValue(2) = .strSerial
                wstrLocation = .strLocation
            End With

            wstrWhere = ""
            wstrWhere = wstrKey(0) & " = '" & wstrKeyValue(0) & "'"
            wstrWhere &= " and " & wstrKey(1) & " = '" & wstrKeyValue(1) & "'"
            wstrWhere &= " and " & wstrKey(2) & " = '" & wstrKeyValue(2) & "'"

            sSQL = "UPDATE " + G_CONST_TBL_PACKCP + " SET StockLctn='" _
                                                             + pstrLocation _
                                                             + "' , PlanSerial='" _
                                                             + pstrSerial _
                                                             + "' WHERE " + wstrWhere


            affected_rows = dbClient.Execute(sSQL)


            If affected_rows = 0 Then
                Exit Function
            End If

            'With gclsDB
            '    .Setcolumn = "StockLctn,PlanSerial"
            '    .SetData = pstrLocation & "," & pstrSerial

            '    If .fUpdRecords(G_CONST_TBL_PACKCP, G_CONST_KEY_PACKCPKEY06, wstrKey, wstrKeyValue) = -1 Then
            '        Exit Function
            '    End If
            'End With

            gstrctPPDisp.fSetQty(gstrctPPDisp.strCrtnNo & wstrLocation & gstrctPPDispPart.strItemInCode, (0 - CInt(gstrctPPDispPart.strPackQty)))
            gstrctPPDisp.fSetPkg(gstrctPPDisp.strCrtnNo & wstrLocation & gstrctPPDispPart.strItemInCode, -1)
            gstrctPPDisp.fSetQty(gstrctPPDisp.strCrtnNo & pstrLocation & gstrctPPDispPart.strItemInCode, CInt(gstrctPPDispPart.strPackQty))
            gstrctPPDisp.fSetPkg(gstrctPPDisp.strCrtnNo & pstrLocation & gstrctPPDispPart.strItemInCode, 1)

            fSetPKCP = 0

            Exit Function

        Catch ex As Exception
            sErrLog(gAppName, "fSetPKCP", ex.ToString)
        End Try



        'Dim rtn As Integer
        'Dim wstrKey(2) As String
        'Dim wstrKeyValue(2) As String
        'Dim wstrLocation As String

        'Try

        '    fSetPKCP = -1

        '    '// PKCPฉ็๕๐๐ๆ“พ
        '    With gstrctPPDispPart
        '        wstrKey(0) = "ItemCode" : wstrKeyValue(0) = .strItemCode
        '        wstrKey(1) = "ItemInCode" : wstrKeyValue(1) = .strItemInCode
        '        wstrKey(2) = "PlanSerial" : wstrKeyValue(2) = .strSerial
        '        wstrLocation = .strLocation
        '    End With

        '    With gclsDB
        '        .Setcolumn = "StockLctn,PlanSerial"
        '        .SetData = pstrLocation & "," & pstrSerial

        '        If .fUpdRecords(G_CONST_TBL_PACKCP, G_CONST_KEY_PACKCPKEY06, wstrKey, wstrKeyValue) = -1 Then
        '            Exit Function
        '        End If
        '    End With

        '    gstrctPPDisp.fSetQty(gstrctPPDisp.strCrtnNo & wstrLocation & gstrctPPDispPart.strItemInCode, (0 - CInt(gstrctPPDispPart.strPackQty)))
        '    gstrctPPDisp.fSetPkg(gstrctPPDisp.strCrtnNo & wstrLocation & gstrctPPDispPart.strItemInCode, -1)
        '    gstrctPPDisp.fSetQty(gstrctPPDisp.strCrtnNo & pstrLocation & gstrctPPDispPart.strItemInCode, CInt(gstrctPPDispPart.strPackQty))
        '    gstrctPPDisp.fSetPkg(gstrctPPDisp.strCrtnNo & pstrLocation & gstrctPPDispPart.strItemInCode, 1)

        '    fSetPKCP = 0

        '    Exit Function

        'Catch ex As Exception
        '    sErrLog(gAppName, "fSetPKCP", ex.ToString)
        'End Try

    End Function

    Private Sub frm7E45_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim rtn As Integer

        Select Case e.KeyCode.ToString()
            Case "Return"
                rtn = fSearchStock()
                If rtn = -1 Then
                    'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, G_CONST_EM_DB, MWA_DB_WARNING, MWA_DB_OK)
                    'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(gclsDB.BtriveStatus, gclsDB.ErrFile), MWA_DB_WARNING, MWA_DB_OK)
                    MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                ElseIf rtn = -2 Then
                    gintStatus = OTR_STATUS.NOTFOUND
                    Me.Close()
                Else
                    gintStatus = OTR_STATUS.PART
                    Me.Close()
                End If

                Exit Sub
            Case "F1"
                gintStatus = OTR_STATUS.PART
                Me.Close()
                Exit Sub
            Case "F2"
        End Select
    End Sub
End Class
