Module modPublicPL070
    Private Structure STRCT_PPLN_REC
        Public strPicsType As String
        Public strCstmid As String
        Public strCstmCode As String
        Public strCenterCode As String
        Public strDate As String
        Public strLine As String
        Public strAic As String
        Public strCaseNo As String
        Public strCartonNo As String
        Public strItemID As String
        Public strItemCode As String
        Public strSerialNo As String
        Public strTempSerial As String
        Public strPickUser As String
        Public strPickDate As String
        Public strPickTime As String
        Public strStockLctn As String
    End Structure



    Public Function fClearJisseki() As Integer

        Dim wstrKey(6), wstrKeyValue(6) As String
        Dim wstrKey2(2), wstrKeyValue2(2) As String
        Dim wstrKey3(12), wstrKeyValue3(12) As String
        Dim wintRet As Integer = -1
        Dim wintRet2 As Integer = -1
        Dim wintRet3 As Integer = -1
        Dim wintFlag As Integer
        Dim wstrctPPLN_REC() As STRCT_PPLN_REC
        Dim wintRecCount As Integer = 0
        Dim wbReadAJ As Boolean = False
        fClearJisseki = -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)

        BtLib.Transaction.BeginConCurrent()
        Try
            wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PACKAJ, G_CONST_KEY_PACKAJKEY00, "KEY00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
            Select Case wintRet
                Case -1
                    Exit Function
                Case 0
                    fClearJisseki = 0
                    wstrKey2(0) = "ItemCode"
                    wstrKey2(1) = "ItemInCode"
                    wstrKey2(2) = "TempSerial"
                    wstrKey3(0) = "PicsType" : wstrKeyValue3(0) = gstrctKDHT.strPackType
                    wstrKey3(1) = "Cstmid" : wstrKeyValue3(1) = gstrctKDHT.strCstmid
                    wstrKey3(2) = "CstmCode" : wstrKeyValue3(2) = gstrctKDHT.strCstmCode
                    wstrKey3(3) = "CenterCode" : wstrKeyValue3(3) = gstrctKDPL.strCenterCode
                    wstrKey3(4) = "Date"
                    wstrKey3(5) = "Line"
                    wstrKey3(6) = "Aic"
                    wstrKey3(7) = "CaseNo"
                    wstrKey3(8) = "CartonNo"
                    wstrKey3(9) = "ItemID"
                    wstrKey3(10) = "ItemCode"
                    wstrKey3(11) = "SerialNo"
                    wstrKey3(12) = "TempSerial"
                    Try
                        Do
                            With gclsDB.Records("KEY00")
                                If RTrim(.Item("PaletteID")) = "A" AndAlso RTrim(.Item("ItemGr")) = wstrKeyValue(1) Then
                                    If wstrKeyValue2(0) <> RTrim(.Item("ItemCode")) Then
                                        If wintRet2 <> -1 Then
                                            gclsDB.fCloseRecord("KEYCJ00")
                                            wintRet2 = -1
                                        End If
                                    End If
                                    wstrKeyValue2(0) = RTrim(.Item("ItemCode"))
                                    If Mid(RTrim(.Item("ItemCode")), 21, 5) = "00000" OrElse RTrim(.Item("ItemID")) = "B" Then '部品
                                        wstrKeyValue2(1) = RTrim(.Item("ItemInCode"))
                                        wstrKeyValue2(2) = Space(6)
                                        wintFlag = 0
                                    Else 'カートン
                                        wstrKeyValue2(1) = Space(30)
                                        wstrKeyValue2(2) = Space(6)
                                        wintFlag = 1
                                    End If

                                    If wintFlag <> 0 AndAlso wintRet2 <> -1 Then
                                        If wintRet2 <> -1 Then
                                            gclsDB.fCloseRecord("KEYCJ00")
                                            wintRet2 = -1
                                        End If
                                    End If
                                    If wintRet2 = -1 Then
                                        wintRet2 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, "KEYCJ00", wstrKey2, wstrKeyValue2, BtLib.Operation.GetGreaterOrEqual)
                                    End If
                                    Select Case wintRet2
                                        Case 0
                                            Do

                                                If Mid(RTrim(.Item("ItemCode")), 21, 5) = "00000" OrElse RTrim(.Item("ItemID")) = "B" Then '部品
                                                    If wintFlag = 0 Then '部品
                                                        If fSearchStock("KEYCJ00") = 1 Then
                                                            Do
                                                                If RTrim(.Item("ItemID")) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemID")) AndAlso _
                                                                   RTrim(.Item("ItemCode")) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemCode")) AndAlso _
                                                                   RTrim(.Item("ItemInID")) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemInID")) AndAlso _
                                                                   RTrim(.Item("ItemInCode")) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemInCode")) Then
                                                                    'PackPlan再ｵｰﾌﾟﾝ処理
                                                                    If Mid(RTrim(gclsDB.Records("KEYCJ00").Item("ItemCode")), 21, 5) <> wstrKeyValue3(8) Then
                                                                        If wintRet3 <> -1 Then
                                                                            gclsDB.fCloseRecord("PKPLN00")
                                                                            wintRet3 = -1
                                                                        End If
                                                                        wstrKeyValue3(4) = RTrim(.Item("ProductDate")).PadRight(8, " ") & RTrim(.Item("Shift")).PadRight(1, " ")
                                                                        wstrKeyValue3(5) = .Item("Line")
                                                                        wstrKeyValue3(6) = Mid(gstrctAicCase.AicCaseNo, 1, 13)
                                                                        wstrKeyValue3(7) = Replace(gstrctAicCase.CaseNo, "-", "")
                                                                        If .Item("ItemID") = "C" Then
                                                                            wstrKeyValue3(8) = Mid(.Item("ItemCode"), 21, 5)
                                                                        ElseIf .Item("ItemID") = "B" Then
                                                                            wstrKeyValue3(8) = "00000"
                                                                        End If
                                                                        wstrKeyValue3(9) = RTrim(.Item("ItemInID"))
                                                                        wstrKeyValue3(10) = RTrim(.Item("ItemInCode"))
                                                                        wstrKeyValue3(11) = Space(6)
                                                                        wstrKeyValue3(12) = Space(6)
                                                                        wintRet3 = gclsDB.fOpenRecord(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, "PKPLN00", wstrKey3, wstrKeyValue3, BtLib.Operation.GetGreaterOrEqual)
                                                                    End If
                                                                    wstrKeyValue3(0) = RTrim(gclsDB.Records("KEYCJ00").Item("PicsType"))
                                                                    wstrKeyValue3(1) = RTrim(gclsDB.Records("KEYCJ00").Item("Cstmid"))
                                                                    wstrKeyValue3(2) = RTrim(gclsDB.Records("KEYCJ00").Item("CstmCode"))
                                                                    wstrKeyValue3(3) = RTrim(gclsDB.Records("KEYCJ00").Item("CenterCode"))
                                                                    wstrKeyValue3(4) = RTrim(gclsDB.Records("KEYCJ00").Item("ProductDate")).PadRight(8, " ") & RTrim(gclsDB.Records("KEYCJ00").Item("Shift")).PadRight(1, " ")
                                                                    wstrKeyValue3(5) = RTrim(gclsDB.Records("KEYCJ00").Item("Line"))
                                                                    wstrKeyValue3(6) = Mid(gstrctAicCase.AicCaseNo, 1, 13)
                                                                    wstrKeyValue3(7) = Replace(gstrctAicCase.CaseNo, "-", "")
                                                                    If RTrim(.Item("ItemID")) = "C" Then
                                                                        wstrKeyValue3(8) = Mid(gclsDB.Records("KEYCJ00").Item("ItemCode"), 21, 5)
                                                                    ElseIf .Item("ItemID") = "B" Then
                                                                        wstrKeyValue3(8) = "00000"
                                                                    End If
                                                                    wstrKeyValue3(9) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemInID"))
                                                                    wstrKeyValue3(10) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemInCode"))
                                                                    'PackCJとPackPlanの突合せ
                                                                    If RTrim(gclsDB.Records("PKPLN00").Item("PicsType")) = wstrKeyValue3(0) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("Cstmid")) = wstrKeyValue3(1) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("CstmCode")) = wstrKeyValue3(2) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("CenterCode")) = wstrKeyValue3(3) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("Date")) = wstrKeyValue3(4) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("Line")) = wstrKeyValue3(5) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("Aic")) = wstrKeyValue3(6) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("CaseNo")) = wstrKeyValue3(7) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("CartonNo")) = wstrKeyValue3(8) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("ItemID")) = wstrKeyValue3(9) AndAlso _
                                                                       RTrim(gclsDB.Records("PKPLN00").Item("ItemCode")) = wstrKeyValue3(10) Then
                                                                        wstrKeyValue3(11) = RTrim(gclsDB.Records("PKPLN00").Item("SerialNo"))
                                                                        wstrKeyValue3(12) = RTrim(gclsDB.Records("PKPLN00").Item("TempSerial"))
                                                                        If RTrim(gclsDB.Records("PKPLN00").Item("Status")) < "30" Then
                                                                            With gclsDB
                                                                                .Setcolumn = "Status,PickDate,PickTime,PickEmp,PickLctn1,SerialNo,PalDate,PalTime,PalEmp,SectionIDPal,SectionCodePal"
                                                                                .SetData = "30," & .Records("KEYCJ00").Item("ScanDate") & "," & .Records("KEYCJ00").Item("ScanTime") & "," _
                                                                                  & .Records("KEYCJ00").Item("UserNo") & "," & .Records("KEYCJ00").Item("StockLctn") & "," & .Records("KEYCJ00").Item("SerialNo") & "," _
                                                                                  & .Records("KEY00").Item("ScanDate") & "," & .Records("KEY00").Item("ScanTime") & "," & .Records("KEY00").Item("UserNo") & "," _
                                                                                  & .Records("KEY00").Item("CarryOutSectioniId") & "," & .Records("KEY00").Item("CarryOutSection")
                                                                            End With
                                                                        Else
                                                                            With gclsDB
                                                                                .Setcolumn = "PalDate,PalTime,PalEmp,SectionIDPal,SectionCodePal"
                                                                                .SetData = .Records("KEY00").Item("ScanDate") & "," & .Records("KEY00").Item("ScanTime") & "," & .Records("KEY00").Item("UserNo") & "," _
                                                                                        & .Records("KEY00").Item("CarryOutSectionId") & "," & .Records("KEY00").Item("CarryOutSection")
                                                                            End With
                                                                        End If
                                                                        If gclsDB.fUpdRecords(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, wstrKey3, wstrKeyValue3) <> 0 Then
                                                                            fClearJisseki = -1
                                                                            Exit Function
                                                                        End If
                                                                        'PACKCJ削除処理
                                                                        wstrKeyValue2(1) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemInCode"))
                                                                        wstrKeyValue2(2) = RTrim(gclsDB.Records("KEYCJ00").Item("TempSerial"))
                                                                        If gclsDB.fDleRecords(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, wstrKey2, wstrKeyValue2) <> 0 Then
                                                                            fClearJisseki = -1
                                                                            Exit Function
                                                                        End If
                                                                        If gclsDB.Records("KEYCJ00").Read(BtLib.Operation.GetNext) <> 0 Then
                                                                            Exit Function
                                                                        End If
                                                                        If .Item("ItemID") = "B" Then '比較のためｶｰﾄﾝを復帰
                                                                            wstrKeyValue3(8) = Mid(.Item("ItemCode"), 21, 5)
                                                                        End If
                                                                        If gclsDB.Records("PKPLN00").Read(BtLib.Operation.GetNext) <> 0 Then
                                                                            Exit Function
                                                                        End If
                                                                        wbReadAJ = True
                                                                        Exit Do
                                                                    Else
                                                                        If .Item("ItemID") = "B" Then '比較のためｶｰﾄﾝを復帰
                                                                            wstrKeyValue3(8) = Mid(.Item("ItemCode"), 21, 5)
                                                                        End If
                                                                        If gclsDB.Records("PKPLN00").Read(BtLib.Operation.GetNext) <> 0 Then
                                                                            Exit Function
                                                                        End If
                                                                    End If
                                                                Else
                                                                    Exit Do
                                                                End If
                                                            Loop
                                                        End If
                                                    End If
                                                Else 'カートン
                                                    If wintFlag = 0 Then  '再オープン
                                                        wstrKeyValue2(0) = RTrim(.Item("ItemCode"))
                                                        wstrKeyValue2(1) = Space(30)
                                                        wstrKeyValue2(2) = Space(6)
                                                        wintFlag = 1
                                                        If wintRet2 <> -1 Then
                                                            gclsDB.fCloseRecord("KEYCJ00")
                                                            wintRet2 = -1
                                                        End If
                                                        wintRet2 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, "KEYCJ00", wstrKey2, wstrKeyValue2, BtLib.Operation.GetGreaterOrEqual)
                                                        If wintRet2 = -1 Then
                                                            Exit Function
                                                        ElseIf wintRet2 <> 0 Then
                                                            fClearJisseki = 0
                                                            Exit Do
                                                        End If
                                                    End If
                                                    If RTrim(.Item("ItemCode")) = RTrim(gclsDB.Records("KEYCJ00").Item("ItemCode")) Then '同一ｶｰﾄﾝ
                                                        Select Case fSearchStock("KEYCJ00")
                                                            Case 1
                                                                'PACKPLANの更新
                                                                ReDim Preserve wstrctPPLN_REC(wintRecCount)
                                                                With gclsDB.Records("KEYCJ00")
                                                                    wstrctPPLN_REC(wintRecCount).strPicsType = gstrctKDHT.strPackType
                                                                    wstrctPPLN_REC(wintRecCount).strCstmid = gstrctKDHT.strCstmid
                                                                    wstrctPPLN_REC(wintRecCount).strCstmCode = gstrctKDHT.strCstmCode
                                                                    wstrctPPLN_REC(wintRecCount).strCenterCode = gstrctKDPL.strCenterCode
                                                                    wstrctPPLN_REC(wintRecCount).strDate = RTrim(.Item("ProductDate")).PadRight(8, " ") & RTrim(.Item("Shift")).PadRight(1, " ")
                                                                    wstrctPPLN_REC(wintRecCount).strLine = .Item("Line")
                                                                    wstrctPPLN_REC(wintRecCount).strAic = Mid(gstrctAicCase.AicCaseNo, 1, 13)
                                                                    wstrctPPLN_REC(wintRecCount).strCaseNo = Replace(gstrctAicCase.CaseNo, "-", "")
                                                                    If .Item("ItemID") = "C" Then
                                                                        wstrctPPLN_REC(wintRecCount).strCartonNo = Mid(.Item("ItemCode"), 21, 5)
                                                                    Else
                                                                        wstrctPPLN_REC(wintRecCount).strCartonNo = "00000"
                                                                    End If
                                                                    wstrctPPLN_REC(wintRecCount).strItemID = RTrim(.Item("ItemInID"))
                                                                    wstrctPPLN_REC(wintRecCount).strItemCode = RTrim(.Item("ItemInCode"))
                                                                    wstrctPPLN_REC(wintRecCount).strSerialNo = .Item("SerialNo")
                                                                    wstrctPPLN_REC(wintRecCount).strTempSerial = .Item("TempSerial")
                                                                    wstrctPPLN_REC(wintRecCount).strStockLctn = .Item("StockLctn")
                                                                    wstrctPPLN_REC(wintRecCount).strPickUser = .Item("UserNo")
                                                                    wstrctPPLN_REC(wintRecCount).strPickDate = .Item("ScanDate")
                                                                    wstrctPPLN_REC(wintRecCount).strPickTime = .Item("ScanTime")

                                                                    wstrKeyValue2(1) = RTrim(.Item("ItemInCode"))
                                                                    wstrKeyValue2(2) = RTrim(.Item("TempSerial"))
                                                                End With

                                                                'If gclsDB.fDleRecords(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, wstrKey2, wstrKeyValue2) <> 0 Then
                                                                '    fClearJisseki = -1
                                                                '    Exit Function
                                                                'End If
                                                                wintRecCount += 1
                                                            Case -1
                                                                Exit Function
                                                        End Select
                                                    Else
                                                        If wintRecCount > 0 Then
                                                            If wintRet3 <> -1 Then
                                                                gclsDB.fCloseRecord("PKPLN00")
                                                                wintRet3 = -1
                                                            End If
                                                            fSearchPackPlan("KEY00", "KEYCJ00", wstrKeyValue2, wintRecCount, wstrctPPLN_REC)
                                                            Exit Do 'データ更新後、次データ取得へ
                                                        End If
                                                    End If

                                                    If gclsDB.Records("KEYCJ00").Read(BtLib.Operation.GetNext) <> 0 Then
                                                        If wintRecCount > 0 Then
                                                            If wintRet3 <> -1 Then
                                                                gclsDB.fCloseRecord("PKPLN00")
                                                                wintRet3 = -1
                                                            End If
                                                            If fSearchPackPlan("KEY00", "KEYCJ00", wstrKeyValue2, wintRecCount, wstrctPPLN_REC) <> 1 Then
                                                                fClearJisseki = -1
                                                                Exit Function
                                                            End If
                                                        End If
                                                        Exit Do 'CJの次データ取得後、AJの次レコードへ
                                                    End If

                                                End If
                                                If wbReadAJ Then Exit Do
                                            Loop
                                    End Select

                                Else
                                    Exit Function
                                End If
                                If .Read(BtLib.Operation.GetNext) <> 0 Then
                                    Exit Function
                                End If
                                If wbReadAJ Then wbReadAJ = False
                            End With

                        Loop
                    Catch ex As Exception
                        fClearJisseki = -1
                        Throw ex
                    Finally
                        If fClearJisseki = -1 Then
                            BtLib.Transaction.Abort()
                        Else
                            BtLib.Transaction.End()
                        End If
                    End Try

                Case Else
                    fClearJisseki = 0
                    Exit Function
            End Select

        Catch ex As Exception
            fClearJisseki = -1
            sErrLog(gAppName, "fClearJisseki", ex.ToString)
        Finally
            If wintRet <> -1 Then gclsDB.fCloseRecord("KEY00")
            If wintRet2 <> -1 Then gclsDB.fCloseRecord("KEYCJ00")
            If wintRet3 <> -1 Then gclsDB.fCloseRecord("PKPLN00")
        End Try
    End Function

    Private Function fSearchPackCJ(ByVal pstrKeyCJ As String, Optional ByVal pintSrchSw As Integer = 0) As Integer
        Dim wstrKey(2), wstrKeyValue(2) As String
        Dim wstrKey2(12), wstrKeyValue2(12) As String
        Dim wintRet As Integer = -1
        Dim wintRet2 As Integer = -1
        fSearchPackCJ = -1
        wstrKey(0) = "ItemCode"
        wstrKey(1) = "ItemInCode"
        wstrKey(2) = "TempSerial"

        Try
            With gclsDB.Records(pstrKeyCJ)
                wstrKeyValue(0) = RTrim(.Item("ItemCode"))
                If pintSrchSw = 0 Then '部品
                    wstrKeyValue(1) = RTrim(.Item("ItemInCode"))
                    wstrKeyValue(2) = Space(6)
                    'wstrKeyValue(2) = RTrim(.Item("TempSerial"))
                    wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, "KEYCJ00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
                    Select Case wintRet
                        Case -1
                            Exit Function
                        Case 0
                            If RTrim(.Item("ItemCode")) = wstrKeyValue(1) Then
                                Do
                                    If fSearchStock("KEYCJ00") = 1 Then
                                        wstrKey2(0) = "PicsType" : wstrKeyValue2(0) = gstrctKDHT.strPackType
                                        wstrKey2(1) = "Cstmid" : wstrKeyValue2(1) = gstrctKDHT.strCstmid
                                        wstrKey2(2) = "CstmCode" : wstrKeyValue2(2) = gstrctKDHT.strCstmCode
                                        wstrKey2(3) = "CenterCode" : wstrKeyValue2(3) = gstrctKDPL.strCenterCode
                                        wstrKey2(4) = "Date" : wstrKeyValue2(4) = RTrim(.Item("ProductDate")).PadRight(8, " ") & RTrim(.Item("Shift")).PadRight(1, " ")
                                        wstrKey2(5) = "Line" : wstrKeyValue2(5) = .Item("Line")
                                        wstrKey2(6) = "Aic" : wstrKeyValue2(6) = Mid(gstrctAicCase.AicCaseNo, 1, 13)
                                        wstrKey2(7) = "CaseNo" : wstrKeyValue2(7) = gstrctAicCase.CaseNo
                                        wstrKey2(8) = "CartonNo"
                                        If .Item("ItemID") = "C" Then
                                            wstrKeyValue2(8) = Mid(.Item("ItemCode"), 21, 5)
                                        ElseIf .Item("ItemID") = "B" Then
                                            wstrKeyValue2(8) = "00000"
                                        End If
                                        wstrKey(9) = "ItemID" : wstrKeyValue2(9) = RTrim(.Item("ItemInID"))
                                        wstrKey(10) = "ItemCode" : wstrKeyValue2(10) = RTrim(.Item("ItemInCode"))
                                        wstrKey(11) = "SerialNo" : wstrKeyValue2(11) = Space(6)
                                        wstrKey(12) = "TempSerial" : wstrKeyValue2(12) = Space(6)
                                        'PACKPLANの更新
                                        wintRet2 = gclsDB.fOpenRecord(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, "PKPLN00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
                                        Select Case wintRet2
                                            Case 0
                                                Do
                                                    With gclsDB.Records("PKPLN00")
                                                        If RTrim(.Item("PicsType")) = wstrKeyValue2(0) AndAlso RTrim(.Item("Cstmid")) = wstrKeyValue2(1) AndAlso _
                                                           RTrim(.Item("CstmCode")) = wstrKeyValue2(2) AndAlso RTrim(.Item("CenterCode")) = wstrKeyValue2(3) AndAlso _
                                                           RTrim(.Item("Date")) = wstrKeyValue2(4) AndAlso RTrim(.Item("Line")) = wstrKeyValue2(5) AndAlso _
                                                           RTrim(.Item("Aic")) = wstrKeyValue2(6) AndAlso RTrim(.Item("CaseNo")) = wstrKeyValue2(7) AndAlso _
                                                           RTrim(.Item("CartonNo")) = wstrKeyValue2(8) AndAlso RTrim(.Item("ItemID")) = wstrKeyValue2(9) AndAlso _
                                                           RTrim(.Item("ItemCode")) = wstrKeyValue2(10) Then
                                                            If RTrim(.Item("Status")) < "30 then" Then
                                                                Dim wstrSerialNo As String
                                                                wstrKeyValue(11) = .Item("SerialNo")
                                                                wstrKeyValue(12) = .Item("TempSerial")
                                                                '// DB更新
                                                                With gclsDB
                                                                    If RTrim(.Records("KEYCJ00").Item("PackQtyJ")).PadLeft(5, "0") = "00000" Then
                                                                        wstrSerialNo = "00000"
                                                                    Else
                                                                        wstrSerialNo = RTrim(.Records("KEYCJ00").Item("SerialNo")).PadLeft(5, "0")
                                                                    End If
                                                                    .Setcolumn = "Status,PickDate,PickTime,PickEmp,PickLctn1,SerialNo,PalDate,PalTime,PalEmp"
                                                                    .SetData = "30," & .Records("KEYCJ00").Item("ScanDate") & "," & .Records("KEYCJ00").Item("ScanTime") & "," _
                                                                      & .Records("KEYCJ00").Item("UserNo") & "," & .Records("KEYCJ00").Item("StockLctn") & "," & wstrSerialNo
                                                                    If .fUpdRecords(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, wstrKey, wstrKeyValue) = -1 Then
                                                                        fSearchPackCJ = -1
                                                                        sErrLog(gAppName, "fSearchPackCJ", gclsDB.ErrMsg)
                                                                        Exit Function
                                                                    End If
                                                                End With
                                                                fSearchPackCJ = 1
                                                                Exit Function
                                                            Else
                                                                If fSearchPackCJ = -1 Then fSearchPackCJ = 0
                                                                Exit Function
                                                            End If
                                                            If .Read(BtLib.Operation.GetNext) <> 0 Then
                                                                Exit Do
                                                            End If
                                                        End If
                                                    End With
                                                Loop
                                            Case -1
                                            Case Else
                                                Exit Function
                                        End Select

                                    End If
                                Loop
                            Else
                                Exit Function
                            End If
                        Case Else
                            fSearchPackCJ = 0
                            Exit Function
                    End Select
                Else 'カートン
                    wstrKeyValue(1) = Space(30)
                    wstrKeyValue(2) = Space(6)
                    wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, "KEYCJ00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
                    Select Case wintRet
                        Case -1
                            Exit Function
                        Case 0
                            Do

                                With gclsDB.Records("KEYCJ00")
                                    If RTrim(.Item("ItemCode")) = wstrKeyValue(0) Then
                                        Select Case fSearchStock("KEYCJ00")
                                            Case -1
                                                fSearchPackCJ = -1
                                                Exit Function
                                            Case 1
                                                'PACKPLANの更新
                                                fUpdPackPlan("KEYCJ00")
                                                wstrKeyValue(1) = RTrim(.Item("ItemInCode"))
                                                wstrKeyValue(2) = RTrim(.Item("TempSerial"))
                                                If gclsDB.fDleRecords(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, wstrKey, wstrKeyValue) <> 0 Then
                                                    Exit Function
                                                End If
                                        End Select
                                        If .Read(BtLib.Operation.GetNext) <> 0 Then Exit Do
                                    Else
                                        Exit Do
                                    End If
                                End With
                            Loop
                        Case Else
                            fSearchPackCJ = 0
                            Exit Function
                    End Select
                End If
            End With

            fSearchPackCJ = 1
        Catch ex As Exception
            fSearchPackCJ = -1
            sErrLog(gAppName, "fSearchPackCJ", ex.ToString)
        Finally
            If wintRet <> -1 Then gclsDB.fCloseRecord("KEYCJ00")
        End Try
    End Function

    Private Function fSearchStock(ByVal pstrKeyStk As String) As Integer
        Dim wstrKey(7), wstrKeyValue(7) As String
        Dim wintRet As Integer = -1
        fSearchStock = -1
        With gstrctKDHT
            wstrKey(0) = "PicsType" : wstrKeyValue(0) = .strPackType
            wstrKey(1) = "Cstmid" : wstrKeyValue(1) = .strCstmid
            wstrKey(2) = "CstmCode" : wstrKeyValue(2) = .strCstmCode
        End With
        With gstrctKDPL
            wstrKey(3) = "CenterCode" : wstrKeyValue(3) = .strCenterCode
            wstrKey(4) = "Bhk" : wstrKeyValue(4) = .strBhk
        End With
        wstrKey(5) = "Itemid" : wstrKeyValue(5) = "P"
        wstrKey(6) = "ItemCode"
        wstrKey(7) = "SerialNo"
        With gclsDB.Records(pstrKeyStk)
            wstrKeyValue(6) = RTrim(.Item("ItemInCode"))
            wstrKeyValue(7) = RTrim(.Item("SerialNo"))
        End With

        Try
            wintRet = gclsDB.fOpenRecord(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY00, "STK00", wstrKey, wstrKeyValue, BtLib.Operation.GetEqual)
            Select Case wintRet
                Case -1
                    Exit Function
                Case 0
                    If gclsDB.Records("STK00").Item("ZaikoStatus") = "7" Then
                        If gclsDB.fDleRecords(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY00, wstrKey, wstrKeyValue) <> 0 Then
                            Exit Function
                        End If
                    Else
                        gclsDB.Setcolumn = "ZaikoStatus"
                        gclsDB.SetData = "9"
                        If gclsDB.fUpdRecords(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY00, wstrKey, wstrKeyValue) <> 0 Then
                            Exit Function
                        End If
                    End If
                    fSearchStock = 1
                Case Else
                    fSearchStock = 0
            End Select
        Catch ex As Exception
            fSearchStock = -1
            sErrLog(gAppName, "fSearchStock", ex.ToString)
        Finally
            If wintRet <> -1 Then gclsDB.fCloseRecord("STK00")
        End Try
    End Function
    Private Function fUpdPackPlan(ByVal pstrKey As String) As Integer
        Dim wstrKey(12), wstrKeyValue(12) As String
        Dim wintRet As Integer = -1
        fUpdPackPlan = -1
        Try
            With gclsDB.Records(pstrKey)
                wstrKey(0) = "PicsType" : wstrKeyValue(0) = gstrctKDHT.strPackType
                wstrKey(1) = "Cstmid" : wstrKeyValue(1) = gstrctKDHT.strCstmid
                wstrKey(2) = "CstmCode" : wstrKeyValue(2) = gstrctKDHT.strCstmCode
                wstrKey(3) = "CenterCode" : wstrKeyValue(3) = gstrctKDPL.strCenterCode
                wstrKey(4) = "Date" : wstrKeyValue(4) = RTrim(.Item("ProductDate")).PadRight(8, " ") & RTrim(.Item("Shift")).PadRight(1, " ")
                wstrKey(5) = "Line" : wstrKeyValue(5) = .Item("Line")
                wstrKey(6) = "Aic" : wstrKeyValue(6) = Mid(gstrctAicCase.AicCaseNo, 1, 13)
                wstrKey(7) = "CaseNo" : wstrKeyValue(7) = gstrctAicCase.CaseNo
                wstrKey(8) = "CartonNo"
                If .Item("ItemID") = "C" Then
                    wstrKeyValue(8) = Mid(.Item("ItemCode"), 21, 5)
                ElseIf .Item("ItemID") = "B" Then
                    wstrKeyValue(8) = "00000"
                End If
                wstrKey(9) = "ItemID" : wstrKeyValue(9) = RTrim(.Item("ItemInID"))
                wstrKey(10) = "ItemCode" : wstrKeyValue(10) = RTrim(.Item("ItemInCode"))
                wstrKey(11) = "SerialNo" : wstrKeyValue(11) = Space(6)
                wstrKey(12) = "TempSerial" : wstrKeyValue(12) = Space(6)
                'wstrKey(11) = "SerialNo" : wstrKeyValue(11) = .Item("SerialNo")
                'wstrKey(12) = "TempSerial" : wstrKeyValue(12) = .Item("TempSerial")
            End With
            wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY03, "PKPLN00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
            Select Case wintRet
                Case -1
                    Exit Function
                Case 0
                    Do
                        With gclsDB.Records("PKPLN00")
                            If RTrim(.Item("PicsType")) = wstrKeyValue(0) AndAlso RTrim(.Item("Cstmid")) = wstrKeyValue(1) AndAlso RTrim(.Item("CstmCode")) = wstrKeyValue(2) AndAlso _
                               RTrim(.Item("CenterCode")) = wstrKeyValue(3) AndAlso RTrim(.Item("Date")) = wstrKeyValue(4) AndAlso RTrim(.Item("Line")) = wstrKeyValue(5) AndAlso _
                               RTrim(.Item("Aic")) = wstrKeyValue(6) AndAlso RTrim(.Item("CaseNo")) = wstrKeyValue(7) AndAlso RTrim(.Item("CartonNo")) = wstrKeyValue(8) AndAlso _
                               RTrim(.Item("ItemID")) = wstrKeyValue(9) AndAlso RTrim(.Item("ItemCode")) = wstrKeyValue(10) Then
                                If RTrim(.Item("Status")) < "30 then" Then
                                    Dim wstrSerialNo As String
                                    wstrKeyValue(11) = .Item("SerialNo")
                                    wstrKeyValue(12) = .Item("TempSerial")
                                    '// DB更新
                                    With gclsDB
                                        If RTrim(.Records(pstrKey).Item("PackQtyJ")).PadLeft(5, "0") = "00000" Then
                                            wstrSerialNo = "00000"
                                        Else
                                            wstrSerialNo = RTrim(.Records(pstrKey).Item("SerialNo")).PadLeft(5, "0")
                                        End If
                                        .Setcolumn = "Status,PickDate,PickTime,PickEmp,PickLctn1,SerialNo"
                                        .SetData = "30," & .Records(pstrKey).Item("ScanDate") & "," & .Records(pstrKey).Item("ScanTime") & "," _
                                          & .Records(pstrKey).Item("UserNo") & "," & .Records(pstrKey).Item("StockLctn") & "," & wstrSerialNo
                                        If .fUpdRecords(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, wstrKey, wstrKeyValue) = -1 Then
                                            fUpdPackPlan = -1
                                            sErrLog(gAppName, "fUpdPackPlan", gclsDB.ErrMsg)
                                            Exit Function
                                        End If
                                    End With
                                    fUpdPackPlan = 1
                                    Exit Function
                                Else
                                    fUpdPackPlan = 0
                                    Exit Function
                                End If
                                If .Read(BtLib.Operation.GetNext) <> 0 Then
                                    If fUpdPackPlan = -1 Then
                                        fUpdPackPlan = 0
                                    End If
                                End If

                            Else
                                fUpdPackPlan = 0
                                Exit Function
                            End If
                        End With
                    Loop
            End Select

        Catch ex As Exception
            fUpdPackPlan = -1
            sErrLog(gAppName, "fUpdPackPlan", ex.ToString)
        Finally
            If wintRet <> -1 Then gclsDB.fCloseRecord("PKPLN00")
        End Try
    End Function

    Private Function fSearchPackPlan(ByVal pstrApKey As String, ByVal pstrCJKey As String, ByRef pstrCJKeyValue() As String, ByRef pintRecCount As Integer, ByRef pstrctPPLN_REC() As STRCT_PPLN_REC) As Integer
        Dim wstrKey(12), wstrKeyValue(12) As String
        Dim wstrKey2(2) As String
        Dim i As Integer
        Dim wintRet As Integer = -1
        fSearchPackPlan = -1
        wstrKey(0) = "PicsType"
        wstrKey(1) = "Cstmid"
        wstrKey(2) = "CstmCode"
        wstrKey(3) = "CenterCode"
        wstrKey(4) = "Date"
        wstrKey(5) = "Line"
        wstrKey(6) = "Aic"
        wstrKey(7) = "CaseNo"
        wstrKey(8) = "CartonNo"
        wstrKey(9) = "ItemID"
        wstrKey(10) = "ItemCode"
        wstrKey(11) = "SerialNo"
        wstrKey(12) = "TempSerial"
        wstrKeyValue(0) = pstrctPPLN_REC(0).strPicsType
        wstrKeyValue(1) = pstrctPPLN_REC(0).strCstmid
        wstrKeyValue(2) = pstrctPPLN_REC(0).strCstmCode
        wstrKeyValue(3) = pstrctPPLN_REC(0).strCenterCode
        wstrKeyValue(4) = pstrctPPLN_REC(0).strDate
        wstrKeyValue(5) = pstrctPPLN_REC(0).strLine
        wstrKeyValue(6) = pstrctPPLN_REC(0).strAic
        wstrKeyValue(7) = pstrctPPLN_REC(0).strCaseNo
        wstrKeyValue(8) = pstrctPPLN_REC(0).strCartonNo
        wstrKeyValue(9) = Space(1)
        wstrKeyValue(10) = Space(30)
        wstrKeyValue(11) = Space(6)
        wstrKeyValue(12) = Space(6)
        wstrKey2(0) = "ItemCode"
        wstrKey2(1) = "ItemInCode"
        wstrKey2(2) = "TempSerial"
        wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, "PKPLN00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
        Try
            If wintRet = -1 Then Exit Function
            For i = 0 To pintRecCount - 1
                If gclsDB.Records("PKPLN00").Item("PicsType") = pstrctPPLN_REC(i).strPicsType AndAlso gclsDB.Records("PKPLN00").Item("Cstmid") = pstrctPPLN_REC(i).strCstmid AndAlso _
                   gclsDB.Records("PKPLN00").Item("CstmCode") = pstrctPPLN_REC(i).strCstmCode AndAlso gclsDB.Records("PKPLN00").Item("CenterCode") = pstrctPPLN_REC(i).strCenterCode AndAlso _
                   gclsDB.Records("PKPLN00").Item("Date") = pstrctPPLN_REC(i).strDate AndAlso gclsDB.Records("PKPLN00").Item("Line") = pstrctPPLN_REC(i).strLine AndAlso _
                   gclsDB.Records("PKPLN00").Item("Aic") = pstrctPPLN_REC(i).strAic AndAlso gclsDB.Records("PKPLN00").Item("CaseNo") = pstrctPPLN_REC(i).strCaseNo AndAlso _
                   gclsDB.Records("PKPLN00").Item("CartonNo") = pstrctPPLN_REC(i).strCartonNo AndAlso gclsDB.Records("PKPLN00").Item("ItemID") = pstrctPPLN_REC(i).strItemID AndAlso _
                   gclsDB.Records("PKPLN00").Item("ItemCode") = pstrctPPLN_REC(i).strItemCode Then
                    wstrKeyValue(0) = pstrctPPLN_REC(i).strPicsType
                    wstrKeyValue(1) = pstrctPPLN_REC(i).strCstmid
                    wstrKeyValue(2) = pstrctPPLN_REC(i).strCstmCode
                    wstrKeyValue(3) = pstrctPPLN_REC(i).strCenterCode
                    wstrKeyValue(4) = pstrctPPLN_REC(i).strDate
                    wstrKeyValue(5) = pstrctPPLN_REC(i).strLine
                    wstrKeyValue(6) = pstrctPPLN_REC(i).strAic
                    wstrKeyValue(7) = pstrctPPLN_REC(i).strCaseNo
                    wstrKeyValue(8) = pstrctPPLN_REC(i).strCartonNo
                    wstrKeyValue(9) = pstrctPPLN_REC(i).strItemID
                    wstrKeyValue(10) = pstrctPPLN_REC(i).strItemCode
                    wstrKeyValue(11) = gclsDB.Records("PKPLN00").Item("SerialNo")
                    wstrKeyValue(12) = gclsDB.Records("PKPLN00").Item("TempSerial")
                    If RTrim(gclsDB.Records("PKPLN00").Item("Status")) < "30" Then
                        With gclsDB
                            .Setcolumn = "Status,PickDate,PickTime,PickEmp,PickLctn1,SerialNo,PalDate,PalTime,PalEmp,SectionIDPal,SectionCodePal"
                            .SetData = "30," & .Records(pstrCJKey).Item("ScanDate") & "," & .Records(pstrCJKey).Item("ScanTime") & "," _
                              & pstrctPPLN_REC(i).strPickUser & "," & pstrctPPLN_REC(i).strStockLctn & "," & pstrctPPLN_REC(i).strSerialNo & "," _
                              & .Records(pstrApKey).Item("ScanDate") & "," & .Records(pstrApKey).Item("ScanTime") & "," & .Records(pstrApKey).Item("UserNo") & "," _
                              & .Records(pstrApKey).Item("CarryOutSectioniId") & "," & .Records(pstrApKey).Item("CarryOutSection")
                        End With
                    Else
                        With gclsDB
                            .Setcolumn = "PalDate,PalTime,PalEmp,SectionIDPal,SectionCodePal"
                            .SetData = .Records(pstrApKey).Item("ScanDate") & "," & .Records(pstrApKey).Item("ScanTime") & "," & .Records(pstrApKey).Item("UserNo") & "," _
                                    & .Records(pstrApKey).Item("CarryOutSectionId") & "," & .Records(pstrApKey).Item("CarryOutSection")
                        End With
                    End If
                    If gclsDB.fUpdRecords(G_CONST_TBL_PACKPLAN, G_CONST_KEY_PACKPLANKEY06, wstrKey, wstrKeyValue) <> 0 Then
                        fSearchPackPlan = -1
                        Exit Function
                    End If
                    pstrCJKeyValue(1) = pstrctPPLN_REC(i).strItemCode
                    pstrCJKeyValue(2) = pstrctPPLN_REC(i).strTempSerial

                    If gclsDB.fDleRecords(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY07, wstrKey2, pstrCJKeyValue) <> 0 Then
                        fSearchPackPlan = -1
                        Exit Function
                    End If
                End If
                gclsDB.Records("PKPLN00").Read(BtLib.Operation.GetNext)
            Next
            pintRecCount = 0
            pstrctPPLN_REC = Nothing
            If wintRet <> -1 Then
                gclsDB.fCloseRecord("PKPLN00")
                wintRet = -1
            End If
            'Exit Do

            fSearchPackPlan = 1
        Catch ex As Exception
        Finally
            If wintRet <> -1 Then gclsDB.fCloseRecord("PKPLN00")
        End Try
    End Function


End Module
