Example #1
0
        public RegiaoAtuacao DefinirPropriedades(Intelbras.Message.Helper.MSG0076 xml)
        {
            var crm = new RegiaoAtuacao(this.Organizacao, this.IsOffline);

            if (!String.IsNullOrEmpty(xml.CodigoConta))
            {
                Model.Conta conta = new Model.Conta(this.Organizacao, this.IsOffline);
                conta = new Intelbras.CRM2013.Domain.Servicos.ContaService(this.Organizacao, this.IsOffline).BuscaConta(new Guid(xml.CodigoConta));
                if (conta != null)
                {
                    crm.Canal = conta.ID.Value;
                }
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Canal não encontrado!";
                return(crm);
            }

            //Municipio
            if (!String.IsNullOrEmpty(xml.ChaveIntegracaoCidade))
            {
                Model.Municipio cidade = new Model.Municipio(this.Organizacao, this.IsOffline);
                cidade = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaMunicipio(xml.ChaveIntegracaoCidade);

                if (cidade != null && cidade.ID.HasValue)
                {
                    crm.MunicipioId = cidade.ID.Value;
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "Cidade não encontrada!";
                    return(crm);
                }
            }

            return(crm);
        }
Example #2
0
        public Municipio Persistir(Model.Municipio objMunicipio)
        {
            var temp = RepositoryService.Municipio.ObterPor(objMunicipio.ChaveIntegracao, "itbc_municipiosid");

            if (temp != null)
            {
                objMunicipio.ID = temp.ID;
                RepositoryService.Municipio.Update(objMunicipio);

                if (!temp.State.Equals(objMunicipio.State) && objMunicipio.State != null)
                {
                    this.MudarStatusMunicipio(temp.ID.Value, objMunicipio.State.Value);
                }

                return(temp);
            }
            else
            {
                objMunicipio.ID = RepositoryService.Municipio.Create(objMunicipio);
            }

            return(objMunicipio);
        }
Example #3
0
        public Contato DefinirPropriedades(Intelbras.Message.Helper.MSG0058 xml)
        {
            var crm = new Contato(this.Organizacao, this.IsOffline);

            #region Propriedades Crm->Xml
            crm.IntegrarNoPlugin = true;
            if (!String.IsNullOrEmpty(xml.Email))
            {
                crm.Email1 = xml.Email;
            }
            else
            {
                crm.AddNullProperty("Email1");
            }

            if (!String.IsNullOrEmpty(xml.EmailAlternativo))
            {
                crm.Email2 = xml.EmailAlternativo;
            }
            else
            {
                crm.AddNullProperty("Email2");
            }

            if (!String.IsNullOrEmpty(xml.Naturalidade))
            {
                crm.Naturalidade = xml.Naturalidade;
            }

            else
            {
                crm.AddNullProperty("Naturalidade");
            }

            if (!String.IsNullOrEmpty(xml.Nacionalidade))
            {
                crm.Nacionalidade = xml.Nacionalidade;
            }
            else
            {
                crm.AddNullProperty("Nacionalidade");
            }

            if (xml.SuspensaoCredito.HasValue)
            {
                crm.SuspensaoCredito = xml.SuspensaoCredito.Value;
            }
            else
            {
                crm.AddNullProperty("SuspensaoCredito");
            }

            if (xml.Sexo.HasValue)
            {
                crm.Sexo = xml.Sexo.Value;
            }
            else
            {
                crm.AddNullProperty("Sexo");
            }

            if (xml.ContatoNFE.HasValue)
            {
                crm.ContatoNFE = xml.ContatoNFE.Value;
            }
            else
            {
                crm.AddNullProperty("ContatoNFE");
            }

            if (!String.IsNullOrEmpty(xml.RamalFax))
            {
                crm.RamalFax = xml.RamalFax;
            }
            else
            {
                crm.AddNullProperty("RamalFax");
            }

            if (!String.IsNullOrEmpty(xml.Celular))
            {
                crm.TelefoneCelular = xml.Celular;
            }
            else
            {
                crm.AddNullProperty("TelefoneCelular");
            }

            if (!String.IsNullOrEmpty(xml.TelefoneAssistente))
            {
                crm.TelefoneAssistente = xml.TelefoneAssistente;
            }
            else
            {
                crm.AddNullProperty("TelefoneAssistente");
            }

            if (!String.IsNullOrEmpty(xml.CNPJ))
            {
                if (xml.CNPJ.Length == 11)
                {
                    crm.CpfCnpj = Intelbras.CRM2013.Domain.Servicos.Helper.FormatarCpf(xml.CNPJ);
                }
                else
                {
                    crm.CpfCnpj = xml.CNPJ;
                }
            }
            else if (!String.IsNullOrEmpty(xml.CPF))
            {
                if (xml.CPF.Length == 11)
                {
                    crm.CpfCnpj = Intelbras.CRM2013.Domain.Servicos.Helper.FormatarCpf(xml.CPF);
                }
                else
                {
                    crm.CpfCnpj = xml.CPF;
                }
            }
            else
            {
                crm.AddNullProperty("CpfCnpj");
            }

            if (!String.IsNullOrEmpty(xml.TipoObjetoCanal) && !String.IsNullOrEmpty(xml.Canal))
            {
                string tipoObjetoCanal;
                if (xml.TipoObjetoCanal == "account" || xml.TipoObjetoCanal == "contact")
                {
                    tipoObjetoCanal = xml.TipoObjetoCanal;
                    crm.AssociadoA  = new Lookup(new Guid(xml.Canal), xml.TipoObjetoCanal);
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "TipoObjetoCanal ou Canal fora do padrão.";
                    return(crm);
                }
            }
            else
            {
                crm.AddNullProperty("AssociadoA");
            }
            //Pode ser um Representante, que não tenha ligação com nenhum canal
            //else
            //{
            //    resultadoPersistencia.Sucesso = false;
            //    resultadoPersistencia.Mensagem = "TipoObjetoCanal ou Canal não enviado.";
            //    return crm;
            //}

            if (xml.Cargo.HasValue)
            {
                crm.Cargo = xml.Cargo.Value;
            }
            else
            {
                crm.AddNullProperty("Cargo");
            }

            if (xml.LimiteCredito.HasValue)
            {
                crm.LimiteCredito = xml.LimiteCredito.Value;
            }
            else
            {
                crm.AddNullProperty("LimiteCredito");
            }

            if (xml.DataNascimento.HasValue)
            {
                crm.Aniversario = xml.DataNascimento.Value;
            }
            else
            {
                crm.AddNullProperty("Aniversario");
            }

            if (!String.IsNullOrEmpty(xml.TelefoneGerente))
            {
                crm.TelefoneGerente = xml.TelefoneGerente;
            }
            else
            {
                crm.AddNullProperty("TelefoneGerente");
            }

            if (!String.IsNullOrEmpty(xml.RamalTelefoneAlternativo))
            {
                crm.Ramal2 = xml.RamalTelefoneAlternativo;
            }
            else
            {
                crm.AddNullProperty("Ramal2");
            }

            if (!String.IsNullOrEmpty(xml.RG))
            {
                crm.DocIdentidade = xml.RG;
            }
            else
            {
                crm.AddNullProperty("DocIdentidade");
            }

            if (xml.Escolaridade.HasValue && xml.Escolaridade.Value != -1)
            {
                crm.Escolaridade = xml.Escolaridade.Value;
            }
            else
            {
                crm.AddNullProperty("Escolaridade");
            }

            if (!String.IsNullOrEmpty(xml.TelefoneAlternativo))
            {
                crm.TelefoneComercial2 = xml.TelefoneAlternativo;
            }
            else
            {
                crm.AddNullProperty("TelefoneComercial2");
            }

            if (xml.TipoContato.HasValue)
            {
                crm.TipoRelacao = xml.TipoContato.Value;
            }
            else
            {
                crm.AddNullProperty("TipoRelacao");
            }

            if (!String.IsNullOrEmpty(xml.Telefone))
            {
                crm.TelefoneComercial = xml.Telefone;
            }
            else
            {
                crm.AddNullProperty("TelefoneComercial");
            }

            if (!String.IsNullOrEmpty(xml.NomeContato))
            {
                crm.PrimeiroNome = xml.NomeContato;
            }
            if (!String.IsNullOrEmpty(xml.DescricaoContato))
            {
                crm.Descricao = xml.DescricaoContato;
            }
            else
            {
                crm.AddNullProperty("Descricao");
            }

            if (!String.IsNullOrEmpty(xml.NumeroContato))
            {
                crm.NumeroContato = xml.NumeroContato;
            }
            else
            {
                crm.AddNullProperty("NumeroContato");
            }

            if (!String.IsNullOrEmpty(xml.SegundoNome))
            {
                crm.SegundoNome = xml.SegundoNome;
            }
            else
            {
                crm.AddNullProperty("SegundoNome");
            }

            if (!String.IsNullOrEmpty(xml.Ramal))
            {
                crm.Ramal1 = xml.Ramal;
            }
            else
            {
                crm.AddNullProperty("Ramal1");
            }

            if (xml.Funcao.HasValue)
            {
                crm.Funcao = xml.Funcao.Value;
            }
            else
            {
                crm.AddNullProperty("Funcao");
            }

            if (!String.IsNullOrEmpty(xml.OrgaoExpeditor))
            {
                crm.EmissorDocIdentidade = xml.OrgaoExpeditor;
            }
            else
            {
                crm.AddNullProperty("EmissorDocIdentidade");
            }

            if (xml.Cargo.HasValue)
            {
                crm.Cargo = xml.Cargo.Value;
            }
            else
            {
                crm.AddNullProperty("Cargo");
            }

            if (!String.IsNullOrEmpty(xml.NomeGerente))
            {
                crm.Gerente = xml.NomeGerente;
            }
            else
            {
                crm.AddNullProperty("Gerente");
            }

            if (!String.IsNullOrEmpty(xml.NomeFilhos))
            {
                crm.NomeFilhos = xml.NomeFilhos;
            }
            else
            {
                crm.AddNullProperty("NomeFilhos");
            }

            if (!String.IsNullOrEmpty(xml.Fax))
            {
                crm.Fax = xml.Fax;
            }
            else
            {
                crm.AddNullProperty("Fax");
            }

            if (!String.IsNullOrEmpty(xml.NomeAssistente))
            {
                crm.Assistente = xml.NomeAssistente;
            }
            else
            {
                crm.AddNullProperty("Assistente");
            }

            if (!String.IsNullOrEmpty(xml.Saudacao))
            {
                crm.Saudacao = xml.Saudacao;
            }
            else
            {
                crm.AddNullProperty("Saudacao");
            }

            if (xml.CodigoRepresentante.HasValue)
            {
                crm.CodigoRepresentante = xml.CodigoRepresentante.ToString();
            }
            else
            {
                crm.AddNullProperty("CodigoRepresentante");
            }

            if (xml.Area.HasValue)
            {
                crm.Area = xml.Area.Value;
            }
            else
            {
                crm.AddNullProperty("Area");
            }

            if (xml.DataEspecial.HasValue)
            {
                crm.DatasEspeciais = xml.DataEspecial.Value;
            }
            else
            {
                crm.AddNullProperty("DatasEspeciais");
            }

            if (xml.TemFilhos.HasValue)
            {
                crm.TemFilhos = xml.TemFilhos.Value == 2 ? 0 : 1;
            }
            else
            {
                crm.AddNullProperty("TemFilhos");
            }

            if (xml.EstadoCivil.HasValue)
            {
                crm.EstadoCivil = xml.EstadoCivil.Value;
            }
            else
            {
                crm.AddNullProperty("EstadoCivil");
            }

            if (!String.IsNullOrEmpty(xml.Departamento))
            {
                crm.Departamento = xml.Departamento;
            }
            else
            {
                crm.AddNullProperty("Departamento");
            }

            if (xml.PapelCanal.HasValue)
            {
                crm.PapelCanal = xml.PapelCanal.Value;
            }
            else
            {
                crm.AddNullProperty("PapelCanal");
            }

            if (!String.IsNullOrEmpty(xml.DescricaoPapelCanal))
            {
                crm.DescricaoPapelCanal = xml.DescricaoPapelCanal;
            }
            else
            {
                crm.AddNullProperty("DescricaoPapelCanal");
            }

            if (xml.ListaAreaAtuacao != null && xml.ListaAreaAtuacao.Count > 0)
            {
                crm.AreaAtuacaoTexto = "";
                foreach (var areaAtuacao in xml.ListaAreaAtuacao)
                {
                    if (!String.IsNullOrEmpty(areaAtuacao.ToString()))
                    {
                        crm.AreaAtuacaoTexto += areaAtuacao.ToString() + ",";
                    }
                    else
                    {
                        resultadoPersistencia.Sucesso  = false;
                        resultadoPersistencia.Mensagem = "Área de Atuação " + areaAtuacao.ToString() + " não contém um código válido";
                        return(null);
                    }
                }
            }

            if (!String.IsNullOrEmpty(xml.DescricaoAreaAtuacao))
            {
                crm.DescricaoAreaAtuacao = xml.DescricaoAreaAtuacao;
            }
            else
            {
                crm.AddNullProperty("DescricaoAreaAtuacao");
            }

            if (!String.IsNullOrEmpty(xml.Sobrenome))
            {
                crm.Sobrenome = xml.Sobrenome;
            }
            else
            {
                crm.AddNullProperty("Sobrenome");
            }

            if (xml.NumeroFilhos.HasValue)
            {
                crm.NumeroFilhos = xml.NumeroFilhos.Value;
            }
            else
            {
                crm.AddNullProperty("NumeroFilhos");
            }

            if (!String.IsNullOrEmpty(xml.CodigoContato))
            {
                crm.ID = new Guid(xml.CodigoContato);
            }

            if (!String.IsNullOrEmpty(xml.Regiao))
            {
                crm.Endereco1Regiaoid = new Lookup(new Guid(xml.Regiao), "");
            }
            else
            {
                crm.AddNullProperty("Endereco1Regiaoid");
            }

            if (!String.IsNullOrEmpty(xml.NomeConjuge))
            {
                crm.NomeConjuge = xml.NomeConjuge;
            }
            else
            {
                crm.AddNullProperty("NomeConjuge");
            }

            if (xml.IntegraIntelbrasPontua != null)
            {
                crm.IntegraIntelbrasPontua = xml.IntegraIntelbrasPontua;
            }
            else
            {
                crm.IntegraIntelbrasPontua = false;
            }

            crm.IntegradoEm          = DateTime.Now;
            crm.IntegradoPor         = usuarioIntegracao.NomeCompleto;
            crm.UsuarioIntegracao    = xml.LoginUsuario;
            crm.IntegrarNoBarramento = true;

            if (!String.IsNullOrEmpty(xml.IMEI))
            {
                crm.IMEI = xml.IMEI;
            }
            else
            {
                crm.AddNullProperty("IMEI");
            }

            if (xml.AcessoPortal.HasValue)
            {
                crm.AcessoPortal = xml.AcessoPortal;
            }

            if (xml.AcessoAPP.HasValue)
            {
                crm.AcessoAPPISOL = xml.AcessoAPP;
            }

            if (!string.IsNullOrEmpty(xml.Treinamentos))
            {
                crm.Treinamento = xml.Treinamentos;
            }

            if (xml.AcessoSolar.HasValue)
            {
                crm.AcessoSolar = xml.AcessoSolar;
            }

            #region Services

            if (!String.IsNullOrEmpty(xml.Loja))
            {
                crm.Loja = new Lookup(new Guid(xml.Loja), "");
            }
            else
            {
                crm.AddNullProperty("Loja");
            }

            if (xml.CondicaoFrete.HasValue)
            {
                crm.Endereco1CondicoesFrete = xml.CondicaoFrete;
            }
            else
            {
                crm.AddNullProperty("Endereco1CondicoesFrete");
            }

            crm.Status = xml.Situacao;

            if (xml.CodigoCliente.HasValue)
            {
                crm.CodigoRemetente = xml.CodigoCliente.ToString();
            }

            // Moeda
            if (!String.IsNullOrEmpty(xml.Moeda))
            {
                Model.Moeda moeda = new Model.Moeda(this.Organizacao, this.IsOffline);
                moeda = new Intelbras.CRM2013.Domain.Servicos.MoedaService(this.Organizacao, this.IsOffline).BuscaMoedaPorNome(xml.Moeda);

                if (moeda != null && moeda.ID.HasValue)
                {
                    crm.Moeda = new Lookup(moeda.ID.Value, "");
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "Moeda não encontrado!";
                    return(crm);
                }
            }
            else
            {
                crm.AddNullProperty("Moeda");
            }

            // ListaPreco
            if (!String.IsNullOrEmpty(xml.ListaPreco))
            {
                Model.ListaPreco listaPreco = new Model.ListaPreco(this.Organizacao, this.IsOffline);
                listaPreco = new Intelbras.CRM2013.Domain.Servicos.ListaPrecoService(this.Organizacao, this.IsOffline).BuscaListaPreco(xml.ListaPreco);

                if (listaPreco != null && listaPreco.ID.HasValue)
                {
                    crm.ListaPrecos = new Lookup(listaPreco.ID.Value, "");
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "ListaPreco não encontrado!";
                    return(crm);
                }
            }
            else
            {
                crm.AddNullProperty("ListaPrecos");
            }

            if (xml.EnderecoPrincipal != null)
            {
                // Cidade
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Cidade))
                {
                    Model.Municipio cidade = new Model.Municipio(this.Organizacao, this.IsOffline);
                    cidade = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaMunicipio(xml.EnderecoPrincipal.Cidade);

                    if (cidade != null && cidade.ID.HasValue)
                    {
                        crm.Endereco1Municipioid = new Lookup(cidade.ID.Value, "");
                    }

                    else
                    {
                        resultadoPersistencia.Sucesso  = false;
                        resultadoPersistencia.Mensagem = "Cidade não encontrada!";
                        return(crm);
                    }
                }

                // Estado (UF)
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Estado))
                {
                    Model.Estado estado = new Model.Estado(this.Organizacao, this.IsOffline);
                    estado = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaEstado(xml.EnderecoPrincipal.Estado);

                    if (estado != null && estado.ID.HasValue)
                    {
                        crm.Endereco1Estadoid = new Lookup(estado.ID.Value, "");
                    }
                    else
                    {
                        resultadoPersistencia.Sucesso  = false;
                        resultadoPersistencia.Mensagem = "Estado não encontrado!";
                        return(crm);
                    }
                }

                // País
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Pais))
                {
                    Model.Pais pais = new Model.Pais(this.Organizacao, this.IsOffline);
                    pais = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaPais(xml.EnderecoPrincipal.Pais);

                    if (pais != null && pais.ID.HasValue)
                    {
                        crm.Endereco1Pais = new Lookup(pais.ID.Value, "");
                    }
                    else
                    {
                        resultadoPersistencia.Sucesso  = false;
                        resultadoPersistencia.Mensagem = "País não encontrado!";
                        return(crm);
                    }
                }
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Endereço Principal não enviado!";
                return(crm);
            }

            ///TODO: ClientePotencialOriginador - Aguardar Rollout - Modelagem não prevista para esta etapa.

            if (!String.IsNullOrEmpty(xml.ClientePotencialOriginador))
            {
                crm.ClientePotencialOriginador = new Lookup(new Guid(xml.ClientePotencialOriginador), "");
            }
            else
            {
                crm.AddNullProperty("ClientePotencialOriginador");
            }

            #endregion

            #region Bloco Endereco
            ///Bloco Endereco

            if (xml.EnderecoPrincipal != null)
            {
                if (xml.EnderecoPrincipal.TipoEndereco.HasValue)
                {
                    crm.Endereco1TipoEndereco = xml.EnderecoPrincipal.TipoEndereco.Value;
                }
                else
                {
                    crm.AddNullProperty("Endereco1TipoEndereco");
                }

                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.NomeEndereco))
                {
                    crm.Endereco1Nome = xml.EnderecoPrincipal.NomeEndereco;
                }
                else
                {
                    crm.AddNullProperty("Endereco1Nome");
                }

                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Logradouro))
                {
                    crm.Endereco1Rua = xml.EnderecoPrincipal.Logradouro;
                }
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Numero))
                {
                    crm.Endereco1Numero = xml.EnderecoPrincipal.Numero;
                }
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Complemento))
                {
                    crm.Endereco1Complemento = xml.EnderecoPrincipal.Complemento;
                }
                else
                {
                    crm.AddNullProperty("Endereco1Complemento");
                }

                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Bairro))
                {
                    crm.Endereco1Bairro = xml.EnderecoPrincipal.Bairro;
                }
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.CEP))
                {
                    crm.Endereco1CEP = Intelbras.CRM2013.Domain.Servicos.Helper.FormatarCep(xml.EnderecoPrincipal.CEP);
                }
                if (!String.IsNullOrEmpty(xml.EnderecoPrincipal.Telefone))
                {
                    crm.Endereco1Telefone = xml.EnderecoPrincipal.Telefone;
                }
                else
                {
                    crm.AddNullProperty("Endereco1Telefone");
                }

                if (xml.MetodoEntrega.HasValue)
                {
                    crm.Endereco1MetodoEntrega = xml.MetodoEntrega.Value;
                }
                else
                {
                    crm.AddNullProperty("Endereco1MetodoEntrega");
                }
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Endereço Principal não enviado!";
                return(crm);
            }

            #endregion

            #endregion

            return(crm);
        }
Example #4
0
        public Fatura DefinirPropriedades(Intelbras.Message.Helper.MSG0094 xml)
        {
            Fatura crm = new Fatura(this.Organizacao, this.IsOffline);

            if (!String.IsNullOrEmpty(xml.NumeroNotaFiscal))
            {
                crm.NumeroNF = xml.NumeroNotaFiscal;
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "NumeroNotaFiscal não enviado.";
                return(crm);
            }

            crm.Serie     = xml.NumeroSerie;
            crm.ClienteId = new Lookup(new Guid(xml.CodigoClienteCRM), xml.TipoObjetoCliente);


            crm.ChaveIntegracao = xml.ChaveIntegracao; //NumeroPedido.ToString();

            if (xml.NumeroPedido != null)
            {
                Pedido pedido = new Servicos.PedidoService(this.Organizacao, this.IsOffline).BuscaPedidoEMS(xml.NumeroPedido.ToString());
                if (pedido != null)
                {
                    crm.PedidoCRM = new Lookup(pedido.ID.Value, "");

                    Fatura fatura = new Servicos.FaturaService(this.Organizacao, this.IsOffline).ObterFaturaPorPedidoEMS(xml.NumeroPedido.ToString());
                    if (fatura != null)
                    {
                        crm.ID = fatura.ID.Value;
                    }
                }
            }

            if (!String.IsNullOrEmpty(xml.NumeroPedidoCliente))
            {
                crm.PedidoCliente = xml.NumeroPedidoCliente;
            }
            else
            {
                crm.AddNullProperty("PedidoCliente");
            }

            if (!String.IsNullOrEmpty(xml.Descricao))
            {
                crm.Descricao = xml.Descricao;
            }
            else
            {
                crm.AddNullProperty("Descricao");
            }

            //Service Estabelecimento
            Estabelecimento ObjEstabelecimento = new Intelbras.CRM2013.Domain.Servicos.EstabelecimentoService(this.Organizacao, this.IsOffline).BuscaEstabelecimentoPorCodigo(xml.Estabelecimento.Value);

            if (ObjEstabelecimento != null)
            {
                crm.Estabelecimento = new Lookup((Guid)ObjEstabelecimento.ID, "");
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Identificador do Estabelecimento não encontrado.";
                return(crm);
            }

            //Service Condição Pagamento
            CondicaoPagamento ObjCondicaoPagamento = null;

            if (xml.CondicaoPagamento.HasValue)
            {
                ObjCondicaoPagamento = new Intelbras.CRM2013.Domain.Servicos.CondicaoPagamentoService(this.Organizacao, this.IsOffline).BuscaCondicaoPagamentoPorCodigo(xml.CondicaoPagamento.Value);

                if (ObjCondicaoPagamento != null)
                {
                    crm.CondicaoPagamento = new Lookup((Guid)ObjCondicaoPagamento.ID, "");
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "Identificador da Condição de Pagamento não encontrado.";
                    return(crm);
                }
            }
            else
            {
                crm.AddNullProperty("CondicaoPagamento");
            }

            crm.NomeAbreviado = xml.NomeAbreviadoCliente;

            //Service Natureza Operação
            NaturezaOperacao ObjNaturezaOperacao = new Intelbras.CRM2013.Domain.Servicos.NaturezaOperacaoService(this.Organizacao, this.IsOffline).BuscaNaturezaOperacaoPorCodigo(xml.NaturezaOperacao);

            if (ObjNaturezaOperacao != null)
            {
                crm.NaturezaOperacao = new Lookup((Guid)ObjNaturezaOperacao.ID, "");
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Identificador da Natureza de Operação não encontrado.";
                return(crm);
            }

            //Service Moeda
            Moeda ObjMoeda = new Intelbras.CRM2013.Domain.Servicos.MoedaService(this.Organizacao, this.IsOffline).BuscaMoedaPorCodigo(xml.Moeda);

            if (ObjMoeda != null)
            {
                crm.Moeda = new Lookup((Guid)ObjMoeda.ID, "");
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Identificador da Moeda não encontrado.";
                return(crm);
            }

            //Service para resolver o status
            if (System.Enum.IsDefined(typeof(Enum.Fatura.Status), xml.SituacaoNota))
            {
                crm.Status = xml.SituacaoNota;
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Identificador da Situação da nota não encontrado.";
                return(crm);
            }

            if (System.Enum.IsDefined(typeof(Enum.Fatura.RazaoStatus), xml.SituacaoEntrega))
            {
                crm.RazaoStatus = xml.SituacaoEntrega;
            }

            crm.DataEmissao = xml.DataEmissao;

            if (xml.DataSaida.HasValue)
            {
                crm.DataSaida = xml.DataSaida;
            }
            else
            {
                crm.AddNullProperty("DataSaida");
            }

            if (xml.DataEntrega.HasValue)
            {
                crm.DataEntrega = xml.DataEntrega;
            }
            else
            {
                crm.AddNullProperty("DataEntrega");
            }

            if (xml.DataConfirmacao != null)
            {
                crm.DataConfirmacao = xml.DataConfirmacao;
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "DataConfirmacao não enviada.";
                return(crm);
            }

            if (xml.DataCancelamento.HasValue)
            {
                crm.DataCancelamento = xml.DataCancelamento;
            }
            else
            {
                crm.AddNullProperty("DataCancelamento");
            }

            if (xml.DataConclusao.HasValue)
            {
                crm.DataConclusao = xml.DataConclusao;
            }
            else
            {
                crm.AddNullProperty("DataConclusao");
            }

            crm.ValorFrete = xml.ValorFrete;

            crm.PesoLiquido = xml.PesoLiquido;

            crm.PesoBruto = xml.PesoBruto;

            if (!String.IsNullOrEmpty(xml.Observacao))
            {
                crm.Observacao = xml.Observacao;
            }
            else
            {
                crm.AddNullProperty("Observacao");
            }

            crm.Volume = xml.Volume.ToString();

            crm.BaseICMS            = xml.ValorBaseICMS;
            crm.ValorICMS           = xml.ValorICMS;
            crm.ValorIPI            = xml.ValorIPI;
            crm.BaseSubstTributaria = xml.ValorBaseSubstituicaoTributaria;
            crm.ValorSubstituicao   = xml.ValorSubstituicaoTributaria;
            crm.ClienteRetira       = xml.RetiraNoLocal;

            if (xml.MetodoEntrega.HasValue)
            {
                crm.MetodoEntrega = xml.MetodoEntrega;
            }
            else
            {
                crm.AddNullProperty("MetodoEntrega");
            }

            //Service Transportadora
            Transportadora ObjTransportadora = new Intelbras.CRM2013.Domain.Servicos.TransportadoraService(this.Organizacao, this.IsOffline).ObterPorCodigoTransportadora(xml.Transportadora.Value);

            if (ObjTransportadora != null)
            {
                crm.Transportadora = new Lookup((Guid)ObjTransportadora.ID, "");
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Identificador da Transportadora não encontrado.";
                return(crm);
            }

            crm.Frete = xml.Frete;

            //crm.CondicoesFrete = xml.tipo;

            if (!String.IsNullOrEmpty(xml.TelefoneCobranca))
            {
                crm.TelefoneCobranca = xml.TelefoneCobranca;
            }
            else
            {
                crm.AddNullProperty("TelefoneCobranca");
            }

            if (!String.IsNullOrEmpty(xml.FaxCobranca))
            {
                crm.FaxCobranca = xml.FaxCobranca;
            }
            else
            {
                crm.AddNullProperty("FaxCobranca");
            }

            //Removido essa validação para que as NF de clientes estrangeiros sejam integrados no CRM
            //if (!String.IsNullOrEmpty(xml.CNPJ))
            //    crm.CpfCnpj = Intelbras.CRM2013.Domain.Servicos.Helper.FormatarCnpj(xml.CNPJ);
            //else if (!String.IsNullOrEmpty(xml.CPF))
            //    crm.CpfCnpj = Intelbras.CRM2013.Domain.Servicos.Helper.FormatarCpf(xml.CPF);
            //else
            //{
            //    resultadoPersistencia.Sucesso = false;
            //    resultadoPersistencia.Mensagem = "CNPJ/CPF não enviado.";
            //    return crm;
            //}

            if (!String.IsNullOrEmpty(xml.InscricaoEstadual))
            {
                crm.InscricaoEstadual = xml.InscricaoEstadual;
            }
            else
            {
                crm.AddNullProperty("InscricaoEstadual");
            }

            if (!string.IsNullOrEmpty(xml.Oportunidade))
            {
                crm.Oportunidade = new Lookup(new Guid(xml.Oportunidade), "");
            }

            xml.PrecoBloqueado = xml.PrecoBloqueado;

            if (xml.ValorDesconto.HasValue)
            {
                crm.ValorDescontoFatura = xml.ValorDesconto;
            }
            else
            {
                crm.AddNullProperty("ValorDescontoFatura");
            }

            if (xml.PercentualDesconto.HasValue)
            {
                crm.DescontoGlobalTotal = xml.PercentualDesconto;
            }
            else
            {
                crm.AddNullProperty("DescontoGlobalTotal");
            }

            //Service Lista Preço
            ListaPreco ObjListaPreco = new Intelbras.CRM2013.Domain.Servicos.ListaPrecoService(this.Organizacao, this.IsOffline).BuscaListaPreco(xml.ListaPreco);

            if (ObjListaPreco != null)
            {
                crm.ListaPrecos = new Lookup((Guid)ObjListaPreco.ID, "");
            }
            else
            {
                resultadoPersistencia.Sucesso  = false;
                resultadoPersistencia.Mensagem = "Identificador da Lista de Preço não encontrado.";
                return(crm);
            }

            crm.Prioridade = xml.Prioridade;

            if (xml.ValorTotal.HasValue)
            {
                crm.ValorTotal = xml.ValorTotal;
            }
            else
            {
                crm.AddNullProperty("ValorTotal");
            }

            if (xml.ValorTotalImpostos.HasValue)
            {
                crm.TotalImpostos = xml.ValorTotalImpostos;
            }
            else
            {
                crm.AddNullProperty("TotalImpostos");
            }

            //if (xml.ValorTotalProdutosSemImposto.HasValue)
            //    crm.ValorTotalProdutosSemIPIST = xml.ValorTotalProdutosSemImposto;

            if (xml.ValorTotalSemFrete.HasValue)
            {
                crm.ValorTotalSemFrete = xml.ValorTotalSemFrete;
            }
            else
            {
                crm.AddNullProperty("ValorTotalSemFrete");
            }

            if (xml.ValorTotalDesconto.HasValue)
            {
                crm.DescontoTotal = xml.ValorTotalDesconto;
            }
            else
            {
                crm.AddNullProperty("DescontoTotal");
            }

            if (xml.ValorTotalProdutos.HasValue)
            {
                crm.TotalProdutosComIPIST = xml.ValorTotalProdutos;
            }
            else
            {
                crm.AddNullProperty("TotalProdutosComIPIST");
            }

            if (xml.ValorTotalProdutosSemImposto.HasValue)
            {
                crm.ValorTotalProdutosSemIPIST = xml.ValorTotalProdutosSemImposto;
            }
            else
            {
                crm.AddNullProperty("ValorTotalProdutosSemIPIST");
            }

            //Service Endereco Entrega

            if (!string.IsNullOrEmpty(xml.EnderecoEntrega.CaixaPostal))
            {
                crm.EnderecoEntregaCaixaPostal = xml.EnderecoEntrega.CaixaPostal;
            }
            else
            {
                crm.AddNullProperty("EnderecoEntregaCaixaPostal");
            }

            if (!string.IsNullOrEmpty(xml.EnderecoEntrega.NomeEndereco))
            {
                crm.NomeEntrega = xml.EnderecoEntrega.NomeEndereco;
            }
            else
            {
                crm.AddNullProperty("NomeEntrega");
            }

            crm.CEPEntrega            = xml.EnderecoEntrega.CEP;
            crm.EnderecoEntregaRua    = xml.EnderecoEntrega.Logradouro;
            crm.EnderecoEntregaNumero = xml.EnderecoEntrega.Numero;
            crm.BairroEntrega         = xml.EnderecoEntrega.Bairro;
            crm.ComplementoEntrega    = xml.EnderecoEntrega.Complemento;
            crm.TipoNotaFiscal        = xml.TipoNotaFiscal;
            crm.NotaDevolucao         = xml.NotaDevolucao;
            crm.IdentificadorUnicoNfe = xml.IdentificadorUnicoNFE;

            if (!string.IsNullOrEmpty(xml.EnderecoEntrega.Complemento))
            {
                crm.ComplementoEntrega = xml.EnderecoEntrega.Complemento;
            }
            else
            {
                crm.AddNullProperty("ComplementoEntrega");
            }


            if (!String.IsNullOrEmpty(xml.EnderecoEntrega.Cidade))
            {
                Model.Municipio cidade = new Model.Municipio(this.Organizacao, this.IsOffline);
                cidade = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaMunicipio(xml.EnderecoEntrega.Cidade);

                if (cidade != null && cidade.ID.HasValue)
                {
                    crm.EnderecoEntregaCidade = new Lookup(cidade.ID.Value, "");
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "Identificador da Cidade não encontrado!";
                    return(crm);
                }
            }

            if (!String.IsNullOrEmpty(xml.EnderecoEntrega.Estado))
            {
                Model.Estado estado = new Model.Estado(this.Organizacao, this.IsOffline);
                estado = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaEstado(xml.EnderecoEntrega.Estado);

                if (estado != null && estado.ID.HasValue)
                {
                    crm.EnderecoEntregaEstado = new Lookup(estado.ID.Value, "");
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "Identificador do Estado não encontrado!";
                    return(crm);
                }
            }

            ///Service Pais
            if (!String.IsNullOrEmpty(xml.EnderecoEntrega.Pais))
            {
                Model.Pais pais = new Model.Pais(this.Organizacao, this.IsOffline);
                pais = new Intelbras.CRM2013.Domain.Servicos.EnderecoServices(this.Organizacao, this.IsOffline).BuscaPais(xml.EnderecoEntrega.Pais);

                if (pais != null && pais.ID.HasValue)
                {
                    crm.EnderecoEntregaPais = new Lookup(pais.ID.Value, "");
                }
                else
                {
                    resultadoPersistencia.Sucesso  = false;
                    resultadoPersistencia.Mensagem = "Identificador do País não encontrado.";
                    return(crm);
                }
            }

            if (!String.IsNullOrEmpty(xml.EnderecoEntrega.NomeContato))
            {
                crm.NomeEntrega = xml.EnderecoEntrega.NomeContato;
            }
            else
            {
                crm.AddNullProperty("NomeEntrega");
            }


            if (!String.IsNullOrEmpty(xml.EnderecoEntrega.Telefone))
            {
                crm.TelefoneEntrega = xml.EnderecoEntrega.Telefone;
            }
            else
            {
                crm.AddNullProperty("TelefoneEntrega");
            }


            if (!String.IsNullOrEmpty(xml.EnderecoEntrega.Fax))
            {
                crm.FaxEntrega = xml.EnderecoEntrega.Fax;
            }
            else
            {
                crm.AddNullProperty("FaxEntrega");
            }


            #region Representante

            crm.IntegradoRepresentanteComErro = false;
            crm.IntegradoComErros             = false;
            crm.RepresentanteOriginal         = xml.Representante.ToString();

            Contato ObjRepresentante = new Intelbras.CRM2013.Domain.Servicos.ContatoService(this.Organizacao, this.IsOffline)
                                       .BuscaContatoPorCodigoRepresentante(xml.Representante.ToString());

            if (ObjRepresentante != null)
            {
                crm.KARepresentante = new Lookup(ObjRepresentante.ID.Value, "");
            }
            else
            {
                crm.IntegradoRepresentanteComErro = true;
                crm.IntegradoComErros             = true;
                crm.KARepresentante = new Lookup(ObterRepresentatePadrao(), "");
            }

            #endregion

            return(crm);
        }