public int IdAgente(string codigoAgente, string rutaEmpresa)
        {
            int           dbResponse, agentId = 0, fqResult;
            StringBuilder sbDBAgente = new StringBuilder(31);
            int           connection = AdminPaqLib.dbLogIn("", rutaEmpresa);

            if (connection == 0)
            {
                ErrLogger.Log("Connection not allowed in adminpaq for concepts");
                return(0);
            }


            dbResponse = AdminPaqLib.dbGetTopNoLock(connection, "MGW10001", "PRIMARYKEY");
            if (dbResponse == 4)
            {
                ErrLogger.Log("La ruta de la empresa es incorrecta.");
                return(0);
            }

            while (dbResponse == 0)
            {
                fqResult = AdminPaqLib.dbFieldChar(connection, "MGW10001", 2, sbDBAgente, 31);
                if (sbDBAgente.ToString().Substring(0, 30).Trim().ToUpper().Equals(codigoAgente.Trim().ToUpper()))
                {
                    fqResult = AdminPaqLib.dbFieldLong(connection, "MGW10001", 1, ref agentId);
                    break;
                }

                dbResponse = AdminPaqLib.dbSkip(connection, "MGW10001", "PRIMARYKEY", 1);
            }

            AdminPaqLib.dbLogOut(connection);
            return(agentId);
        }
        private long ConceptId(int conceptCode, Dictionary <string, int> connections)
        {
            int  connection, dbResponse, conceptId = 0, fqResult = 0, dbCode = 0;
            bool connected = connections.TryGetValue("concepts", out connection);

            if (!connected || connection == 0)
            {
                ErrLogger.Log("Connection not allowed in adminpaq for concepts");
                return(0);
            }

            dbResponse = AdminPaqLib.dbGetTopNoLock(connection, "MGW10006", "PRIMARYKEY");
            if (dbResponse == 4)
            {
                ErrLogger.Log("La ruta de la empresa es incorrecta.");
                return(0);
            }

            while (dbResponse == 0)
            {
                fqResult = AdminPaqLib.dbFieldLong(connection, "MGW10006", 2, ref dbCode);
                if (dbCode == conceptCode)
                {
                    fqResult = AdminPaqLib.dbFieldLong(connection, "MGW10006", 1, ref conceptId);
                    return(conceptId);
                }


                dbResponse = AdminPaqLib.dbSkip(connection, "MGW10006", "PRIMARYKEY", 1);
            }

            return(0);
        }
        private long conceptCode(int conceptId, Dictionary <string, int> connections)
        {
            int           connection, dbResponse, spaceCounter;
            bool          connected     = connections.TryGetValue("concepts", out connection);
            StringBuilder sbConceptCode = new StringBuilder(30);

            if (!connected || connection == 0)
            {
                ErrLogger.Log("Connection not allowed in adminpaq for concepts");
                return(0);
            }

            spaceCounter = 11 - conceptId.ToString().Length;
            string key = new string(' ', spaceCounter);

            key = key + conceptId.ToString();

            dbResponse = AdminPaqLib.dbGetNoLock(connection, "MGW10006", "PRIMARYKEY", key);
            if (dbResponse == 4)
            {
                ErrLogger.Log("La ruta de la empresa es incorrecta.");
                return(0);
            }

            if (dbResponse == 0)
            {
                int    fqResult = AdminPaqLib.dbFieldChar(connection, "MGW10006", 2, sbConceptCode, 30);
                string sConcept = sbConceptCode.ToString().Substring(0, 29).Trim();
                long   result   = 0;
                bool   casted   = long.TryParse(sConcept, out result);
                if (!casted)
                {
                    return(0);
                }

                return(result);
            }

            return(0);
        }
        public void InitializeSDK()
        {
            int connEmpresas, dbResponse, fieldResponse;

            connEmpresas = AdminPaqLib.dbLogIn("", lib.DataDirectory);

            if (connEmpresas == 0)
            {
                ErrLogger.Log("No se pudo crear conexión a la tabla de Empresas de AdminPAQ.");
                return;
            }

            dbResponse = AdminPaqLib.dbGetTopNoLock(connEmpresas, TableNames.EMPRESAS, IndexNames.EMPRESAS_PK);
            while (dbResponse == 0)
            {
                Empresa empresa = new Empresa();

                int idEmpresa = 0;
                fieldResponse = AdminPaqLib.dbFieldLong(connEmpresas, TableNames.EMPRESAS, Empresa.ID_EMPRESA, ref idEmpresa);
                empresa.Id    = idEmpresa;

                StringBuilder nombreEmpresa = new StringBuilder(151);
                fieldResponse = AdminPaqLib.dbFieldChar(connEmpresas, TableNames.EMPRESAS, Empresa.NOMBRE_EMPRESA, nombreEmpresa, 151);
                string sNombreEmpresa = nombreEmpresa.ToString(0, 150).Trim();
                empresa.Nombre = sNombreEmpresa;

                StringBuilder rutaEmpresa = new StringBuilder(254);
                fieldResponse = AdminPaqLib.dbFieldChar(connEmpresas, TableNames.EMPRESAS, Empresa.RUTA_EMPRESA, rutaEmpresa, 254);
                string sRutaEmpresa = rutaEmpresa.ToString(0, 253).Trim();
                empresa.Ruta = sRutaEmpresa;

                empresas.Add(empresa);
                dbResponse = AdminPaqLib.dbSkip(connEmpresas, TableNames.EMPRESAS, IndexNames.EMPRESAS_PK, 1);
            }

            AdminPaqLib.dbLogOut(connEmpresas);
        }
        private void RetrieveSales(DateTime date, Empresa empresa, Dictionary <string, int> connections)
        {
            string        filterDate;
            int           cancelled, returned, conceptId, agentId, currencyId, connection, dbResponse;
            double        changeValue, sold;
            StringBuilder docDate = new StringBuilder(9);
            long          conceptCode;

            bool isSale, isReturn, validAgent, connected;

            connected = connections.TryGetValue("documents", out connection);
            if (!connected || connection == 0)
            {
                ErrLogger.Log("Connection not allowed in adminpaq for company [" + empresa.Nombre + "] @ route [" + empresa.Ruta + "]");
                return;
            }

            filterDate = date.ToString("yyyyMMdd");
            dbResponse = AdminPaqLib.dbGetNoLock(connection, "MGW10008", "CFECHA", filterDate);
            if (dbResponse == 4)
            {
                ErrLogger.Log("La ruta de la empresa es incorrecta.");
                return;
            }

            while (dbResponse == 0)
            {
                int fqResult = 0;
                cancelled = 0;
                fqResult  = AdminPaqLib.dbFieldLong(connection, "MGW10008", 26, ref cancelled);
                if (cancelled == 1)
                {
                    dbResponse = AdminPaqLib.dbSkip(connection, "MGW10008", "CFECHA", 1);
                    continue;
                }

                returned = 0;
                fqResult = AdminPaqLib.dbFieldLong(connection, "MGW10008", 27, ref returned);
                if (returned == 1)
                {
                    dbResponse = AdminPaqLib.dbSkip(connection, "MGW10008", "CFECHA", 1);
                    continue;
                }

                fqResult = AdminPaqLib.dbFieldChar(connection, "MGW10008", 6, docDate, 9);
                string sDocDate = docDate.ToString().Substring(0, 8).Trim();
                if (!filterDate.Equals(sDocDate))
                {
                    break;
                }


                conceptId   = 0;
                fqResult    = AdminPaqLib.dbFieldLong(connection, "MGW10008", 3, ref conceptId);
                conceptCode = this.conceptCode(conceptId, connections);

                isReturn = false;
                isSale   = empresa.CodigosVenta.Contains(conceptCode);
                if (!isSale)
                {
                    isReturn = empresa.CodigosDevolucion.Contains(conceptCode);
                }

                if (!isSale && !isReturn)
                {
                    dbResponse = AdminPaqLib.dbSkip(connection, "MGW10008", "CFECHA", 1);
                    continue;
                }

                agentId    = 0;
                fqResult   = AdminPaqLib.dbFieldLong(connection, "MGW10008", 10, ref agentId);
                validAgent = agentInCompany(agentId, empresa);

                if (!validAgent)
                {
                    dbResponse = AdminPaqLib.dbSkip(connection, "MGW10008", "CFECHA", 1);
                    continue;
                }


                // Currency 1 = Pesos Mexicanos
                currencyId  = 1;
                changeValue = 1;
                fqResult    = AdminPaqLib.dbFieldLong(connection, "MGW10008", 15, ref currencyId);

                if (currencyId != 1)
                {
                    fqResult = AdminPaqLib.dbFieldDouble(connection, "MGW10008", 16, ref changeValue);
                }

                sold     = 0;
                fqResult = AdminPaqLib.dbFieldDouble(connection, "MGW10008", 31, ref sold);

                if (isReturn)
                {
                    sold = sold * -1;
                }

                if (currencyId != 1)
                {
                    sold = sold * changeValue;
                }

                addSale(agentId, empresa, sold, date);

                dbResponse = AdminPaqLib.dbSkip(connection, "MGW10008", "CFECHA", 1);
            }
        }