private void TmStatus_Tick(object sender, EventArgs e) { EnviarStatus status = new EnviarStatus(); TmStatus.Enabled = false; try { var dtRet = FuncoesGerais.LojasEmitentes(); for (int i = 0; i < dtRet.Rows.Count; i++) { eRet = new Entidade_Status(); eRet.Loja = int.Parse(dtRet.Rows[i]["idloja"].ToString()); eRet.tpAmbiente = FuncoesGerais.TipoAmbiente(); eRet.cUf = FuncoesGerais.UfIbgeEmpresa(int.Parse(dtRet.Rows[i]["idloja"].ToString())); eRet.Uf = dtRet.Rows[i]["uf"].ToString(); eRet.versao = dtRet.Rows[i]["VersaoNfe"].ToString(); status.Enviar(eRet, out eRet); if (eRet != null) { txtStatus.Text = eRet.cStatus; txtAmb.Text = eRet.AmbienteFormatado; txtMsg.Text = eRet.sMotivo; TxtVersao.Text = eRet.versao; txtUltConsulta.Text = eRet.dhRet; } } TmStatus.Enabled = true; } catch (XmlException exm) { TmStatus.Enabled = true; throw new Exception(exm.Message.ToString()); } catch (Exception ex) { TmStatus.Enabled = true; throw new Exception(ex.Message.ToString()); } }
public void Enviar(Entidade_NotaFiscal ObjEnt, out Entidade_NotaFiscal objDados) { docTran = new XmlDocument(); ns = new XmlSerializerNamespaces(); xmlStatus = new XmlSerializer(typeof(nfe.TNFe)); Settings = new XmlWriterSettings(); DataTable DtLoteNfe = new DataTable(); mLog = new Model_LogNfe(); ObjLote = new Model_Lote(); ObjModNfe = new Model_NotaFiscal(); ObjNotaFiscal = ObjEnt; NFuncoes = new NegocioFuncoesGerais(); ObjNotaFiscal = new Entidade_NotaFiscal(); var Lojas = FuncoesGerais.LojasEmitentes(); for (int i = 0; i < Lojas.Rows.Count; i++) { int Loja = 0; int Nota = 0; int Fornecedor = 0; string Serie = ""; int TpEmis = 0; var Lote = GerarLoteNfe(Convert.ToInt32(Lojas.Rows[i]["id_loja"].ToString())); docEnviNfe = new XmlDocument(); if (Lote != 0) { docEnviNfe.PreserveWhitespace = false; docEnviNfe.LoadXml(CabecalhoEnvieNfe(Lote)); nodeListNfe = docEnviNfe.GetElementsByTagName("enviNFe"); DtLoteNfe = PesquisaNotasFiscaisLoteNfe(Convert.ToInt32(Lojas.Rows[i]["id_loja"].ToString()), Lote); foreach (DataRow RowNFDt in DtLoteNfe.Rows) { try { docTran = new XmlDocument(); var RetEnt = CarregarDados(Convert.ToInt32(RowNFDt["id_loja"]), Convert.ToInt32(RowNFDt["NrNf"]), RowNFDt["serienf"].ToString(), Convert.ToInt32(RowNFDt["CdFornec"]), RowNFDt["TpNFe"].ToString()); if (RetEnt != null) { ObjNotaFiscal.cUf = RetEnt.cUf; // Uf do Emitente ObjNotaFiscal.Loja = RetEnt.Loja; Loja = RetEnt.Loja; Nota = RetEnt.NotaFiscal; Fornecedor = RetEnt.CdFornecedor; Serie = RetEnt.sSerieNf; TpEmis = RetEnt.TpEmis; ObjNotaFiscal.Lote = Lote; Nfe = new nfe.TNFe(RetEnt); } // E DEFINIDO O TIPO DE LEITURA DO XML Settings.Encoding = UTF8Encoding.UTF8; Settings.NewLineHandling = NewLineHandling.None; Settings.Indent = true; Settings.IndentChars = ""; ns.Add(string.Empty, "http://www.portalfiscal.inf.br/nfe"); Sw = new UTF8StringWriter(); Wx = XmlWriter.Create(Sw, Settings); xmlStatus.Serialize(Sw, Nfe, ns); string xmlGer = Sw.ToString(); docTran.LoadXml(xmlGer); docTran.PreserveWhitespace = false; CertEmpresa = AssinaturaDigital.FindCertOnStore(Convert.ToInt32(RowNFDt["id_loja"])); docTran = AssinaturaDigital.SignXml(docTran, CertEmpresa, "infNFe"); if (NFuncoes.ValidarEstruturaXml(docTran.OuterXml, "nfe_v3.10")) { Mensagem.MensagemErro(Mensagem.TipoMensagem.XmlLoteGerados, "Entrada", Lote.ToString() + "|" + "Nota :" + RetEnt.sSerieNf + "-" + RetEnt.NotaFiscal.ToString() + "|" + docTran.OuterXml); nodeListCarregarNfe = docTran.GetElementsByTagName("NFe", "http://www.portalfiscal.inf.br/nfe"); nodeListNfe.Item(0).AppendChild(docEnviNfe.ImportNode(nodeListCarregarNfe.Item(0), true)); ObjLote.UpdateXmlTpSaida(RetEnt.Loja, RetEnt.NotaFiscal, RetEnt.sSerieNf, Lote, RetEnt.TpEmis.ToString(), docTran.OuterXml, RetEnt.CdFornecedor); ObjModNfe.UpdateNfTpEmisVersaoNFe(RetEnt.TpEmis.ToString(), null, "3.10", Lote.ToString(), RetEnt.NotaFiscal.ToString(), RetEnt.Loja.ToString(), RetEnt.sSerieNf, RetEnt.CdFornecedor, Model_NotaFiscal.NotaFiscal.Entrada); } } catch (Exception Ex) { Mensagem.MensagemErro(Mensagem.TipoMensagem.Nfe, "Entrada", Ex.Message.ToString()); ObjLote.UpdateXmlTpSaida(Loja, Nota, Serie, Lote, TpEmis.ToString(), docTran.OuterXml, Fornecedor); ObjModNfe.UpdateNfErro(225, Loja, Serie, Nota, Fornecedor, Model_NotaFiscal.NotaFiscal.Entrada); ObjLote.UpdateLoteErro(225, Loja, Serie, Nota, Fornecedor); mLog.InsertErroLog("Erro Lote:" + Lote + ", Nota Fiscal Entrada:" + Serie + "-" + Nota + ". " + NFuncoes.TiraCampos(Ex.Message.ToString())); } } if (CertEmpresa != null) { ObjNotaFiscal.TpAmbiente = FuncoesGerais.TipoAmbiente(); EnviarXml(docEnviNfe, CertEmpresa, ref ObjNotaFiscal); } objDados = null; } else { objDados = null; } } objDados = null; }