Funções básicas

Iniciando o primeiro post desta categoria mostrando algumas das funções básicas (Mas essenciais) na utilização da NFe com .NET.

Módulo 11 – Para calculo do digito verificado da Chave.
Com certeza esta função pode ser reduzida para menos linhas… se o fizer, por favor deixe um comentário com o código, sugestões são sempre bem vindas :P

        public static int modulo11(string chaveNFE)
        {
            if (chaveNFE.Length != 43)
                throw new Exception("Chave inválida, não é possível calcular o digito verificador");

            string baseCalculo = "4329876543298765432987654329876543298765432";
            int somaResultados = 0;

            for (int i = 0; i <= chaveNFE.Length - 1; i++)
            {
                int numNF = Convert.ToInt32(chaveNFE[i].ToString());
                int numBaseCalculo = Convert.ToInt32(baseCalculo[i].ToString());

                somaResultados += (numBaseCalculo * numNF);
            }

            int restoDivisao = (somaResultados % 11);
            int dv = 11 - restoDivisao;
            if ((dv == 0) || (dv > 9))
                return 0;
            else
                return dv;
        }

Remover Acentos
Esta função não é de minha autoria, peguei no Google um tempo atrás e realmente não lembro mais de onde ela foi retirada.

        public static string TirarAcento(string palavra)
        {
            string palavraSemAcento = "";
            string caracterComAcento = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ";
            string caracterSemAcento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC";

            for (int i = 0; i < palavra.Length; i++)
            {
                if (caracterComAcento.IndexOf(Convert.ToChar(palavra.Substring(i, 1))) >= 0)
                {
                    int car = caracterComAcento.IndexOf(Convert.ToChar(palavra.Substring(i, 1)));
                    palavraSemAcento += caracterSemAcento.Substring(car, 1);
                }
                else
                {
                    palavraSemAcento += palavra.Substring(i, 1);
                }
            }

            return palavraSemAcento;
        }

Remove formatação
Remove formatação de CNPJ/IE/RG/CPF, entre outros

        public static string removeFormatacao(string texto)
        {
            string txt = "";

            txt = texto.Replace(".", "");
            txt = txt.Replace("-", "");
            txt = txt.Replace("/", "");
            txt = txt.Replace("(", "");
            txt = txt.Replace(")", "");
            txt = txt.Replace(" ", "");

            return txt;
        }

Por enquanto é isso, em breve (Ainda esta semana acredito eu) estarei postando uma classe para geração de XML, assim como exemplos para conexão com WebService, assinatura digital, cadeias de certificado, utilização de certificados A1 e A3 e etc.

Abraços

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2.687 Comments »

 
 

Deixe uma resposta

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>