public CadastroOportunidade ConsultaUltimoOportunidade()
        {
            WS.ServiceLayer.ServiceLayer Service  = new WS.ServiceLayer.ServiceLayer();
            CadastroOportunidade         _Retorno = new CadastroOportunidade();

            try
            {
                using (HanaConnection conn = new HanaConnection(ConfigurationManager.ConnectionStrings["Hana"].ConnectionString))
                {
                    conn.Open();

                    var    Schema = ConfigurationManager.AppSettings["CompanyDB"];
                    string Sql    = string.Format(Properties.Resources.ConsultaUltimoOportunidade, Schema);

                    using (HanaCommand cmd3 = new HanaCommand(Sql, conn))
                    {
                        using (HanaDataReader productInfoReader3 = cmd3.ExecuteReader())
                        {
                            HanaCommand    cmd = new HanaCommand(Sql, conn);
                            HanaDataReader productInfoReader = cmd.ExecuteReader();

                            while (productInfoReader.Read())
                            {
                                var DocEntry = productInfoReader.GetString(0);
                                var CardName = productInfoReader.GetString(1);

                                var GetOportunidade = Service.Get($"SalesOpportunities({DocEntry})");
                                _Retorno = JsonConvert.DeserializeObject <CadastroOportunidade>(GetOportunidade.Documento, new Newtonsoft.Json.JsonSerializerSettings {
                                    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
                                });
                                _Retorno.CardName = CardName;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            finally
            {
                //Service.Logout();
            }

            return(_Retorno);
        }
        public CadastroOportunidades ConsultaOportunidade(CadastroOportunidade Oportunidade)
        {
            WS.ServiceLayer.ServiceLayer Service  = new WS.ServiceLayer.ServiceLayer();
            CadastroOportunidades        _Retorno = new CadastroOportunidades();

            try
            {
                using (HanaConnection conn = new HanaConnection(ConfigurationManager.ConnectionStrings["Hana"].ConnectionString))
                {
                    conn.Open();

                    var Schema = ConfigurationManager.AppSettings["CompanyDB"];

                    string Sql = string.Format(Properties.Resources.ConsultaOportunidadee, Schema, Oportunidade.CardCode, Oportunidade.CardName, Oportunidade.SequentialNo);

                    using (HanaCommand cmd3 = new HanaCommand(Sql, conn))
                    {
                        using (HanaDataReader productInfoReader3 = cmd3.ExecuteReader())
                        {
                            HanaCommand    cmd = new HanaCommand(Sql, conn);
                            HanaDataReader productInfoReader = cmd.ExecuteReader();

                            if (productInfoReader.HasRows)
                            {
                                productInfoReader.Read();

                                var Count    = productInfoReader.GetString(2);
                                var DocEntry = productInfoReader.GetString(0);
                                var CardName = productInfoReader.GetString(1);

                                var GetPN = Service.Get($"SalesOpportunities({DocEntry})");
                                var _RetornoOportunidade = JsonConvert.DeserializeObject <CadastroOportunidade>(GetPN.Documento, new Newtonsoft.Json.JsonSerializerSettings {
                                    NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
                                });
                                _RetornoOportunidade.CardName = CardName;

                                _Retorno.value    = new CadastroOportunidade[int.Parse(Count)];
                                _Retorno.value[0] = new CadastroOportunidade();
                                _Retorno.value[0] = _RetornoOportunidade;

                                int i = 1;

                                while (productInfoReader.Read())
                                {
                                    _Retorno.value[i] = new CadastroOportunidade();

                                    DocEntry = productInfoReader.GetString(0);
                                    CardName = productInfoReader.GetString(1);

                                    GetPN = Service.Get($"SalesOpportunities({DocEntry})");
                                    _RetornoOportunidade = JsonConvert.DeserializeObject <CadastroOportunidade>(GetPN.Documento, new Newtonsoft.Json.JsonSerializerSettings {
                                        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
                                    });
                                    _RetornoOportunidade.CardName = CardName;

                                    _Retorno.value[i] = _RetornoOportunidade;

                                    i++;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            finally
            {
                //Service.Logout();
            }

            return(_Retorno);
        }
        public Retorno InsereAtualizaOportunidade(CadastroOportunidade Oportunidade)
        {
            WS.ServiceLayer.ServiceLayer Service = new WS.ServiceLayer.ServiceLayer();
            Retorno _Retorno = new Retorno();

            try
            {
                if (!string.IsNullOrEmpty(Oportunidade.CardName))
                {
                    Oportunidade.CardName = null;
                }

                if (Oportunidade.SequentialNo == null)
                {
                    #region Insere Oportunidade

                    var Json = JsonConvert.SerializeObject(Oportunidade, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings {
                        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
                    });

                    _Retorno = Service.Add("SalesOpportunities", Json);

                    #endregion
                }
                else
                {
                    #region Atualiza Oportunidade


                    if (Oportunidade.SalesOpportunitiesInterests != null)
                    {
                        Oportunidade.SalesOpportunitiesInterests[0].SequenceNo = Oportunidade.SequentialNo;
                    }

                    if (Oportunidade.SalesOpportunitiesLines != null)
                    {
                        int count = 0;
                        foreach (var item in Oportunidade.SalesOpportunitiesLines)
                        {
                            item.LineNum = count;
                            count++;
                        }
                    }

                    var Json = Newtonsoft.Json.JsonConvert.SerializeObject(Oportunidade, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings {
                        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
                    });

                    _Retorno = Service.Update($"SalesOpportunities({Oportunidade.SequentialNo})", Json);

                    #endregion
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            finally
            {
                //Service.Logout();
            }

            return(_Retorno);
        }