Beispiel #1
0
        ///  <summary>
        ///  Envia uma NFe para o servidor da sefaz.
        ///  </summary>
        /// <param name="nfeFacade"></param>
        /// <param name="dadosDoEnvio">Informações do envio no formato: path_do_arquivo_xml_da_nfe_nao_assinada#numero_do_lote.</param>
        private static void EnviarNFe(NFeFacade nfeFacade, string dadosDoEnvio)
        {
            string strPathArquivoXml;
            int numeroLote;
            TipoXmlNFe tipoXml;

            try
            {
                var arrayStrDados = dadosDoEnvio.Split('#');
                strPathArquivoXml = arrayStrDados[0];
                numeroLote = int.Parse(arrayStrDados[1]);
                switch (arrayStrDados[2])
                {
                    case "D": tipoXml = TipoXmlNFe.Destinatario;
                        break;
                    case "N":
                        tipoXml = TipoXmlNFe.NFe;
                        break;
                    case "L":
                        tipoXml = TipoXmlNFe.Lote;
                        break;
                    default:
                        throw new ArgumentException();
                }
            }
            catch
            {
                Console.WriteLine("Um ou mais parâmetros foram informados de forma incorreta.");
                return;
            }

            try
            {
                File.ReadAllText(strPathArquivoXml);
            }
            catch (UnauthorizedAccessException ex)
            {
                var strMensagem = string.Format("Não foi possível acessar o arquivo \"{0}\"", strPathArquivoXml);
                Console.WriteLine(strMensagem);
                Console.WriteLine("Detalhes:");
                Console.WriteLine(ex.Message);
                return;
            }

            try
            {
                var booStatusOk = ConsultarStatusServico(nfeFacade);

                if (!booStatusOk)
                {
                    return;
                }

                var nfeBuilder = new NFeBuilder(strPathArquivoXml, tipoXml);
                Console.WriteLine("Preparando a NFe...");
                var retorno = nfeFacade.EnviarNFe(numeroLote, nfeBuilder.Build());
                if (retorno.Retorno.cStat == 103)
                {
                    Console.WriteLine("#NFe#"+retorno.Retorno.infRec.nRec);
                }
                else
                {
                    Console.WriteLine("#Erro#"+retorno.Retorno.xMotivo);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Houve uma falha não esperada durante o processo de envio.");
                Console.WriteLine("Detalhes:");
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #2
0
        ///  <summary>
        ///  Envia uma NFe para o servidor da sefaz.
        ///  </summary>
        /// <param name="nfeFacade"></param>
        /// <param name="dadosDoEnvio">Informações do envio no formato: path_do_arquivo_xml_da_nfe_nao_assinada#numero_do_lote.</param>
        private static void EnviarNFe(NFeFacade nfeFacade, string dadosDoEnvio)
        {
            string     strPathArquivoXml;
            int        numeroLote;
            TipoXmlNFe tipoXml;

            try
            {
                var arrayStrDados = dadosDoEnvio.Split('#');
                strPathArquivoXml = arrayStrDados[0];
                numeroLote        = int.Parse(arrayStrDados[1]);
                switch (arrayStrDados[2])
                {
                case "D": tipoXml = TipoXmlNFe.Destinatario;
                    break;

                case "N":
                    tipoXml = TipoXmlNFe.NFe;
                    break;

                case "L":
                    tipoXml = TipoXmlNFe.Lote;
                    break;

                default:
                    throw new ArgumentException();
                }
            }
            catch
            {
                Console.WriteLine("Um ou mais parâmetros foram informados de forma incorreta.");
                return;
            }

            try
            {
                File.ReadAllText(strPathArquivoXml);
            }
            catch (UnauthorizedAccessException ex)
            {
                var strMensagem = string.Format("Não foi possível acessar o arquivo \"{0}\"", strPathArquivoXml);
                Console.WriteLine(strMensagem);
                Console.WriteLine("Detalhes:");
                Console.WriteLine(ex.Message);
                return;
            }

            try
            {
                var booStatusOk = ConsultarStatusServico(nfeFacade);

                if (!booStatusOk)
                {
                    return;
                }

                var nfeBuilder = new NFeBuilder(strPathArquivoXml, tipoXml);
                Console.WriteLine("Preparando a NFe...");
                var retorno = nfeFacade.EnviarNFe(numeroLote, nfeBuilder.Build());
                if (retorno.Retorno.cStat == 103)
                {
                    Console.WriteLine("#NFe#" + retorno.Retorno.infRec.nRec);
                }
                else
                {
                    Console.WriteLine("#Erro#" + retorno.Retorno.xMotivo);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Houve uma falha não esperada durante o processo de envio.");
                Console.WriteLine("Detalhes:");
                Console.WriteLine(ex.Message);
            }
        }