Imports System.Data.SqlClient
Imports BD.ABD
Imports Funcion.Funcion
Public Class Configuracion
    Dim ser As New BD.ABD
    Dim max As New Funcion.Funcion
    Dim sql As String
    Dim cmd As SqlCommand
    Dim ds As DataSet
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter
    Dim cnn As SqlConnection
    Dim codPlanes As Long
    Dim strFecha As String
    Dim formato As New System.Globalization.CultureInfo("es-PY", True)
    Dim fecha, b As System.DateTime

    Public Function Insertar(ByVal codConfiguracion As Long, ByVal codigo_usuario As Integer, ByVal codigo_empresa As Long, ByVal canDecimalesImporte As String, ByVal canDecimalesCantidad As String, ByVal Moneda1 As String, ByVal Moneda2 As String, ByVal Producto1 As String, ByVal Producto2 As String, ByVal Producto3 As String, ByVal Producto4 As String, ByVal Producto5 As String, ByVal CalcIva As String, ByVal CalcDesc As String, ByVal PorcenDesc As String, ByVal ConCostopp As String, ByVal Costuc As String, ByVal Color1 As String, ByVal Color2 As String, ByVal Color3 As String, ByVal Color4 As String, ByVal Color5 As String, ByVal Color6 As String, ByVal Color7 As String, ByVal Color8 As String, ByVal Color9 As String, ByVal Color10 As String, ByVal Tamano1 As String, ByVal Tamano2 As String, ByVal Tamano3 As String, ByVal Tamano4 As String, ByVal Tamano5 As String, ByVal Tamano6 As String, ByVal FormatoFecha As String, ByVal ConCompras As String, ByVal ConVentas As String, ByVal ConCaja As String, ByVal ConCobros As String, ByVal ConPagos As String, ByVal ConBancos As String, ByVal ConDevo As String, ByVal ConTransfer As String, ByVal ConAjuste As String, ByVal ConTarje As String, ByVal ConDebito As String, ByVal ConCredito As String, ByVal ConRetenPro As String, ByVal ConRetenCli As String, ByVal CueDifCamPago As String, ByVal CueDifCamCobro As String) As Long
        cnn = New SqlConnection
        cnn = ser.Abrir()
        codConfiguracion = max.MaximoMas1("CODCONFIGURACION", "CONFIGURACION", cnn)
        If codConfiguracion = 0 Then
            'para grabar el primer numero con 1
            codConfiguracion = 1
        End If
        b = System.DateTime.Parse(Now, formato)
        strFecha = b

        ' Reverse month and day to conform to a different format.
        Try
            If Val(canDecimalesImporte) = 0 Then
                canDecimalesImporte = "NULL"
            Else
            End If
            If Val(canDecimalesCantidad) = 0 Then
                canDecimalesCantidad = "NULL"
            Else
            End If
            If Val(Moneda1) = 0 Then
                Moneda1 = "NULL"
            Else
            End If
            If Val(Moneda2) = 0 Then
                Moneda2 = "NULL"
            Else
            End If
            If Producto1 = "" Then
                Producto1 = "NULL"
            Else
                Producto1 = "'" & Producto1 & "'"
            End If
            If Producto2 = "" Then
                Producto2 = "NULL"
            Else
                Producto2 = "'" & Producto2 & "'"
            End If
            If Producto3 = "" Then
                Producto3 = "NULL"
            Else
                Producto3 = "'" & Producto3 & "'"
            End If
            If Producto4 = "" Then
                Producto4 = "NULL"
            Else
                Producto4 = "'" & Producto4 & "'"
            End If
            If Producto5 = "" Then
                Producto5 = "NULL"
            Else
                Producto5 = "'" & Producto5 & "'"
            End If
            If Val(CalcIva) = 0 Then
                CalcIva = "NULL"
            Else
            End If
            If Val(CalcDesc) = 0 Then
                CalcDesc = "NULL"
            Else
            End If
            If Val(PorcenDesc) = 0 Then
                PorcenDesc = "NULL"
            Else
            End If
            If Val(ConCostopp) = 0 Then
                ConCostopp = "NULL"
            Else
            End If
            If Val(Costuc) = 0 Then
                Costuc = "NULL"
            Else
            End If
            If Color1 = "" Then
                Color1 = "NULL"
            Else
                Color1 = "'" & Color1 & "'"
            End If
            If Color2 = "" Then
                Color2 = "NULL"
            Else
                Color2 = "'" & Color2 & "'"
            End If
            If Color3 = "" Then
                Color3 = "NULL"
            Else
                Color3 = "'" & Color3 & "'"
            End If
            If Color4 = "" Then
                Color4 = "NULL"
            Else
                Color4 = "'" & Color4 & "'"
            End If
            If Color5 = "" Then
                Color5 = "NULL"
            Else
                Color5 = "'" & Color5 & "'"
            End If
            If Color6 = "" Then
                Color6 = "NULL"
            Else
                Color6 = "'" & Color6 & "'"
            End If
            If Color7 = "" Then
                Color7 = "NULL"
            Else
                Color7 = "'" & Color7 & "'"
            End If
            If Color8 = "" Then
                Color8 = "NULL"
            Else
                Color8 = "'" & Color8 & "'"
            End If
            If Color9 = "" Then
                Color9 = "NULL"
            Else
                Color9 = "'" & Color9 & "'"
            End If
            If Color10 = "" Then
                Color10 = "NULL"
            Else
                Color10 = "'" & Color10 & "'"
            End If
            If Val(Tamano1) = 0 Then
                Tamano1 = "NULL"
            Else
            End If
            If Val(Tamano2) = 0 Then
                Tamano2 = "NULL"
            Else
            End If
            If Val(Tamano3) = 0 Then
                Tamano3 = "NULL"
            Else
            End If
            If Val(Tamano4) = 0 Then
                Tamano4 = "NULL"
            Else
            End If
            If Val(Tamano5) = 0 Then
                Tamano5 = "NULL"
            Else
            End If
            If Val(Tamano6) = 0 Then
                Tamano6 = "NULL"
            Else
            End If
            If FormatoFecha = "" Then
                FormatoFecha = "NULL"
            Else
                FormatoFecha = "'" & FormatoFecha & "'"
            End If
            If Val(ConCompras) = 0 Then
                ConCompras = "NULL"
            Else
            End If
            If Val(ConVentas) = 0 Then
                ConVentas = "NULL"
            Else
            End If
            If Val(ConCaja) = 0 Then
                ConCaja = "NULL"
            Else
            End If
            If Val(ConCobros) = 0 Then
                ConCobros = "NULL"
            Else
            End If
            If Val(ConPagos) = 0 Then
                ConPagos = "NULL"
            Else
            End If
            If Val(ConBancos) = 0 Then
                ConBancos = "NULL"
            Else
            End If
            If Val(ConDevo) = 0 Then
                ConDevo = "NULL"
            Else
            End If
            If Val(ConTransfer) = 0 Then
                ConTransfer = "NULL"
            Else
            End If
            If Val(ConAjuste) = 0 Then
                ConAjuste = "NULL"
            Else
            End If
            If Val(ConTarje) = 0 Then
                ConTarje = "NULL"
            Else
            End If
            If Val(ConDebito) = 0 Then
                ConDebito = "NULL"
            Else
            End If
            If Val(ConCredito) = 0 Then
                ConCredito = "NULL"
            Else
            End If
            If Val(ConRetenPro) = 0 Then
                ConRetenPro = "NULL"
            Else
            End If
            If Val(ConRetenCli) = 0 Then
                ConRetenCli = "NULL"
            Else
            End If
            If Val(CueDifCamPago) = 0 Then
                CueDifCamPago = "NULL"
            Else
            End If
            If Val(CueDifCamCobro) = 0 Then
                CueDifCamCobro = "NULL"
            Else
            End If

            sql = ""
            sql = "INSERT INTO CONFIGURACION(CODCONFIGURACION, CODUSUARIO, CODEMPRESA, CANDECIMALESIMPORTE, " & _
            "CANDECIMALESCANTIDAD, MONEDA1, MONEDA2, PRODUCTO1, PRODUCTO2, PRODUCTO3, PRODUCTO4, PRODUCTO5, " & _
            "CALCIVA, CALCDESC, PORCENDESC, COSTOPP, COSTOUC, " & _
            "COLOR1, COLOR2, COLOR3, COLOR4, COLOR5, COLOR6, COLOR7, COLOR8, COLOR9, COLOR10, " & _
            "TAMANO1, TAMANO2, TAMANO3, TAMANO4, TAMANO5, TAMANO6, FORMATOFECHA, FECGRA, " & _
            "CONCOMPRAS, CONVENTAS, CONCAJA, CONCOBROS, CONPAGOS, CONBANCOS, CONDEVO, CONTRANSFER, " & _
            "CONAJUSTE, CONTARJE, CONNDEBITO, CONNCREDITO, CONRETENPRO, CONRETENCLI, CUEDIFCAMPAGO, CUEDIFCAMCOBRO) " & _
            "VALUES (" & codConfiguracion & ", " & codigo_usuario & ", " & codigo_empresa & ", " & canDecimalesImporte & ", " & _
            "" & canDecimalesCantidad & ", " & Moneda1 & ", " & Moneda2 & ", " & Producto1 & ", " & _
            "" & Producto2 & "," & Producto3 & ", " & Producto4 & ", " & Producto5 & "," & CalcIva & ", " & CalcDesc & ", " & PorcenDesc & ", " & _
            "" & ConCostopp & ", " & Costuc & ", " & _
            "" & Color1 & ", " & Color2 & ", " & Color3 & ", " & Color4 & ", " & Color5 & ", " & _
            "" & Color6 & ", " & Color7 & ", " & Color8 & ", " & Color9 & ", " & Color10 & ", " & _
            "" & Tamano1 & ", " & Tamano2 & ", " & Tamano3 & ", " & Tamano4 & ", " & Tamano5 & ", " & Tamano6 & ", " & _
            "" & FormatoFecha & ",'" & strFecha & "', " & ConCompras & ", " & ConVentas & ", " & _
            "" & ConCaja & ", " & ConCobros & ", " & _
            "" & ConPagos & ", " & ConBancos & ", " & ConDevo & ", " & ConTransfer & ", " & _
            "" & ConAjuste & ", " & ConTarje & ", " & ConDebito & ", " & ConCredito & ", " & ConRetenPro & ", " & ConRetenCli & ", " & _
            "" & CueDifCamPago & ", " & CueDifCamCobro & ")"

            cmd = New SqlCommand(sql, cnn)                    'crea nuevo comando SQL con sql y la conexxion1
            cmd.ExecuteNonQuery()                               'ejecuta el comando
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return 0
        Finally
            cnn.Close()
        End Try
        Return codConfiguracion
    End Function
    Public Function Eliminar(ByVal valor_de_la_clave_primaria As String) As Boolean
        cnn = ser.Abrir()
        sql = ""
        Try
            sql = "DELETE FROM CONFIGURACION WHERE CODCONFIGURACION = " & valor_de_la_clave_primaria.Trim() & ""  'sintaxis sql
            cmd = New SqlCommand(sql, cnn)                    'crea nuevo comando SQL con sql y la conexxion1
            cmd.ExecuteNonQuery()                               'ejecuta el comando
        Catch
            Return False
            Exit Function
        Finally
            cnn.Close()
        End Try
        Return True
    End Function
    Public Function Modificar(ByVal codConfiguracion As Long, ByVal codigo_usuario As Integer, ByVal codigo_empresa As Long, ByVal canDecimalesImporte As String, ByVal canDecimalesCantidad As String, ByVal Moneda1 As String, ByVal Moneda2 As String, ByVal Producto1 As String, ByVal Producto2 As String, ByVal Producto3 As String, ByVal Producto4 As String, ByVal Producto5 As String, ByVal CalcIva As String, ByVal CalcDesc As String, ByVal PorcenDesc As String, ByVal ConCostopp As String, ByVal Costuc As String, ByVal Color1 As String, ByVal Color2 As String, ByVal Color3 As String, ByVal Color4 As String, ByVal Color5 As String, ByVal Color6 As String, ByVal Color7 As String, ByVal Color8 As String, ByVal Color9 As String, ByVal Color10 As String, ByVal Tamano1 As String, ByVal Tamano2 As String, ByVal Tamano3 As String, ByVal Tamano4 As String, ByVal Tamano5 As String, ByVal Tamano6 As String, ByVal FormatoFecha As String, ByVal ConCompras As String, ByVal ConVentas As String, ByVal ConCaja As String, ByVal ConCobros As String, ByVal ConPagos As String, ByVal ConBancos As String, ByVal ConDevo As String, ByVal ConTransfer As String, ByVal ConAjuste As String, ByVal ConTarje As String, ByVal ConDebito As String, ByVal ConCredito As String, ByVal ConRetenPro As String, ByVal ConRetenCli As String, ByVal CueDifCamPago As String, ByVal CueDifCamCobro As String) As Boolean

        cnn = New SqlConnection
        cnn = ser.Abrir()
        b = System.DateTime.Parse(Now, formato)
        strFecha = b
        Try

            If canDecimalesImporte = 0 Then
                canDecimalesImporte = "NULL"
            Else
            End If
            If canDecimalesCantidad = 0 Then
                canDecimalesCantidad = "NULL"
            Else
            End If
            If Moneda1 = 0 Then
                Moneda1 = "NULL"
            Else
            End If
            If Moneda2 = 0 Then
                Moneda2 = "NULL"
            Else
            End If
            If Producto1 = "" Then
                Producto1 = "NULL"
            Else
                Producto1 = "'" & Producto1 & "'"
            End If
            If Producto2 = "" Then
                Producto2 = "NULL"
            Else
                Producto2 = "'" & Producto2 & "'"
            End If
            If Producto3 = "" Then
                Producto3 = "NULL"
            Else
                Producto3 = "'" & Producto3 & "'"
            End If
            If Producto4 = "" Then
                Producto4 = "NULL"
            Else
                Producto4 = "'" & Producto4 & "'"
            End If
            If Producto5 = "" Then
                Producto5 = "NULL"
            Else
                Producto5 = "'" & Producto5 & "'"
            End If
            If CalcIva = 0 Then
                CalcIva = "NULL"
            Else
            End If
            If CalcDesc = 0 Then
                CalcDesc = "NULL"
            Else
            End If
            If PorcenDesc = 0 Then
                PorcenDesc = "NULL"
            Else
            End If
            If ConCostopp = 0 Then
                ConCostopp = "NULL"
            Else
            End If
            If Costuc = 0 Then
                Costuc = "NULL"
            Else
            End If
            If Color1 = "" Then
                Color1 = "NULL"
            Else
                Color1 = "'" & Color1 & "'"
            End If
            If Color2 = "" Then
                Color2 = "NULL"
            Else
                Color2 = "'" & Color2 & "'"
            End If
            If Color3 = "" Then
                Color3 = "NULL"
            Else
                Color3 = "'" & Color3 & "'"
            End If
            If Color4 = "" Then
                Color4 = "NULL"
            Else
                Color4 = "'" & Color4 & "'"
            End If
            If Color5 = "" Then
                Color5 = "NULL"
            Else
                Color5 = "'" & Color5 & "'"
            End If
            If Color6 = "" Then
                Color6 = "NULL"
            Else
                Color6 = "'" & Color6 & "'"
            End If
            If Color7 = "" Then
                Color7 = "NULL"
            Else
                Color7 = "'" & Color7 & "'"
            End If
            If Color8 = "" Then
                Color8 = "NULL"
            Else
                Color8 = "'" & Color8 & "'"
            End If
            If Color9 = "" Then
                Color9 = "NULL"
            Else
                Color9 = "'" & Color9 & "'"
            End If
            If Color10 = "" Then
                Color10 = "NULL"
            Else
                Color10 = "'" & Color10 & "'"
            End If
            If Tamano1 = 0 Then
                Tamano1 = "NULL"
            Else
            End If
            If Tamano2 = 0 Then
                Tamano2 = "NULL"
            Else
            End If
            If Tamano3 = 0 Then
                Tamano3 = "NULL"
            Else
            End If
            If Tamano4 = 0 Then
                Tamano4 = "NULL"
            Else
            End If
            If Tamano5 = 0 Then
                Tamano5 = "NULL"
            Else
            End If
            If Tamano6 = 0 Then
                Tamano6 = "NULL"
            Else
            End If
            If FormatoFecha = "" Then
                FormatoFecha = "NULL"
            Else
                FormatoFecha = "'" & FormatoFecha & "'"
            End If
            If ConCompras = 0 Then
                ConCompras = "NULL"
            Else
            End If
            If ConVentas = 0 Then
                ConVentas = "NULL"
            Else
            End If
            If ConCaja = 0 Then
                ConCaja = "NULL"
            Else
            End If
            If ConCobros = 0 Then
                ConCobros = "NULL"
            Else
            End If
            If ConPagos = 0 Then
                ConPagos = "NULL"
            Else
            End If
            If ConBancos = 0 Then
                ConBancos = "NULL"
            Else
            End If
            If ConDevo = 0 Then
                ConDevo = "NULL"
            Else
            End If
            If ConTransfer = 0 Then
                ConTransfer = "NULL"
            Else
            End If
            If ConAjuste = 0 Then
                ConAjuste = "NULL"
            Else
            End If
            If ConTarje = 0 Then
                ConTarje = "NULL"
            Else
            End If
            If ConDebito = 0 Then
                ConDebito = "NULL"
            Else
            End If
            If ConCredito = 0 Then
                ConCredito = "NULL"
            Else
            End If
            If ConRetenPro = 0 Then
                ConRetenPro = "NULL"
            Else
            End If
            If ConRetenCli = 0 Then
                ConRetenCli = "NULL"
            Else
            End If
            If CueDifCamPago = 0 Then
                CueDifCamPago = "NULL"
            Else
            End If
            If CueDifCamCobro = 0 Then
                CueDifCamCobro = "NULL"
            Else
            End If

            sql = ""
            sql = "UPDATE CONFIGURACION SET CODUSUARIO = " & codigo_usuario & ", CANDECIMALESIMPORTE = " & canDecimalesImporte & ", CANDECIMALESCANTIDAD = " & canDecimalesCantidad & ", MONEDA1  = " & Moneda1 & " ,  MONEDA2 = " & Moneda2 & " , " & _
            "PRODUCTO1  = " & Producto1 & ", PRODUCTO2 = " & Producto2 & " , PRODUCTO3 = " & Producto3 & ", PRODUCTO4 = " & Producto4 & ", PRODUCTO5 = " & Producto5 & ", CALCIVA = " & CalcIva & ", CALCDESC = " & CalcDesc & " , PORCENDESC = " & PorcenDesc & ", COSTOPP = " & ConCostopp & ", COSTOUC = " & Costuc & " , " & _
            "COLOR1 = " & Color1 & ", COLOR2 = " & Color2 & ", COLOR3 = " & Color3 & ", COLOR4 = " & Color4 & ", COLOR5 = " & Color5 & ", COLOR6 = " & Color6 & ", COLOR7 = " & Color7 & ", COLOR8 = " & Color8 & ", COLOR9 = " & Color9 & ", COLOR10 = " & Color10 & " , " & _
            "TAMANO1 = " & Tamano1 & ", TAMANO2 = " & Tamano2 & ", TAMANO3 = " & Tamano3 & ", TAMANO4 = " & Tamano4 & ", TAMANO5 = " & Tamano5 & ", TAMANO6 =" & Tamano6 & ", " & _
            "FORMATOFECHA = " & FormatoFecha & ", FECGRA = '" & strFecha & "', CUEDIFCAMPAGO = " & CueDifCamPago & ", CUEDIFCAMCOBRO = " & CueDifCamCobro & ", " & _
            "CONCOMPRAS = " & ConCompras & " , CONVENTAS = " & ConVentas & ", CONCAJA = " & ConCaja & ", CONCOBROS = " & ConCobros & ", CONPAGOS = " & ConPagos & ", CONBANCOS = " & ConBancos & ", CONDEVO = " & ConDevo & ", " & _
            "CONTRANSFER = " & ConTransfer & " , CONAJUSTE = " & ConAjuste & ", CONTARJE = " & ConTarje & ", CONNDEBITO = " & ConDebito & ", CONNCREDITO = " & ConCredito & ", CONRETENPRO = " & ConRetenPro & ", CONRETENCLI = " & ConRetenCli & " WHERE CODCONFIGURACION = " & codConfiguracion & "" 'sintaxis sql
            cmd = New SqlCommand(sql, cnn)                    'crea nuevo comando SQL con sql y la conexxion1
            cmd.ExecuteNonQuery()                               'ejecuta el comando
        Catch
            Return False
            Exit Function
            '   MsgBox("Se ha producido un error al tratar de grabar los datos, contacte con su proveedor", MsgBoxStyle.Critical)
            'Return 1
        Finally
            cnn.Close()
            '100% SEGURIDAD
            'cnn.Close()
        End Try
        Return True
    End Function
    Public Function DsRecorrerCodigo(ByVal codigo_empresa As Long) As DataSet
        cnn = New SqlConnection
        cnn = ser.Abrir()
        da = New SqlDataAdapter
        da = New SqlDataAdapter("SELECT CODCONFIGURACION FROM CONFIGURACION WHERE CODEMPRESA = " & codigo_empresa & "", cnn)
        ds = New DataSet
        da.Fill(ds)
        cnn.Close()
        Return ds
    End Function

    Public Function DsRecorrerDescripcion(ByVal codigo_de_busqueda As Long, ByVal codigo_empresa As Long) As DataSet
        Try
            cnn = New SqlConnection
            cnn = ser.Abrir()
            da = New SqlDataAdapter
            sql = ""
            sql = "SELECT C.CODCONFIGURACION, ISNULL(C.CODEMPRESA, 0) AS CODEMPRESA, rtrim(isnull(C.CANDECIMALESIMPORTE,0)) as CANDECIMALESIMPORTE , isnull(C.CANDECIMALESCANTIDAD, 0) as CANDECIMALESCANTIDAD,  " & _
            "rtrim(isnull(C.PRODUCTO1, ' ')) as PRODUCTO1, rtrim(isnull(C.PRODUCTO2, ' ')) as PRODUCTO2, rtrim(isnull(C.PRODUCTO3, ' ')) as PRODUCTO3,  " & _
            "rtrim(isnull(C.PRODUCTO4, ' ')) as PRODUCTO4, rtrim(isnull(C.PRODUCTO5, ' ')) as PRODUCTO5, " & _
            "rtrim(isnull((SELECT RTRIM(M.NUMMONEDA) AS MONEDA11 FROM MONEDA M WHERE C.MONEDA1 = M.CODMONEDA), ' ')) AS MONEDA1,  " & _
            "rtrim(isnull((SELECT RTRIM(M.NUMMONEDA) AS MONEDA11 FROM MONEDA M WHERE C.MONEDA1 = M.CODMONEDA), ' ')) AS MONEDA1,  " & _
            "rtrim(isnull((SELECT RTRIM(M.NUMMONEDA) AS MONEDA12 FROM MONEDA M WHERE C.MONEDA2 = M.CODMONEDA), ' ')) AS MONEDA2,  " & _
            "rtrim(isnull(C.CALCIVA, 0)) as CALCIVA, rtrim(isnull(C. CALCDESC, 0)) as CALCDESC, rtrim(isnull(C.PORCENDESC, 0 )) as PORCENDESC,   " & _
            "rtrim(isnull(C.COSTOPP, 0)) as COSTOPP, rtrim(isnull(C.COSTOUC, 0)) as COSTOUC, rtrim(isnull(C.COLOR1, ' ')) as COLOR1,  " & _
            "rtrim(isnull(C.COLOR2, ' ')) as COLOR2, rtrim(isnull(C.COLOR3, ' ')) as COLOR3, rtrim(isnull(C.COLOR4, ' ')) as COLOR4,  " & _
            "rtrim(isnull(C.COLOR5, ' ')) as COLOR5, rtrim(isnull(C.COLOR6, ' ')) as COLOR6, rtrim(isnull(C.COLOR7, ' ')) as COLOR7, " & _
            "rtrim(isnull(C.COLOR8, ' ')) as COLOR8, rtrim(isnull(C.COLOR9, ' ')) as COLOR9, rtrim(isnull(C.COLOR10, ' ')) as COLOR10,  " & _
            "rtrim(isnull(C.TAMANO1, 8)) as TAMANO1, rtrim(isnull(C.TAMANO2, 8)) as TAMANO2, rtrim(isnull(C.TAMANO3, 8)) as TAMANO3, " & _
            "rtrim(isnull(C.TAMANO4, 8)) as TAMANO4, rtrim(isnull(C.TAMANO5, 8)) as TAMANO5, rtrim(isnull(C.TAMANO6, 8)) as TAMANO6, " & _
            "rtrim(C.FORMATOFECHA) as FORMATOFECHA, rtrim(isnull(C.CONCOMPRAS, 0)) as CONCOMPRAS, rtrim(isnull(C.CONVENTAS, 0)) as CONVENTAS,  " & _
            "rtrim(isnull(C.CONCAJA, 0)) as CONCAJA, rtrim(isnull(C.CONCOBROS, 0)) as CONCOBROS, rtrim(isnull(C.CONPAGOS, 0)) as CONPAGOS, " & _
            "rtrim(isnull(C.CONBANCOS, 0)) as CONBANCOS, rtrim(isnull(C.CONDEVO, 0)) as CONDEVO, rtrim(isnull(C.CONTRANSFER, 0)) as CONTRANSFER, " & _
            "rtrim(isnull(C.CONAJUSTE, 0)) as CONAJUSTE, rtrim(isnull(C.CONTARJE, 0)) as CONTARJE, rtrim(isnull(C.CONNDEBITO, 0)) as CONNDEBITO, " & _
            "rtrim(isnull(C.CONNCREDITO, 0)) AS CONNCREDITO, rtrim(isnull(C.CONRETENPRO, 0)) AS CONRETENPRO, rtrim(isnull(C.CONRETENCLI, 0)) AS CONRETENCLI,  " & _
            "rtrim(isnull((SELECT RTRIM(PLA.CODPLANCUENTA) AS CodCuenta1 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMPAGO = PLA.CODPLANCUENTA), 0))  AS CUEDIFCAMPAGO,   " & _
            "rtrim(isnull((SELECT RTRIM(PLA.NUMPLANCUENTA) AS Cuenta1 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMPAGO = PLA.CODPLANCUENTA), 0))  AS NUMDIFCAMPAGO,  " & _
            "rtrim(isnull((SELECT RTRIM(PLA.DESPLANCUENTA) AS Descripcion FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMPAGO = PLA.CODPLANCUENTA), ' '))  AS DesDIFCAMPAGO,  " & _
            "rtrim(isnull((SELECT RTRIM(PLA.CODPLANCUENTA) AS Cuenta3 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMCOBRO = PLA.CODPLANCUENTA), 0)) AS CUEDIFCAMCOBRO, " & _
            "rtrim(isnull((SELECT RTRIM(PLA.NUMPLANCUENTA) AS Cuenta3 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMCOBRO = PLA.CODPLANCUENTA), 0)) AS NUMDIFCAMCOBRO, " & _
            "rtrim(isnull((SELECT RTRIM(PLA.DESPLANCUENTA) AS Descripcion FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMCOBRO = PLA.CODPLANCUENTA), ' '))  AS DesDIFCAMCOBRO   " & _
            "FROM CONFIGURACION C WHERE C.CODEMPRESA = " & codigo_empresa & ""
            da = New SqlDataAdapter(sql, cnn)
            ds = New DataSet
            da.Fill(ds)
            cnn.Close()
            Return ds
        Catch e As Exception
            MsgBox(e.ToString)
        End Try
    End Function

    Public Function BuscarConfiguracion(ByVal Codigo_Empresa As Long) As SqlDataReader
        Dim temp As Integer
        ser = New BD.ABD
        cnn = ser.Abrir
        sql = ""
        sql = "SELECT C.CODCONFIGURACION, ISNULL(C.CODEMPRESA, 0) AS CODEMPRESA, rtrim(isnull(C.CANDECIMALESIMPORTE,0)) as CANDECIMALESIMPORTE , isnull(C.CANDECIMALESCANTIDAD, 0) as CANDECIMALESCANTIDAD,  " & _
        "rtrim(isnull(C.PRODUCTO1, ' ')) as PRODUCTO1, rtrim(isnull(C.PRODUCTO2, ' ')) as PRODUCTO2, rtrim(isnull(C.PRODUCTO3, ' ')) as PRODUCTO3, " & _
        "rtrim(isnull(C.PRODUCTO4, ' ')) as PRODUCTO4, rtrim(isnull(C.PRODUCTO5, ' ')) as PRODUCTO5, " & _
        "rtrim(isnull((SELECT RTRIM(M.NUMMONEDA) AS MONEDA11 FROM MONEDA M WHERE C.MONEDA1 = M.CODMONEDA), ' ')) AS MONEDA1,  " & _
        "rtrim(isnull((SELECT RTRIM(M.NUMMONEDA) AS MONEDA11 FROM MONEDA M WHERE C.MONEDA1 = M.CODMONEDA), ' ')) AS MONEDA1,  " & _
        "rtrim(isnull((SELECT RTRIM(M.NUMMONEDA) AS MONEDA12 FROM MONEDA M WHERE C.MONEDA2 = M.CODMONEDA), ' ')) AS MONEDA2,  " & _
        "rtrim(isnull(C.CALCIVA, 0)) as CALCIVA, rtrim(isnull(C. CALCDESC, 0)) as CALCDESC, rtrim(isnull(C.PORCENDESC, 0 )) as PORCENDESC,   " & _
        "rtrim(isnull(C.COSTOPP, 0)) as COSTOPP, rtrim(isnull(C.COSTOUC, 0)) as COSTOUC, rtrim(isnull(C.COLOR1, ' ')) as COLOR1,  " & _
        "rtrim(isnull(C.COLOR2, ' ')) as COLOR2, rtrim(isnull(C.COLOR3, ' ')) as COLOR3, rtrim(isnull(C.COLOR4, ' ')) as COLOR4,  " & _
        "rtrim(isnull(C.COLOR5, ' ')) as COLOR5, rtrim(isnull(C.COLOR6, ' ')) as COLOR6, rtrim(isnull(C.COLOR7, ' ')) as COLOR7, " & _
        "rtrim(isnull(C.COLOR8, ' ')) as COLOR8, rtrim(isnull(C.COLOR9, ' ')) as COLOR9, rtrim(isnull(C.COLOR10, ' ')) as COLOR10,  " & _
        "rtrim(isnull(C.TAMANO1, 8)) as TAMANO1, rtrim(isnull(C.TAMANO2, 8)) as TAMANO2, rtrim(isnull(C.TAMANO3, 8)) as TAMANO3, " & _
        "rtrim(isnull(C.TAMANO4, 8)) as TAMANO4, rtrim(isnull(C.TAMANO5, 8)) as TAMANO5, rtrim(isnull(C.TAMANO6, 8)) as TAMANO6, " & _
        "rtrim(C.FORMATOFECHA) as FORMATOFECHA, rtrim(isnull(C.CONCOMPRAS, 0)) as CONCOMPRAS, rtrim(isnull(C.CONVENTAS, 0)) as CONVENTAS,  " & _
        "rtrim(isnull(C.CONCAJA, 0)) as CONCAJA, rtrim(isnull(C.CONCOBROS, 0)) as CONCOBROS, rtrim(isnull(C.CONPAGOS, 0)) as CONPAGOS, " & _
        "rtrim(isnull(C.CONBANCOS, 0)) as CONBANCOS, rtrim(isnull(C.CONDEVO, 0)) as CONDEVO, rtrim(isnull(C.CONTRANSFER, 0)) as CONTRANSFER, " & _
        "rtrim(isnull(C.CONAJUSTE, 0)) as CONAJUSTE, rtrim(isnull(C.CONTARJE, 0)) as CONTARJE, rtrim(isnull(C.CONNDEBITO, 0)) as CONNDEBITO, " & _
        "rtrim(isnull(C.CONNCREDITO, 0)) AS CONNCREDITO, rtrim(isnull(C.CONRETENPRO, 0)) AS CONRETENPRO, rtrim(isnull(C.CONRETENCLI, 0)) AS CONRETENCLI,  " & _
        "rtrim(isnull((SELECT RTRIM(PLA.CODPLANCUENTA) AS CodCuenta1 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMPAGO = PLA.CODPLANCUENTA), 0))  AS CUEDIFCAMPAGO,   " & _
        "rtrim(isnull((SELECT RTRIM(PLA.NUMPLANCUENTA) AS Cuenta1 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMPAGO = PLA.CODPLANCUENTA), 0))  AS NUMDIFCAMPAGO,  " & _
        "rtrim(isnull((SELECT RTRIM(PLA.DESPLANCUENTA) AS Descripcion FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMPAGO = PLA.CODPLANCUENTA), ' '))  AS DesDIFCAMPAGO,  " & _
        "rtrim(isnull((SELECT RTRIM(PLA.CODPLANCUENTA) AS Cuenta3 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMCOBRO = PLA.CODPLANCUENTA), 0)) AS CUEDIFCAMCOBRO, " & _
        "rtrim(isnull((SELECT RTRIM(PLA.NUMPLANCUENTA) AS Cuenta3 FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMCOBRO = PLA.CODPLANCUENTA), 0)) AS NUMDIFCAMCOBRO, " & _
        "rtrim(isnull((SELECT RTRIM(PLA.DESPLANCUENTA) AS Descripcion FROM PLANCUENTAS PLA WHERE C.CUEDIFCAMCOBRO = PLA.CODPLANCUENTA), ' '))  AS DesDIFCAMCOBRO   " & _
        "FROM CONFIGURACION C WHERE C.CODEMPRESA = " & Codigo_Empresa & ""
        Try
            cmd = New SqlCommand(sql, cnn)
            dr = cmd.ExecuteReader()
            Return dr
        Catch
        Finally
        End Try
    End Function

    Public Function VerificarLicencia(ByVal codigo As String, _
    ByVal mac As String, ByVal serial As String) As Boolean
        Dim tran As SqlTransaction
        Try
            ser = New BD.ABD
            cnn = ser.Abrir
            cmd = New SqlCommand(sql, cnn)

            sql = ""
            sql = "P_VERIFICAR_LICENCIA"

            cmd.Parameters.Add("@Phrase", SqlDbType.VarChar, 20)
            cmd.Parameters.Item(0).Direction = ParameterDirection.Input
            cmd.Parameters.Item(0).Value = codigo

            cmd.Parameters.Add("@Mac", SqlDbType.VarChar, 17)
            cmd.Parameters.Item(1).Direction = ParameterDirection.Input
            cmd.Parameters.Item(1).Value = mac

            cmd.Parameters.Add("@Serial", SqlDbType.VarChar, 300)
            cmd.Parameters.Item(2).Direction = ParameterDirection.Input
            cmd.Parameters.Item(2).Value = serial

            cmd.Parameters.Add("@Result", SqlDbType.Bit, 1)
            cmd.Parameters.Item(3).Direction = ParameterDirection.ReturnValue

            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = sql

            tran = cnn.BeginTransaction(IsolationLevel.ReadCommitted, "licencia")
            cmd.Transaction = tran
            cmd.Connection = cnn

            cmd.ExecuteNonQuery()

            If CType(cmd.Parameters.Item(3).Value, Integer) = 0 Then
                tran.Rollback("licencia")
                Return False
            Else
                tran.Commit()
                Return True
            End If
        Catch ex As Exception
            tran.Rollback("licencia")
            'MsgBox(ex.ToString)

            Return False
        Finally
            cmd.Parameters.Clear()
            ser.Cerrar(cnn)
        End Try
    End Function
    Public Function InsertarLicencia(ByVal codigo As String, _
ByVal mac As String, ByVal serial As String, ByVal tipo As Integer, _
ByVal fechaInicio As String, ByVal fechaFin As String) As Boolean
        Dim tran As SqlTransaction
        Try
            ser = New BD.ABD
            cnn = ser.Abrir
            cmd = New SqlCommand(sql, cnn)

            sql = ""
            sql = "P_INSERTAR_LICENCIA"

            cmd.Parameters.Add("@Phrase", SqlDbType.VarChar, 20)
            cmd.Parameters.Item(0).Direction = ParameterDirection.Input
            cmd.Parameters.Item(0).Value = codigo

            cmd.Parameters.Add("@Mac", SqlDbType.VarChar, 17)
            cmd.Parameters.Item(1).Direction = ParameterDirection.Input
            cmd.Parameters.Item(1).Value = mac

            cmd.Parameters.Add("@Tipo", SqlDbType.Int, 2)
            cmd.Parameters.Item(2).Direction = ParameterDirection.Input
            cmd.Parameters.Item(2).Value = tipo

            cmd.Parameters.Add("@Serial", SqlDbType.VarChar, 300)
            cmd.Parameters.Item(3).Direction = ParameterDirection.Input
            cmd.Parameters.Item(3).Value = serial

            cmd.Parameters.Add("@FechaInicio", SqlDbType.VarChar, 8)
            cmd.Parameters.Item(4).Direction = ParameterDirection.Input
            cmd.Parameters.Item(4).Value = fechaInicio

            cmd.Parameters.Add("@FechaFin", SqlDbType.VarChar, 8)
            cmd.Parameters.Item(5).Direction = ParameterDirection.Input
            cmd.Parameters.Item(5).Value = fechaFin

            cmd.Parameters.Add("@Result", SqlDbType.Bit, 1)
            cmd.Parameters.Item(6).Direction = ParameterDirection.ReturnValue

            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = sql

            tran = cnn.BeginTransaction(IsolationLevel.ReadCommitted, "InsertarLicencia")
            cmd.Transaction = tran
            cmd.Connection = cnn

            cmd.ExecuteNonQuery()

            If CType(cmd.Parameters.Item(6).Value, Integer) <> 0 Then
                tran.Rollback("InsertarLicencia")
                Return False
            Else
                tran.Commit()
                Return True
            End If
        Catch ex As Exception
            tran.Rollback("InsertarLicencia")
            'MsgBox(ex.ToString)

            Return False
        Finally
            cmd.Parameters.Clear()
            ser.Cerrar(cnn)
        End Try
    End Function

    Public Function BuscarConfiguracionStock(ByVal Codigo_Empresa As Long) As SqlDataReader
        ser = New BD.ABD
        cnn = ser.Abrir

        sql = ""
        sql = "SELECT V.CODCONFIG, V.CODEMPRESA, " & _
        "ISNULL(V.VENCIMIENTOFACTURA, 0) AS VENCIMIENTOFACTURA,  " & _
        "ISNULL(V.PORCIENTODESCUENTO, 0) AS PORCIENTODESCUENTO,  " & _
        "ISNULL(V.CONTROLLIMITE, 0) AS CONTROLLIMITE,  " & _
        "ISNULL(V.MODIFICAPRECIO, 0) AS MODIFICAPRECIO,  " & _
        "ISNULL(V.STOCKNEGATIVO, 0) AS STOCKNEGATIVO,  " & _
        "ISNULL(V.MAYORISTA,0) AS MAYORISTA, " & _
        "ISNULL(V.MINORISTA,0) AS MINORISTA, " & _
        "ISNULL(V.CLIENTE, 0) AS CLIENTES, " & _
        "ISNULL(V.RANGOCOMPROBANTE,0) AS RANGOCOMPROBANTE, " & _
        "ISNULL(V.RANGOTICKET,0) AS RANGOTICKET, " & _
        "ISNULL(V.PRODUCTODESCUENTO,0) AS PRODUCTODESCUENTO " & _
        "FROM VENTASCONFIG V WHERE V.CODEMPRESA = " & Codigo_Empresa & ""

        Try
            cmd = New SqlCommand(sql, cnn)
            dr = cmd.ExecuteReader()

            Return dr
        Catch

        Finally

        End Try
    End Function

    Public Function BuscarConfiguracionProduccion(ByVal Codigo_Empresa As Long) As SqlDataReader
        ser = New BD.ABD
        cnn = ser.Abrir

        sql = ""
        sql = "SELECT PC.CODPOLLITO, PC.CODPOLLO " & _
        "FROM PRODUCCIONCONFIG PC " & _
        "WHERE PC.CODEMPRESA = " & Codigo_Empresa & ""

        Try
            cmd = New SqlCommand(sql, cnn)
            dr = cmd.ExecuteReader()

            Return dr
        Catch

        Finally

        End Try
    End Function
End Class