﻿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 Bt
Imports Bt.ScanLib
'Imports KDPL1000.BtScanLib
Imports BTClientLib
Imports System.IO
Imports System.Reflection
Imports Newtonsoft
Imports Microsoft.WindowsCE.Forms
Imports BtScan2

Public Class frm1015
    Inherits System.Windows.Forms.Form
    Private btScanLib As BtScanLib
    Private scanHandler As Boolean = False
    '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 WarpFrame1 As WarpControl.WarpFrame
    'Friend WithEvents WarpBox1 As WarpControl.WarpBox
    'Friend WithEvents wfName As WarpControl.WarpField
    'Friend WithEvents WarpHWBcr1 As WarpControl.WarpHWBcr
    'Friend WithEvents wfInput As WarpControl.WarpField
    'Friend WithEvents WarpBox2 As WarpControl.WarpBox
    '<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    '    Me.WarpFrame1 = New WarpControl.WarpFrame()
    '    Me.WarpBox2 = New WarpControl.WarpBox()
    '    Me.wfInput = New WarpControl.WarpField()
    '    Me.wfName = New WarpControl.WarpField()
    '    Me.WarpBox1 = New WarpControl.WarpBox()
    '    Me.WarpHWBcr1 = New WarpControl.WarpHWBcr()
    '    Me.WarpFrame1.SuspendLayout()
    '    Me.SuspendLayout()
    '
    'WarpFrame1
    '
    'Me.WarpFrame1.Controls.Add(Me.WarpBox2)
    'Me.WarpFrame1.Controls.Add(Me.wfInput)
    'Me.WarpFrame1.Controls.Add(Me.wfName)
    'Me.WarpFrame1.Controls.Add(Me.WarpBox1)
    '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 = ""
    '
    'WarpBox2
    '
    'Me.WarpBox2.BoxType = WarpControl.BoxTypes.Box
    'Me.WarpBox2.DrawMode = WarpControl.DrawModes.Paint
    'Me.WarpBox2.Font = New System.Drawing.Font("lr SVbN", 33.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel)
    'Me.WarpBox2.ForeColor = CType(15, Short)
    'Me.WarpBox2.Image = ""
    'Me.WarpBox2.Location = New System.Drawing.Point(0, 36)
    'Me.WarpBox2.Name = "WarpBox2"
    'Me.WarpBox2.Size = New System.Drawing.Size(240, 96)
    'Me.WarpBox2.TabIndex = 48
    '
    '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.ForeColor = CType(15, Short)
    'Me.wfInput.Format = ""
    'Me.wfInput.InputMode.Secret = True
    'Me.wfInput.Length = CType(1, Short)
    'Me.wfInput.Location = New System.Drawing.Point(192, 138)
    '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(47, Short)
    '
    '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(1, 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 = "C:\H081-0005.jpg"
    'Me.WarpBox1.Location = New System.Drawing.Point(0, 192)
    'Me.WarpBox1.Name = "WarpBox1"
    'Me.WarpBox1.Size = New System.Drawing.Size(240, 24)
    'Me.WarpBox1.TabIndex = 0
    'Me.WarpBox1.Text = "Echo System"
    '
    'WarpHWBcr1
    '
    'Me.WarpHWBcr1.Decode.Code39 = 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)
    '
    'frm1015
    '
    '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 = "frm1015"
    'Me.Text = "Form1"
    'Me.WarpFrame1.ResumeLayout(False)
    'Me.ResumeLayout(False)

    'End Sub

#End Region
    Public Enum M1015_STATUS As Integer
        INIT = 0
        MENUBACK = 1
        NOTA = 2
        NOT31 = 3
        DIFPLAN = 4
        DONE = 5
        PCSCAN = 6
        RDYPRT = 7
        PRINT = 8
        PRTNOW = 9
        CHKEND = 10
        PKGEND = 11
        CHKBGN = 12
        SHUTDOWN = 13
        PALTOVER = 14
    End Enum

    Public mint1015status As Integer

    Private Sub frm1015_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode.ToString()
            Case "Return"
                Exit Sub
            Case "F1"
            Case "F2"
                gintStatus = PL_STATUS.QUIT
                Me.Close()
                Exit Sub
        End Select
    End Sub

    Private Sub frm1015_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 = 0

        Try
            mint1015status = M1015_STATUS.INIT

            '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_H1010, WarpBox1)
#End If
            sDisp1015()
            ' 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
            sErrLog(gAppName, "frm1015_Load", ex.ToString)
        End Try

    End Sub

    '/*
    '|| ึ”–ผFsDisp1015
    '|| T—v  F1015ๆ–ส•\ฆ
    '*/
    Private Sub sDisp1015()

        'wfName.Puts(gstrctKDHT.strUserName)

        wfName.Text = gstrctKDHT.strUserName

    End Sub

    Private Sub btScanLib_OnScan(ByVal result As String)
        Dim buf As String = result
        Dim rtn As Integer

        If (Not fCheckCode(buf)) Then
            sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E1E10)
#End If
            Exit Sub
        End If

        '“—อ—
        rtn = fProcInput(RTrim(buf))

        If rtn = 0 Then
            Select Case mint1015status
                Case M1015_STATUS.INIT

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = PL_STATUS.PALETTESCN
                    Me.Close()
                    Exit Sub

                Case M1015_STATUS.MENUBACK          '–ผD“—อึ

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = PL_STATUS.SECTION
                    Me.Close()
                    Exit Sub

                Case M1015_STATUS.PALTOVER
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                        fFnc_WrpMsg("E", G_CONST_MID_E1E19)
#End If

                Case M1015_STATUS.PKGEND
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                        fFnc_WrpMsg("E", G_CONST_MID_E1E40)
#End If
                Case M1015_STATUS.CHKEND
#If Not Debug Then
                                        fFnc_WrpMsg("E", G_CONST_MID_E1E20)
#End If
                Case M1015_STATUS.PRTNOW
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                        fFnc_WrpMsg("E", G_CONST_MID_E1E16)
#End If
                Case M1015_STATUS.NOT31              '31…ศOศฬลดืฐาฏพฐผ()
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                    sOutputLog(gstrctPicsIni.strOutput_KDPL, "KDPL1E15", G_CONST_BC_PL, buf)
#If Not Debug Then
                                        rtn = fFnc_WrpMsg("E", G_CONST_MID_E1E10) '31…ศOฝทฌ
#End If
                Case M1015_STATUS.NOTA               '‘ไ”ยxศOฝทฌดืฐาฏพฐผ
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                    sOutputLog(gstrctPicsIni.strOutput_KDPL, "KDPL1E15", G_CONST_BC_PL, buf)
#If Not Debug Then
                                        fFnc_WrpMsg("E", G_CONST_MID_E1E10) '‘ไ”ยxศOชฝทฌ
#End If
                Case M1015_STATUS.DIFPLAN           '•sฎศvๆ
                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
                    sOutputLog(gstrctPicsIni.strOutput_KDPL, "KDPL1E17", G_CONST_BC_PL, buf)
#If Not Debug Then
                                        fFnc_WrpMsg("E", G_CONST_MID_H1016, "ND", 4)
#End If
                Case M1015_STATUS.DONE              'ณํษQฦลซฝ

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = PL_STATUS.ENTKEY
                    Me.Close()
                    Exit Sub

                Case M1015_STATUS.PCSCAN            '‘ผสรจลf[^์ฌฯ

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = PL_STATUS.PCSCAN
                    Me.Close()
                    Exit Sub

                Case M1015_STATUS.CHKBGN

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = PL_STATUS.CHKBGN
                    Me.Close()
                    Exit Sub

                    'Case M1015_STATUS.PRINT

                    '    gintStatus = PL_STATUS.PRINT

                Case M1015_STATUS.RDYPRT

                    RemoveHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
                    btScanLib.Disable()

                    gintStatus = PL_STATUS.RDYPRINT
                    Me.Close()
                    Exit Sub

                Case M1015_STATUS.SHUTDOWN
                    gintStatus = PL_STATUS.QUIT
                    Me.Close()
                    Exit Sub

            End Select

        Else

            '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)
            'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
            MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

        End If


    End Sub
    ' t[€EC“
    Public Sub sMain()

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

        '        Try

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

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

        '                If WKey = WKY_ENT Then

        '                ElseIf WKey = WKY_BCR Then

        '                    'สฐบฐฤ“—อฬๆ“พ
        '                    rtn = WarpHWBcr1.Get(buf)
        '                    sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.OK)
        '                    If (rtn <> 0) Then
        '                        Err.Raise(vbObjectError + 1, gAppName, "WarpError")
        '                    End If

        '                    'มชฏธรจผฏฤ
        '                    If (Not fCheckCode(buf)) Then
        '                        sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                        fFnc_WrpMsg("E", G_CONST_MID_E1E10)
        '#End If
        '                        Exit Do
        '                    End If

        '                    '“—อ—
        '                    rtn = fProcInput(RTrim(buf))

        '                    If rtn = 0 Then
        '                        Select Case mint1015status
        '                            Case M1015_STATUS.INIT

        '                                gintStatus = PL_STATUS.PALETTESCN

        '                            Case M1015_STATUS.MENUBACK          '–ผD“—อึ

        '                                gintStatus = PL_STATUS.SECTION

        '                            Case M1015_STATUS.PALTOVER
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E1E19)
        '#End If

        '                            Case M1015_STATUS.PKGEND
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E1E40)
        '#End If
        '                            Case M1015_STATUS.CHKEND
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E1E20)
        '#End If
        '                            Case M1015_STATUS.PRTNOW
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E1E16)
        '#End If
        '                            Case M1015_STATUS.NOT31              '31…ศOศฬลดืฐาฏพฐผ()
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '                                sOutputLog(gstrctPicsIni.strOutput_KDPL, "KDPL1E15", G_CONST_BC_PL, buf)
        '#If Not Debug Then
        '                                rtn = fFnc_WrpMsg("E", G_CONST_MID_E1E10) '31…ศOฝทฌ
        '#End If
        '                            Case M1015_STATUS.NOTA               '‘ไ”ยxศOฝทฌดืฐาฏพฐผ
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '                                sOutputLog(gstrctPicsIni.strOutput_KDPL, "KDPL1E15", G_CONST_BC_PL, buf)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_E1E10) '‘ไ”ยxศOชฝทฌ
        '#End If
        '                            Case M1015_STATUS.DIFPLAN           '•sฎศvๆ
        '                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
        '                                sOutputLog(gstrctPicsIni.strOutput_KDPL, "KDPL1E17", G_CONST_BC_PL, buf)
        '#If Not Debug Then
        '                                fFnc_WrpMsg("E", G_CONST_MID_H1016, "ND", 4)
        '#End If
        '                            Case M1015_STATUS.DONE              'ณํษQฦลซฝ

        '                                gintStatus = PL_STATUS.ENTKEY

        '                            Case M1015_STATUS.PCSCAN            '‘ผสรจลf[^์ฌฯ

        '                                gintStatus = PL_STATUS.PCSCAN

        '                            Case M1015_STATUS.CHKBGN

        '                                gintStatus = PL_STATUS.CHKBGN

        '                                'Case M1015_STATUS.PRINT

        '                                '    gintStatus = PL_STATUS.PRINT

        '                            Case M1015_STATUS.RDYPRT

        '                                gintStatus = PL_STATUS.RDYPRINT

        '                            Case M1015_STATUS.SHUTDOWN
        '                                gintStatus = PL_STATUS.QUIT

        '                        End Select

        '                    Else

        '                        '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)
        '                        f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)

        '                    End If

        '                    Exit Do

        '                ElseIf WKey = WKY_F1 Then


        '                ElseIf WKey = WKY_F2 Then
        '                    'gintStatus = PL_STATUS.QUIT
        '                    'Exit Do

        '                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

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

    End Sub
    '/*
    '|| ึ”–ผFfProcInput
    '|| T—v  F“—อ—
    '*/
    Private Function fProcInput(ByVal pstrInput As String) As Integer
        Dim rtn, rtn2 As Integer
        Dim wstrEmplCode As String
        Dim wintCnt As Integer
        Dim wintCnt2 As Integer
        Dim wstrWhere As String
        Dim wstrKey(0) As String, wstrKeyValue(0) As String
        Dim wstrctSPM As STRCT_KEY_SPM2
        Dim wstrctPACKAP() As STRCT_PACKAP_DATA

        fProcInput = -1

        Try

            If pstrInput = "OCMENUBACK" Then             'OCMENUBACKศ็ฮ

                mint1015status = M1015_STATUS.MENUBACK
                fProcInput = 0
                Exit Function
            ElseIf pstrInput = "OCAPRINT" Then

                mint1015status = M1015_STATUS.PRINT
                fProcInput = 0
                Exit Function

            ElseIf pstrInput = "OCSHUTDOWN" Then             'OCSHUTDOWNศ็ฮ
                mint1015status = M1015_STATUS.SHUTDOWN
                fProcInput = 0
                fMakeCommandFile(G_CONST_PROCESS_SHUTDOWN)
                Exit Function

            ElseIf pstrInput.StartsWith("A") OrElse pstrInput.StartsWith("L") Then   'ๆ“ชชAฝอLศ็ฮ

                If pstrInput.Length <> 31 Then             '31…ศOศ็ฮ
                    mint1015status = M1015_STATUS.NOT31
                    fProcInput = 0
                    Exit Function
                End If

                sDecodeScanData2AicCase(gstrctAicCase, pstrInput)
                gstrctAicCase.KeyData = Mid(pstrInput, 2)
                Do
                    'PLAN“ว—
                    rtn = fSearchPlan(wstrctSPM)
                    Select Case rtn
                        Case 3
                            gstrctPLDispHead.initialize()
                            gstrctPLDispData.initialize()
                            If gstrctKDHT.bLinkFlag Then
                                'ฯoพธผฎฬมชฏธ
                                rtn2 = fUpdCryOutSection()
                                Select Case rtn2
                                    Case -1
                                        Exit Function
                                    Case 1
                                        'BtLib.Transaction.BeginConCurrent()
                                        'pbg”มZ
                                        rtn2 = fUpdPaletteQty(1)
                                        mint1015status = M1015_STATUS.DONE
                                        If fSetPlan("3", "", 1) = -1 Then
                                            'BtLib.Transaction.Abort()
                                            Exit Function
                                        End If
                                        fMakeCommandFile(G_CONST_PROCESS_PAL_DSP, G_CONST_SPEECH_STRTPAL)
                                        sBeepSound(modPublicHT.BEEP_TYPE.B, modPublicHT.BEEP_SELECTION.NG)
                                        'BtLib.Transaction.End()
                                        Exit Do
                                End Select
                            End If
                            'rtn2 = fSearchPackAP2("1", "1")
                            rtn2 = fSearchPackAP3("1")
                            If rtn2 = 1 OrElse rtn2 = 0 Then
                                mint1015status = M1015_STATUS.PCSCAN
                                fMakeCommandFile(G_CONST_PROCESS_PAL_DSP, G_CONST_SPEECH_STRTPAL)
                                Exit Do
                            ElseIf rtn2 <> 0 Then
                                'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                                Exit Function
                            End If

                        Case 0 'รฐภ์ฌJn
                            If gstrctKDHT.bLinkFlag Then
                                fMakeCommandFile(G_CONST_PROCESS_VOICE, G_CONST_SPEECH_NOTPALT)
                                sBeepSound(modPublicHT.BEEP_TYPE.A, modPublicHT.BEEP_SELECTION.NG)
#If Not Debug Then
                                fFnc_WrpMsg("E", G_CONST_MID_E1E14) 'p^CYJn•sย
#End If
                                mint1015status = M1015_STATUS.INIT
                                Exit Do
                            End If
                            '•”•i/ถฐฤฝทฌษเ“ฏ—lฬWbNช•K—vลทB
                            'BtLib.Transaction.BeginConCurrent()
                            'pbg”มZ
                            rtn2 = fUpdPaletteQty(1)
                            If rtn2 = -1 Then
                                'BtLib.Transaction.Abort()
                                'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                                MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                                Exit Function
                            ElseIf rtn2 = 0 Then  '์ฦส฿ฺฏฤ”ตฐสฐ
                                'BtLib.Transaction.Abort()
                                mint1015status = M1015_STATUS.PALTOVER
                                fProcInput = 0
                                Exit Function
                            End If
                            If fSetPlan("1", "", 1) = -1 Then
                                'BtLib.Transaction.Abort()
                                Exit Function
                            End If
                            'BtLib.Transaction.End()
#If Not Debug Then
                            sSetImgFName(G_CONST_MID_H1015, WarpBox1)
                            WarpFrame1.Flush()
#End If
                            gstrctPLDispHead.initialize()
                            gstrctPLDispData.initialize()
                            'BtLib.Transaction.BeginConCurrent()
                            rtn2 = fMakePackAP(wstrctSPM)
                            If rtn2 = -1 OrElse rtn2 = 0 Then
                                'ฬงฒูดืฐ
                                'BtLib.Transaction.Abort()
                                'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                                Exit Function
                            ElseIf rtn2 = -2 Then 'l”ฑฯฏมดืฐ
                                'BtLib.Transaction.Abort()
                                mint1015status = M1015_STATUS.DIFPLAN
                                fProcInput = 0
                                Exit Function
                            End If
                            'BtLib.Transaction.End()
                            mint1015status = M1015_STATUS.DONE
                            fMakeCommandFile(G_CONST_PROCESS_PAL_DSP, G_CONST_SPEECH_STRTPAL)
                            sBeepSound(modPublicHT.BEEP_TYPE.B, modPublicHT.BEEP_SELECTION.NG)
                            Exit Do

                        Case 1 '‘ผสรจษฤรฐภ์ฌ’
#If Not Debug Then
                            sSetImgFName(G_CONST_MID_H1020, WarpBox2)
                            WarpFrame1.Flush()
#End If
                            System.Threading.Thread.Sleep(2000)
                        Case 2 'xแฯ
                            mint1015status = M1015_STATUS.CHKBGN
                            fProcInput = 0
                            Exit Function

                        Case 4 'มชฏธฎ—น
                            mint1015status = M1015_STATUS.RDYPRT
                            Exit Do

                        Case 30 '‘ผสรจล๓’
                            mint1015status = M1015_STATUS.PRTNOW
                            Exit Do

                        Case 5 'oืฎ—น
                            mint1015status = M1015_STATUS.PKGEND
                            Exit Do

                        Case -1
                            'BtLib.Transaction.Abort()
                            Exit Function
                    End Select
                Loop
                gstrctKDPL.strSearchKey = gstrctAicCase.KeyData
                'HTCTRLษ—f[^๐’วม
                'BtLib.Transaction.BeginConCurrent()
                rtn = fUpdateHTCTRL4PL()
                If rtn = -1 Then
                    'BtLib.Transaction.Abort()
                    'f0.WarpCtrl1.MessageBox(G_CONST_EMT_DB, fEditErrorInfo(RTrim(gclsDB.BtriveStatus), RTrim(gclsDB.ErrFile)), MWA_DB_WARNING, MWA_DB_OK)
                    MessageBox.Show(G_CONST_EM_DB, G_CONST_EMT_DB, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                    Exit Function
                End If
                'BtLib.Transaction.End()
            Else
                mint1015status = M1015_STATUS.NOTA
            End If

            fProcInput = 0

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

    Private Function fUpdCryOutSection() As Integer
        Dim wstrKey(6), wstrKeyValue(6) As String
        Dim wintRet As Integer = -1
        Dim wstrWhere As String
        fUpdCryOutSection = -1
        wstrKey(0) = "PaletteID" : wstrKeyValue(0) = "A"
        wstrKey(1) = "ItemGr" : wstrKeyValue(1) = gstrctAicCase.AicCaseNo & gstrctAicCase.YYYYMMDD
        wstrKey(2) = "ItemID" : wstrKeyValue(2) = Space(1)
        wstrKey(3) = "ItemCode" : wstrKeyValue(3) = Space(30)
        wstrKey(4) = "ItemInID" : wstrKeyValue(4) = Space(1)
        wstrKey(5) = "ItemInCode" : wstrKeyValue(5) = Space(30)
        wstrKey(6) = "TempSerial" : wstrKeyValue(6) = Space(6)
        gstrctPLDispHead.intNumOfPkg = 0

        Try
            wstrWhere = ""
            wstrWhere = wstrKey(0) & " = '" & wstrKeyValue(0) & "'"
            wstrWhere &= " and " & wstrKey(1) & " = '" & wstrKeyValue(1) & "'"
            wstrWhere &= " and " & wstrKey(2) & " = '" & wstrKeyValue(2) & "'"
            wstrWhere &= " and " & wstrKey(3) & " = '" & wstrKeyValue(3) & "'"
            wstrWhere &= " and " & wstrKey(4) & " = '" & wstrKeyValue(4) & "'"
            wstrWhere &= " and " & wstrKey(5) & " = '" & wstrKeyValue(5) & "'"
            wstrWhere &= " and " & wstrKey(6) & " = '" & wstrKeyValue(6) & "'"

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

            Dim values As Dictionary(Of String, String) = New Dictionary(Of String, String)
            Dim sPaletteID As String = ""
            Dim sItemGr As String = ""
            Dim sCarryOutSection As String = ""
            Dim sItemID As String = ""
            Dim sItemCode As String = ""
            Dim sItemInID As String = ""
            Dim sItemInCode As String = ""
            Dim sTempSerial As String = ""
            Dim i As Integer = 0

            If tmpStr.Count > 0 Then
                Try

                    Do
                        values = tmpStr(i)

                        Dim result As String = Nothing
                        If values.TryGetValue("PaletteID", result) Then
                            sPaletteID = RTrim(result)
                        End If
                        If values.TryGetValue("ItemGr", result) Then
                            sItemGr = RTrim(result)
                        End If
                        If values.TryGetValue("CarryOutSection", result) Then
                            sCarryOutSection = 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("ItemInID", result) Then
                            sItemInID = RTrim(result)
                        End If
                        If values.TryGetValue("ItemInCode", result) Then
                            sItemInCode = RTrim(result)
                        End If
                        If values.TryGetValue("TempSerial", result) Then
                            sTempSerial = RTrim(result)
                        End If

                        If RTrim(sPaletteID) = "A" AndAlso RTrim(sItemGr) = wstrKeyValue(1) Then
                            If RTrim(sCarryOutSection) = "????????" OrElse RTrim(sCarryOutSection) = "" Then


                                wstrKeyValue(2) = RTrim(sItemID)
                                wstrKeyValue(3) = RTrim(sItemCode)
                                wstrKeyValue(4) = RTrim(sItemInID)
                                wstrKeyValue(5) = RTrim(sItemInCode)
                                wstrKeyValue(6) = RTrim(sTempSerial)

                                wstrWhere = ""
                                wstrWhere = wstrKey(0) & " = '" & wstrKeyValue(0) & "'"
                                wstrWhere &= " and " & wstrKey(1) & " = '" & wstrKeyValue(1) & "'"
                                wstrWhere &= " and " & wstrKey(2) & " = '" & wstrKeyValue(2) & "'"
                                wstrWhere &= " and " & wstrKey(3) & " = '" & wstrKeyValue(3) & "'"
                                wstrWhere &= " and " & wstrKey(4) & " = '" & wstrKeyValue(4) & "'"
                                wstrWhere &= " and " & wstrKey(5) & " = '" & wstrKeyValue(5) & "'"
                                wstrWhere &= " and " & wstrKey(6) & " = '" & wstrKeyValue(6) & "'"

                                affected_rows = dbClient.Execute("UPDATE " + G_CONST_TBL_SECTION + " SET CarryOutSectionId='" _
                                                                 + gstrctKDPL.strSectionID _
                                                                 + "' , CarryOutSection='" _
                                                                 + gstrctKDPL.strSection _
                                                                 + "' WHERE " + wstrWhere)

                                If affected_rows = 0 Then
                                    fUpdCryOutSection = -1
                                    Exit Function
                                End If
                                gstrctPLDispHead.intNumOfPkg += 1
                                fUpdCryOutSection = 1

                            End If

                        Else

                            Exit Do

                        End If
                        i = i + 1

                        If tmpStr.Count = i Then
                            Exit Do
                        End If

                    Loop
                    If fSetPlan("3", CStr(gstrctPLDispHead.intNumOfPkg).PadLeft(4, "0")) = -1 Then
                        fUpdCryOutSection = -1
                    End If

                Catch ex As Exception
                    fUpdCryOutSection = -1
                    Throw ex
                Finally

                End Try

            Else
                fUpdCryOutSection = -1

            End If

        Catch ex As Exception
            fUpdCryOutSection = -1
            sErrLog(gAppName, "fUpdCryOutSection", ex.ToString)
        Finally
            With gstrctPLDispHead
                .intNumOfScan += .intNumOfPkg
            End With
            With gstrctPLDispData
                .intCheckCnt = 0
                .intTotalCnt = gstrctPLDispHead.intNumOfScan
            End With

        End Try



        'Try
        '    wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PACKAP, G_CONST_KEY_PACKAPKEY00, "AP00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
        '    Select Case wintRet
        '        Case -1
        '            Exit Function
        '        Case 0
        '            fUpdCryOutSection = 0
        '            With gclsDB.Records("AP00")
        '                If RTrim(.Item("PaletteID")) = "A" AndAlso RTrim(.Item("ItemGr")) = wstrKeyValue(1) Then
        '                    If RTrim(.Item("CarryOutSection")) = "????????" OrElse RTrim(.Item("CarryOutSection")) = "" Then
        '                        gclsDB.Setcolumn = "CarryOutSectionId,CarryOutSection"
        '                        gclsDB.SetData = gstrctKDPL.strSectionID & "," & gstrctKDPL.strSection
        '                        'BtLib.Transaction.BeginConCurrent()
        '                        Try
        '                            Do
        '                                wstrKeyValue(2) = RTrim(.Item("ItemID"))
        '                                wstrKeyValue(3) = RTrim(.Item("ItemCode"))
        '                                wstrKeyValue(4) = RTrim(.Item("ItemInID"))
        '                                wstrKeyValue(5) = RTrim(.Item("ItemInCode"))
        '                                wstrKeyValue(6) = RTrim(.Item("TempSerial"))
        '                                If gclsDB.fUpdRecords(G_CONST_TBL_PACKAP, G_CONST_KEY_PACKAPKEY00, wstrKey, wstrKeyValue) <> 0 Then
        '                                    fUpdCryOutSection = -1
        '                                    Exit Function
        '                                End If
        '                                gstrctPLDispHead.intNumOfPkg += 1
        '                                fUpdCryOutSection = 1
        '                                If .Read(BtLib.Operation.GetNext) <> 0 Then Exit Do
        '                                If Not (RTrim(.Item("PaletteID")) = "A" AndAlso RTrim(.Item("ItemGr")) = wstrKeyValue(1)) Then Exit Do
        '                            Loop
        '                            If fSetPlan("3", CStr(gstrctPLDispHead.intNumOfPkg).PadLeft(4, "0")) = -1 Then
        '                                fUpdCryOutSection = -1
        '                            End If
        '                        Catch ex As Exception
        '                            fUpdCryOutSection = -1
        '                            Throw ex
        '                        Finally
        '                            If fUpdCryOutSection = -1 Then
        '                                BtLib.Transaction.Abort()
        '                            Else
        '                                BtLib.Transaction.End()
        '                            End If
        '                        End Try
        '                    End If
        '                End If
        '            End With
        '        Case Else
        '            fUpdCryOutSection = -1
        '    End Select
        'Catch ex As Exception
        '    fUpdCryOutSection = -1
        '    sErrLog(gAppName, "fUpdCryOutSection", ex.ToString)
        'Finally
        '    With gstrctPLDispHead
        '        .intNumOfScan += .intNumOfPkg
        '    End With
        '    With gstrctPLDispData
        '        .intCheckCnt = 0
        '        .intTotalCnt = gstrctPLDispHead.intNumOfScan
        '    End With

        '    If wintRet <> -1 Then gclsDB.fCloseRecord("AP00")
        'End Try
    End Function

    Private Sub frm1015_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        ' Enable BTScanLib
        If Not scanHandler Then
            btScanLib = btScanLib.getInstance()
            AddHandler btScanLib.OnScan, AddressOf btScanLib_OnScan
            scanHandler = True
            btScanLib.Debug = False
            btScanLib.Enable()
        End If
    End Sub
End Class
