'*******************************************************************************
'* 作成者   : 株式会社エコー・システム 
'* 作成日   : 2005/08/30
'* 概要     : PICS2004 共通処理
'* 更新履歴 : 
'* ﾊﾞｰｼﾞｮﾝ  : 1.0
'*******************************************************************************
Imports System.Text
Imports WarpControl
Imports WarpControl.WarpApi2
Module modPublicPL010
 
    '*******************************************************************************
    '* 機能     : PACKAP検索処理
    '* 引数     : 1.検索対象ﾌﾗｸﾞ  2.ﾊﾟﾚﾀｲｽﾞ計画数取得ﾌﾗｸﾞ(0:AP作成時、1:AP作成後)
    '* 戻り値   : -1:DB接続エラー  1:データ検索 0:対象ﾃﾞｰﾀ読込み終了
    '* 備考     : ﾊﾟﾚﾀｲｽﾞ計画数取得ﾌﾗｸﾞは残数照会時はAP作成後なので1を指定。
    '*******************************************************************************
    Public Function fSearchPackAP2(ByVal Status As String, Optional ByVal pstrTotalQtyFlg As String = "0") As Integer
        Dim wstrKey(7) As String
        Dim wstrKeyValue(7) As String
        Dim rtn As Integer
        Dim wintQty As Integer
        fSearchPackAP2 = -1
        wstrKey(0) = "Flag"
        wstrKey(1) = "PaletteID"
        wstrKey(2) = "ItemGr"
        wstrKey(3) = "ItemID"
        wstrKey(4) = "ItemCode"
        wstrKey(5) = "ItemInID"
        wstrKey(6) = "ItemInCode"
        wstrKey(7) = "TempSerial"
        Try
            With gstrctAicCase
                wstrKeyValue(0) = Status
                wstrKeyValue(1) = "A"
                wstrKeyValue(2) = .AicCaseNo & .YYYYMMDD
                wstrKeyValue(3) = Space(1)
                wstrKeyValue(4) = Space(30)
                wstrKeyValue(5) = Space(1)
                wstrKeyValue(6) = Space(30)
                wstrKeyValue(7) = Space(6)
            End With

            rtn = gclsDB.fOpenRecord(G_CONST_TBL_PACKAP, G_CONST_KEY_PACKAPKEY01, "PKAP00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
            If rtn = 9 OrElse rtn = 4 Then
                gclsDB.fCloseRecord("PKAP00")
                fSearchPackAP2 = 0
                Exit Function
            ElseIf rtn <> 0 Then
                gclsDB.fCloseRecord("PKAP00")
                Exit Function
            ElseIf rtn = 0 Then
                With gclsDB.Records("PKAP00")
                    Do
                        If wstrKeyValue(0) = .Item("Flag") AndAlso wstrKeyValue(1) = .Item("PaletteID") AndAlso wstrKeyValue(2) = .Item("ItemGr") Then
                            wintQty += 1
                        End If
                        If .Read(BtLib.Operation.GetNext) <> 0 Then
                            'レコードがなくなれば終了する
                            Exit Do
                        End If
                    Loop
                End With
                gclsDB.fCloseRecord("PKAP00")
                If pstrTotalQtyFlg = "0" Then 'AP作成時
                    gstrctPLDispData.intTotalCnt = wintQty
                Else 'AP作成後
                    rtn = fGetTotalQty(gstrctPLDispData.intTotalCnt)
                    If rtn = -1 Then
                        Exit Function
                    End If
                    gstrctPLDispData.intCheckCnt = gstrctPLDispData.intTotalCnt - wintQty
                End If
            End If
            fSearchPackAP2 = 1
        Catch ex As Exception
            sErrLog(gAppName, "fSearchPackAP2", ex.ToString)
        End Try
    End Function

    Public Function fSearchPackAP3(Optional ByVal pstrTotalQtyFlg As String = "0") As Integer
        Dim wstrKey(6), wstrKeyValue(6) As String
        Dim rtn As Integer = -1
        Dim wintQty As Integer = 0
        fSearchPackAP3 = -1
        With gstrctAicCase
            wstrKey(0) = "PaletteID" : wstrKeyValue(0) = "A"
            wstrKey(1) = "ItemGr" : wstrKeyValue(1) = .AicCaseNo & .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)
        End With
        Try
            rtn = gclsDB.fOpenRecord(G_CONST_TBL_PACKAP, G_CONST_KEY_PACKAPKEY00, "PKAP00", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
            Select Case rtn
                Case -1
                    Exit Function
                Case 0
                    Do
                        With gclsDB.Records("PKAP00")
                            If RTrim(.Item("PaletteID")) = RTrim(wstrKeyValue(0)) AndAlso RTrim(.Item("ItemGr")) = RTrim(wstrKeyValue(1)) Then
                                wintQty += 1
                            End If
                            If .Read(BtLib.Operation.GetNext) <> 0 Then Exit Do
                        End With
                    Loop
                    If pstrTotalQtyFlg = "0" Then 'AP作成時
                        gstrctPLDispData.intTotalCnt = wintQty
                    Else 'AP作成後
                        rtn = fGetTotalQty(gstrctPLDispData.intTotalCnt)
                        If rtn = -1 Then
                            Exit Function
                        End If
                        gstrctPLDispData.intCheckCnt = gstrctPLDispData.intTotalCnt - wintQty
                    End If
                    fSearchPackAP3 = 1
                Case Else
                    fSearchPackAP3 = 0
            End Select

        Catch ex As Exception
            sErrLog(gAppName, "fSearchPackAP2", ex.ToString)
        Finally
            If rtn <> -1 Then gclsDB.fCloseRecord("PKAP00")
        End Try
    End Function


    '/*
    '||梱包台板ラベルの分解
    '||
    '*/
    Public Sub sDecodeScanData2AicCase(ByRef AicCase As STRCT_KEY_AICCASE, ByVal ScnLabelData As String)
        With AicCase
            .Msc = Mid(ScnLabelData, 2, 7)
            .Msc_Disp = Mid(ScnLabelData, 2, 4) & "-" & Mid(ScnLabelData, 6, 3)
            .Pnt = Mid(ScnLabelData, 9, 3)
            .Trm = Mid(ScnLabelData, 12, 3)
            .Lot = Mid(ScnLabelData, 15, 3)
            .Gr = Mid(ScnLabelData, 18, 2)
            .Seq = Mid(ScnLabelData, 20, 2)
            .CaseNo = .Lot & "-" & .Gr & "-" & .Seq
            .YYYYMMDD = Mid(ScnLabelData, 22, 8)
            .Line = Mid(ScnLabelData, 30, 2)
            .AicCaseNo = .Msc & .Pnt & .Trm & .Lot & .Gr & .Seq

        End With
    End Sub
    '/*
    '|| 関数名：fUpdateHTCTRL4PL
    '|| 概要　：パレタイズ用HTCTRL更新
    '|| 引数　：
    '|| 戻り値：-1:NG 0:YES
    '*/
    Public Function fUpdateHTCTRL4PL() As Integer

        Dim wstrKey(0) As String
        Dim wstrKeyValue(0) As String
        Dim wintRet As Integer

        '検索キー設定
        wstrKey(0) = "HTNumber"
        wstrKeyValue(0) = gstrctKDHT.strHdNo

        fUpdateHTCTRL4PL = -1

        Try

            gclsDB.Setcolumn = ("SearchKey")
            If gstrctKDHT.strPrgID = G_CONST_PGID_1000 OrElse gstrctKDHT.strPrgID = G_CONST_PGID_2000 OrElse gstrctKDHT.strPrgID = G_CONST_PGID_3000 Then
                gclsDB.SetData = gstrctAicCase.KeyData
            End If

            wstrKey(0) = "HTNumber"
            wstrKeyValue(0) = gstrctKDHT.strHdNo
            If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
                Exit Function
            End If

            fUpdateHTCTRL4PL = 0

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

    '/*
    '|| 関数名：fGetTotalQty
    '|| 概要　：総計数取得
    '|| 引数　：
    '|| 戻り値：-1:NG 0:YES
    '*/
    Public Function fGetTotalQty(ByRef pstrQty As String) As Integer
        Dim wstrKey(10) As String
        Dim wstrKeyValue(10) As String
        Dim ret As Integer
        fGetTotalQty = -1
        pstrQty = -1
        wstrKey(0) = "PicsType"
        wstrKey(1) = "CstmId"
        wstrKey(2) = "CstmCode"
        wstrKey(3) = "Kukuri"
        wstrKey(4) = "Line"
        wstrKey(5) = "Msc"
        wstrKey(6) = "Pnt"
        wstrKey(7) = "Trm"
        wstrKey(8) = "Lot"
        wstrKey(9) = "Gr"
        wstrKey(10) = "Seq"
        wstrKeyValue(0) = gstrctKDHT.strPackType
        wstrKeyValue(1) = gstrctKDHT.strCstmid
        wstrKeyValue(2) = gstrctKDHT.strCstmCode
        wstrKeyValue(3) = gstrctAicCase.YYYYMMDD
        wstrKeyValue(4) = gstrctAicCase.Line
        wstrKeyValue(5) = gstrctAicCase.Msc
        wstrKeyValue(6) = gstrctAicCase.Pnt
        wstrKeyValue(7) = gstrctAicCase.Trm
        wstrKeyValue(8) = gstrctAicCase.Lot
        wstrKeyValue(9) = gstrctAicCase.Gr
        wstrKeyValue(10) = gstrctAicCase.Seq
        Try
            ret = gclsDB.fOpenRecord(G_CONST_TBL_PLAN, G_CONST_KEY_PLANKEY00, "PLAN00", wstrKey, wstrKeyValue, BtLib.Operation.GetEqual)
            If ret = -1 Then
                Exit Function
            ElseIf ret <> 0 Then
                gclsDB.fCloseRecord("PLAN00")
                Exit Function
            End If
            pstrQty = Val(RTrim(gclsDB.Records("PLAN00").Item("CheckQty")))
            gclsDB.fCloseRecord("PLAN00")
            fGetTotalQty = 1
        Catch ex As Exception
            sErrLog(gAppName, "fGetTotalQty", ex.ToString)
        End Try
    End Function
End Module
