Imports System.Text

'*******************************************************************************
'* 作成者   : 株式会社エコー・システム 
'* 作成日   : 2004/10/02
'* 概要     : PICS2004 共通処理
'* 更新履歴 : 
'* ﾊﾞｰｼﾞｮﾝ  : 1.0
'*******************************************************************************

Imports WarpControl
Imports WarpControl.WarpApi2

Module modPublic050

    '/*
    '|| 関数名：fEditIPAddress
    '|| 概要  ：IPアドレス編集
    '|| 引数　：IPアドレス
    '|| 戻り値：-1(NG)   0(OK)  
    '*/
    Public Function fEditIPAddress(ByRef pstrIPAddress As String) As Integer
        Dim wstrIP As String
        fEditIPAddress = -1
        Try
            If Not IsNumeric(pstrIPAddress) Then Exit Function
            wstrIP = fIntZerosapress(Mid(pstrIPAddress, 1, 3)) & "." & fIntZerosapress(Mid(pstrIPAddress, 4, 3)) & "." _
              & fIntZerosapress(Mid(pstrIPAddress, 7, 3)) & "." & fIntZerosapress(Mid(pstrIPAddress, 10, 3))

            pstrIPAddress = wstrIP

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

    '/*
    '|| 関数名：fMakeFolder
    '|| 概要  ：フォルダを作成
    '|| 引数　：1.フォルダ名 2.フォルダ属性
    '|| 戻り値：-1(NG)   0(OK)  
    '*/
    Public Function fMakeFolder(ByVal pstrFolder As String, ByVal intAttr As Integer) As Integer
        Dim wstrWkDir As String
        fMakeFolder = -1

        Try
            'フォルダの存在チェック
            wstrWkDir = Dir(pstrFolder, FileAttribute.Directory)

            If wstrWkDir = "" Then
                MkDir(pstrFolder)
                SetAttr(pstrFolder, intAttr)
            End If
            fMakeFolder = 0

        Catch ex As Exception
            sErrLog(gAppName, "fMakeFolder", ex.ToString)
        End Try
    End Function
    '/*
    '|| 関数名：fGetPrintKind（大物出庫用)
    '|| 概要  ：PACKCPをPickFlag=0 or PickFlag = 3のItemCodeで検索して
    '||         支障ﾗﾍﾞﾙまたはｺﾝﾃﾝﾂﾘｽﾄの判定をする
    '|| 引数　：1.ItemCode
    '||         2.キー
    '|| 戻り値：-1(NG)  -2(支障ﾗﾍﾞﾙ) 0:(ｺﾝﾃﾝﾂﾘｽﾄ)
    '*/
    Public Function fGetPrintKind(ByVal pstrItemCode As String, ByVal pstrKey As String) As Integer
        Dim wintAllCnt As Integer
        Dim wintICnt As Integer
        Dim wintCCnt As Integer
        fGetPrintKind = -1
        wintAllCnt = 0
        wintICnt = 0
        wintCCnt = 0
        Try
            With gclsDB.Records(pstrKey)
                Do
                    If RTrim(pstrItemCode) = RTrim(.Item("ItemCode")) Then
                        wintAllCnt += 1
                        If RTrim(.Item("PickFlag")) = "0" Then     'NotPacked
                            wintICnt += 1
                        ElseIf RTrim(.Item("PickFlag")) = "3" Then 'Packed (ZeroOKも含む)
                            wintCCnt += 1
                        End If
                    Else
                        Exit Do
                    End If
                    If .Read(BtLib.Operation.GetNext) <> 0 Then Exit Do
                Loop
                If (wintICnt > 0) Then          '支障ﾗﾍﾞﾙ
                    fGetPrintKind = -2
                    Exit Function
                End If
                If (wintAllCnt = wintCCnt) Then 'ｺﾝﾃﾝﾂﾘｽﾄ
                    fGetPrintKind = 0
                    Exit Function
                End If
            End With

        Catch ex As Exception
            sErrLog(gAppName, "fGetPrintKind", ex.ToString)
        End Try
    End Function
    '/*
    '|| 関数名：fMakeLabelData
    '|| 概要  ：PACKCP,PACKCJをItemCodeGrpで検索して印刷用ラベルデータを作成
    '|| 引数　：1.ItemGr
    '||         2.ファイルパス
    '||         3.支障ﾌﾗｸﾞ  0:通常出庫 1:支障処理
    '|| 戻り値：-1(NG)   0:(OK)
    '*/
    Public Function fMakeLabelData(ByVal pstrItemCodeGrp As String, ByVal pstrFilePath As String, Optional ByVal intSisyoFlg As Integer = 0) As Integer
        Dim intType() As PRINT_TYPE
        Dim strRecDat() As String
        Dim wstrctRec As LABEL_DATA_STR
        Dim lngIndex As Long
        Dim i, j As Long
        Dim rtn1, rtn2, rtn3 As Integer
        Dim wlngStartNo, wlngEndNo As Long
        Dim wstrKeyPKCP = "KEY1"
        Dim wstrKeyPKCJ = "KEY2"
        Dim wstrItemCode As String
        Dim wstrKey(6), wstrKeyValue(6) As String
        Dim wstrKey2(2), wstrKeyValue2(2) As String
        Dim wbUpdAPFlag As Boolean = False

        fMakeLabelData = -1

        Try
            fPrintInit(pstrFilePath)
            lngIndex = 0
            wlngStartNo = gstrctPPDisp.strCrtnFrom
            wlngEndNo = gstrctPPDisp.strCrtnTo
            wstrKey(0) = "ItemGr" : wstrKeyValue(0) = RTrim(pstrItemCodeGrp)
            wstrKey(1) = "ItemCode" : wstrKeyValue(1) = Space(30)
            wstrKey(2) = "SortLctn1" : wstrKeyValue(2) = Space(5)
            wstrKey(3) = "SortLctn2" : wstrKeyValue(3) = Space(5)
            wstrKey(4) = "SortLctn3" : wstrKeyValue(4) = Space(5)
            wstrKey(5) = "ItemInCode" : wstrKeyValue(5) = Space(30)
            wstrKey(6) = "TempSerial" : wstrKeyValue(6) = Space(6)

            rtn1 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCP, G_CONST_KEY_PACKCPKEY03, wstrKeyPKCP, wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
            If rtn1 = -1 Then
                'DBエラー
                gclsDB.fCloseRecord(wstrKeyPKCP)
                Exit Function
            End If
            wstrKey2(0) = "ItemCode" : wstrKeyValue2(0) = Mid(RTrim(pstrItemCodeGrp), 1, 25)
            wstrKey2(1) = "ItemInCode" : wstrKeyValue2(1) = Space(30)
            wstrKey2(2) = "PlanSerial" : wstrKeyValue2(2) = Space(6)
            rtn2 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY06, wstrKeyPKCJ, wstrKey2, wstrKeyValue2, BtLib.Operation.GetGreaterOrEqual)
            If rtn2 = -1 Then
                'DBエラー
                gclsDB.fCloseRecord(wstrKeyPKCP)
                gclsDB.fCloseRecord(wstrKeyPKCJ)
                Exit Function
            End If

            j = 0
            For i = wlngStartNo To wlngEndNo 'カートンFrom カートンTo分繰り返し
                '通常出庫及び支障処理時の該当ｶｰﾄﾝﾗﾍﾞﾙ発行処理
                If intSisyoFlg = 0 Or (intSisyoFlg = 1 And i = CLng(gstrctPPDisp.strCrtnNo)) Then
                    'PACKCPを検索
                    wstrItemCode = Mid(gstrctKDPP.strItemGr, 1, 20) & CStr(wlngStartNo + lngIndex).PadLeft(5, "0")
                    rtn1 = fSearchPackCP2(wstrItemCode, wstrKeyPKCP)
                    If rtn1 = -1 Then
                        'DBエラー
                        gclsDB.fCloseRecord(wstrKeyPKCP)
                        gclsDB.fCloseRecord(wstrKeyPKCJ)
                        Exit Function
                    ElseIf rtn1 = -2 Then
                        gclsDB.fCloseRecord(wstrKeyPKCP)
                        rtn3 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCP, G_CONST_KEY_PACKCPKEY03, wstrKeyPKCP, wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
                        If rtn3 = -1 Then
                            'DBエラー
                            gclsDB.fCloseRecord(wstrKeyPKCP)
                            gclsDB.fCloseRecord(wstrKeyPKCJ)
                            Exit Function
                        End If
                    End If

                    If rtn1 = 0 Then
                        With wstrctRec
                            .strAIC = gstrctPPDisp.strAic
                            .strCaseNO = gstrctPPDisp.strCaseNo
                            .strCarton = Mid(RTrim(gclsDB.Records(wstrKeyPKCP).Item("ItemCode")), 21, 5)
                            ReDim Preserve intType(j)
                            If RTrim(gclsDB.Records(wstrKeyPKCP).Item("ItemID")) = "C" Then
                                intType(j) = modPrint.PRINT_TYPE.NotPackLabel
                            Else
                                intType(j) = modPrint.PRINT_TYPE.OilNotPackLabel
                            End If
                            ReDim Preserve strRecDat(j)
                            strRecDat(j) = .strAIC.PadRight(13, " ") & .strCaseNO.PadRight(7, " ") & _
                              .strCarton.PadRight(5, " ") & Space(5) & "PACKCJ"
                            j += 1
                        End With
                    Else '一件もPACKCPに無し
                        rtn2 = fSearchPackCJ(wstrItemCode, wstrKeyPKCJ)
                        If rtn2 = -1 Then
                            gclsDB.fCloseRecord(wstrKeyPKCP)
                            gclsDB.fCloseRecord(wstrKeyPKCJ)
                            Exit Function
                        ElseIf rtn2 = -2 Then
                            gclsDB.fCloseRecord(wstrKeyPKCJ)
                            rtn3 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCJ, G_CONST_KEY_PACKCJKEY06, wstrKeyPKCJ, wstrKey2, wstrKeyValue2, BtLib.Operation.GetGreaterOrEqual)
                            If rtn3 = -1 Then
                                'DBエラー
                                gclsDB.fCloseRecord(wstrKeyPKCP)
                                gclsDB.fCloseRecord(wstrKeyPKCJ)
                                Exit Function
                            End If
                        Else
                            With wstrctRec
                                .strAIC = gstrctPPDisp.strAic
                                .strCaseNO = gstrctPPDisp.strCaseNo
                                .strCarton = Mid(RTrim(gclsDB.Records(wstrKeyPKCJ).Item("ItemCode")), 21, 5)
                                j += 1
                                ReDim Preserve intType(j)
                                If RTrim(gclsDB.Records(wstrKeyPKCJ).Item("ItemID")) = "C" Then
                                    intType(j - 1) = modPrint.PRINT_TYPE.ContentsList
                                    intType(j) = modPrint.PRINT_TYPE.CartonLabel
                                Else
                                    intType(j - 1) = modPrint.PRINT_TYPE.OilContentsList
                                    intType(j) = modPrint.PRINT_TYPE.OilCartonLabel
                                End If
                                ReDim Preserve strRecDat(j)
                                strRecDat(j - 1) = .strAIC.PadRight(13, " ") & .strCaseNO.PadRight(7, " ") & _
                                  .strCarton.PadRight(5, " ") & Space(5) & "PACKCJ"
                                strRecDat(j) = .strAIC.PadRight(13, " ") & .strCaseNO.PadRight(7, " ") & _
                                  .strCarton.PadRight(5, " ") & Space(5) & "PACKCJ"
                                j += 1
                            End With
                            wbUpdAPFlag = True
                        End If
                    End If
                End If
                lngIndex += 1
            Next

            gclsDB.fCloseRecord(wstrKeyPKCP)
            gclsDB.fCloseRecord(wstrKeyPKCJ)

            rtn3 = fPrintProc2(intType, strRecDat, pstrFilePath)
            If rtn3 = 0 Then
                fMakeLabelData = 0
            End If

        Catch ex As Exception
            sErrLog(gAppName, "fMakeLabelData", ex.ToString)
        End Try
    End Function
    '/*
    '|| 関数名：fMakeLabelData2
    '|| 概要  ：PACKCPをItemCodeGrpで検索して印刷用ラベルデータを作成（大物出庫用)
    '|| 引数　：1.ItemGr
    '||         2.ファイルパス
    '||         3.支障ﾌﾗｸﾞ
    '||           0: 通常出庫 1:支障処理
    '|| 戻り値：-1(NG)   0:(OK)
    '*/
    Public Function fMakeLabelData2(ByVal pstrItemCodeGrp As String, ByVal pstrFilePath As String, Optional ByVal intSisyoFlg As Integer = 0) As Integer
        Dim lngIndex As Long
        Dim i As Long
        Dim rtn1, rtn2 As Integer
        Dim wlngStartNo, wlngEndNo As Long
        Dim wstrItemCode As String
        Dim wstrKey(6), wstrKeyValue(6) As String
        Dim strRecDat() As String
        Dim wstrctRec As LABEL_DATA_STR
        Dim wstrKeyPKCP = "KEY1"

        fMakeLabelData2 = -1
        Try

            wlngStartNo = gstrctPPDisp.strCrtnFrom
            wlngEndNo = gstrctPPDisp.strCrtnTo

            wstrKey(0) = "ItemGr" : wstrKeyValue(0) = RTrim(pstrItemCodeGrp)
            wstrKey(1) = "ItemCode" : wstrKeyValue(1) = Space(30)
            wstrKey(2) = "SortLctn1" : wstrKeyValue(2) = Space(5)
            wstrKey(3) = "SortLctn2" : wstrKeyValue(3) = Space(5)
            wstrKey(4) = "SortLctn3" : wstrKeyValue(4) = Space(5)
            wstrKey(5) = "ItemInCode" : wstrKeyValue(5) = Space(30)
            wstrKey(6) = "TempSerial" : wstrKeyValue(6) = Space(6)

            rtn1 = gclsDB.fOpenRecord(G_CONST_TBL_PACKCP, G_CONST_KEY_PACKCPKEY03, wstrKeyPKCP, wstrKey, wstrKeyValue, BtLib.Operation.GetGreaterOrEqual)
            If rtn1 = -1 Then
                'DBエラー
                gclsDB.fCloseRecord(wstrKeyPKCP)
                sErrLog(gAppName, "fMakeLabelData2", gclsDB.ErrMsg)
                Exit Function
            End If

            fPrintInit(pstrFilePath)

            lngIndex = 0
            For i = wlngStartNo To wlngEndNo 'カートンFrom カートンTo分繰り返し
                '通常出庫及び支障処理時の該当ｶｰﾄﾝﾗﾍﾞﾙ発行処理
                If intSisyoFlg = 0 Or (intSisyoFlg = 1 And i = CLng(gstrctPPDisp.strCrtnNo)) Then
                    Erase strRecDat
                    'PACKCPを検索
                    wstrItemCode = Mid(gstrctKDPP.strItemGr, 1, 20) & CStr(wlngStartNo + CLng(lngIndex)).PadLeft(5, "0")
                    rtn2 = fGetPrintKind(wstrItemCode, wstrKeyPKCP) 'PickFlag = "0" or "3" のみ検索対象
                    If rtn2 = -1 Then '一件もPACKCPに無し
                        gclsDB.fCloseRecord(wstrKeyPKCP)
                        Exit Function
                    Else

                        With wstrctRec
                            .strAIC = gstrctPPDisp.strAic
                            .strCaseNO = gstrctPPDisp.strCaseNo
                            .strCarton = Mid(wstrItemCode, 21, 5)
                            ReDim strRecDat(0)
                            strRecDat(0) = .strAIC.PadRight(13, " ") & .strCaseNO.PadRight(7, " ") & _
                              .strCarton.PadRight(5, "0") & Space(5) & "PACKCP"
                        End With

                        If rtn2 = 0 Then      'ｺﾝﾃﾝﾂﾘｽﾄ
                            fPrintProc(modPrint.PRINT_TYPE.ContentsList, strRecDat, pstrFilePath)
                        ElseIf rtn2 = -2 Then '支障ﾗﾍﾞﾙ
                            fPrintProc(modPrint.PRINT_TYPE.NotPackLabel, strRecDat, pstrFilePath)
                        End If
                    End If
                End If
                lngIndex += 1
            Next

            gclsDB.fCloseRecord(wstrKeyPKCP)
            fMakeLabelData2 = 0
        Catch ex As Exception
            sErrLog(gAppName, "fMakeLabelData2", ex.ToString)
        End Try
    End Function
    '/*
    '|| 関数名：fSendFTP
    '|| 概要  ：ラベルデータ転送処理
    '|| 引数　：1.IPアドレス
    '||         2.データファイルパス
    '|| 戻り値： 0(OK)  -1(FTPｴﾗｰ)
    '*/
    Public Function fSendFTP(ByVal pstrIPAddress As String, ByVal pstrPath As String)
        Dim ret As Integer
        Dim ff As clsFTP

        fSendFTP = -1

        Try

            ff = New clsFTP(pstrIPAddress, _
                        "/PICS2004/", _
                        "anonymous", _
                        "", _
                        21)

            If (ff.Login() = True) Then
                ff.UploadFile(pstrPath)
            End If
            fSendFTP = 0
        Catch ex As Exception
            fSendFTP = -1
            sErrLog(gAppName, "fSendFTP", ex.ToString)

        Finally

            Try
                If Not (ff.flag_bool = True) Then
                    fSendFTP = -1
                End If
                ff.CloseConnection()
            Catch ex As Exception
                fSendFTP = -1
                sErrLog(gAppName, "fSendFTP", ex.ToString)
            End Try

        End Try
    End Function
    '/*
    '|| 関数名：fInitItemCode
    '|| 概要  ：ﾁｪｯｸ用ItemCodeの取得
    '*/
    Public Function fInitItemCode(ByRef pItemCode As Chk_ItemCode, Optional ByVal intSisyoFlg As Integer = 0)
        Dim i, lngIndex As Long
        Dim lngCartnFrom, lngCartnTo As Long

        fInitItemCode = -1

        Try

            With gstrctPPDisp
                lngCartnFrom = CLng(.strCrtnFrom)
                lngCartnTo = CLng(.strCrtnTo)
            End With
            'スキャン上限数の取得
            If intSisyoFlg = 1 Then
                pItemCode.intMaxCount = 1
            Else
                pItemCode.intMaxCount = (lngCartnTo - lngCartnFrom) + 1
            End If
            pItemCode.intCount = pItemCode.intMaxCount
            'スキャン用データ配列の編集
            For i = lngCartnFrom To lngCartnTo
                lngIndex = i - lngCartnFrom
                With pItemCode
                    ReDim Preserve .arryOfChkItemCode(lngIndex)
                    .arryOfChkItemCode(lngIndex).initialize()
                    .arryOfChkItemCode(lngIndex).strItemCode = Mid(gstrctKDPP.strItemGr, 1, 20) & CStr(lngCartnFrom + lngIndex).PadLeft(5, "0")
                End With
            Next
            fInitItemCode = 0

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