Exemplo n.º 1
0
        public void Enviar(Entidade_Status ObjEnt, out Entidade_Status objDados)
        {
            Entidade_Status eRetStatus = new Entidade_Status();

            eStatus  = new Entidade_Status();
            mLog     = new Model_LogNfe();
            NFuncoes = new NegocioFuncoesGerais();

            eStatus = ObjEnt;

            docTran   = new XmlDocument();
            ns        = new XmlSerializerNamespaces();
            Settings  = new XmlWriterSettings();
            xmlStatus = new XmlSerializer(typeof(ConsStatServ.TConsStatServ));

            try
            {
                //Passando os dados para a Class que vai ser serelizada
                cStatus = new ConsStatServ.TConsStatServ(eStatus);

                // 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, cStatus, ns);
                string xmlGer = Sw.ToString();

                docTran.LoadXml(xmlGer);
                docTran.PreserveWhitespace = false;

                if (nFG.ValidarEstruturaXml(docTran.OuterXml, "consStatServ_v3.10"))
                {
                    CertEmpresa = AssinaturaDigital.FindCertOnStore(eStatus.Loja);
                    EnviarXml(docTran, CertEmpresa, ref eRetStatus);
                    objDados = eRetStatus;
                }
                else
                {
                    objDados = null;
                }
            }
            catch (Exception Ex)
            {
                Mensagem.MensagemErro(Mensagem.TipoMensagem.Nfe, "Saida", Ex.Message.ToString());
                mLog.InsertErroLog(NFuncoes.TiraCampos(Ex.Message.ToString()));
                objDados = null;
            }
        }
Exemplo n.º 2
0
        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;
        }