public void PopulaDadosInutilizacao(string sNumInicial, string sNumFinal, string sJustificativa)
        {
            try
            {
                objBelInutiliza = new belInutilizaFaixaCte();

                StringBuilder sQuery = new StringBuilder();
                sQuery.Append("Select ");
                sQuery.Append("coalesce(empresa.cd_ufnor,'') cUF,");
                sQuery.Append("coalesce(empresa.cd_cgc,'')CNPJ ");
                sQuery.Append("from empresa ");
                sQuery.Append("where empresa.cd_empresa ='" + belStatic.CodEmpresaCte + "' ");
                FbCommand fbConn = new FbCommand(sQuery.ToString(), cx.get_Conexao());
                cx.Open_Conexao();
                fbConn.ExecuteNonQuery();
                FbDataReader dr = fbConn.ExecuteReader();
                dr.Read();

                bel.belUF objbelUf = new bel.belUF();
                objbelUf.SiglaUF = dr["cUF"].ToString();

                objBelInutiliza.versao = "1.03";
                objBelInutiliza.tpAmb = belStatic.TpAmb.ToString();
                objBelInutiliza.xServ = "INUTILIZAR";
                objBelInutiliza.cUF = objbelUf.CUF;
                objBelInutiliza.ano = HLP.Util.Util.GetDateServidor().ToString("yy");
                objBelInutiliza.CNPJ = util.TiraSimbolo(dr["CNPJ"].ToString());
                objBelInutiliza.mod = "57";
                objBelInutiliza.serie = "1";
                objBelInutiliza.nCTIni = sNumInicial;
                objBelInutiliza.nCTFin = sNumFinal;
                objBelInutiliza.xJust = sJustificativa;
                objBelInutiliza.Id = GeraChave(objBelInutiliza.cUF, objBelInutiliza.CNPJ, objBelInutiliza.nCTIni, objBelInutiliza.nCTFin);
            }
            catch (Exception)
            {
                throw;
            }
            finally { cx.Close_Conexao(); }

        }
Example #2
0
        public List<belStatusCte> GerarXmlInutilizacao(belInutilizaFaixaCte objInutiliza)
        {
            XNamespace pf = "http://www.portalfiscal.inf.br/cte";
            XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

            XContainer inutCTe = new XElement(pf + "inutCTe", new XAttribute("versao", "1.04"),
                                                                 new XAttribute(ns + "schemaLocation", "http://www.portalfiscal.inf.br/inutCte_v1.04.xsd"),
                                                                 new XAttribute("xmlns", "http://www.portalfiscal.inf.br/cte"),
                                                                 new XAttribute(XNamespace.Xmlns + "ds", "http://www.w3.org/2000/09/xmldsig#"),
                                                                 new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));

            XContainer infInut = new XElement(pf + "infInut", new XAttribute("Id", objInutiliza.Id),
                                                                 new XElement(pf + "tpAmb", objInutiliza.tpAmb),
                                                                 new XElement(pf + "xServ", objInutiliza.xServ),
                                                                 new XElement(pf + "cUF", objInutiliza.cUF),
                                                                 new XElement(pf + "ano", objInutiliza.ano),
                                                                 new XElement(pf + "CNPJ", objInutiliza.CNPJ),
                                                                 new XElement(pf + "mod", objInutiliza.mod),
                                                                 new XElement(pf + "serie", objInutiliza.serie),
                                                                 new XElement(pf + "nCTIni", objInutiliza.nCTIni),
                                                                 new XElement(pf + "nCTFin", objInutiliza.nCTFin),
                                                                 new XElement(pf + "xJust", objInutiliza.xJust));


            inutCTe.Add(infInut);

            #region Assinatura

            belAssinaCte Assinatura = new belAssinaCte();
            string sInutCte = Assinatura.ConfigurarArquivo(inutCTe.ToString(), "infInut", this.cert);
            XElement xInutCTe = XElement.Parse(sInutCte);


            #endregion

            #region Valida_Xml

            belGlobais getschema = new belGlobais();
            XmlSchemaCollection myschema = new XmlSchemaCollection();

            XmlValidatingReader reader;

            try
            {
                XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                reader = new XmlValidatingReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xInutCTe.ToString(), XmlNodeType.Element, context);

                myschema.Add("http://www.portalfiscal.inf.br/cte", belStaticPastas.SCHEMA_CTE + "\\inutCte_v1.04.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                {
                }

            }
            catch (XmlException x)
            {
                throw new Exception(x.Message);
            }
            catch (XmlSchemaException x)
            {
                throw new Exception(x.Message);
            }

            #endregion

            #region Salva Xml

            string sData = HLP.Util.Util.GetDateServidor().Date.ToString("dd-MM-yyyy");
            belGlobais objGlobais = new belGlobais();
            DirectoryInfo dPastaProtocolo = new DirectoryInfo(belStaticPastas.PROTOCOLOS);
            if (!dPastaProtocolo.Exists) { dPastaProtocolo.Create(); }

            DirectoryInfo dPastaMesAtual = new DirectoryInfo(dPastaProtocolo + @"\\" + sData.Substring(3, 2) + "-" + sData.Substring(8, 2));
            if (!dPastaMesAtual.Exists) { dPastaMesAtual.Create(); }

            string sCaminho = dPastaMesAtual.ToString() + "\\" + "Inut_" + objInutiliza.nCTIni + "_" + objInutiliza.nCTFin + ".xml";

            XDocument XmlInut = new XDocument(xInutCTe);
            XmlInut.Save(sCaminho);

            #endregion

            #region Inutilizar Cte


            XmlDocument doc = new XmlDocument();
            doc.Load(sCaminho);

            string sRetorno = InutilizaFaixaCte(doc);
            List<belStatusCte> ListaStatus = TrataDadosRetorno(sRetorno);

            foreach (belStatusCte cte in ListaStatus)
            {
                if (cte.CodRetorno == "102")
                {
                    XDocument xRet = XDocument.Parse(sRetorno);
                    xRet.Save(sCaminho);
                }
                else
                {
                    File.Delete(sCaminho);
                }
            }
            #endregion


            return ListaStatus;

        }