Exemplo n.º 1
0
        public void DeserilizarEvento(object obj, ref Entidade_NotaFiscal objDes)
        {
            //mStatus = new Model_StatusNfe();
            //Entidade_Status eStatusRet = new Entidade_Status();
            mLog    = new Model_LogNfe();
            ObjLote = new Model_Lote();

            object objRet = new object();

            try
            {
                retXmlNodeReader = new XmlNodeReader((XmlNode)obj);
                xmlDesSerializar = new XmlSerializer(typeof(TRetEnviNFe));

                objRet = xmlDesSerializar.Deserialize(retXmlNodeReader);

                var Ret = (TRetEnviNFe)objRet;

                var InfRet = (TRetEnviNFeInfRec)Ret.Item;

                if (InfRet != null)
                {
                    ObjLote.UpdateLoteRecebidos("R", Convert.ToDateTime(Ret.dhRecbto), InfRet.tMed, InfRet.nRec, Ret.cStat, objDes.Lote.ToString(), objDes.Loja);
                }
                else
                {
                    ObjLote.UpdateLoteRecebidos("R", Convert.ToDateTime(Ret.dhRecbto), "0", "0", Ret.cStat, objDes.Lote.ToString(), objDes.Loja);
                }
            }
            catch (Exception Ex)
            {
                Mensagem.MensagemErro(Mensagem.TipoMensagem.Nfe, "Saida", Ex.Message.ToString());
                mLog.InsertErroLog(Ex.Message.ToString());
            }
        }
Exemplo n.º 2
0
        private void TmConsultaLote_Tick(object sender, EventArgs e)
        {
            TmConsultaLote.Enabled = false;

            ConRecibo  nRec  = new ConRecibo();
            Model_Lote mLote = new Model_Lote();

            eRec = new Entidade_Recibo();

            var retDt = mLote.LotesNaoProcessados();

            for (int i = 0; i < retDt.Rows.Count; i++)
            {
                eRec.TpNf   = retDt.Rows[i]["TpNFe"].ToString();
                eRec.Recibo = retDt.Rows[i]["NrRecibo"].ToString();
                eRec.Loja   = int.Parse(retDt.Rows[i]["id"].ToString());
                eRec.cUf    = int.Parse(FuncoesGerais.UfIbgeEmpresa(int.Parse(retDt.Rows[i]["id"].ToString())));
                eRec.TpAmb  = FuncoesGerais.TipoAmbiente();
                nRec.Enviar(eRec, out eRec);
            }

            TmConsultaLote.Enabled = true;
        }
Exemplo n.º 3
0
        //public static int RetornoCodigoCFOP(string CFOP)
        //{
        //    int i = 0;
        //    foreach (var item in Enum.GetValues(typeof(nfe.TCfop)))
        //    {
        //        if (item.ToString().Substring(item.ToString().Count() - 4, 4) == CFOP.ToString())
        //        {
        //            break;
        //        }
        //        i += 1;
        //    }
        //    return i;
        //}
        public string GerarChaveAcessoNfe(int Loja, int CodFornecedor, string Uf, DateTime DtEmissao, string CnpjEmissor, string ModNfe, string Serie, string NumeroNf, string TpEmis, int Lote, out string cNF, out string cDV)
        {
            mNF   = new Model_NotaFiscal();
            mLote = new Model_Lote();

            Random rd           = new Random();
            string CodigoRandom = string.Format("{0:00000000}", rd.Next(99999999));

            List <int> arrayMult = new List <int>()
            {
                4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2
            };
            string ChaveNfe = Uf + string.Format("{0:yyMM}", DtEmissao) + TiraCampos(CnpjEmissor) + ModNfe + Serie.PadLeft(3, '0') + NumeroNf.PadRight(9, '0') + TpEmis + CodigoRandom;

            int[] arryaCh = ChaveNfe.Select(x => Int32.Parse(x.ToString())).ToArray();
            int   soma    = 0;
            int   resto   = 0;
            int   DV      = 0;

            for (int i = 0; i < arryaCh.Length; i++)
            {
                soma += arrayMult[i] * Convert.ToInt32(arryaCh[i]);
            }

            resto = soma % 11;

            cNF = CodigoRandom;

            if (resto == 0 || resto == 1)
            {
                cDV = "0";

                if (CodFornecedor == 0)
                {
                    mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, 0, ChaveNfe + cDV.ToString(), CodigoRandom);
                    mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie, Lote, ChaveNfe + cDV.ToString());
                }
                else
                {
                    mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, CodFornecedor, ChaveNfe + cDV.ToString(), CodigoRandom);
                    mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie, Lote, ChaveNfe + cDV.ToString());
                }

                return(ChaveNfe += "0");
            }
            else
            {
                DV = 11 - resto;

                cDV = DV.ToString();

                if (CodFornecedor == 0)
                {
                    mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, 0, ChaveNfe + DV.ToString(), CodigoRandom);
                    mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Lote, ChaveNfe + DV.ToString());
                }
                else
                {
                    mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, CodFornecedor, ChaveNfe + DV.ToString(), CodigoRandom);
                    mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Lote, ChaveNfe + DV.ToString());
                }
                return(ChaveNfe += DV.ToString());
            }
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
0
        public void DeserilizarEvento(object obj, ref Entidade_Cancelamento objDes)
        {
            Entidade_Cancelamento EntCa  = new Entidade_Cancelamento();
            Model_NotaFiscal      mNf    = new Model_NotaFiscal();
            Model_Lote            mLote  = new Model_Lote();
            XmlDocument           docRet = new XmlDocument();

            RecebEnvento = new TRetEnvEvento();
            StreamWriter SW;
            string       xProtNFe = string.Empty;

            EntCa = objDes;


            object retCancelamentoSefaz = new object();

            mCancelamento = new Model_Cancelamento();

            TRetEnvEvento EventoRetCancalentoSefaz = new TRetEnvEvento();

            try
            {
                XmlNodeReader retXmlNodeReader = new XmlNodeReader((XmlNode)obj);
                XmlSerializer xmlDesSerializar = new XmlSerializer(typeof(retEnvEventoCancNFe.TRetEnvEvento));

                retCancelamentoSefaz = xmlDesSerializar.Deserialize(retXmlNodeReader);

                EventoRetCancalentoSefaz = (TRetEnvEvento)retCancelamentoSefaz;

                if (EventoRetCancalentoSefaz.cStat != "410")
                {
                    foreach (var item in EventoRetCancalentoSefaz.retEvento)
                    {
                        if (item.infEvento.cStat.Trim() == "135" || item.infEvento.cStat.Trim() == "136" || item.infEvento.cStat.Trim() == "155")
                        {
                            mCancelamento.UpdateCancelamentoNfe(101, Convert.ToDateTime(item.infEvento.dhRegEvento), EntCa.Loja, EntCa.NmSerie, EntCa.NrNf.ToString(), 0);

                            if (EntCa.TpNf == "S")
                            {
                                mCancelamento.UpdateNfSaidaCancelamento(101, item.infEvento.nProt, EntCa.Loja, EntCa.NmSerie.Trim(), EntCa.NrNf.ToString());
                            }
                            else
                            {
                                mCancelamento.UpdateNfEntradaCancelamento(101, item.infEvento.nProt, EntCa.Loja, EntCa.NmSerie.Trim(), EntCa.NrNf.ToString(), EntCa.CdFornec);
                            }

                            //mCancelamento.TramitacaoNfe(EntCa.Loja, EntCa.NmSerie.Trim(), EntCa.NrNf.ToString(), EntCa.CdFornec, 0, "Cancelamento autorizado");
                        }
                        else
                        {
                            mCancelamento.UpdateCancelamentoNfe(int.Parse(item.infEvento.cStat), Convert.ToDateTime(item.infEvento.dhRegEvento), EntCa.Loja, EntCa.NmSerie.Trim() == "9" ? "D1" : EntCa.NmSerie.Trim(), EntCa.NrNf.ToString(), EntCa.CdFornec);
                            mCancelamento.TramitacaoNfe(EntCa.Loja, EntCa.NmSerie.Trim() == "9" ? "D1" : EntCa.NmSerie.Trim(), EntCa.NrNf.ToString(), EntCa.CdFornec, 0, "Cancelamento não autorizado! Erro:" + int.Parse(item.infEvento.cStat));
                        }
                    }
                }
                else
                {
                    vWriter = new StreamWriter(@"c:\MensagensNFe\ServicoNotaFiscal.txt", true);
                    vWriter.WriteLine("=====================CANCELAMENTO - " + string.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.Now) + "================================");
                    vWriter.WriteLine("OCORREU O SEGUINTE EERRO: " + EventoRetCancalentoSefaz.xMotivo);
                    vWriter.WriteLine("=====================================================");
                    vWriter.Flush();
                    vWriter.Close();
                }
            }
            catch (Exception ex)
            {
                vWriter = new StreamWriter(@"c:\MensagensNFe\ServicoNotaFiscal.txt", true);
                vWriter.WriteLine("=====================CANCELAMENTO - " + string.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.Now) + "================================");
                vWriter.WriteLine("OCORREU O SEGUINTE EERRO: " + ex.Message.ToString());
                vWriter.WriteLine("=====================================================");
                vWriter.Flush();
                vWriter.Close();
            }
        }
Exemplo n.º 6
0
        public void DeserilizarEvento(object obj, ref Entidade_Recibo objDes)
        {
            Entidade_Recibo  retRecibo = new Entidade_Recibo();
            Model_NotaFiscal mNf       = new Model_NotaFiscal();
            Model_Lote       mLote     = new Model_Lote();
            XmlDocument      docRet    = new XmlDocument();

            RetReciNFe = new TRetConsReciNFe();
            StreamWriter SW;
            string       xProtNFe = string.Empty;

            object objRet = new object();

            retXmlNodeReader = new XmlNodeReader((XmlNode)obj);
            xmlDesSerializar = new XmlSerializer(typeof(TRetConsReciNFe));

            objRet = xmlDesSerializar.Deserialize(retXmlNodeReader);

            var Retorno = (TRetConsReciNFe)objRet;


            XmlNodeList xNodList = ((XmlNode)obj).ChildNodes;

            try
            {
                if (Retorno.protNFe != null)
                {
                    foreach (var Prot in Retorno.protNFe)
                    {
                        if (objDes.TpNf.Trim() == "S")
                        {
                            mNf.AtualizaRetornoNfeSaida(Prot.infProt.cStat, Prot.infProt.nProt, objDes.Loja, Prot.infProt.chNFe, Model_NotaFiscal.NotaFiscal.Saida);
                        }
                        else
                        {
                            mNf.AtualizaRetornoNfeSaida(Prot.infProt.cStat, Prot.infProt.nProt, objDes.Loja, Prot.infProt.chNFe, Model_NotaFiscal.NotaFiscal.Entrada);
                        }

                        for (int i = 0; i < xNodList.Count; i++)
                        {
                            if (xNodList.Item(i).Name.Equals("protNFe"))
                            {
                                XmlNode     xNodListProc  = xNodList.Item(i).FirstChild;
                                XmlNodeList xNodListProc2 = xNodListProc.ChildNodes;

                                for (int j = 0; j < xNodListProc2.Count; j++)
                                {
                                    if (xNodListProc2.Item(j).Name.Equals("chNFe"))
                                    {
                                        xProtNFe = xNodList.Item(i).OuterXml;
                                        break;
                                    }
                                }
                            }
                        }
                        mLote.ItemLoteNfe(Prot.infProt.cStat, Prot.infProt.chNFe, xProtNFe);
                        mLote.LoteProcessado(Retorno.cStat, Convert.ToDateTime(Prot.infProt.dhRecbto), Retorno.nRec);
                    }
                }
                else
                {
                    mLote.LoteProcessado(Retorno.cStat, Convert.ToDateTime(Retorno.dhRecbto), Retorno.nRec);
                }

                if (!Directory.Exists(@"C:\NFe\Retorno_Proc_NFe\"))
                {
                    Directory.CreateDirectory(@"C:\NFe\Retorno_Proc_NFe\");
                }
                FileStream FS = new FileStream(@"C:\NFe\Retorno_Proc_NFe\" + string.Format("{0:MMddyyyy}", DateTime.Now.Date) + "_" + Retorno.nRec.Trim() + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                SW = new StreamWriter(FS);

                xmlDesSerializar.Serialize(SW, Retorno);
            }
            catch (Exception Ex)
            {
                Mensagem.MensagemErro(Mensagem.TipoMensagem.RetAutoriz, "Saida", Ex.Message.ToString());

                if (!Directory.Exists(@"C:\NFe\Retorno_Proc_NFe\"))
                {
                    Directory.CreateDirectory(@"C:\NFe\Retorno_Proc_NFe\");
                }
                FileStream FS = new FileStream(@"C:\NFe\Retorno_Proc_NFe\" + string.Format("{0:MMddyyyy}", DateTime.Now.Date) + "_" + Retorno.nRec.Trim() + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                SW = new StreamWriter(FS);

                xmlDesSerializar.Serialize(SW, Retorno);
            }
        }