Classe para auxiliar retornos dos recibos a serem consultados no SEFAZ
Пример #1
0
        /// <summary>
        /// Criar uma lista com os recibos a serem consultados no servidor do SEFAZ
        /// </summary>
        /// <returns>Lista dos recibos</returns>
        /// <by>Wandrey Mundin Ferreira</by>
        public List <ReciboCons> CriarListaRec()
        {
            CriarXml(true);

            List <ReciboCons> lstRecibo = new List <ReciboCons>();
            List <string>     lstNumRec = new List <string>();

            DateTime startTime;
            DateTime stopTime;
            TimeSpan elapsedTime;

            long elapsedMillieconds;

            startTime = DateTime.Now;

            while (true)
            {
                stopTime           = DateTime.Now;
                elapsedTime        = stopTime.Subtract(startTime);
                elapsedMillieconds = (int)elapsedTime.TotalMilliseconds;
                FileStream fsArquivo = null;

                try
                {
                    lock (Smf.Fluxo)
                    {
                        XmlDocument doc = new XmlDocument(); //Criar instância do XmlDocument Class
                        fsArquivo = OpenFileFluxo(false);
                        doc.Load(fsArquivo);                 //Carregar o arquivo aberto no XmlDocument
                        fsArquivo.Close();

                        XmlNodeList documentoList = doc.GetElementsByTagName(ElementoFixo.Documento.ToString()); //Pesquisar o elemento Documento no arquivo XML
                        foreach (XmlNode documentoNode in documentoList)
                        {
                            XmlElement documentoElemento = (XmlElement)documentoNode;

                            string nRec        = Functions.LerTag(documentoElemento, ElementoEditavel.nRec.ToString(), "");
                            string versao      = Functions.LerTag(documentoElemento, ElementoEditavel.versao.ToString(), "");
                            string ChaveNFe    = documentoElemento.GetAttribute(ElementoFixo.ChaveNFe.ToString());
                            string NomeArquivo = Functions.LerTag(documentoElemento, ElementoFixo.ArqNFe.ToString(), "");
                            string NomeArquivoEmProcessamento = Empresas.Configuracoes[empresa].PastaXmlEnviado + "\\" + PastaEnviados.EmProcessamento.ToString() + "\\" + NomeArquivo;
                            string NomeArquivoAssinado        = Empresas.Configuracoes[empresa].PastaXmlEnvio + "\\temp\\" + NomeArquivo;
                            string NomeArquivoAssinadoLote    = Empresas.Configuracoes[empresa].PastaXmlEmLote + "\\temp\\" + NomeArquivo;
                            string mod        = Functions.LerTag(documentoElemento, ElementoEditavel.mod.ToString(), "");
                            bool   excluiNota = false;
                            if (File.Exists(NomeArquivoEmProcessamento))
                            {
                                int      tMed    = 3; //3 segundos
                                DateTime dPedRec = DateTime.Now.AddMinutes(-60);

                                tMed    = Convert.ToInt32(Functions.LerTag(documentoElemento, ElementoEditavel.tMed.ToString(), tMed.ToString()));
                                dPedRec = Convert.ToDateTime(Functions.LerTag(documentoElemento, ElementoEditavel.dPedRec.ToString(), dPedRec.ToString("yyyy-MM-dd HH:mm:ss")));

                                //Se tiver mais de 2 dias no fluxo, vou excluir a nota dele.
                                //Não faz sentido uma nota ficar no fluxo todo este tempo, então vou fazer uma limpeza
                                //Wandrey 11/09/2009
                                if (DateTime.Now.Subtract(dPedRec).Days >= 2)
                                {
                                    excluiNota = true;
                                }
                                else
                                {
                                    if (nRec != string.Empty && !lstNumRec.Contains(nRec))
                                    {
                                        lstNumRec.Add(nRec);

                                        ReciboCons oReciboCons = new ReciboCons();
                                        oReciboCons.dPedRec = dPedRec;
                                        oReciboCons.nRec    = nRec;
                                        oReciboCons.tMed    = tMed;
                                        oReciboCons.versao  = versao;
                                        oReciboCons.mod     = mod;
                                        lstRecibo.Add(oReciboCons);
                                    }
                                }
                            }
                            else if (!File.Exists(NomeArquivoAssinado) && !File.Exists(NomeArquivoAssinadoLote))
                            {
                                excluiNota = true;
                            }

                            if (excluiNota)
                            {
                                //Deletar o arquivo da pasta em processamento
                                Auxiliar oAux = new Auxiliar();
                                oAux.MoveArqErro(NomeArquivoEmProcessamento);

                                //Deletar a NFE do arquivo de controle de fluxo
                                ExcluirNfeFluxo(ChaveNFe);
                            }
                        }
                        break;
                    }
                }
                catch
                {
                    if (fsArquivo != null)
                    {
                        fsArquivo.Close();
                    }

                    if (elapsedMillieconds >= 120000) //120.000 ms que corresponde á 120 segundos que corresponde a 2 minuto
                    {
                        throw;
                    }
                }

                Thread.Sleep(100);
            }

            return(lstRecibo);
        }
Пример #2
0
        /// <summary>
        /// Criar uma lista com os recibos a serem consultados no servidor do SEFAZ
        /// </summary>
        /// <returns>Lista dos recibos</returns>
        /// <by>Wandrey Mundin Ferreira</by>
        public List<ReciboCons> CriarListaRec()
        {
            CriarXml(true);

            List<ReciboCons> lstRecibo = new List<ReciboCons>();
            List<string> lstNumRec = new List<string>();

            DateTime startTime;
            DateTime stopTime;
            TimeSpan elapsedTime;

            long elapsedMillieconds;
            startTime = DateTime.Now;

            while (true)
            {
                stopTime = DateTime.Now;
                elapsedTime = stopTime.Subtract(startTime);
                elapsedMillieconds = (int)elapsedTime.TotalMilliseconds;
                FileStream fsArquivo = null;

                try
                {
                    lock (Smf.Fluxo)
                    {
                        XmlDocument doc = new XmlDocument(); //Criar instância do XmlDocument Class
                        fsArquivo = OpenFileFluxo(false);
                        doc.Load(fsArquivo); //Carregar o arquivo aberto no XmlDocument
                        fsArquivo.Close();

                        XmlNodeList documentoList = doc.GetElementsByTagName(ElementoFixo.Documento.ToString()); //Pesquisar o elemento Documento no arquivo XML
                        foreach (XmlNode documentoNode in documentoList)
                        {
                            XmlElement documentoElemento = (XmlElement)documentoNode;

                            string nRec = Functions.LerTag(documentoElemento, ElementoEditavel.nRec.ToString(), "");
                            string versao = Functions.LerTag(documentoElemento, ElementoEditavel.versao.ToString(), "");
                            string ChaveNFe = documentoElemento.GetAttribute(ElementoFixo.ChaveNFe.ToString());
                            string NomeArquivo = Functions.LerTag(documentoElemento, ElementoFixo.ArqNFe.ToString(), "");
                            string NomeArquivoEmProcessamento = Empresas.Configuracoes[empresa].PastaXmlEnviado + "\\" + PastaEnviados.EmProcessamento.ToString() + "\\" + NomeArquivo;
                            string NomeArquivoAssinado = Empresas.Configuracoes[empresa].PastaXmlEnvio + "\\" + Propriedade.NomePastaXMLAssinado + "\\" + NomeArquivo;
                            string NomeArquivoAssinadoLote = Empresas.Configuracoes[empresa].PastaXmlEmLote + "\\" + Propriedade.NomePastaXMLAssinado + "\\" + NomeArquivo;
                            string mod = Functions.LerTag(documentoElemento, ElementoEditavel.mod.ToString(), "");
                            bool excluiNota = false;
                            if (File.Exists(NomeArquivoEmProcessamento))
                            {
                                int tMed = 3; //3 segundos
                                DateTime dPedRec = DateTime.Now.AddMinutes(-60);

                                tMed = Convert.ToInt32(Functions.LerTag(documentoElemento, ElementoEditavel.tMed.ToString(), tMed.ToString()));
                                dPedRec = Convert.ToDateTime(Functions.LerTag(documentoElemento, ElementoEditavel.dPedRec.ToString(), dPedRec.ToString("yyyy-MM-dd HH:mm:ss")));                                

                                //Se tiver mais de 2 dias no fluxo, vou excluir a nota dele.
                                //Não faz sentido uma nota ficar no fluxo todo este tempo, então vou fazer uma limpeza
                                //Wandrey 11/09/2009
                                if (DateTime.Now.Subtract(dPedRec).Days >= 2)
                                {
                                    excluiNota = true;
                                }
                                else
                                {
                                    if (nRec != string.Empty && !lstNumRec.Contains(nRec))
                                    {
                                        lstNumRec.Add(nRec);

                                        ReciboCons oReciboCons = new ReciboCons();
                                        oReciboCons.dPedRec = dPedRec;
                                        oReciboCons.nRec = nRec;
                                        oReciboCons.tMed = tMed;
                                        oReciboCons.versao = versao;
                                        oReciboCons.mod = mod;
                                        lstRecibo.Add(oReciboCons);
                                    }
                                }
                            }
                            else if (! File.Exists(NomeArquivoAssinado) && ! File.Exists(NomeArquivoAssinadoLote))
                                excluiNota = true;

                            if (excluiNota)
                            {
                                //Deletar o arquivo da pasta em processamento
                                Auxiliar oAux = new Auxiliar();
                                oAux.MoveArqErro(NomeArquivoEmProcessamento);

                                //Deletar a NFE do arquivo de controle de fluxo
                                ExcluirNfeFluxo(ChaveNFe);
                            }
                        }
                        break;
                    }
                }
                catch
                {
                    if (fsArquivo != null)
                    {
                        fsArquivo.Close();
                    }

                    if (elapsedMillieconds >= 120000) //120.000 ms que corresponde á 120 segundos que corresponde a 2 minuto
                    {
                        throw;
                    }
                }

                Thread.Sleep(1000);
            }

            return lstRecibo;
        }