예제 #1
0
        /// <summary>
        /// Lê um arquivo do tipo .err, preenche os dados do NFeResult e retorna
        /// </summary>
        /// <param name="result">Variável do tipo NFeResult para receber os erros</param>
        /// <param name="fullPathErrFile">Caminho completo do arquivo .err</param>
        /// <exception cref="FileNotFoundException">Se o arquivo não existir.</exception>
        /// <returns></returns>
        public static ResultBase SetErrorResult(ResultBase result, string fullPathErrFile)
        {
            FileInfo fi = new FileInfo(fullPathErrFile);

            if(!fi.Exists)
                throw new FileNotFoundException();

            if(result == null) result = new NFeResult();

            if(WaitFile(new[] { fullPathErrFile }))
            {
                using (StreamReader stream = new StreamReader(fi.FullName, System.Text.Encoding.GetEncoding(1252)))
                {
                    string line = stream.ReadToEnd();

                    result.Message = line;
                    result.Exception = new Exception(line);
                }
            }

            return result;
        }
예제 #2
0
파일: NFBase.cs 프로젝트: njmube/openposbr
        /// <summary>
        /// Aguarda e processa o retorno da NF-e por parte da sefaz.
        /// </summary>
        public NFeResult ProcessaResposta()
        {
            NFeResult retornoSefaz = new NFeResult();

            IList<string> wantedFiles = new List<String>();
            wantedFiles.Add(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-num-lot.xml"));
            wantedFiles.Add(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-nfe.err"));
            wantedFiles.Add(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, "canc110111" + Chave + (Eventos.Count + 1).ToString("D2") + "-eve.xml"));

            // Verifica se os arquivos de resposta já estão na
            // pasta de retorno do UniNFe, seja um número de lote
            // ou então um arquivo de erro.
            if(OpenPOS.NFe.Utilities.WaitFile(wantedFiles))
            {
                //pega o numero do lote
                if(File.Exists(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-num-lot.xml")))
                {
                    retornoSefaz = NFeResult.ReadNumlote(retornoSefaz, Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-num-lot.xml"));

                    LogNFe.Save(Enuns.TipoEvento.NFeNumeroLoteProcessado,
                                "Número do lote processado com sucesso",
                                this.Chave,
                                this.StatusNF);

                    //System.Diagnostics.Debug.Assert(false, "DEBUG: AGUARDANDO O NÚMERO DO LOTE");

                    wantedFiles.Clear();

                    // Deleta os arquivos

                    File.Delete(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-num-lot.xml"));

                    string numeroLote = retornoSefaz.NumLote;
                    wantedFiles.Add(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.err"));
                    wantedFiles.Add(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.xml"));

                    if(OpenPOS.NFe.Utilities.WaitFile(wantedFiles))
                    {
                        if(File.Exists(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.xml")))
                        {
                            retornoSefaz = NFeResult.CreateFromXML(retornoSefaz, Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.xml"));

                            LogNFe.Save(Enuns.TipoEvento.NFeNumeroReciboProcessado,
                                        "Número do recibo processado com sucesso",
                                        this.Chave,
                                        retornoSefaz.Status);

                            File.Delete(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.xml"));
                            wantedFiles.Clear();
                            wantedFiles.Add(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, retornoSefaz.NumRecbto + "-pro-rec.xml"));

                            if(OpenPOS.NFe.Utilities.WaitFile(wantedFiles))
                            {
                                retornoSefaz = NFeResult.CreateFromXML(retornoSefaz, Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, retornoSefaz.NumRecbto + "-pro-rec.xml"));

                                LogNFe.Save(Enuns.TipoEvento.NFeNumeroProtocoloProcessado,
                                            retornoSefaz.Motivo,
                                            this.Chave,
                                            retornoSefaz.Status);

                                this.Status = Enuns.Faturamento.Lancamento.Status.ObtidoNumeroLoteEnviado;
                                System.Diagnostics.Debug.Assert(false, "DEBUG: AGUARDANDO O NÚMERO DO PROTOCOLO");

                                File.Delete(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, retornoSefaz.NumRecbto + "-pro-rec.xml"));
                            }
                        }
                        else if(File.Exists(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.err")))
                        {
                            ResultBase resultBase = OpenPOS.NFe.Utilities.SetErrorResult(retornoSefaz, Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, numeroLote + "-rec.err"));

                            retornoSefaz.Message = resultBase.Message;
                            resultBase.Exception = resultBase.Exception;

                            LogNFe.Save(Enuns.TipoEvento.NFeErroXML,
                                        "Ocorreu algum erro no XML da nota fiscal eletrônica",
                                        this.Chave,
                                        this.StatusNF,
                                        retornoSefaz.Motivo);
                        }
                    }
                }
                else if(File.Exists(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, "canc110111" + Chave + (Eventos.Count + 1).ToString("D2") + "-eve.xml")))
                {
                    retornoSefaz = NFeResult
                        .CreateFromXML(retornoSefaz,
                        Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, "canc110111" + Chave + (Eventos.Count + 1).ToString("D2") + "-eve.xml"));

                    File.Delete(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, "canc110111" + Chave + (Eventos.Count + 1).ToString("D2") + "-eve.xml"));

                }
                // Se for um arquivo de erro retorna o erro da seFaz
                else if(File.Exists(Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-nfe.err")))
                {
                    ResultBase resultBase = OpenPOS.NFe.Utilities.SetErrorResult(retornoSefaz, Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-nfe.err"));

                    retornoSefaz.Message = resultBase.Message;
                    retornoSefaz.Exception = resultBase.Exception;

                    LogNFe.Save(Enuns.TipoEvento.NFeErroXML,
                                "Ocorreu algum erro no XML da nota fiscal eletrônica",
                                this.Chave,
                                retornoSefaz.Status == null ? NFe.Status.ErroNaoCatalogado : retornoSefaz.Status,
                                retornoSefaz.Message);
                }
                else
                    throw new Exceptions.NFe.WaitFileExpired();

                return retornoSefaz;
            }
            else
            {
                // Verifica se o arquivo da nota fiscal está em
                // processamento pelo UniNFe.
                if(File.Exists(OpenPOS.Settings.NFe.PastaRetorno + "Processamento/" + Chave + "-nfe.xml"))
                {
                    // Consulta situação da nota
                    retornoSefaz = this.ConsultaSituacaoNFe();

                    // Verifica se o número de protocolo existe na resposta
                    if(!String.IsNullOrEmpty(retornoSefaz.Protocolo))
                    {
                        // Retorna o NFeResult contendo o valor da tag nProt.
                        // Nesse caso pela facilidade, a parte pertinente ao terceiro
                        // bloco do diagrama já foi implementada, pois essa NFeResult
                        // já é carregada de um arquivo XML.
                        return retornoSefaz;
                    }
                    else
                    {
                        retornoSefaz.Message = "Sefaz não responde.";
                        return retornoSefaz;
                    }
                }
                else
                {
                    retornoSefaz.Message = "Sefaz não responde.";
                    return retornoSefaz;
                }
            }
        }
예제 #3
0
파일: NFBase.cs 프로젝트: njmube/openposbr
        /// <summary>
        /// Consulta situação da NF-e na Sefaz através do UniNFe.
        /// </summary>
        public NFeResult ConsultaSituacaoNFe()
        {
            NFeResult nfeResult = new NFeResult();

            this.GenerateXMLConsulta();

            IList<String> arquivos = new List<String>();
            arquivos.Add(System.IO.Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-sit.xml"));
            arquivos.Add(System.IO.Path.Combine(OpenPOS.Settings.NFe.PastaRetorno, Chave + "-sit.err"));

            if(OpenPOS.NFe.Utilities.WaitFile(arquivos) && File.Exists(arquivos.First()))
                return NFeResult.CreateFromXML(nfeResult, arquivos.First());

            nfeResult.Exception = new Exception("Sefaz não responde");
            nfeResult.Message = nfeResult.Exception.Message;
            return nfeResult;
        }