Imports System.Text
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms
'*******************************************************************************
'* Function    : Error logging system 
'* Created     : 2004/10/02
'* Project     : PICS2004 Development
'* Updates     : 
'* Version     : 1.0
'*******************************************************************************
'* Convert By  : Thanthima 
'* Date        : 2024/06/15
'*******************************************************************************
Module modPublic
    '*******************************************************************************
    '   Purpose: File check mode (Definitions for the fCheckDate return values)
    '*******************************************************************************
    Public Const G_CONST_CHK_YEAR_DATE As Integer = 0   ' Year date check
    Public Const G_CONST_CHK_MONTH_DATE As Integer = 1  ' Month date check
    Public Const G_CONST_CHK_YEAR_MONTH As Integer = 2  ' Year month check
    '*******************************************************************************
    '   Purpose: Time check mode (Definitions for the fCheckTime return values)
    '*******************************************************************************
    Public Const G_CONST_CHK_HHMM As Integer = 0   ' Hour minute check
    Public Const G_CONST_CHK_HHMMSS As Integer = 1  ' Hour minute second check
    '*******************************************************************************
    '   Purpose: Message return values
    '*******************************************************************************
    Public Enum MSG_RET As Integer
        OK = 1
        ABORT = 2
        RETRY = 3
        IGNORE = 4
        CANCEL = 5
        YES = 6
        NO = 7
    End Enum
    '*******************************************************************************
    '   Purpose: Key input definitions
    '*******************************************************************************
    Public Enum KEY As Integer
        Number = 1       ' Numeric input
        Alpha = 2        ' Alphabetic input
        NumAlpha = 3     ' Alphanumeric input
        TelPost = 4      ' Telephone/Postcode input
        NumberDec = 5    ' Decimal number input
        DateShift = 6    ' Date shift input
    End Enum
    '*******************************************************************************
    '   Purpose: Logging event return codes
    '*******************************************************************************
    Public Enum EVENT_RTN As Integer
        JOB_SRT = 1   ' Job started
        JOB_NML = 2   ' Job normal (completed successfully)
        JOB_ERR = 3   ' Job error
        DAT_ERR = 4   ' Data error
        DAT_NON = 5   ' Data not found
    End Enum

    '*******************************************************************************
    ' Purpose      : Initialize the application
    ' Parameters   : pstrPrgID    String  Program ID (related to department)
    ' Return Value : Initialization success status (True/False)
    '*******************************************************************************
    Public Function fAppInit(ByVal pstrPrgID As String) As Boolean

        fAppInit = False
        gAppName = fGetAppName()

        ''// Check for existing instance (mutex)
        'gobjMutex = New System.Threading.Mutex(False, gAppName)
        'If gobjMutex.WaitOne(0, False) = False Then
        '    Application.Exit()
        'End If

        '// Load INI configuration
        If fGetIniPics() = False Then Exit Function
        If fGetIniParam(pstrPrgID) = False Then Exit Function

        '// Determine application mode
        If gstrctParamIni.intKeyCount = 0 Then
            gintAppMode = G_CONST_APP_NEW
        Else
            gintAppMode = G_CONST_APP_UPD
        End If

        '// Open database connection
        'If gclsDB.fOpenDB(gstrctPicsIni.strMain_PICS2004) = False Then
        '    sErrLog(gAppName, "fAppInit", gclsDB.ErrMsg)
        '    MsgBox("DB OPEN ERROR", MsgBoxStyle.Critical)
        '    Exit Function
        'End If

        fAppInit = True

    End Function
    '*******************************************************************************
    ' Purpose      : Sets the standard settings for the application form
    ' Parameters   : pobjForm     Object  Form to be configured
    ' Return Value : None
    '*******************************************************************************
    'Public Sub sFormStandardSetting(ByVal pobjForm As Object)
    '    pobjForm.MaximizeBox = False
    '    pobjForm.MinimizeBox = True
    '    pobjForm.FormBorderStyle = FormBorderStyle.FixedDialog
    '    pobjForm.StartPosition = FormStartPosition.CenterScreen
    'End Sub
    Public Sub sFormStandardSetting(ByVal pobjForm As Form)
        pobjForm.MaximizeBox = False
        pobjForm.MinimizeBox = True
        pobjForm.FormBorderStyle = FormBorderStyle.FixedDialog
        'pobjForm.StartPosition = FormStartPosition.CenterScreen
        ' Calculate center position of the screen
        Dim screen As Screen = screen.PrimaryScreen
        Dim screenWidth As Integer = screen.WorkingArea.Width
        Dim screenHeight As Integer = screen.WorkingArea.Height
        Dim formWidth As Integer = pobjForm.Width
        Dim formHeight As Integer = pobjForm.Height
        Dim centerX As Integer = (screenWidth - formWidth) \ 2
        Dim centerY As Integer = (screenHeight - formHeight) \ 2

        ' Set form's location to center of the screen
        pobjForm.Location = New Point(centerX, centerY)

    End Sub
    '*******************************************************************************
    ' Purpose      : Load and process configuration settings from Pics.INI file
    ' Parameters   : None
    ' Return Value : Success status of loading configuration (True/False)
    '*******************************************************************************
    Public Function fGetIniPics() As Boolean
        Dim wstrBuf As String = ""

        Try
            '// PICS.INI
            gstrPicsIniPath = fgetAppPath() & "\" & G_CONST_PICSINI

            'MessageBox.Show(gstrPicsIniPath & " | " & G_CONST_PicsSec01 & " | " & G_CONST_PicsKey01)

            '// PICS.INI 
            With gstrctPicsIni

                .strImport_SPM = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec01, G_CONST_PicsKey01)

                .strImport_PLAN = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec01, G_CONST_PicsKey02)

                .strImport_RCVJUMP = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec01, G_CONST_PicsKey03)
                .strImport_MCLOCATION = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec01, G_CONST_PicsKey04)
                .strImport_MATERIAL = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec01, G_CONST_PicsKey05)

                .strMain_PICS2004 = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec02, G_CONST_PicsKey06)

                .strExport_SENDJUMP = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec03, G_CONST_PicsKey07)
                .strExport_SENDPACK1 = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec03, G_CONST_PicsKey08)
                .strExport_SENDPACK2 = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec03, G_CONST_PicsKey09)
                .strExport_SENDVAN = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec03, G_CONST_PicsKey10)

                .strErrorLog_Path = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec04, G_CONST_PicsKey11)

                .strParameter_Path = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec05, G_CONST_PicsKey12)

                .strConstant_DataKeepYear = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec06, G_CONST_PicsKey13)
                .strConstant_EditDate = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec06, G_CONST_PicsKey14)
                .strConstant_WaitTime = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec06, G_CONST_PicsKey28)

                .strOutput_PICSC = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey15)
                .strOutput_PICSH = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey16)
                .strOutput_PICSI = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey17)
                .strOutput_PICSM = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey18)
                .strOutput_KDPA = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey19)
                .strOutput_KDST = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey26)
                .strOutput_KDPP = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey27)
                .strOutput_KDPL = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec07, G_CONST_PicsKey29)

                .strLabelWork_LABELWORK = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec08, G_CONST_PicsKey20)
                .strMtlWork_MTLWORK = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec09, G_CONST_PicsKey21)
                .strWork_PSERIAL = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec10, G_CONST_PicsKey22)
                .strWork_FREELOCATION = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec11, G_CONST_PicsKey23)
                .strWork_LOOKLCTN = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec12, G_CONST_PicsKey24)
                .strPicsc114_Def = gclsReg.fGetIniValue(gstrPicsIniPath, G_CONST_PicsSec13, G_CONST_PicsKey25)

            End With

            Return True
            Exit Function

        Catch ex As Exception
            sErrLog(gAppName, "fGetIniPics", ex.ToString)
            Return False
        End Try

    End Function
    '*******************************************************************************
    '* Purpose     : Load and process configuration settings from PICSPARAM.INI file
    '* Parameters  : pstrPrgID    String  Program ID for which settings are retrieved
    '* Return Value: Success status of loading configuration (True/False)
    '*******************************************************************************
    Public Function fGetIniParam(ByVal pstrPrgID As String) As Boolean
        Dim wstrBuf As String

        Try
            '// PICSPARAM.INI file path
            gstrParamIniPath = fgetAppPath() & G_CONST_PARAMINI

            '// Retrieve settings from PICSPARAM.INI
            With gstrctParamIni
                .strUserID = gclsReg.fGetIniValue(gstrParamIniPath, G_CONST_ParamSec01, G_CONST_ParamKey01)
                .strProgramID = gclsReg.fGetIniValue(gstrParamIniPath, pstrPrgID, G_CONST_ParamKey02)

                ' Check if the Program ID is valid (here "0" might indicate invalid or default)
                If .strProgramID = "0" Then
                    .intKeyCount = 0
                Else
                    ' Read additional settings based on the Program ID
                    wstrBuf = gclsReg.fGetIniValue(gstrParamIniPath, pstrPrgID, G_CONST_ParamKey03)

                    ' Handle the case where key count is determined from a string separated by commas
                    If Trim(wstrBuf).Replace(",", "") = "" Then
                        .intKeyCount = 0
                    Else
                        .intKeyCount = fStringSep(wstrBuf, ",", .strKey)
                        ' Optionally process additional data
                        ' gstrctS1000.strdata = .strKey(0)
                    End If
                End If
            End With

            Return True

        Catch ex As Exception
            ' Log any errors encountered during the process
            sErrLog(gAppName, "fGetIniPics", ex.ToString)
            Return False
        End Try
    End Function
    '*******************************************************************************
    '* Purpose     : Load and process configuration settings from Zaiko.INI file
    '* Return Value: Success status of loading configuration (True/False)
    '*******************************************************************************
    Public Function fGetIniZaiko() As Boolean
        Dim wstrBuf As String

        Try
            '// Zaiko.INI file path
            gstrZaikoIniPath = fgetAppPath() & G_CONST_ZAIKOINI

            '// Retrieve settings from Zaiko.INI
            With gstrctZaikoIni
                .strDate = gclsReg.fGetIniValue(gstrZaikoIniPath, G_CONST_ZaikoSec01, G_CONST_ZaikoKey01)
                ' Add more properties as needed
                ' .strAnotherSetting = gclsReg.fGetIniValue(gstrZaikoIniPath, G_CONST_ZaikoSec01, G_CONST_ZaikoKey02)
            End With

            Return True

        Catch ex As Exception
            ' Log any errors encountered during the process
            sErrLog(gAppName, "fGetIniZaiko", ex.ToString)
            Return False
        End Try
    End Function

    '*******************************************************************************
    '* Purpose     : Load and process configuration settings from RELESE.INI file
    '* Return Value: Success status of loading configuration (True/False)
    '*******************************************************************************
    Public Function fGetIniRelese() As Boolean
        Dim wstrBuf As String

        Try
            '// RELESE.INI file path
            gstrReleceIniPath = fgetAppPath() & G_CONST_RELECEINI

            '// Retrieve settings from RELESE.INI
            With gstrctReleceIni
                .strProduction = gclsReg.fGetIniValue(gstrReleceIniPath, G_CONST_RelecSec01, G_CONST_RelecKey01)
                .strShipPlan = gclsReg.fGetIniValue(gstrReleceIniPath, G_CONST_RelecSec01, G_CONST_RelecKey02)
                .strProductFrom = gclsReg.fGetIniValue(gstrReleceIniPath, G_CONST_RelecSec01, G_CONST_RelecKey03)
                .strProductTo = gclsReg.fGetIniValue(gstrReleceIniPath, G_CONST_RelecSec01, G_CONST_RelecKey04)
            End With

            Return True

        Catch ex As Exception
            ' Log any errors encountered during the process
            sErrLog(gAppName, "fGetIniRelese", ex.ToString)
            Return False
        End Try
    End Function

    '*******************************************************************************
    '* Purpose     : Retrieve a flag from APLINK.INI to indicate feature status
    '* Return Value: Success status of retrieving flag (True/False)
    '*******************************************************************************
    Public Function fGetCreateAPFlag() As Boolean
        Dim wstrBuf As String

        Try
            '// APLINK.INI file path
            gstrAPLinkIniPath = gstrctPicsIni.strMain_PICS2004 & "\" & G_CONST_APLINKINI

            '// Retrieve flag from APLINK.INI
            With gstrctKDHT
                If UCase(gclsReg.fGetIniValue(gstrAPLinkIniPath, G_CONST_APLinkSec01, G_CONST_APLinkKey01)) = "ON" Then
                    .bLinkFlag = True
                End If
            End With

            Return True

        Catch ex As Exception
            ' Log any errors encountered during the process
            sErrLog(gAppName, "fGetCreateAPFlag", ex.ToString)
            Return False
        End Try
    End Function
    '*******************************************************************************
    '* Purpose     : Retrieve values from KDMC.INI and store in structure
    '* Return Value: Success status of retrieving values (True/False)
    '*******************************************************************************
    Public Function fGetIniKdmc() As Boolean
        Dim wstrBuf As String

        Try
            '// KDMC.INI file path
            gstrKdmcIniPath = fgetAppPath() & G_CONST_KDMCINI

            '// Retrieve values from KDMC.INI
            With gstrctKdmcIni
                .strMain_PicsType = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey01)
                .strMain_CstmID = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey02)
                .strMain_CstmCode = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey03)
                .strMain_CenterCode = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey04)
                .strMain_Bhk = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey05)
                .strMain_SectionID = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey06)
                .strMain_SectionCode = gclsReg.fGetIniValue(gstrKdmcIniPath, G_CONST_KdmcSec01, G_CONST_KdmcKey07)
            End With

            Return True

        Catch ex As Exception
            ' Log any errors encountered during the process
            sErrLog(gAppName, "fGetIniKdmc", ex.ToString)
            Return False
        End Try
    End Function
    '*******************************************************************************
    '* Purpose     : Write values to PICSPARAM.INI based on provided parameters
    '* Return Value: Success status of writing values (True/False)
    '*******************************************************************************
    Public Function fPutIniParam(ByVal pstrAppID As String, ByVal pstrPrgID As String, ByVal ParamArray pstrKey() As String) As Boolean
        Dim wstrBuf As String
        Dim wblnRet As Boolean
        Dim i As Integer

        Try
            '// PICSPARAM.INI file path
            gstrParamIniPath = fgetAppPath() & G_CONST_PARAMINI

            '// Write ProgramID (pstrPrgID) to PICSPARAM.INI
            wblnRet = gclsReg.fPutIniValue(gstrParamIniPath, pstrAppID, G_CONST_ParamKey02, pstrPrgID)

            '// Write keys (pstrKey) to PICSPARAM.INI
            If pstrPrgID = "0" Then
                ' If ProgramID is "0", clear the key field
                wblnRet = gclsReg.fPutIniValue(gstrParamIniPath, pstrAppID, G_CONST_ParamKey03, "")
            Else
                ' Concatenate keys with comma separator and write to PICSPARAM.INI
                wstrBuf = ""
                For i = 0 To UBound(pstrKey)
                    If i = 0 Then
                        wstrBuf = pstrKey(i)
                    Else
                        wstrBuf &= "," & pstrKey(i)
                    End If
                Next
                wblnRet = gclsReg.fPutIniValue(gstrParamIniPath, pstrAppID, G_CONST_ParamKey03, wstrBuf)
            End If

            Return True

        Catch ex As Exception
            ' Log any errors encountered during the process
            sErrLog(gAppName, "fPutIniParam", ex.ToString)
            Return False
        End Try
    End Function
    '*******************************************************************************
    '* Purpose     : Log errors to a specified log file along with relevant information
    '* Parameters  : pstrPGName     String  Program ID
    '*             : pstrSubName    String  Subroutine ID
    '*             : plngErrCode    String  Error Code
    '*             : pstrErrName    String  Error Name
    '* Return      : None
    '*******************************************************************************
    Public Sub sErrLog(ByVal pstrPGName As String, ByVal pstrSubName As String, ByVal pstrErrName As String)
        Dim wstrCode As String = ""
        Dim wstrWorkCode As String = ""
        Dim wstrDate As String
        Dim wstrTime As String
        Dim wstrRecord As String
        Dim wstrFileName As String
        Dim wintFileNum As Integer = 0
        Dim wintRecCount As Integer = 0
        Dim wintSpace As Integer
        Dim wstrSub As String
        Dim wstrPG As String
        Dim wintErrCode As Integer = 0
        Dim wstrErrMsg As String = ""

        Try

            wintSpace = 12 - Len(pstrPGName) ' ƒvƒƒOƒ‰ƒ€–¼‚Ì•¶Žš”‚ÌŽæ“¾‚ÆƒXƒy[ƒX‚ÌŒÂ”‚ÌŽæ“¾
            wstrPG = Left(pstrPGName & Space(wintSpace), 12) ' •Ï”—Ìˆæ‚ÖƒXƒy[ƒX‚ð•t‰Á‚µ‚Ä‘ã“ü

            wintSpace = 30 - Len(pstrSubName) ' ƒTƒuƒ‹[ƒ`ƒ“–¼‚Ì•¶Žš”‚ÌŽæ“¾‚ÆƒXƒy[ƒX‚ÌŒÂ”‚ÌŽæ“¾
            wstrSub = Left(pstrSubName & Space(wintSpace), 30) ' •Ï”—Ìˆæ‚ÖƒXƒy[ƒX‚ð•t‰Á‚µ‚Ä‘ã“ü

            wstrTime = fgetDefaultTime(":") ' "hh:mm:ss" ‚ð•Ô‚µ‚Ü‚·B
            wstrDate = fgetDefaultDate("/") ' "yyyy/mm/dd" ‚ð•Ô‚µ‚Ü‚·B

            ''ƒ[ƒvƒGƒ‰[ƒƒbƒZ[ƒW‚ÌŽæ“¾
            'If pstrErrName.IndexOf("WarpError") > 0 Then
            '    'f0.WarpCtrl1.GetLastError(wintErrCode)
            '    Select Case wintErrCode
            '        Case 10001
            '            wstrErrMsg = "Screen not open."
            '            'wstrErrMsg = "‰æ–Ê‚ªƒI[ƒvƒ“‚³‚ê‚Ä‚¢‚Ü‚¹‚ñ"
            '        Case 10002
            '            wstrErrMsg = "Object non support."
            '            'wstrErrMsg = "ƒIƒuƒWƒFƒNƒg‚ªƒTƒ|[ƒg‚³‚ê‚Ä‚¢‚È‚¢‹@”\‚ð—˜—p‚µ‚Ü‚µ‚½"
            '        Case 10003
            '            wstrErrMsg = "Screen open over."
            '            'wstrErrMsg = "‰æ–Ê‚ª‚»‚êˆÈãƒI[ƒvƒ“‚Å‚«‚Ü‚¹‚ñ"
            '        Case 10004
            '            wstrErrMsg = "Memory over flow."
            '            'wstrErrMsg = "ƒƒ‚ƒŠ‚Ì‹ó‚«—e—Ê‚ª‘«‚è‚Ü‚¹‚ñ"
            '        Case 10005
            '            wstrErrMsg = "Parameter error."
            '            'wstrErrMsg = "ƒpƒ‰ƒ[ƒ^“™‚ªˆê’v‚µ‚Ä‚¢‚Ü‚¹‚ñ"
            '        Case 10006
            '            wstrErrMsg = "Screen double open."
            '            'wstrErrMsg = "ƒI[ƒvƒ“‚³‚ê‚Ä‚¢‚é‰æ–Ê‚ð‚³‚ç‚ÉƒI[ƒvƒ“‚µ‚Ü‚µ‚½"
            '        Case 10009
            '            wstrErrMsg = "I/O error."
            '            'wstrErrMsg = "IOƒGƒ‰["
            '        Case 10010
            '            wstrErrMsg = "Screen error. Object used."
            '            'wstrErrMsg = "ƒI[ƒvƒ“‚³‚ê‚Ä‚¢‚È‚¢‰æ–Ê‚ÉƒIƒuƒWƒFƒNƒg‚ð—˜—p‚µ‚Ü‚µ‚½"
            '        Case 20000
            '            wstrErrMsg = "Not initialize."
            '            'wstrErrMsg = "‰Šú‰»‚³‚ê‚Ä‚¢‚Ü‚¹‚ñ"
            '        Case 20001
            '            wstrErrMsg = "Initialize parameter error."
            '            'wstrErrMsg = "‰Šú‰»‚Ìƒpƒ‰ƒ[ƒ^‚ªˆá‚¢‚Ü‚·"
            '        Case 20002
            '            wstrErrMsg = "Parameter error."
            '            'wstrErrMsg = "ƒpƒ‰ƒ[ƒ^‚ªˆá‚¢‚Ü‚·"
            '        Case 20003
            '            wstrErrMsg = "Not object."
            '            'wstrErrMsg = "ƒIƒuƒWƒFƒNƒg‚ª‘¶Ý‚µ‚Ü‚¹‚ñ"
            '        Case 20004
            '            wstrErrMsg = "The buffer doesn't exist in the bar code buffer."
            '            'wstrErrMsg = "ƒo[ƒR[ƒhƒoƒbƒtƒ@‚Éƒoƒbƒtƒ@‚ª‘¶Ý‚µ‚Ü‚¹‚ñ"
            '        Case 20005
            '            wstrErrMsg = "Warp message error."
            '            'wstrErrMsg = "ƒ[ƒv‚ÌƒƒbƒZ[ƒWƒGƒ‰["
            '        Case 30000
            '            wstrErrMsg = "MORS network error."
            '            'wstrErrMsg = "MORS‚Ìƒlƒbƒgƒ[ƒN‚ÉˆÙí‚ª”­¶‚µ‚Ü‚µ‚½"
            '        Case 30001
            '            wstrErrMsg = "MORS is not running."
            '            'wstrErrMsg = "MORS‚ª‰Ò“®‚µ‚Ä‚¢‚Ü‚¹‚ñ"
            '        Case 30002
            '            wstrErrMsg = "MORS frame error."
            '            'wstrErrMsg = "MORS‚ª’ÊM˜H‚ÌŠm—§‚ÉŽ¸”s‚µ‚Ü‚µ‚½"
            '        Case 30003
            '            wstrErrMsg = "MORS data send error."
            '            'wstrErrMsg = "MORS‚ªƒf[ƒ^‚Ì‘—M‚ÉŽ¸”s‚µ‚Ü‚µ‚½"
            '        Case 30004
            '            wstrErrMsg = "MORS data recive error."
            '            'wstrErrMsg = "MORS‚ªƒf[ƒ^‚ÌŽóM‚ÉŽ¸”s‚µ‚Ü‚µ‚½"
            '        Case 30005
            '            wstrErrMsg = "MORS wait error."
            '            'wstrErrMsg = "MORS‚ÌƒEƒGƒCƒgƒGƒ‰[‚ª”­¶‚µ‚Ü‚µ‚½"
            '    End Select

            'wstrErrMsg = "WarpError:" & wstrErrMsg

            '' wstrRecord ‚ÉŽæ“¾‚µ‚½ƒpƒ‰ƒ[ƒ^‚ð•ÒW‚µ‚ÄŠi”[
            'wstrRecord = wstrDate & "   " & wstrTime & "   " & gstrctKDHT.strHdNo & "   " & wstrPG & "   " & wstrSub & "   " & wstrErrMsg

            'Else

            '' wstrRecord ‚ÉŽæ“¾‚µ‚½ƒpƒ‰ƒ[ƒ^‚ð•ÒW‚µ‚ÄŠi”[
            'wstrRecord = wstrDate & "   " & wstrTime & "   " & gstrctKDHT.strHdNo & "   " & wstrPG & "   " & wstrSub & "   " & pstrErrName

            'End If

            wstrRecord = wstrDate & "   " & wstrTime & "   " & gstrctKDHT.strHdNo & "   " & wstrPG & "   " & wstrSub & "   " & pstrErrName

            'wintFileNum = FreeFile()

            wstrFileName = Trim(gstrctPicsIni.strErrorLog_Path)
            If wstrFileName = "" Then wstrFileName = ".\PICSERR.LOG"

            wstrFileName = LOG_DIR + "PICSERR.LOG"

            'FileOpen(wintFileNum, wstrFileName, OpenMode.Append)

            'PrintLine(wintFileNum, wstrRecord)
            'FileClose(wintFileNum)

            Try

                'If File.Exists(wstrFileName) = False Then
                '    File.Create(wstrFileName)
                'End If

                ' Open the file in append mode
                Using writer As StreamWriter = New StreamWriter(wstrFileName, True)
                    ' Write the record to the file
                    writer.WriteLine(wstrRecord)
                End Using

                'Using writer As StreamWriter = File.AppendText(wstrFileName)
                '    ' Write the record to the file
                '    writer.WriteLine(wstrRecord)
                'End Using

            Catch ex As Exception
                ' Handle any potential exceptions here
                MessageBox.Show("An error occurred while writing to the log file: " & ex.Message)
            End Try

            Exit Sub

        Catch ex As Exception
            MessageBox.Show(G_CONST_EM_FILEIO, G_CONST_EMT_FILEIO, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        End Try

    End Sub
    '*******************************************************************************
    '*Arguments : pstrPath     String
    '           : pstrPGName   String  
    '           : pintIvent    Integer 
    '           : pstrMsg      String  
    '           : pstrNaiyou   String  
    '*******************************************************************************
    Public Sub sOutputLog(ByVal pstrPath As String, ByVal pstrMsgNo As String, Optional ByVal pstrTrueScan As String = " ", Optional ByVal pstrFalseScan As String = " ")

        Dim wstrRecord As String = ""
        Dim wstrFileName As String = ""
        Dim wintFileNum As Integer = 0

        Try
            wstrRecord = fgetDefaultDate()
            wstrRecord &= fgetDefaultTime()
            wstrRecord &= gstrctKDHT.strUserNo
            wstrRecord &= Space(1) & Space(8)
            wstrRecord &= gstrctKDHT.strPrgID
            wstrRecord &= pstrMsgNo
            wstrRecord &= Space(1) & Space(30)
            pstrTrueScan = pstrTrueScan.PadRight(40)
            wstrRecord &= pstrTrueScan
            pstrFalseScan = pstrFalseScan.PadRight(40)
            wstrRecord &= pstrFalseScan
            wstrRecord &= Space(14)

            'wintFileNum = FreeFile() 

            wstrFileName = Trim(pstrPath)
            'If wstrFileName = "" Then wstrFileName = "C:\HTERRLOG.TXT"

            wstrFileName = LOG_DIR + "HTERRLOG.TXT"

            'FileOpen(wintFileNum, wstrFileName, OpenMode.Append)

            'PrintLine(wintFileNum, wstrRecord) 
            'FileClose(wintFileNum) 

            Try
                ' Open the file in append mode
                Using writer As StreamWriter = New StreamWriter(wstrFileName, True)
                    ' Write the record to the file
                    writer.WriteLine(wstrRecord)
                End Using

                'If File.Exists(wstrFileName) = False Then
                '    File.Create(wstrFileName)
                '    File.
                'End If

                'Using writer As StreamWriter = File.AppendText(wstrFileName)
                '    ' Write the record to the file
                '    writer.WriteLine(wstrRecord)
                'End Using


            Catch ex As Exception
                ' Handle any potential exceptions here
                MessageBox.Show("An error occurred while writing to the HTERRLOG.TXT file: " & ex.Message)
            End Try
            Exit Sub

        Catch ex As Exception
            'f0.WarpCtrl1.MessageBox(G_CONST_EMT_FILEIO, G_CONST_EM_FILEIO, MWA_DB_WARNING, MWA_DB_OK)
            MessageBox.Show(G_CONST_EM_FILEIO, G_CONST_EMT_FILEIO, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        End Try
    End Sub
    '* Purpose   : Retrieve the current application directory
    '* Return    : String - The current directory
    '* Notes     : The retrieved path is used as is
    Public Function fGetAppName() As String
        Dim wstrAppName As String
        On Error Resume Next

        wstrAppName = ""
        wstrAppName = System.AppDomain.CurrentDomain.FriendlyName

        Return wstrAppName

    End Function
    '*******************************************************************************
    '* purpose                    : Retrieve the program ID.
    '* Function returning a string: Program ID retrieval.
    '*                            : By truncating the file name without extension encode.
    '*******************************************************************************
    Public Function fGetPrgID() As String
        Dim wstrAppName As String
        On Error Resume Next

        wstrAppName = fGetAppName()
        wstrAppName = Left(wstrAppName, Len(wstrAppName) - 4)

        Return wstrAppName

    End Function

    '   Purpose: To convert a time string given in the format HHNNSS.
    '   Arguments: None
    '   Return value: String
    Public Function fgetDefaultTime(Optional ByVal pstrSep As String = "") As String
        Dim strH As String
        Dim strM As String
        Dim strS As String

        strH = Now.Hour
        strH = strH.PadLeft(2, "0")
        strM = Now.Minute
        strM = strM.PadLeft(2, "0")
        strS = Now.Second
        strS = strS.PadLeft(2, "0")

        Return strH + pstrSep + strM + pstrSep + strS

    End Function
    '   Purpose: To convert a date given in the format YYYYMMDD.
    '   Arguments: None
    '   Return value: String
    Public Function fgetDefaultDate(Optional ByVal pstrSep As String = "") As String
        Dim strY As String
        Dim strM As String
        Dim strD As String

        strY = Now.Year
        strM = Now.Month
        strM = strM.PadLeft(2, "0")
        strD = Now.Day
        strD = strD.PadLeft(2, "0")

        Return strY & pstrSep & strM & pstrSep & strD

    End Function

    Public Function fGetIniData(ByVal pstrPrgID As String, ByRef pstrIniName As String) As Boolean

        fGetIniData = False

        gAppName = fGetAppName()
        pstrIniName = G_CONST_PICSINI

        '// “ñd‹N“®‚Ì–hŽ~
        'gobjMutex = New System.Threading.Mutex(False, gAppName)
        'If gobjMutex.WaitOne(0, False) = False Then
        'Application.Exit()
        'End If


        '// PICS.INIÌ§²Ù“Ç‚Ýž‚Ý
        'If Dir(fgetAppPath() & G_CONST_PICSINI) = "" Then Exit Function
        If Not FileExistsInAppDirectory(G_CONST_PICSINI) Then
            Exit Function ' Exit the function if the file does not exist
        End If


        If fGetIniPics() = False Then Exit Function

        '// PICSPARAM.INIÌ§²Ù“Ç‚Ýž‚Ý
        pstrIniName = G_CONST_PARAMINI
        'If Dir(fgetAppPath() & G_CONST_PICSINI) = "" Then Exit Function
        If Not FileExistsInAppDirectory(G_CONST_PICSINI) Then
            Exit Function ' Exit the function if the file does not exist
        End If


        'If fGetIniParam(pstrPrgID) = False Then Exit Function



        '// ‹N“®Ó°ÄÞ¾¯Ä
        If gstrctParamIni.intKeyCount = 0 Then
            gintAppMode = G_CONST_APP_NEW
        Else
            gintAppMode = G_CONST_APP_UPD
        End If

        fGetIniData = True

    End Function

    ' Function to check if a file exists in the application directory
    Public Function FileExistsInAppDirectory(ByVal fileName As String) As Boolean
        Dim appPath As String = fgetAppPath() ' Assuming fgetAppPath() retrieves the application directory
        Dim filePath As String = Path.Combine(appPath, fileName)

        Return File.Exists(filePath)
    End Function
    '* Purpose   : Retrieve the current application directory
    '* Return    : String - The current directory (ends with a backslash "\")
    '* Notes     : The retrieved path is used as is
    Public Function fgetAppPath() As String
        ' Get the directory of the currently executing assembly (the application)
        Return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
    End Function

    '* Purpose   : Splits a string using a delimiter and returns the resulting substrings
    '* Parameters: pstrTarget     String  - The input string
    '             : pstrDelimiter  String  - The delimiter
    '             : pstrResult()   String  - Array to hold the resulting substrings
    '* Return    : Array of resulting substrings
    Public Function fStringSep(ByVal strTarget As String, ByVal strDelimiter As String, ByRef strResult() As String) As Integer
        Dim wstrArray() As String

        wstrArray = Split(strTarget, strDelimiter)
        strResult = wstrArray
        Return wstrArray.Length

    End Function
    Public Function fcheckNullString(ByVal data As Object, Optional ByVal ret As String = "", Optional ByVal pbilTrimMode As Boolean = True) As String
        On Error GoTo checkNullString_ERR

        If IsNothing(data) Or IsDBNull(data) Then
            Return ret
        Else
            If pbilTrimMode Then
                Return Trim(data)
            Else
                Return data
            End If
        End If
        Exit Function

checkNullString_ERR:
        Return ret
    End Function
    Public Function fcheckNullInt(ByVal data As Object, Optional ByVal ret As Integer = 0) As Integer

        Try
            If IsNothing(data) Or IsDBNull(data) Then
                Return ret
            Else
                Return data
            End If

            Exit Function

        Catch ex As Exception
            Return ret
        End Try

    End Function
    '/*
    '|| ŠÖ”–¼FfOpenDB
    '|| ŠT—v  FDBÚ‘±ˆ—
    '|| ˆø”  FpclsDB,     clsDBAccess, DBƒNƒ‰ƒX
    '||       FpstrDbPath, String,      ƒpƒX–¼
    '*/
    Public Function fOpenDB(ByVal pstrDbPath As String) As Boolean


        Dim wstrImage As String = ""
        fOpenDB = False

        'DBÚ‘±
        'If gclsDB.fOpenDB(pstrDbPath) = False Then
        '    sErrLog(gAppName, "fOpenDB", gclsDB.ErrMsg)
        '    'MsgBox("DB OPEN ERROR", MsgBoxStyle.Critical)
        '    Exit Function
        'End If

        fOpenDB = True

    End Function
  

    '/*
    '|| Function Name: fUpdateStock
    '|| Version: STOCK not found
    '*/
    Private Function fUpdateStock(ByVal wstrInput As String) As Integer

        Dim rtn As Integer
        Dim wintRet As Integer
        Dim wstrKey(7) As String
        Dim wstrKeyValue(7) As String
        Dim wstrItemCode As String
        Dim tmp As String
        fUpdateStock = -1

        Try

            tmp = Mid(wstrInput, 2, 13)
            wstrItemCode = fBackDispItem(tmp, True)

            'XV—
            wstrKey(0) = "PicsType"
            wstrKey(1) = "Cstmid"
            wstrKey(2) = "CstmCode"
            wstrKey(3) = "CenterCode"
            wstrKey(4) = "Bhk"
            wstrKey(5) = "Itemid"
            wstrKey(6) = "ItemCode"
            wstrKey(7) = "SerialNo"
            wstrKeyValue(0) = gstrctKDHT.strPackType
            wstrKeyValue(1) = gstrctKDHT.strCstmid
            wstrKeyValue(2) = gstrctKDHT.strCstmCode
            wstrKeyValue(3) = gstrctP2000.strCenterCode
            wstrKeyValue(4) = gstrctP2000.strBhk
            wstrKeyValue(5) = Mid(wstrInput, 1, 1)
            wstrKeyValue(6) = wstrItemCode
            wstrKeyValue(7) = Mid(wstrInput, 20, 6)

            'gclsDB.Setcolumn = ("ZaikoStatus,StockDate,StockTime,StockEmp")          '“o^/XV‘ฮ—๑–ผ
            'gclsDB.SetData = ("3," & fgetDefaultDate() & "," & Mid(fgetDefaultTime(), 1, 4) & "," & gstrctKDHT.strUserNo)    '“o^/XVf[^

            'If gclsDB.fUpdRecords(G_CONST_TBL_STOCK, G_CONST_KEY_STOCKKEY00, wstrKey, wstrKeyValue) = -1 Then
            '    Exit Function

            'End If

            'mint2015status = M2015_STATUS.DONE

            fUpdateStock = 0

        Catch ex As Exception
            'gintStatus = FREE_STATUS.QUIT
            sErrLog(gAppName, "fUpdateStock", ex.ToString)

        End Try

    End Function
    '*
    '* Function   : Retrieves information based on the specified code.
    '* Parameter  : pstrCode       String  - Code to be queried
    '*             :
    '* Return     : Information retrieved based on the specified code
    '* Remarks    : 
    '*
    Public Function fBackDispItem(ByVal pstrCode As String, Optional ByVal pbolNoSep As Boolean = False) As String
        Dim wstrChg As String
        Dim wstrArray(34) As String
        Dim wintCnt As Integer
        Dim i As Integer

        wstrChg = ""

        '// ”z—๑ษพฏฤ
        For i = 1 To 34
            wstrArray(i) = Mid(pstrCode, i, 1)
            If wstrArray(i) = "-" Then wintCnt = wintCnt + 1
        Next

        'R[h•ฯท
        If wintCnt = 1 Then
            wstrChg = wstrArray(1) & wstrArray(2) & wstrArray(3) & wstrArray(4) & wstrArray(5) & _
                      wstrArray(7) & wstrArray(8) & wstrArray(9) & wstrArray(10) & wstrArray(11) & _
                      wstrArray(12) & wstrArray(13) & wstrArray(14) & wstrArray(15) & wstrArray(16) & _
                      wstrArray(17) & wstrArray(18) & wstrArray(19) & wstrArray(20) & wstrArray(21) & _
                      wstrArray(22) & wstrArray(23) & wstrArray(24) & wstrArray(25) & wstrArray(26) & _
                      wstrArray(27) & wstrArray(28) & wstrArray(29) & wstrArray(30) & wstrArray(31)
        ElseIf wintCnt = 2 Then
            wstrChg = wstrArray(6) & wstrArray(7) & wstrArray(9) & wstrArray(10) & wstrArray(11) & _
                      wstrArray(1) & wstrArray(2) & wstrArray(3) & wstrArray(4) & wstrArray(12) & _
                      wstrArray(14) & wstrArray(15) & wstrArray(17) & wstrArray(18) & wstrArray(19) & _
                      wstrArray(20) & wstrArray(21) & wstrArray(22) & wstrArray(23) & wstrArray(24) & _
                      wstrArray(25) & wstrArray(26) & wstrArray(27) & wstrArray(28) & wstrArray(29) & _
                      wstrArray(30) & wstrArray(31) & wstrArray(32) & wstrArray(33) & wstrArray(34)
        Else
            If pbolNoSep Then
                If wstrArray(1) = "9" Then
                    wstrChg = wstrArray(1) & wstrArray(2) & wstrArray(3) & wstrArray(4) & wstrArray(5) & _
                              wstrArray(6) & wstrArray(7) & wstrArray(8) & wstrArray(9) & wstrArray(10) & _
                              wstrArray(11) & wstrArray(12) & wstrArray(13) & wstrArray(14) & wstrArray(15) & _
                              wstrArray(16) & wstrArray(17) & wstrArray(18) & wstrArray(19) & wstrArray(20) & _
                              wstrArray(21) & wstrArray(22) & wstrArray(23) & wstrArray(24) & wstrArray(25) & _
                              wstrArray(26) & wstrArray(27) & wstrArray(28) & wstrArray(29) & wstrArray(30)
                Else
                    wstrChg = wstrArray(5) & wstrArray(6) & wstrArray(7) & wstrArray(8) & wstrArray(9) & _
                              wstrArray(1) & wstrArray(2) & wstrArray(3) & wstrArray(4) & wstrArray(10) & _
                              wstrArray(11) & wstrArray(12) & wstrArray(13) & wstrArray(14) & wstrArray(15) & _
                              wstrArray(16) & wstrArray(17) & wstrArray(18) & wstrArray(19) & wstrArray(20) & _
                              wstrArray(21) & wstrArray(22) & wstrArray(23) & wstrArray(24) & wstrArray(25) & _
                              wstrArray(26) & wstrArray(27) & wstrArray(28) & wstrArray(29) & wstrArray(30)
                End If
            Else
                wstrChg = pstrCode
            End If
        End If

        fBackDispItem = wstrChg

        Return wstrChg
    End Function
End Module
