Example #1
0
        public string Enviar()
        {
            tempo.Tick += new EventHandler(tempo_Tick);
            try
            {
                if (Acesso.TP_AMB_SERV == 1)
                {
                    lt.ClientCertificates.Add(Acesso.cert_NFs);
                }
                string x = "";
                for (int i = 5; ((i != 0) && (x == "")); i--)
                {
                    try
                    {
                        tempo.Start();
                        if (Acesso.TP_AMB_SERV == 1)
                        {
                            x = lt.enviar(dadosRPS.xmlLote.InnerXml.ToString());
                        }
                        else
                        {
                            x = lt.testeEnviar(dadosRPS.xmlLote.InnerXml.ToString());
                        }
                        tempo.Stop();
                    }
                    catch (Exception ex)
                    {
                        if (i == 0)
                        {
                            throw ex;
                        }
                    }
                }
                //Deserializa o retorno do webservice.
                XmlSerializer deserializer = new XmlSerializer(typeof(RetornoEnvioLoteRPS));
                RetornoEnvioLoteRPS ret;
                byte[] bytearray = Encoding.ASCII.GetBytes(x);
                MemoryStream valorStream = new MemoryStream(bytearray);
                ret = (RetornoEnvioLoteRPS)deserializer.Deserialize(valorStream);

                //Salva protocolo de retorno.
                StreamWriter sw = new StreamWriter(Pastas.PROTOCOLOS + "\\NFSE_LOTE_" + ret.cabec.NumeroLote + ".xml");
                sw.Write(x);
                sw.Close();

                string sMessageRetorno = "Método enviado Sincronamente";

                if (ret.cabec.Assincrono == "S")
                {
                    ReqConsultaLote consultaLote = new ReqConsultaLote();

                    consultaLote.cabec = new belConsultaLote();
                    consultaLote.cabec.CodCidade = ret.cabec.CodCidade;
                    consultaLote.cabec.CPFCNPJRemetente = ret.cabec.CPFCNPJRemetente;
                    consultaLote.cabec.NumeroLote = ret.cabec.NumeroLote;
                    consultaLote.cabec.Versao = ret.cabec.Versao.ToString();

                    //hora de salvar o numero do Lote nos RPS contidos no lote.
                    foreach (LoteRPS rps in this.dadosRPS.objLoteEnvio.lote.RPS)
                    {
                        base.SalvarRecibo(consultaLote.cabec.NumeroLote, rps.CD_NFSEQ);
                    }


                    string sPath = Pastas.PROTOCOLOS + "\\CONSULTA_LOTE_" + ret.cabec.NumeroLote + ".xml";

                    if (File.Exists(sPath))
                    {
                        File.Delete(sPath);
                    }

                    XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                    nameSpaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    nameSpaces.Add("tipos", "http://localhost:8080/WsNFe2/tp");
                    nameSpaces.Add("ns1", "http://localhost:8080/WsNFe2/lote");

                    SerializeClassToXml.SerializeClasse<ReqConsultaLote>(consultaLote, sPath, nameSpaces);
                    sMessageRetorno = BuscaRetorno(ret.cabec.NumeroLote);

                }
                else if (ret.erros.Erro.Count() > 0)
                {
                    sMessageRetorno = "Erros:" + Environment.NewLine;
                    foreach (ErrosErro err in ret.erros.Erro)
                    {
                        sMessageRetorno += string.Format("Codigo:{0} - Descrição:{1}{2}", err.Codigo, err.Descricao, Environment.NewLine);
                    }
                    foreach (LoteRPS rps in this.dadosRPS.objLoteEnvio.lote.RPS)
                    {
                        base.LimpaCampoRecibo(rps.CD_NFSEQ);
                    }
                }
                else if (ret.alertas.alert.Count() > 0)
                {
                    sMessageRetorno = "Alertas:" + Environment.NewLine;
                    foreach (Alerta_retlote alert in ret.alertas.alert)
                    {
                        sMessageRetorno += string.Format("Codigo:{0} - Descrição:{1}{2}", alert.Codigo, alert.Descricao, Environment.NewLine);
                    }
                }

                return sMessageRetorno;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
Example #2
0
        public string Enviar()
        {
            tempo.Tick += new EventHandler(tempo_Tick);
            try
            {
                if (Acesso.TP_AMB_SERV == 1)
                {
                    lt.ClientCertificates.Add(Acesso.cert_NFs);
                }
                string x = "";
                for (int i = 5; ((i != 0) && (x == "")); i--)
                {
                    try
                    {
                        tempo.Start();
                        if (Acesso.TP_AMB_SERV == 1)
                        {
                            x = lt.enviar(dadosRPS.xmlLote.InnerXml.ToString());
                        }
                        else
                        {
                            x = lt.testeEnviar(dadosRPS.xmlLote.InnerXml.ToString());
                        }
                        tempo.Stop();
                    }
                    catch (Exception ex)
                    {
                        if (i == 0)
                        {
                            throw ex;
                        }
                    }
                }
                //Deserializa o retorno do webservice.
                XmlSerializer       deserializer = new XmlSerializer(typeof(RetornoEnvioLoteRPS));
                RetornoEnvioLoteRPS ret;
                byte[]       bytearray   = Encoding.ASCII.GetBytes(x);
                MemoryStream valorStream = new MemoryStream(bytearray);
                ret = (RetornoEnvioLoteRPS)deserializer.Deserialize(valorStream);

                //Salva protocolo de retorno.
                StreamWriter sw = new StreamWriter(Pastas.PROTOCOLOS + "\\NFSE_LOTE_" + ret.cabec.NumeroLote + ".xml");
                sw.Write(x);
                sw.Close();

                string sMessageRetorno = "Método enviado Sincronamente";

                if (ret.cabec.Assincrono == "S")
                {
                    ReqConsultaLote consultaLote = new ReqConsultaLote();

                    consultaLote.cabec                  = new belConsultaLote();
                    consultaLote.cabec.CodCidade        = ret.cabec.CodCidade;
                    consultaLote.cabec.CPFCNPJRemetente = ret.cabec.CPFCNPJRemetente;
                    consultaLote.cabec.NumeroLote       = ret.cabec.NumeroLote;
                    consultaLote.cabec.Versao           = ret.cabec.Versao.ToString();

                    //hora de salvar o numero do Lote nos RPS contidos no lote.
                    foreach (LoteRPS rps in this.dadosRPS.objLoteEnvio.lote.RPS)
                    {
                        base.SalvarRecibo(consultaLote.cabec.NumeroLote, rps.CD_NFSEQ);
                    }


                    string sPath = Pastas.PROTOCOLOS + "\\CONSULTA_LOTE_" + ret.cabec.NumeroLote + ".xml";

                    if (File.Exists(sPath))
                    {
                        File.Delete(sPath);
                    }

                    XmlSerializerNamespaces nameSpaces = new XmlSerializerNamespaces();
                    nameSpaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    nameSpaces.Add("tipos", "http://localhost:8080/WsNFe2/tp");
                    nameSpaces.Add("ns1", "http://localhost:8080/WsNFe2/lote");

                    SerializeClassToXml.SerializeClasse <ReqConsultaLote>(consultaLote, sPath, nameSpaces);
                    sMessageRetorno = BuscaRetorno(ret.cabec.NumeroLote);
                }
                else if (ret.erros.Erro.Count() > 0)
                {
                    sMessageRetorno = "Erros:" + Environment.NewLine;
                    foreach (ErrosErro err in ret.erros.Erro)
                    {
                        sMessageRetorno += string.Format("Codigo:{0} - Descrição:{1}{2}", err.Codigo, err.Descricao, Environment.NewLine);
                    }
                    foreach (LoteRPS rps in this.dadosRPS.objLoteEnvio.lote.RPS)
                    {
                        base.LimpaCampoRecibo(rps.CD_NFSEQ);
                    }
                }
                else if (ret.alertas.alert.Count() > 0)
                {
                    sMessageRetorno = "Alertas:" + Environment.NewLine;
                    foreach (Alerta_retlote alert in ret.alertas.alert)
                    {
                        sMessageRetorno += string.Format("Codigo:{0} - Descrição:{1}{2}", alert.Codigo, alert.Descricao, Environment.NewLine);
                    }
                }

                return(sMessageRetorno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }