Пример #1
0
        public int CalcularDataEntrega(string tipoServico, string cepOrigem, string cepDestino, string dataPostagem)
        {
            string retorno     = string.Empty;
            string tipoEntrega = tipoServicoCorreios(tipoServico);

            // Instancio o web-service
            Correios.CalcPrecoPrazoWSSoapClient webServiceCorreios = new Correios.CalcPrecoPrazoWSSoapClient();

            //calcula o prazo de entrega
            Correios.cResultado retornoCorreios = webServiceCorreios.CalcPrazoData(tipoEntrega, cepOrigem.Replace("-", "").Trim(), cepDestino.Replace("-", "").Trim(), dataPostagem);

            if (retornoCorreios.Servicos.Length > 0)
            {
                // Se deu tudo certo, então retorna o valor
                if (retornoCorreios.Servicos[0].Erro == "0")
                {
                    retorno = retornoCorreios.Servicos[0].PrazoEntrega;
                }
                else
                {
                    String ret = retornoCorreios.Servicos[0].MsgErro;
                    retorno = retornoCorreios.Servicos[0].PrazoEntrega;
                }
            }
            else
            {
                retorno = "-1";//"NÃO FOI POSSÍVEL CONSULTAR O SERVIÇO DESEJADO!";
            }

            return(Convert.ToInt32(retorno));
        }
        public void EnviarPedidos(string[] idPedidos)
        {
            string cepUnisinos     = "93022750";
            var    servicoEmail    = new Util.EmailService();
            var    servicoCorreios = new Correios.CalcPrecoPrazoWSSoapClient("CalcPrecoPrazoWSSoap12");

            foreach (var id in idPedidos)
            {
                if (!string.IsNullOrEmpty(id))
                {
                    long idPesquisa = Convert.ToInt64(id);
                    var  pedido     = contexto.PedidoCliente
                                      .Include("Cliente")
                                      .Include("EnderecoEntrega")
                                      .FirstOrDefault(p => p.IdPedidoCliente == idPesquisa);
                    servicoCorreios.Open();
                    var retorno = servicoCorreios.CalcPrazoData(
                        "40010", // sedex
                        cepUnisinos,
                        pedido.EnderecoEntrega.CEP.ToString(),
                        DateTime.Now.ToString("dd/MM/yyyy"));

                    if (retorno.Servicos.Count() > 0)
                    {
                        if (!string.IsNullOrEmpty(retorno.Servicos.FirstOrDefault().Erro) ||
                            !string.IsNullOrEmpty(retorno.Servicos.FirstOrDefault().MsgErro))
                        {
                            throw new Exception(retorno.Servicos.FirstOrDefault().Erro + " - " + retorno.Servicos.FirstOrDefault().MsgErro);
                        }
                        else
                        {
                            AlterarStatusParaAguardandoColeta(pedido);
                            servicoEmail.SendEmail(
                                new List <string>()
                            {
                                pedido.Cliente.Email
                            },
                                "Envio",
                                "Olá " + pedido.Cliente.Nome +
                                ", \n Seu pedido será enviado em breve.\n" +
                                "O prazo de entrega de seu pedido pelos correios é " + retorno.Servicos.FirstOrDefault().PrazoEntrega + " dias.");
                        }
                    }
                }
            }
        }