'*******************************************************************************
'* 作成者   : 株式会社エコー・システム 
'* 作成日   : 2005/07/25
'* 概要     : PICS2004 共通処理
'* 更新履歴 : 
'* ﾊﾞｰｼﾞｮﾝ  : 1.0
'*******************************************************************************
Imports System.Text
Imports WarpControl
Imports WarpControl.WarpApi2
Module modPublicPL000
    Public Structure ITEMINCODE_STR

        Public astrItemInCode() As String
        Public intIndex As Integer

        Public Sub initialize()
            Erase astrItemInCode
            intIndex = -1
        End Sub

        Public Sub ProcItem(ByVal strItemInCode As String)

            Dim i As Integer
            Dim flag As Boolean

            If intIndex = -1 Then
                ReDim astrItemInCode(0)
                astrItemInCode(0) = strItemInCode
                intIndex = 1
            Else
                For i = 0 To intIndex - 1
                    If astrItemInCode(i) = strItemInCode Then
                        Exit Sub
                    End If
                Next
                ReDim Preserve astrItemInCode(intIndex)
                astrItemInCode(intIndex) = strItemInCode
                intIndex += 1
            End If

        End Sub
    End Structure


    '/*
    '|| 関数名：fGetCountry
    '|| 概要  ：国コード取得
    '*/
    Public Function fGetCountry() As Integer

        '端末ID確認＆国ｺｰﾄﾞ入力
        Dim wintCnt As Integer
        Dim wstrWhere As String
        Dim wstrCount As String
        Dim wstrUserNo As String
        Dim wstrKeyFlg As String
        Dim wstrKey(0) As String, wstrKeyValue(0) As String

        fGetCountry = -1

        Try

            BtLib.Transaction.BeginConCurrent()

            wstrWhere = ""
            wstrWhere = "@HTNumber = " & gstrctKDHT.strHdNo
            wintCnt = gclsDB.fGetData(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrWhere, "HTCTRL1")

            If wintCnt > 0 Then             'ﾃﾞｰﾀがあれば国ｺｰﾄﾞを検索

                With gclsDB.Extended("HTCTRL1")

                    wstrCount = fstrZeroput(3, .Item("Country"))    '前Zero埋め
                    wstrUserNo = .Item("EmplCode")
                    gstrctKDHT.strSectionID = .Item("SectionID")
                    gstrctKDHT.strSection = .Item("Section")
                    gstrctKDHT.strCstmid = .Item("Cstmid")
                    gstrctKDHT.strCstmCode = .Item("CstmCode")
                    gstrctKDHT.strPackType = .Item("PackType")
                    gstrctKDHT.strS6030 = .Item("S6030")
                    'gstrctKDPL.initialize()
                    gstrctKDPL.strSectionID = RTrim(.Item("SectionID"))
                    gstrctKDPL.strSection = RTrim(.Item("Section"))
                    gstrctKDPL.strSearchKey = RTrim(.Item("SearchKey"))

                    If wstrCount <> "000" Then        '国ｺｰﾄﾞが入力されている

                        gstrctKDHT.strCountry = wstrCount
                        gstrctKDHT.strUserNo = wstrUserNo
                        gclsDB.fCloseRecord("HTCTRL1")
                        fGetCountry = 0
                        Exit Function

                    ElseIf wstrCount = "000" Then     '国ｺｰﾄﾞが入力されていなければ"001"を入力する

                        wstrCount = "001"
                        wstrKeyFlg = "0"

                    End If
                End With

            ElseIf wintCnt = 0 Then       'ﾃﾞｰﾀがなければ端末番号・国ｺｰﾄﾞ("001")を追加する

                With gclsDB.Extended("HTCTRL1")
                    wstrCount = "001"
                    wstrKeyFlg = "1"
                End With

            ElseIf wintCnt = -1 Then
                gclsDB.fCloseRecord("HTCTRL1")
                Exit Function

            End If

            gclsDB.fCloseRecord("HTCTRL1")

            '更新ﾃﾞｰﾀ、新規ﾃﾞｰﾀをDBに登録
            gstrctKDHT.strCountry = "001"
            Select Case wstrKeyFlg

                Case "0"        '更新ﾃﾞｰﾀ

                    gclsDB.Setcolumn = ("Country")          '登録/更新対象列名
                    gclsDB.SetData = (gstrctKDHT.strCountry)      '登録/更新データ

                    wstrKey(0) = "HTNumber"
                    wstrKeyValue(0) = gstrctKDHT.strHdNo
                    If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
                        BtLib.Transaction.Abort()

                    End If

                Case "1"        '新規ﾃﾞｰﾀ
                    gclsDB.Setcolumn = ("HTNumber,Country")          '登録/更新対象列名
                    gclsDB.SetData = (gstrctKDHT.strHdNo & "," & gstrctKDHT.strCountry)      '登録/更新データ

                    If gclsDB.fInsRecord(G_CONST_TBL_HTCTRL) = -1 Then
                        BtLib.Transaction.Abort()

                    End If

            End Select

            BtLib.Transaction.End()

            fGetCountry = 0

            Exit Function

        Catch ex As Exception
            BtLib.Transaction.Abort()
            sErrLog(gAppName, "fGetCountry", ex.ToString)
        End Try

    End Function


    '/*
    '|| 関数名：fGetTitle
    '|| 概要  ：タイトルコード取得
    '*/
    Public Function fGetTitle() As Integer

        Dim wstrWhere As String
        Dim wstrCount As String
        Dim wintCnt As Integer
        Dim wstrKey(0) As String, wstrKeyValue(0) As String

        fGetTitle = -1

        Try

            BtLib.Transaction.BeginConCurrent()

            wstrWhere = ""
            wstrWhere = "@Country = " & gstrctKDHT.strCountry
            wstrWhere &= " & @PrgID = " & gstrctKDHT.strPrgID

            wintCnt = gclsDB.fGetData(G_CONST_TBL_HTTITLE, G_CONST_KEY_HTTITLEKEY00, wstrWhere, "TITLE1")

            If wintCnt > 0 Then

                gstrctKDHT.strTitle = gclsDB.Extended("TITLE1").Item("Title")


            ElseIf wintCnt = 0 Then
                gclsDB.fCloseRecord("TITLE1")
                fGetTitle = -2
                Exit Function

            ElseIf wintCnt = -1 Then
                gclsDB.fCloseRecord("TITLE1")
                Exit Function

            End If

            gclsDB.fCloseRecord("TITLE1")

            gclsDB.Setcolumn = ("ExeName")              '登録/更新対象列名
            gclsDB.SetData = (gstrctKDHT.strPrgID)     '登録/更新データ

            wstrKey(0) = "HTNumber"
            wstrKeyValue(0) = gstrctKDHT.strHdNo

            If gclsDB.fUpdRecords(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, wstrKey, wstrKeyValue) = -1 Then
                BtLib.Transaction.Abort()

            End If

            BtLib.Transaction.End()

            fGetTitle = 0

        Catch ex As Exception
            BtLib.Transaction.Abort()
            sErrLog(gAppName, "fGetTitle", ex.ToString)
        End Try

    End Function


    '/*
    '|| 関数名：fGetEmplName
    '|| 概要  ：作業者名取得
    '*/
    Public Function fGetEmplName(ByVal pstrUserNo As String) As Integer

        Dim rtn As Integer
        Dim wstrWhere As String
        Dim wintCnt As Integer

        fGetEmplName = -1

        Try

            '検索を開始します
            wstrWhere = ""
            wstrWhere = "@EmplCode = " & pstrUserNo

            wintCnt = gclsDB.fGetData(G_CONST_TBL_PICSUSER, G_CONST_KEY_PICSUSERKEY00, wstrWhere, "USER1")
            If wintCnt > 0 Then

                gstrctKDHT.strUserName = gclsDB.Extended("USER1").Item("EmplName")

            ElseIf wintCnt = 0 Then

                fGetEmplName = -2
                gclsDB.fCloseRecord("USER1")
                Exit Function

            ElseIf wintCnt = -1 Then

                gclsDB.fCloseRecord("USER1")
                Exit Function

            End If

            gclsDB.fCloseRecord("USER1")
            fGetEmplName = 0

            Exit Function

        Catch ex As Exception
            sErrLog(gAppName, "fGetEmplName", ex.ToString)
        End Try
    End Function
    '/*
    '|| 関数名： 
    '|| 概要  ：SPMからCartonTypeを読み込む
    '*/
    Public Function fGetCartonType(ByVal wstrDBName As String, ByRef pstrCartonType As String) As Integer

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

        Try
            fGetCartonType = -1

            With gclsDB.Records(wstrDBName)

                '検索キー設定
                wstrKey(0) = "YYYYMMDD"
                wstrKey(1) = "PicsType"
                wstrKey(2) = "CstmId"
                wstrKey(3) = "CstmCode"
                wstrKey(4) = "Msc"
                wstrKey(5) = "Pnt"
                wstrKey(6) = "Trm"
                wstrKey(7) = "Gr"
                wstrKey(8) = "PartNo"
                wstrKey(9) = "PartCL"
                wstrKey(10) = "PartZnc"
                wstrKey(11) = "PartID"
                wstrKeyValue(0) = RTrim(.Item("WKSumDate"))
                wstrKeyValue(1) = RTrim(.Item("PicsType"))
                wstrKeyValue(2) = RTrim(.Item("Cstmid"))
                wstrKeyValue(3) = RTrim(.Item("CstmCode"))
                wstrKeyValue(4) = RTrim(Mid(.Item("ItemCode"), 1, 7))
                wstrKeyValue(5) = RTrim(Mid(.Item("ItemCode"), 8, 3))
                wstrKeyValue(6) = RTrim(Mid(.Item("ItemCode"), 11, 3))
                wstrKeyValue(7) = RTrim(Mid(.Item("ItemCode"), 17, 2))
                wstrKeyValue(8) = RTrim(Mid(.Item("ItemInCode"), 1, 10))
                wstrKeyValue(9) = RTrim(Mid(.Item("ItemInCode"), 11, 2))
                wstrKeyValue(10) = RTrim(Mid(.Item("ItemInCode"), 13, 1))
                wstrKeyValue(11) = RTrim(.Item("PartID"))

                '検索処理
                wintRet = gclsDB.fOpenRecord(G_CONST_TBL_SPM, G_CONST_KEY_SPMKEY00, "SPM1", wstrKey, wstrKeyValue, BtLib.Operation.GetEqual)
                If wintRet <> 0 Then
                    'DBエラーの場合
                    gclsDB.fCloseRecord("SPM1")
                    Exit Function
                End If

                If wintRet = 0 Then
                    'Keyがある場合
                    pstrCartonType = RTrim(gclsDB.Records("SPM1").Item("CartonType"))
                End If
                gclsDB.fCloseRecord("SPM1")

            End With

            fGetCartonType = 0
            Exit Function

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

    End Function
    '/*
    '|| 関数名： 
    '|| 概要  ：PLANからKasoLineを読み込む
    '*/
    Public Function fGetKasoLine(ByVal wstrDBName As String, ByRef pstrKasoLine As String) As Integer

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

        Try
            fGetKasoLine = -1

            With gclsDB.Records(wstrDBName)


                '検索キー設定
                wstrKey(0) = "PicsType"
                wstrKey(1) = "CstmId"
                wstrKey(2) = "CstmCode"
                wstrKey(3) = "Line"
                wstrKey(4) = "YYYYMMDDS"
                wstrKey(5) = "Msc"
                wstrKey(6) = "Pnt"
                wstrKey(7) = "Trm"
                wstrKey(8) = "Lot"
                wstrKey(9) = "Gr"
                wstrKey(10) = "Seq"
                wstrKeyValue(0) = .Item("PicsType")
                wstrKeyValue(1) = .Item("Cstmid")
                wstrKeyValue(2) = .Item("CstmCode")
                wstrKeyValue(3) = .Item("Line")
                wstrKeyValue(4) = .Item("ProductDate") & .Item("Shift")
                wstrKeyValue(5) = Mid(.Item("ItemCode"), 1, 7)
                wstrKeyValue(6) = Mid(.Item("ItemCode"), 8, 3)
                wstrKeyValue(7) = Mid(.Item("ItemCode"), 11, 3)
                wstrKeyValue(8) = Mid(.Item("ItemCode"), 14, 3)
                wstrKeyValue(9) = Mid(.Item("ItemCode"), 17, 2)
                wstrKeyValue(10) = Mid(.Item("ItemCode"), 19, 2)

                '検索処理
                wintRet = gclsDB.fOpenRecord(G_CONST_TBL_PLAN, G_CONST_KEY_PLANKEY02, "PLN2", wstrKey, wstrKeyValue, BtLib.Operation.GetEqual)
                If wintRet <> 0 Then
                    'DBエラーの場合
                    gclsDB.fCloseRecord("PLN2")
                    Exit Function
                End If

                If wintRet = 0 Then
                    'Keyがある場合
                    pstrKasoLine = RTrim(gclsDB.Records("PLN2").Item("KasoLine")).PadRight(2, " ")
                End If
                gclsDB.fCloseRecord("PLN2")

            End With

            fGetKasoLine = 0
            Exit Function

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

    End Function
    '/*
    '|| 関数名：fUpdPalleteQty
    '|| 概要  ：パレット数を更新
    '*/
    Public Function fUpdPaletteQty(ByVal intFlg As Integer) As Integer
        Dim wstrKey(2), wstrKeyValue(2) As String
        Dim ret As Integer
        Dim wstrWorkQty As String
        Dim wintQty As Integer
        fUpdPaletteQty = -1
        wstrKey(0) = "CenterCode"
        wstrKey(1) = "SectionID"
        wstrKey(2) = "Section"
        wstrKeyValue(0) = gstrctKDPL.strCenterCode
        wstrKeyValue(1) = gstrctKDPL.strSectionID
        wstrKeyValue(2) = gstrctKDPL.strSection
        Try
            ret = gclsDB.fOpenRecord(G_CONST_TBL_SECTION, G_CONST_KEY_SECTIONKEY00, "SEC00", wstrKey, wstrKeyValue, BtLib.Operation.GetEqual)
            If ret = -1 Then
                Exit Function
            ElseIf ret <> 0 Then
                gclsDB.fCloseRecord("SEC00")
                Exit Function
            End If
            wstrWorkQty = gclsDB.Records("SEC00").Item("Seq")
            gclsDB.fCloseRecord("SEC00")
            If IsNumeric(wstrWorkQty) Then
                wintQty = CInt(wstrWorkQty)
            Else
                wintQty = 0
            End If
            If intFlg = 1 Then '加算
                If wintQty >= G_CONST_MAX_PALT Then
                    fUpdPaletteQty = 0
                    Exit Function
                Else
                    wintQty += 1
                End If
            ElseIf intFlg = 0 Then  '減算
                If wintQty <= 0 Then
                    wintQty = 0
                Else
                    wintQty -= 1
                End If
            End If
            With gclsDB
                .Setcolumn = "Seq"
                .SetData = CStr(wintQty).PadLeft(6, "0")
                .fUpdRecords(G_CONST_TBL_SECTION, G_CONST_KEY_SECTIONKEY00, wstrKey, wstrKeyValue)
            End With

            fUpdPaletteQty = 1
        Catch ex As Exception
            sErrLog(gAppName, "fUpdPaletteQty", ex.ToString)
        End Try
    End Function

    '/*
    '|| 関数名：fSrchHTCTRL
    '|| 概要  ：SrchKeyを検索
    '*/
    Public Function fSrchHTCTRL(ByVal pstrSrchKey As String) As Integer
        Dim wstrKey(1), wstrKeyValue(1) As String
        Dim wintRet As Integer
        fSrchHTCTRL = -1
        Try
            wstrKey(0) = "SearchKey"
            wstrKey(1) = "HTNumber"
            wstrKeyValue(0) = pstrSrchKey
            wstrKeyValue(1) = Space(8)
            wintRet = gclsDB.fOpenRecord(G_CONST_TBL_HTCTRL, G_CONST_KEY_HTCTRLKEY00, "HTCTRL01", wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)

            If wintRet = -1 Then
                Exit Function
            ElseIf wintRet <> 0 Then
                fSrchHTCTRL = 0
            Else
                Do

                    With gclsDB.Records("HTCTRL01")
                        If RTrim(wstrKeyValue(0)) = RTrim(.Item("SearchKey")) Then
                            fSrchHTCTRL = 1
                            Exit Do
                        Else
                            If .Read(BtLib.Operation.GetNext) <> 0 Then
                                fSrchHTCTRL = 0
                                Exit Do
                            End If
                        End If
                    End With
                Loop
            End If
            gclsDB.fCloseRecord("HTCTRL01")
        Catch ex As Exception
            sErrLog(gAppName, "fSrchHTCTRL", ex.ToString)
        End Try
    End Function

End Module
