Esempio n. 1
0
        public ScriptResponse Execute(ContextDataObject context, string query)
        {
            ErpBS bsERP = new ErpBS();

            if (!context.Parameters.ContainsKey("TipoPlataforma"))
            {
                throw new Exception("TipoPlataforma inválido");
            }

            EnumTipoPlataforma tipoPlataforma;

            if (!Enum.TryParse <EnumTipoPlataforma>((string)context.Parameters["TipoPlataforma"], out tipoPlataforma))
            {
                throw new Exception("TipoPlataforma inválido");
            }

            try
            {
                bsERP.AbreEmpresaTrabalho(tipoPlataforma, context.Company, context.Username, context.Password);
            }
            catch (Exception e)
            {
                throw new Exception("Erro a abrir a empresa no ERP: " + e.Message);
            }

            StdBELista queryResults = bsERP.Consulta(query);

            int numLinhas  = queryResults.NumLinhas();
            int numColunas = queryResults.NumColunas();

            string[] headers = new string[numColunas];
            for (short i = 0; i < numColunas; i++)
            {
                headers[i] = queryResults.Nome(i);
            }

            object[,] data = new object[numLinhas, numColunas];
            for (short i = 0; i < numLinhas; i++)
            {
                for (short j = 0; j < numColunas; j++)
                {
                    var nome = headers[j];
                    data[i, j] = queryResults.Valor(nome);
                }
                queryResults.Seguinte();
            }

            QueryResult response = new QueryResult()
            {
                Headers = headers,
                Data    = data
            };

            bsERP.FechaEmpresaTrabalho();

            return(new ScriptResponse
            {
                Object = response
            });
        }
        //Method that executes the query in the external system.
        private ScriptResponse executeQuery(ContextData context, Entity document, string query)
        {
            ErpBS bsERP = new ErpBS();

            if (context.ExternalSystems == null || context.ExternalSystems.Count == 0)
            {
                throw new Exception("External System em falta");
            }

            var externalSystem = context.ExternalSystems.FirstOrDefault().Value;

            if (!externalSystem.Parameters.ContainsKey("TipoPlataforma"))
            {
                throw new Exception("TipoPlataforma inválido");
            }

            EnumTipoPlataforma tipoPlataforma;

            if (!Enum.TryParse <EnumTipoPlataforma>((string)externalSystem.Parameters["TipoPlataforma"], out tipoPlataforma))
            {
                throw new Exception("TipoPlataforma inválido");
            }

            try
            {
                bsERP.AbreEmpresaTrabalho(tipoPlataforma, externalSystem.Code, (string)externalSystem.Parameters["Username"], (string)externalSystem.Parameters["Password"]);
            }
            catch (Exception e)
            {
                throw new Exception("Erro a abrir a empresa no ERP: " + e.Message);
            }

            StdBELista queryResults = bsERP.Consulta(query);

            int numLinhas  = queryResults.NumLinhas();
            int numColunas = queryResults.NumColunas();

            string[] headers = new string[numColunas];
            for (short i = 0; i < numColunas; i++)
            {
                headers[i] = queryResults.Nome(i);
            }

            object[,] data = new object[numLinhas, numColunas];
            for (short i = 0; i < numLinhas; i++)
            {
                for (short j = 0; j < numColunas; j++)
                {
                    var nome = headers[j];
                    data[i, j] = queryResults.Valor(nome);
                }
                queryResults.Seguinte();
            }

            QueryResult response = new QueryResult()
            {
                Headers = headers,
                Data    = data
            };

            bsERP.FechaEmpresaTrabalho();

            return(new ScriptResponse
            {
                Result = response
            });
        }
        public ScriptResponse Execute(ContextData context, Entity document, Dictionary <string, object> parameters)
        {
            /* **************************************** */
            /* **************************************** */
            /*          ADD YOUR CODE HERE              */
            ErpBS bsERP          = new ErpBS();
            var   externalSystem = context.ExternalSystems.FirstOrDefault().Value;

            try
            {
                if (!externalSystem.Parameters.ContainsKey("TipoPlataforma"))
                {
                    throw new Exception("TipoPlataforma invalido");
                }

                EnumTipoPlataforma tipoPlataforma;
                if (!Enum.TryParse <EnumTipoPlataforma>((string)externalSystem.Parameters["TipoPlataforma"], out tipoPlataforma))
                {
                    throw new Exception("TipoPlataforma invalido");
                }

                try
                {
                    bsERP.AbreEmpresaTrabalho(tipoPlataforma, externalSystem.Code, (string)externalSystem.Parameters["Username"], (string)externalSystem.Parameters["Password"]);
                }
                catch (Exception e)
                {
                    throw new Exception("Erro a abrir a empresa no ERP: " + e.Message);
                }

                string str       = externalSystem.Parameters["0"].ToString();
                int    first     = str.IndexOf("Commitments.GoodsPurchaseRequest[") + "Commitments.GoodsPurchaseRequest[".Length;
                int    last      = str.LastIndexOf("].ERPCode");
                int    commIndex = int.Parse(str.Substring(first, last - first));

                StdBELista queryResults = bsERP.Consulta($"SELECT Nome,Pais FROM Fornecedores WHERE Fornecedor='{document.Commitments.GoodsPurchaseRequest[commIndex].Attributes.ERPCode}'");

                int numLinhas  = queryResults.NumLinhas();
                int numColunas = queryResults.NumColunas();

                string[] headers = new string[numColunas];
                for (short i = 0; i < numColunas; i++)
                {
                    headers[i] = queryResults.Nome(i);
                }

                object[,] data = new object[numLinhas, numColunas];
                for (short i = 0; i < numLinhas; i++)
                {
                    for (short j = 0; j < numColunas; j++)
                    {
                        var nome = headers[j];
                        data[i, j] = queryResults.Valor(nome);
                    }
                    queryResults.Seguinte();
                }

                QueryResult response = new QueryResult()
                {
                    Headers         = headers,
                    Data            = data,
                    NumberOfRecords = numLinhas
                };

                bsERP.FechaEmpresaTrabalho();

                return(new ScriptResponse
                {
                    Result = response
                });
            }
            catch (Exception ex)
            {
                bsERP.FechaEmpresaTrabalho();

                throw ex;
            }
        }