Attribute VB_Name = "cfgAdmin"
Option Explicit

'Leer, escribir archivo configuración
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function LeerCfg(sGrupo As String, sLlave As String, sRuta As String) As String
    Dim sSQL As String
    Dim ADORS As New ADODB.Recordset
    
    sSQL = "SELECT " & sLlave & " FROM CONFIGURACION"
    
    If oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then
      If Not ADORS.EOF Then
        LeerCfg = IIf(IsNull(LeerCampo(ADORS(sLlave))), "", LeerCampo(ADORS(sLlave)))
      Else
        LeerCfg = ""
      End If
    Else
      LeerCfg = ""
    End If
    
    If LeerCfg = "" Then
      LeerCfg = LeerCfgFile(sGrupo, sLlave, sRuta)
    End If
End Function

Public Function LeerCfgFile(sGrupo As String, sLlave As String, sRuta As String) As String
  'Lee el valor del dato especificado en el archivo de configuración

  Dim sCadena As String
  
  LeerCfgFile = ""
  
  sCadena = String(255, " ")
  
  GetPrivateProfileString sGrupo, sLlave, "", sCadena, 255, sRuta
    
  LeerCfgFile = Trim(sCadena)
  LeerCfgFile = Mid(LeerCfgFile, 1, Len(LeerCfgFile) - 1)
  If Trim(LeerCfgFile) = "" Then
    Select Case sRuta
      Case "OC_Serie"
        'EscribirCfg sGrupo, sLlave, sRuta, "OC"
        LeerCfgFile = "OC"
      Case "PADE_Serie"
        'EscribirCfg sGrupo, sLlave, sRuta, "PA"
        LeerCfgFile = "PA"
      Case "PADV_Serie"
        'EscribirCfg sGrupo, sLlave, sRuta, "PV"
        LeerCfgFile = "PV"
      Case "NC_serie"
        'EscribirCfg sGrupo, sLlave, sRuta, "N" & Trim(Right(kPAR_CAJA, 2))
        LeerCfgFile = "N" & Trim(Right(kPAR_CAJA, 2))
      Case "LIQ_Serie"
        'EscribirCfg sGrupo, sLlave, sRuta, "L"
        LeerCfgFile = "L"
     End Select
  End If
  
  EscribirCfg sGrupo, sLlave, sRuta, LeerCfgFile
End Function

Public Sub EscribirCfg(sGrupo As String, sLlave As String, sRuta As String, sValor As String)
  'Escribe el valor del dato especificado en el archivo de configuración
  WritePrivateProfileString sGrupo, sLlave, sValor, sRuta
  
  Dim sSQL As String
  sSQL = "UPDATE CONFIGURACION SET CONFIGURACION." + sLlave + "='" + sValor + "'"
  Call oDBAdmin.EjecutarSentencia(ADOCnn, sSQL)
End Sub
