コード例 #1
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public belGerarXML()
        {
            FbCommand InsertCmd = new FbCommand();
            FbCommand UpDate = new FbCommand();
            FbCommand DelCmd = new FbCommand();
            InicializeConnection();
            using (FbCommand cmd = new FbCommand("select control.cd_conteud from control where control.cd_nivel = '0016'", Conn))
            {
                Conn.Open();
                nm_Cliente = Convert.ToString(cmd.ExecuteScalar()).Trim();
                Conn.Close();
            }
            Globais LeRegWin = new Globais();
            this.psNM_Banco = LeRegWin.LeRegConfig("BancoDados");

            string[] sRamo = this.psNM_Banco.Split('\\');
            if (sRamo[sRamo.Count() - 1].ToUpper().Contains("TRANSPOR"))
            {
                belStatic.RAMO = "TRANSPORTE";
            }
            else if (sRamo[sRamo.Count() - 1].ToUpper().Contains("INDUSTRI"))
            {
                belStatic.RAMO = "INDUSTRIA";
            }
            else if (sRamo[sRamo.Count() - 1].ToUpper().Contains("COMERCIO"))
            {
                belStatic.RAMO = "COMERCIO";
            }
            else if (sRamo[sRamo.Count() - 1].ToUpper().Contains("CERAMICA"))
            {
                belStatic.RAMO = "CERAMICA";
            }
            sTipoIndustrializacao = LeRegWin.LeRegConfig("Industrializacao");
        }
コード例 #2
0
ファイル: belConnection.cs プロジェクト: dramosti/GeraXml_2.0
        public static string sMontaStringConexao()
        {
            try
            {
                Globais LeRegWin = new Globais();
                belGlobais MontaStringConexao = new belGlobais();
                StringBuilder sbConexao = new StringBuilder();

                sbConexao.Append("User ="******"SYSDBA");
                sbConexao.Append(";");
                sbConexao.Append("Password="******"masterkey");
                sbConexao.Append(";");
                sbConexao.Append("Database=");
                string sdatabase = LeRegWin.LeRegConfig("BancoDados");
                sbConexao.Append(sdatabase);
                sbConexao.Append(";");
                sbConexao.Append("DataSource=");
                sbConexao.Append(LeRegWin.LeRegConfig("Servidor"));
                sbConexao.Append(";");
                sbConexao.Append("Port=3050;Dialect=3; Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=2000;Packet Size=8192;ServerType=0;");
                return (string)sbConexao.ToString();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
コード例 #3
0
        public belNfeInutilizacao(int sTpAmb, string sCoduf, string sCnpj, string sMod, string sSerie, string sJust,
                                  string sNNFini, string sNNFfim, X509Certificate2 cert, string uf_empresa, bool bModoSCAN, int iStatusAtualSistema)
        {
            Globais LeRegWin = new Globais();
            _tpamp = sTpAmb;
            _cuf = sCoduf;
            _cnpj = Util.Util.TiraSimbolo(sCnpj, "");
            _mod = sMod;
            _serie = sSerie;
            _sjust = sJust;
            _nnffim = sNNFfim;
            _nnfini = sNNFini;
            _uf_empresa = uf_empresa;
            _spath = LeRegWin.LeRegConfig("PastaProtocolos");
            _sschema = LeRegWin.LeRegConfig("PastaSchema");
            this.pf = "http://www.portalfiscal.inf.br/nfe";
            this.cert = cert;
            if ((bModoSCAN) && (iStatusAtualSistema == 3))
            {

            }
            else
            {
                buscaRetorno();
            }
        }
コード例 #4
0
 public belVerificaPastas()
 {
     Globais objLeregwis = new Globais();
     this.pastaenvio = objLeregwis.LeRegConfig("PastaXmlEnvio").ToString();
     this.pastaenviados = objLeregwis.LeRegConfig("PastaXmlEnviado").ToString();
     this.pastacancelados = objLeregwis.LeRegConfig("PastaXmlCancelados").ToString();
     this.pastaprotocolos = objLeregwis.LeRegConfig("PastaProtocolos").ToString();
     this.pastaretorno = objLeregwis.LeRegConfig("PastaXmlRetorno").ToString();
     this.pastaschemas = objLeregwis.LeRegConfig("PastaSchema").ToString();
     VerificaConsistencia();
 }
コード例 #5
0
        public belNfeConsultaNF(string sVersao, string sVerDados, int iTpamb, string sChnfe, string sXServ)
        {
            Globais Gb = new Globais();
            _tpamb = iTpamb;
            _pathschemas = Gb.LeRegConfig("PastaSchema").ToString();
            _versao = sVersao;
            _versaodados = sVerDados;
            _xserv = sXServ;
            _chnfe = sChnfe;

        }
コード例 #6
0
ファイル: belEmail.cs プロジェクト: dramosti/GeraXml_2.0
        private bool _autentica;//Danner - o.s. 24329 - 08/04/2010


        public belEmail(string sSeq, string sEmp, string sHost, string sPorta, string sDe, string sSenha, string sPara, bool bAutentica)//NFe_2.0
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                Globais objGlobais = new Globais();
                xml.Load(objGlobais.LeRegConfig("PastaProtocolos") + "\\" + sSeq + "_ped-can.xml");
                _anexo2 = objGlobais.LeRegConfig("PastaProtocolos") + "\\" + sSeq + "_ped-can.xml";
                string sPath = objGlobais.LeRegConfig("PastaXmlCancelados") + "\\" + xml.GetElementsByTagName("chNFe")[0].InnerText + "-can.xml.xml";
                if (System.IO.File.Exists(sPath))
                {
                    if (sPara == "")
                    {
                        _para = retEmailDestinatario(sSeq, sEmp);
                    }
                    else
                    {
                        _para = sPara;
                    }

                    _paraTransp = "";
                    _envia = true;
                    _sSeq = sSeq;

                    _autentica = bAutentica;
                    _de = sDe;
                    _porta = Convert.ToInt16(sPorta);
                    _host = sHost;
                    _senha = sSenha;
                    _corpo = geraCorpoEmailCanc(sPath);
                    _anexo = sPath;
                    _assunto = "Mensagem Automática de Nota Fiscal Eletrônica de " + _razaoemit;
                }
                else
                {
                    throw new Exception("Arquivo ref. a Nota Fiscal " + xml.GetElementsByTagName("chNFe")[0].InnerText.Substring(25, 9) + " não se Encontra na Pasta de Cancelados"+
                    Environment.NewLine
                    +Environment.NewLine
                    + "Arquivo : " + xml.GetElementsByTagName("chNFe")[0].InnerText);
                }

            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            
            
        }
コード例 #7
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
 public belGerarXML()
 {
     FbCommand InsertCmd = new FbCommand();
     FbCommand UpDate = new FbCommand();
     FbCommand DelCmd = new FbCommand();
     InicializeConnection();
     using (FbCommand cmd = new FbCommand("select control.cd_conteud from control where control.cd_nivel = '0016'", Conn))
     {
         Conn.Open();
         nm_Cliente = Convert.ToString(cmd.ExecuteScalar()).Trim();
         Conn.Close();
     }
     Globais LeRegWin = new Globais();
     string psNM_Banco = LeRegWin.LeRegConfig("BancoDados");
     sTipoIndustrializacao = LeRegWin.LeRegConfig("Industrializacao");
 }
コード例 #8
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public static string sMontaStringConexao()
        {
            Globais MontaStringConexao = new Globais();
            StringBuilder sbConexao = new StringBuilder();

            sbConexao.Append("User ="******"Usuario"));
            sbConexao.Append(";");
            sbConexao.Append("Password="******"Senha"));
            sbConexao.Append(";");
            sbConexao.Append("Database=");
            string sdatabase = MontaStringConexao.LeRegConfig("BancoDados");
            sbConexao.Append(sdatabase);
            sbConexao.Append(";");
            sbConexao.Append("DataSource=");
            sbConexao.Append(MontaStringConexao.LeRegConfig("Servidor"));
            sbConexao.Append(";");
            sbConexao.Append("Port=3050;Dialect=3; Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=2000;Packet Size=8192;ServerType=0;");


            return (string)sbConexao.ToString();
        }
コード例 #9
0
        public belNfeCancelamento(string versao, string tpamb, string xserv, string xjust, string versaodados, string sEmp, string sSeq,
                                  X509Certificate2 xCert, string UF_Empresa, bool bModoSCAN, int iStatusAtualSistema)
        {
            this._versao = versao;
            this._emp = sEmp;
            this._tpamb = tpamb;
            this._xserv = xserv;
            this._seq = sSeq;
            this._xjust = xjust;
            this._versaodados = versaodados;
            Globais caminho = new Globais();
            this._spath = caminho.LeRegConfig("PastaProtocolos");
            this._sschema = caminho.LeRegConfig("PastaSchema");
            this._cert = xCert; 

            if ((bModoSCAN)&&(iStatusAtualSistema == 3))
            {
                buscaRetornoSCAN(UF_Empresa);
            }
            else
            {
                buscaRetorno(UF_Empresa);
            }
        }
コード例 #10
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        /// <summary>
        /// Popula as Classes da NF-e.
        /// </summary>
        /// <param name="sEmp"></param>
        /// <param name="sNF"></param>
        /// <param name="sNomeArq"></param>
        /// <param name="sFormaEmissao"></param>
        /// <param name="sForDanfe"></param>
        /// <param name="tp_amb"></param>
        /// <param name="cd_ufnro"></param>
        /// <param name="cert"></param>
        public void NFe(string sEmp, List<string> sNF, string sNomeArq, string sFormaEmissao, string sForDanfe, string cd_ufnro, X509Certificate2 cert, bool bModoSCAN, int iSerieSCAN, string sFormaEmiNFe, Version versao)
        {
            objbelGeraXml = new belGerarXML();
            Conn = objbelGeraXml.Conn;
            Conn.Open();
            this.sFormaEmiNFe = sFormaEmiNFe;
            this.bModoSCAN = bModoSCAN;
            this.iSerieSCAN = iSerieSCAN;

            string sPath = "";
            sPath = belStaticPastas.ENVIO + @sNomeArq;
            sTipoIndustrializacao = CarregarDadosXml("Industrializacao")[0].ToString();
            if (File.Exists(sPath))
            {
                File.Delete(sPath);
            }

            XDocument xdoc = new XDocument();
            #region XML_Principal
            XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
            //XContainer conenv = new XElement(pf + "enviNFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
            //                                         new XAttribute("versao", "1.10"),
            //                                         new XElement(pf + "idLote", sNomeArq.Substring(7, 15)));
            #endregion


            Globais glob = new Globais();
            int notaindex = 0;

            try
            {
                foreach (string sNota in sNF)
                {
                    List<object> lObjNotas = new List<object>();
                    notaindex++;
                    string sNFe = "NFe" + GeraChave(sEmp, sNota, Conn);


                    // infNFE                
                    // Começa a Popular as Classes
                    //Ide               
                    belIde objide;
                    objide = BuscaIde(sEmp, sNota, sForDanfe, sFormaEmissao, versao);
                    lObjNotas.Add(objide);//Danner - o.s. 24092 - 03/02/2010
                    //Fim - Ide

                    //Emit
                    belEmit objemit;
                    objemit = BuscaEmit(sEmp, sForDanfe, sForDanfe, sNota); //Claudinei - o.s. 24222 - 08/03/2010
                    lObjNotas.Add(objemit);//Danner - o.s. 24092 - 03/02/2010
                    //Fim - Emit

                    //dest
                    belDest objdest;
                    objdest = BuscaDest(sEmp, sNota);
                    lObjNotas.Add(objdest);//Danner - o.s. 24092 - 03/02/2010
                    //Fim - dest

                    //Endereço de entrega
                    belEndEnt objendent;
                    objendent = BuscaEndEnt(sEmp, sNota);
                    lObjNotas.Add(objendent);
                    //Fim - Endereço de entrega

                    //Itens da NFe
                    List<belDet> objdet = new List<belDet>();
                    objdet = BuscaItem(sEmp, sNota, objdest);
                    //RetiraValorBCICMSret dos Totais;
                    decimal dVbcIcmsRt = objdet.Where(p => p.belImposto.belIcms.belICMSSN500 != null).Select(p => p.belImposto.belIcms.belICMSSN500.vBCSTRet).Sum();
                    decimal dVIcmsRt = objdet.Where(p => p.belImposto.belIcms.belICMSSN500 != null).Select(p => p.belImposto.belIcms.belICMSSN500.vICMSSTRet).Sum();
                    lObjNotas.Add(objdet);

                    //Totais               
                    belTotal objtotal;
                    objtotal = BuscaTotais(sEmp, sNota, objdest);
                    objtotal.belIcmstot.Vbcst = objtotal.belIcmstot.Vbcst - dVbcIcmsRt; //25695
                    objtotal.belIcmstot.Vst = objtotal.belIcmstot.Vst - dVIcmsRt;//25695
                    lObjNotas.Add(objtotal);
                    //Fim - Totais

                    //Frete
                    belTransp objtransp;
                    objtransp = BuscaFrete(sEmp, sNota);
                    lObjNotas.Add(objtransp);

                    //Fim - Frete

                    //Duplicatas                
                    belCobr objcob;
                    objcob = BuscaFat(sEmp, sNota);
                    lObjNotas.Add(objcob);
                    //Fim - Duplicatas


                    //OBS
                    belInfAdic objobs;
                    objobs = BuscaObs(sEmp, sNota, objdest, objdet, objbelGeraXml);
                    string sIcmsRet = ""; //25695
                    if ((dVbcIcmsRt > 0) && (dVIcmsRt > 0))
                    {
                        sIcmsRet = "VbcIcmsRetido: " + dVbcIcmsRt.ToString("#0.00") + " | VIcmsRetido: " + dVIcmsRt.ToString("#0.00") + " ;";
                    }
                    string msgInsumos = "";

                    if (objbelGeraXml.nm_Cliente.Equals("ZINCOBRIL")) // OS_25787
                    {
                        decimal dmaoObra = objdet.Where(p => p.tp_industrializacao.Equals("M")).Sum(P => P.belProd.Vprod);
                        decimal dinsumos = objdet.Where(p => p.tp_industrializacao.Equals("I")).Sum(P => P.belProd.Vprod);

                        if (dmaoObra > 0)
                        {
                            msgInsumos += "VALOR DA MÃO DE OBRA = R$" + dmaoObra.ToString() + ";";
                        }
                        if (dinsumos > 0)
                        {
                            msgInsumos += "VALOR DOS INSUMOS = R$" + dinsumos.ToString() + ";";
                        }
                    }


                    objobs.Infcpl = msgInsumos + sIcmsRet + objobs.Infcpl;
                    lObjNotas.Add(objobs);
                    //Fim - OBS

                    belExporta objExporta = new belExporta();
                    //objExporta = buscaExporta
                    lObjNotas.Add(objExporta);

                    lTotNota.Add(lObjNotas);

                    //os_25923 Carrega Declaração de Importação    


                    // Update para Gravar o numero da Chave no banco da nota corrente.
                    for (int i = 0; i < 4; i++)
                    {
                        if (GravaNumeroChaveNota(sEmp, sNota, sNFe))
                        {
                            break;
                        }
                    }
                }
                Conn.Close();
            }
            catch (Exception ex)
            {
                Conn.Close();
                throw ex;
            }
        }
コード例 #11
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public belInfAdic BuscaObs(string sEmp,
                           string sNF,
                           belDest objbeldest,
                           List<belDet> objbelDet, belGerarXML objbelGeraXml)
        {
            belInfAdic objinfadic = new belInfAdic();

            try
            {
                StringBuilder sSql = new StringBuilder();

                //Campos do Select
                sSql.Append("Select ");
                sSql.Append("nf.ds_anota ");

                if (((objbelGeraXml.nm_Cliente == "MOGPLAST") || (objbelGeraXml.nm_Cliente == "TSA")) && (sEmp == "003"))
                {
                    sSql.Append(", nf.cd_nfseq_fat_origem ");
                }
                if (objbelGeraXml.nm_Cliente == "MACROTEX")
                {
                    sSql.Append(", vendedor.nm_vend, ");
                    sSql.Append("nf.DS_DOCORIG ");
                }
                //Tabela
                sSql.Append("From NF ");
                //Relacionamentos
                sSql.Append("left join vendedor on (vendedor.cd_vend = nf.cd_vend1) ");
                //Where
                sSql.Append("Where ");
                sSql.Append("(NF.cd_empresa ='");
                sSql.Append(sEmp);
                sSql.Append("')");
                sSql.Append(" and ");
                sSql.Append("(nf.cd_nfseq = '");
                sSql.Append(sNF);
                sSql.Append("') ");
                string sObs = "";
                sObs = RetornaBlob(sSql, sEmp, Conn, objbelGeraXml);
                if (sObs.IndexOf("\\fs") != -1)// DIEGO - OS_24854 
                {
                    sObs = sObs.Substring((sObs.IndexOf("\\fs") + 6), sObs.Length - (sObs.IndexOf("\\fs") + 6));
                }
                if (objbelGeraXml.nm_Cliente == "MARPA")
                {
                    sObs += MontaObsAgrup(sEmp, sNF, Conn);
                }
                Globais LeRegWin = new Globais();
                string sBanco = LeRegWin.LeRegConfig("BancoDados");
                {
                    //Fim - Danner - o.s. 24383 - 22/04/2010
                    //belGerarXML BuscaConexao = new belGerarXML();
                    //FbConnection Conn = BuscaConexao.Conn;
                    #region OS_24245

                    try
                    {
                        if (sBanco.ToUpper().IndexOf("CERAMICAC0") == -1) //Claudinei - o.s. 24245 - 08/03/2010
                        {
                            StringBuilder sSuframa = new StringBuilder();
                            sSuframa.Append("Select First 1 ");
                            sSuframa.Append("nf.ds_anota, ");
                            sSuframa.Append("clifor.st_descsuframa, ");
                            sSuframa.Append("clifor.cd_suframa, ");
                            sSuframa.Append("clifor.ST_PISCOFINS_SUFRAMA, ");
                            sSuframa.Append("nf.vl_aliqcofins_suframa, ");
                            sSuframa.Append("nf.vl_aliqpis_suframa, ");
                            sSuframa.Append("nf.vl_cofins_suframa, ");
                            sSuframa.Append("NF.vl_pis_suframa, ");

                            //Claudinei - o.s. 23683 - 11/09/2009
                            sSuframa.Append("(select Sum(movitem.vl_descsuframa) from movitem where (movitem.cd_empresa = nf.cd_empresa) and (movitem.cd_nfseq = nf.cd_nfseq)) vl_suframa, ");
                            sSuframa.Append("icm.vl_aliquot vl_persuframa ");
                            //Fim - Claudinei - o.s. 23683 - 11/09/2009

                            //Claudinei - o.s. 23827 - 17/11/2009

                            sSuframa.Append(", ");
                            sSuframa.Append("case when empresa.vl_aliqfatcred > 0 then ");
                            sSuframa.Append("(nf.vl_totnf * empresa.vl_aliqfatcred)/100 ");
                            sSuframa.Append("else ");
                            sSuframa.Append("0 ");
                            sSuframa.Append("end aliq, ");
                            sSuframa.Append("empresa.vl_aliqfatcred, ");
                            sSuframa.Append("coalesce(tpdoc.st_hevenda,'N') st_hevenda ");

                            //Fim - Claudinei - o.s. 23827 - 17/11/2009

                            //Tabela
                            sSuframa.Append("From NF ");

                            //Relacionamentos
                            sSuframa.Append("left join clifor on (clifor.cd_clifor = nf.cd_clifor) ");
                            sSuframa.Append("left join icm on (icm.cd_ufnor = clifor.cd_ufnor) ");
                            sSuframa.Append("left join movitem on (movitem.cd_empresa = nf.cd_empresa) ");
                            sSuframa.Append("and ");
                            sSuframa.Append("(movitem.cd_nfseq = nf.cd_nfseq) ");

                            //Claudinei - o.s. 23827 - 17/11/2009
                            sSuframa.Append("Inner join ");
                            sSuframa.Append("Empresa on ");
                            sSuframa.Append("(Empresa.cd_empresa = nf.cd_empresa) ");

                            sSuframa.Append("Left join ");
                            sSuframa.Append("TPDoc on ");
                            sSuframa.Append("(TPDoc.cd_tipodoc = nf.cd_tipodoc) ");

                            //Fim - Claudinei - o.s. 23827 - 17/11/2009


                            //Where
                            sSuframa.Append("Where ");
                            sSuframa.Append("(NF.cd_empresa ='");
                            sSuframa.Append(sEmp);
                            sSuframa.Append("')");
                            sSuframa.Append(" and ");
                            sSuframa.Append("(nf.cd_nfseq = '");
                            sSuframa.Append(sNF);
                            sSuframa.Append("') ");

                            //Conn.Open();

                            FbCommand cmdSuframa = new FbCommand(sSuframa.ToString(), Conn);
                            cmdSuframa.ExecuteNonQuery();

                            FbDataReader drSuframa = cmdSuframa.ExecuteReader();
                            drSuframa.Read();

                            if (drSuframa["st_descsuframa"].ToString() == "S")
                            {
                                //Claudinei - o.s. 23683 - 11/09/2009
                                decimal dvlSuframa = Math.Round(Convert.ToDecimal(drSuframa["vl_suframa"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                decimal dvlPerSuframa = Math.Round(Convert.ToDecimal(drSuframa["vl_persuframa"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                //Fim - Claudinei - o.s. 23683 - 11/09/2009

                                if (sObs.Trim() != "")
                                {
                                    sObs += string.Format(" - DESCONTO DE {0:C2} REF. AO ICMS {1:f2}% CODIGO SUFRAMA: {2}",
                                                          dvlSuframa,
                                                          dvlPerSuframa,
                                                          drSuframa["cd_suframa"].ToString());
                                }
                                else
                                {
                                    sObs += string.Format("DESCONTO DE {0:C2} REF. AO ICMS 7.00% CODIGO SUFRAMA: {1}",
                                                          dvlSuframa,
                                                          drSuframa["cd_suframa"].ToString());
                                }
                            }

                            //Claudinei - o.s. sem - 01/09/2009

                            if (drSuframa["ST_PISCOFINS_SUFRAMA"].ToString() == "S")
                            {
                                decimal dvl_aliqcofins_suframa = Math.Round(Convert.ToDecimal(drSuframa["vl_aliqcofins_suframa"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                decimal dvl_cofins_suframa = Math.Round(Convert.ToDecimal(drSuframa["vl_cofins_suframa"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                decimal dvl_aliqpis_suframa = Math.Round(Convert.ToDecimal(drSuframa["vl_aliqpis_suframa"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                decimal dvl_pis_suframa = Math.Round(Convert.ToDecimal(drSuframa["vl_pis_suframa"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010

                                if (sObs.Trim() != "")
                                {
                                    sObs += string.Format(" - ABATIMENTO COFINS ({0}%) - VALOR R$ {1} - ABATIMENTO PIS ({2}%) - VALOR R$ {3} ",
                                                          dvl_aliqcofins_suframa.ToString("#0.00").Replace(',', '.'),
                                                          dvl_cofins_suframa.ToString("#0.00").Replace(',', '.'),
                                                          dvl_aliqpis_suframa.ToString("#0.00").Replace(',', '.'),
                                                          dvl_pis_suframa.ToString("#0.00").Replace(',', '.'));
                                }
                                else
                                {
                                    sObs += string.Format("ABATIMENTO COFINS ({0}%) - VALOR R$ {1} - ABATIMENTO PIS ({2}%) - VALOR R$ {3} ",
                                                          dvl_aliqcofins_suframa.ToString("#0.0000").Replace(',', '.'),
                                                          dvl_cofins_suframa.ToString("#0.0000").Replace(',', '.'),
                                                          dvl_aliqpis_suframa.ToString("#0.0000").Replace(',', '.'),
                                                          dvl_pis_suframa.ToString("#0.0000").Replace(',', '.'));
                                }
                            }


                            decimal dvlnf = 0;
                            if (drSuframa["aliq"].ToString() != "")
                            {
                                dvlnf = Math.Round(Convert.ToDecimal(drSuframa["aliq"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                            }

                        }
                        StringBuilder sDevolucao = new StringBuilder();


                        //Tabela
                        sDevolucao.Append("Select ");
                        sDevolucao.Append("movitem.cd_doc, ");
                        sDevolucao.Append("nf.cd_clifor, ");
                        sDevolucao.Append("nf.dt_emi, ");
                        sDevolucao.Append("movensai.dt_emi EmissaoEntrada, ");
                        sDevolucao.Append("movitem.vl_totbruto ");
                        sDevolucao.Append("From Movitem ");
                        sDevolucao.Append("inner join NF on ");
                        sDevolucao.Append("(nf.cd_empresa = Movitem.cd_empresa) ");
                        sDevolucao.Append("and ");
                        sDevolucao.Append("(nf.cd_nfseq = movitem.cd_nfseq) ");
                        sDevolucao.Append("inner join opereve on ");
                        sDevolucao.Append("(opereve.cd_oper = movitem.cd_oper) ");
                        sDevolucao.Append("left join movensai on (movensai.cd_empresa = movitem.cd_empresa) ");
                        sDevolucao.Append("and ");
                        sDevolucao.Append("(movensai.cd_doc = movitem.cd_doc) ");
                        sDevolucao.Append("and ");
                        sDevolucao.Append("(movensai.cd_clifor = nf.cd_clifor) ");

                        //Where
                        sDevolucao.Append("Where ");
                        sDevolucao.Append("(Movitem.cd_empresa ='");
                        sDevolucao.Append(sEmp);
                        sDevolucao.Append("')");
                        sDevolucao.Append(" and ");
                        sDevolucao.Append("(Movitem.cd_nfseq = '");
                        sDevolucao.Append(sNF);
                        sDevolucao.Append("') ");
                        sDevolucao.Append("and ");
                        sDevolucao.Append("(opereve.ST_ESTTERC = 'S') ");
                        sDevolucao.Append("and ");
                        sDevolucao.Append("Movitem.cd_oper <> '202' ");
                        sDevolucao.Append("and ");
                        sDevolucao.Append("Movitem.cd_oper <> '227' ");//TESTE-DANI
                        sDevolucao.Append("Order by movitem.cd_doc");

                        //if (Conn.State != ConnectionState.Open)
                        {
                            //Conn.Open();
                        }
                        FbCommand cmdDevolucoes = new FbCommand(sDevolucao.ToString(), Conn);
                        cmdDevolucoes.ExecuteNonQuery();
                        FbDataReader drDevolucoes = cmdDevolucoes.ExecuteReader();
                        List<strucDevolucoes> Devolucoes = new List<strucDevolucoes>();
                        decimal dvlTotBruto = 0;
                        string scdDoc = string.Empty;
                        while (drDevolucoes.Read())
                        {
                            if (scdDoc != drDevolucoes["cd_doc"].ToString())
                            {
                                dvlTotBruto = 0;

                            }

                            dvlTotBruto += Math.Round(Convert.ToDecimal(drDevolucoes["vl_totbruto"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010

                            StringBuilder sStore = new StringBuilder();
                            sStore.Append("SELECT ");
                            sStore.Append("QT_SALDOEN ");
                            sStore.Append("FROM SP_SALDOTER('");
                            sStore.Append(sEmp);
                            sStore.Append("', '");
                            sStore.Append(drDevolucoes["cd_clifor"].ToString());
                            sStore.Append("', '");
                            sStore.Append("       "); //Claudinei - o.s. 24075 - 29/01/2010
                            sStore.Append("', '");
                            sStore.Append("|||||||"); //Claudinei - o.s. 24075 - 29/01/2010
                            sStore.Append("', '");
                            sStore.Append("X");
                            sStore.Append("', '");
                            sStore.Append("N");
                            sStore.Append("', '");
                            sStore.Append(sNF);
                            sStore.Append("', '");
                            sStore.Append(Convert.ToDateTime(drDevolucoes["dt_emi"]).ToString("dd.MM.yyyy"));
                            sStore.Append("') ");
                            sStore.Append("where SP_SALDOTER.cd_doc ='");
                            sStore.Append(drDevolucoes["cd_doc"].ToString().Trim());
                            sStore.Append("'");
                            sStore.Append(" and ");
                            sStore.Append("SP_SALDOTER.qt_saldoen > 0");

                            FbCommand cmd = new FbCommand();
                            cmd.Connection = Conn;
                            cmd.CommandText = sStore.ToString();
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Clear();

                            //if (Conn.State != ConnectionState.Open)
                            {
                                //Conn.Open();
                            }

                            Int32 iSaldoTer = Convert.ToInt32(cmd.ExecuteScalar());
                            strucDevolucoes Devolucao = new strucDevolucoes();
                            if (drDevolucoes["cd_doc"].ToString() != "")
                            {
                                Devolucao.scdNotafis = drDevolucoes["cd_doc"].ToString();
                                if (drDevolucoes["EmissaoEntrada"] != System.DBNull.Value)
                                {
                                    Devolucao.dDtEmi = Convert.ToDateTime(drDevolucoes["EmissaoEntrada"]);
                                }
                                Devolucao.dValorNF = dvlTotBruto.ToString("#0.00");
                                Devolucao.sSaldo = (iSaldoTer > 0 ? "Parcial" : "Total");
                                if (!Devolucoes.Exists(c => c.scdNotafis == Devolucao.scdNotafis))
                                {
                                    Devolucoes.Add(Devolucao);
                                }
                                else
                                {
                                    for (int i = 0; i < Devolucoes.Count; i++)
                                    {
                                        if ((Devolucoes[i].scdNotafis == Devolucao.scdNotafis) && (Devolucoes[i].dDtEmi == Devolucao.dDtEmi)) //OS_25220
                                        {
                                            Devolucoes[i] = Devolucao;
                                            break;
                                        }
                                    }
                                }
                            }
                            scdDoc = drDevolucoes["cd_doc"].ToString();
                        }

                        for (int i = 0; i < Devolucoes.Count; i++)
                        {

                            if (sObs.Trim().Length > 0)
                            {
                                sObs += string.Format(" - Devolucao {0} ref. sua NF {1} de {2} de valor R$ {3}", //Claudinei - o.s. 24043 - 25/01/2010
                                                      Devolucoes[i].sSaldo,
                                                      Devolucoes[i].scdNotafis,
                                                      Devolucoes[i].dDtEmi.ToString("dd/MM/yyyy"),
                                                      Devolucoes[i].dValorNF); //Claudinei - o.s. 24043 - 25/01/2010
                            }
                            else
                            {
                                sObs += string.Format("Devolucao {0} ref. sua NF {1} de {2} de valor R$ {3}", //Claudinei - o.s. 24043 - 25/01/2010
                                                      Devolucoes[i].sSaldo,
                                                      Devolucoes[i].scdNotafis,
                                                      Devolucoes[i].dDtEmi.ToString("dd/MM/yyyy"),
                                                      Devolucoes[i].dValorNF); //Claudinei - o.s. 24043 - 25/01/2010

                            }
                        }

                        string sMensagemSuperSimples = MensagemSuperSimples(sEmp, sNF, Conn);
                        bool cfopsValidos = (objbelDet.Count(p => (p.belProd.Cfop.Equals("5101"))
                                                                   || (p.belProd.Cfop.Equals("6107"))
                                                                   || (p.belProd.Cfop.Equals("6101"))) > 0 ? true : false);

                        if (sMensagemSuperSimples != ""
                           && (objbeldest.Cnpj != null && objbeldest.Cnpj != "")
                           || (objbelGeraXml.nm_Cliente == "TERRAVIS")
                            && cfopsValidos) // OS_25182 
                        {

                            if (sObs.Trim().Length > 0)
                            {
                                sObs += " - " + sMensagemSuperSimples;
                            }
                            else
                            {
                                sObs += sMensagemSuperSimples;

                            }
                        }
                        //Claudinei - o.s. 24118 - 11/02/2010
                    }
                    catch (Exception Ex)
                    {
                        sExecao = " - Problemas ao Buscar Suframa";
                        throw new Exception(Ex.Message + sExecao);
                    }
                    finally
                    {
                        //Conn.Close();
                    }

                    #endregion
                }
                // Diego - 22/06/2010 - OS_24576
                //Monta Mensagem de PIS, COFINS
                try
                {
                    //belGerarXML BuscaConexao = new belGerarXML();
                    //FbConnection Conn = BuscaConexao.Conn;
                    //if (Conn.State != ConnectionState.Open)
                    {
                        //Conn.Open();
                    }
                    StringBuilder sQuery = new StringBuilder();
                    sQuery.Append("SELECT ");
                    sQuery.Append("NF.VL_COFINS, NF.VL_PIS, ");
                    sQuery.Append("empresa.vl_aliqpis_suframa,empresa.vl_aliqcofins_suframa, ");
                    sQuery.Append("CLIFOR.st_desc_piscofins_dupl ");
                    sQuery.Append("from nf inner join empresa ");
                    sQuery.Append("on (nf.cd_empresa = empresa.cd_empresa)");
                    sQuery.Append("inner join clifor ");
                    sQuery.Append("on (nf.cd_clifor = clifor.cd_clifor) ");
                    sQuery.Append("where (empresa.cd_empresa = '");
                    sQuery.Append(sEmp);
                    sQuery.Append("') ");
                    sQuery.Append("and ( nf.cd_nfseq = '");
                    sQuery.Append(sNF);
                    sQuery.Append("') ");


                    FbCommand cmd = new FbCommand(sQuery.ToString(), Conn);
                    FbDataReader drPisCofins = cmd.ExecuteReader();

                    while (drPisCofins.Read())
                    {
                        if ((drPisCofins["st_desc_piscofins_dupl"].ToString() == "S"))
                        {
                            if ((drPisCofins["vl_aliqpis_suframa"].ToString() != "") && (drPisCofins["vl_aliqcofins_suframa"].ToString() != ""))
                            {
                                string sMensagemPisCofins = "(PIS e COFINS retido conforme artigo 3º paragrafo 4º da lei 10.485/02, PIS "
                                                            + drPisCofins["vl_aliqpis_suframa"].ToString()
                                                            + "% R$" + drPisCofins["VL_PIS"].ToString()
                                                            + " , COFINS "
                                                            + drPisCofins["vl_aliqcofins_suframa"].ToString()
                                                            + "% R$"
                                                            + drPisCofins["VL_COFINS"].ToString()
                                                            + " Total R$" + ((Convert.ToDouble(drPisCofins["VL_PIS"].ToString())) + (Convert.ToDouble(drPisCofins["VL_COFINS"].ToString()))).ToString() + ")";
                                sObs += (sObs != "" ? " - " : "") + sMensagemPisCofins;
                            }
                        }
                    }
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }// Diego - 22/06/2010 - OS_24576 - FIM


                // Diego - 15/07/2010 - OS_24665
                //Obs de ICMS Recolhido por Substituição
                try
                {
                    //belGerarXML BuscaConexao = new belGerarXML();
                    //FbConnection Conn = BuscaConexao.Conn;


                    StringBuilder sQuery = new StringBuilder();
                    sQuery.Append("SELECT ");
                    sQuery.Append("movitem.cd_sittrib, EMPRESA.ST_SUPERSIMPLES, EMPRESA.ST_IMP_SUPERSIMPLES ");
                    sQuery.Append("from empresa INNER JOIN movitem ON (EMPRESA.cd_empresa = movitem.cd_empresa)");
                    sQuery.Append("where (empresa.cd_empresa = '");
                    sQuery.Append(sEmp);
                    sQuery.Append("') ");
                    sQuery.Append("and ( movitem.cd_nfseq = '");
                    sQuery.Append(sNF);
                    sQuery.Append("') ");


                    FbCommand cmd = new FbCommand(sQuery.ToString(), Conn);
                    FbDataReader drIcmsRecolhido = cmd.ExecuteReader();
                    string sMensagemIcmsRecolhido = "";

                    while (drIcmsRecolhido.Read())
                    {
                        if ((drIcmsRecolhido["ST_SUPERSIMPLES"].ToString() == "S") && (drIcmsRecolhido["ST_IMP_SUPERSIMPLES"].ToString() == "S"))
                        {
                            if ((drIcmsRecolhido["cd_sittrib"].ToString().Equals("010")) ||
                                        (drIcmsRecolhido["cd_sittrib"].ToString().Equals("030")) ||
                                        (drIcmsRecolhido["cd_sittrib"].ToString().Equals("060")) ||
                                        (drIcmsRecolhido["cd_sittrib"].ToString().Equals("070")))
                            {
                                sMensagemIcmsRecolhido = "ICMS RECOLHIDO POR SUBSTITUICAO TRIBUTARIA CONFORME DECRETO 54251/09 ART 313 RICMS/2000";
                                break;
                            }
                        }
                    }
                    if (sMensagemIcmsRecolhido != "")
                    {
                        sObs += (sObs.Trim() != "" ? " - " : "") + sMensagemIcmsRecolhido;
                    }
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
                // Diego - 15/07/2010 - OS_24665 - FIM
                //OS_25201 - DIEGO
                if (LeRegWin.LeRegConfig("TotalizaCFOP").Equals("True"))
                {
                    sObs += " " + MessagemTotalizaCFOP(sEmp, sNF, Conn);
                }//OS_25201 - FIM

                //OS_25224 - INICIO

                string sCNPJdest = (objbeldest.Cnpj != null ? Util.Util.RetiraCaracterCNPJ(objbeldest.Cnpj) : "");

                try
                {
                    //belGerarXML BuscaConexao = new belGerarXML();
                    //FbConnection Conn = BuscaConexao.Conn;

                    if (objbelGeraXml.Equals("JAMAICA"))
                    {
                        StringBuilder sQuery = new StringBuilder();
                        sQuery.Append("select coalesce(clifor.cd_alter2,'') cd_alter2 from clifor ");
                        sQuery.Append("where clifor.cd_cgc ='" + sCNPJdest + "'");
                        FbCommand cmd = new FbCommand(sQuery.ToString(), Conn);
                        FbDataReader dr = cmd.ExecuteReader();
                        string sMesgCodDest = "";
                        while (dr.Read())
                        {
                            sMesgCodDest = dr["cd_alter2"].ToString();
                        }
                        if (sMesgCodDest != "")
                        {
                            sObs = "<<COD FORNECEDOR " + sMesgCodDest + ">> " + sObs;
                        }
                    }
                    if (objbelGeraXml.nm_Cliente.Equals("LORENZON"))
                    {
                        StringBuilder sQuery = new StringBuilder();
                        sQuery.Append("select prazos.ds_prazo, vendedor.nm_vend , clifor.cd_clifor from nf ");
                        sQuery.Append("inner join clifor on nf.cd_clifor = clifor.cd_clifor ");
                        sQuery.Append("inner join prazos on nf.cd_prazo = prazos.cd_prazo ");
                        sQuery.Append(" inner join vendedor  on nf.cd_vendint = vendedor.cd_vend ");
                        sQuery.Append("where nf.cd_nfseq = '" + sNF + "' ");
                        sQuery.Append("and nf.cd_empresa = '" + sEmp + "' ");
                        FbCommand cmd = new FbCommand(sQuery.ToString(), Conn);
                        FbDataReader dr = cmd.ExecuteReader();
                        string sMsgLorenzon = "";


                        while (dr.Read())
                        {
                            sMsgLorenzon = "COND.PGTO = " + dr["ds_prazo"].ToString() + " | VENDEDOR = " + dr["nm_vend"].ToString() + " | COD. CLIENTE = " + dr["cd_clifor"].ToString();
                        }
                        if (sMsgLorenzon != "")
                        {
                            sObs = "<< " + sMsgLorenzon + " >> " + sObs;
                        }
                    }



                }
                catch (Exception ex)
                {
                    throw ex;
                }

                //OS_25224 - FIM
                if (sObs.Trim() != "")
                {
                    objinfadic.Infcpl = sObs.Trim();
                }
                //Fim - Montagem do XML
            }
            catch (Exception Ex)
            {
                sExecao = " - Problemas nas Obs´s da Nota";
                throw new Exception(Ex.Message + sExecao);
            }

            return objinfadic; //Danner - o.s. sem - 16/11/2009


        }
コード例 #12
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        /// <summary>
        /// Gera a Estrutura do XML da NF-e
        /// </summary>
        /// <param name="sNF"></param>
        /// <param name="cert"></param>
        /// <param name="sEmp"></param>
        /// <param name="sNomeArq"></param>
        /// <param name="iStatusAtualSistema"</param>
        public void geraArquivoNFE(List<string> sNF, X509Certificate2 cert, string sEmp, string sNomeArq)
        {
            objbelGeraXml = new belGerarXML();
            Conn = objbelGeraXml.Conn;
            Conn.Open();
            try
            {
                string sPath = "";
                sPath = (sFormaEmiNFe.Equals("2") ? CarregarDadosXml("PastaContingencia").ToString() + @sNomeArq : CarregarDadosXml("PastaXmlEnvio").ToString() + @sNomeArq);
                Globais glob = new Globais();
                int iCount = 0;

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


                foreach (var i in lTotNota)
                {
                    string sNota = sNF[iCount];
                    string sNFe = "NFe" + GeraChave(sEmp, sNota, Conn);
                    XDocument xdoc = new XDocument();

                    #region XML_Principal
                    XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
                    XContainer conenv = new XElement(pf + "enviNFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                               new XAttribute("versao", "2.00"),
                                                               new XElement(pf + "idLote", sNomeArq.Substring(7, 15)));
                    #endregion

                    #region XML_Cabeçalho


                    XContainer concabec = (new XElement(pf + "NFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe")));
                    XContainer coninfnfe = (new XElement(pf + "infNFe", new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XAttribute("Id", sNFe),
                                                                        new XAttribute("versao", "2.00")));


                    #endregion

                    #region Ide
                    XContainer conide;
                    try
                    {
                        belIde objide = i[0] as belIde;
                        #region XML_ide
                        conide = (new XElement(pf + "ide", new XElement(pf + "cUF", objide.Cuf.ToString()),
                                                                    new XElement(pf + "cNF", objide.Cnf.ToString()),
                                                                    new XElement(pf + "natOp", objide.Natop.ToString()),
                                                                    new XElement(pf + "indPag", objide.Indpag.ToString()),
                                                                    new XElement(pf + "mod", objide.Mod.ToString()),
                                                                    new XElement(pf + "serie", objide.Serie.ToString()),
                                                                    new XElement(pf + "nNF", objide.Nnf.ToString()),
                                                                    new XElement(pf + "dEmi", objide.Demi.ToString("yyyy-MM-dd")),
                                                                    new XElement(pf + "dSaiEnt", objide.Dsaient.ToString("yyyy-MM-dd")),
                                                                    new XElement(pf + "hSaiEnt", objide.HSaiEnt.ToString("HH:mm:ss")), // NFe_2.0
                                                                    new XElement(pf + "tpNF", objide.Tpnf.ToString()),
                                                                    new XElement(pf + "cMunFG", objide.Cmunfg.ToString()),
                                                                    (objide.belNFref != null ?
                                                                    (from c in objide.belNFref
                                                                     select new XElement(pf + "NFref",
                                                                                (c.RefNFe != null ? new XElement(pf + "refNFe", c.RefNFe) : null),
                                                                               (c.cUF != null ? (new XElement(pf + "refNF",
                                                                                (c.cUF != null ? new XElement(pf + "cUF", c.cUF) : null),
                                                                                 (c.AAMM != null ? new XElement(pf + "AAMM", c.AAMM) : null),
                                                                                 (c.CNPJ != null ? new XElement(pf + "CNPJ", c.CNPJ) : null),
                                                                                 (c.mod != null ? new XElement(pf + "mod", c.mod) : null),
                                                                                 (c.serie != null ? new XElement(pf + "serie", c.serie) : null),
                                                                                 (c.nNF != null ? new XElement(pf + "nNF", c.nNF) : null))) : null))) : null),//NFe_2.0_Verificar ID B14 - B20a - B20i - 
                                                                    new XElement(pf + "tpImp", objide.Tpimp.ToString()),
                                                                    new XElement(pf + "tpEmis", objide.Tpemis.ToString()),
                                                                    new XElement(pf + "cDV", objide.Cdv.ToString()),
                                                                    new XElement(pf + "tpAmb", objide.Tpamb.ToString()),
                                                                    new XElement(pf + "finNFe", objide.Finnfe.ToString()),
                                                                    new XElement(pf + "procEmi", objide.Procemi.ToString()),
                                                                    new XElement(pf + "verProc", objide.Verproc.ToString()),

                                                                    ((objide.Tpemis.Equals("2")) || (objide.Tpemis.Equals("3")) ?
                                                                                          new XElement(pf + "dhCont", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")) : null), // NFe_2.0
                                                                    ((objide.Tpemis.Equals("2")) || (objide.Tpemis.Equals("3")) ?
                                                                                          new XElement(pf + "xJust", (iStatusAtualSistema == 2 ? "FALHA DE CONEXÃO COM INTERNET" : "FALHA COM WEB SERVICE DO ESTADO")) : null)));// NFe_2.0
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_ide - " + x.Message);
                    }
                    #endregion

                    #region Emit

                    XContainer conemit;
                    try
                    {
                        belEmit objemit = i[1] as belEmit;
                        #region XML_Emit

                        conemit = (new XElement(pf + "emit", (new XElement(pf + "CNPJ", objemit.Cnpj.ToString())),
                                                  new XElement(pf + "xNome", objemit.Xnome.ToString()),
                                                  new XElement(pf + "xFant", objemit.Xfant.ToString()),
                                                          new XElement(pf + "enderEmit",
                                                                  new XElement(pf + "xLgr", objemit.Xlgr.ToString()),
                                                                  new XElement(pf + "nro", objemit.Nro.ToString()),
                                                                  (objemit.Xcpl != null ? new XElement(pf + "xCpl", objemit.Xcpl.ToString()) : null),
                                                                  new XElement(pf + "xBairro", objemit.Xbairro.ToString()),
                                                                  new XElement(pf + "cMun", objemit.Cmun.ToString()),
                                                                  new XElement(pf + "xMun", objemit.Xmun.ToString()),
                                                                  new XElement(pf + "UF", objemit.Uf.ToString()),
                                                                  new XElement(pf + "CEP", objemit.Cep.ToString()),
                                                                  new XElement(pf + "cPais", objemit.Cpais.ToString()),
                                                                  new XElement(pf + "xPais", objemit.Xpais.ToString()),
                                                                  (objemit.Fone != null ? new XElement(pf + "fone", objemit.Fone.ToString()) : null)),
                                                  (objemit.Ie != null ? new XElement(pf + "IE", objemit.Ie.ToString()) : null),
                                                  (objemit.Iest != null ? new XElement(pf + "IEST", objemit.Iest.ToString()) : null),
                                                  (objemit.Im != null ? new XElement(pf + "IM", objemit.Im.ToString()) : null),
                                                  (objemit.Cnae != null ? new XElement(pf + "CNAE", objemit.Cnae.ToString()) : null),
                                                  new XElement(pf + "CRT", objemit.CRT.ToString()))); // NFe_2.0

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Emit - " + x.Message);
                    }
                    #endregion

                    #region Dest
                    XContainer condest;
                    try
                    {
                        belDest objdest = i[2] as belDest;
                        #region XML_Dest
                        objdest.Ie = (objdest.Ie == null ? "" : objdest.Ie);

                        condest = (new XElement(pf + "dest",
                                                  (objdest.Cnpj == "EXTERIOR" ? new XElement(pf + "CNPJ") :
                                                     (objdest.Cnpj != null ? new XElement(pf + "CNPJ", objdest.Cnpj) :
                                                                            new XElement(pf + "CPF", objdest.Cpf))),
                                                  new XElement(pf + "xNome", objdest.Xnome.ToString()),
                                                  new XElement(pf + "enderDest",
                                                      new XElement(pf + "xLgr", objdest.Xlgr.ToString()),
                                                      new XElement(pf + "nro", (objdest.Nro != null ? objdest.Nro.ToString() : "0")),
                                                      (objdest.Xcpl != null ? new XElement(pf + "xCpl", objdest.Xcpl.ToString()) : null),
                                                      new XElement(pf + "xBairro", objdest.Xbairro.ToString()),
                                                      new XElement(pf + "cMun", objdest.Cmun.ToString()),
                                                      new XElement(pf + "xMun", objdest.Xmun.ToString()),
                                                      new XElement(pf + "UF", objdest.Uf.ToString()),
                                                      (objdest.Cep != null ? new XElement(pf + "CEP", objdest.Cep.ToString()) : null),
                                                      new XElement(pf + "cPais", objdest.Cpais.ToString()),
                                                      (objdest.Xpais != null ? new XElement(pf + "xPais", objdest.Xpais.ToString()) : null),
                                                     (objdest.Fone != null ? new XElement(pf + "fone", objdest.Fone.ToString()) : null)),
                                                  ((objdest.Ie != null) ? (objdest.Ie != "EXTERIOR" ? new XElement(pf + "IE", objdest.Ie.ToString()) : new XElement(pf + "IE")) : null), //Claudinei - o.s. sem - 11/02/2010
                                                  (objdest.Isuf != null ? new XElement(pf + "ISUF", objdest.Isuf.ToString()) : null)));


                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Dest - " + x.Message);
                    }

                    #endregion

                    #region Det
                    List<XElement> lcondet = new List<XElement>();
                    try
                    {
                        List<belDet> objdet = new List<belDet>();
                        objdet = i[4] as List<belDet>;
                        #region XML_Detalhes

                        foreach (var det in objdet)
                        {
                            XElement condet = (new XElement(pf + "det", new XAttribute("nItem", det.Nitem),
                                               new XElement(pf + "prod",
                                                   (det.belProd.Cprod != null ? new XElement(pf + "cProd", det.belProd.Cprod.ToString()) : null),
                                                   (det.belProd.Cean != null ? new XElement(pf + "cEAN", det.belProd.Cean.ToString()) : null),
                                                   (det.belProd.Xprod != null ? new XElement(pf + "xProd", det.belProd.Xprod.ToString()) : null),
                                                   (det.belProd.Ncm != null ? new XElement(pf + "NCM", det.belProd.Ncm.ToString()) : new XElement(pf + "NCM", det.belProd.Ncm = "00000000")), //Claudinei - o.s. 24200 - 01/03/2010
                                                   (det.belProd.Extipi != null ? new XElement(pf + "EXTIPI", det.belProd.Extipi.ToString()) : null),
                                                   (det.belProd.Genero != null ? new XElement(pf + "genero", det.belProd.Genero.ToString()) : null),
                                                   (det.belProd.Cfop != null ? new XElement(pf + "CFOP", det.belProd.Cfop.ToString()) : null),
                                                   (det.belProd.Ucom != null ? new XElement(pf + "uCom", det.belProd.Ucom.ToString()) : null),
                                                   (det.belProd.Qcom != null ? new XElement(pf + "qCom", det.belProd.Qcom.ToString("#0.0000").Replace(",", ".")) : null),
                                                   (det.belProd.Vuncom != null ? new XElement(pf + "vUnCom", det.belProd.Vuncom.ToString("#0.0000").Replace(",", ".")) : null),
                                                   (det.belProd.Vprod != null ? new XElement(pf + "vProd", det.belProd.Vprod.ToString("#0.00").Replace(",", ".")) : null),
                                                   (det.belProd.Ceantrib != null ? new XElement(pf + "cEANTrib", det.belProd.Ceantrib.ToString().PadLeft(8, '0')) : null),
                                                   (det.belProd.Utrib != null ? new XElement(pf + "uTrib", det.belProd.Utrib.ToString()) : null),
                                                   (det.belProd.Qtrib != null ? new XElement(pf + "qTrib", det.belProd.Qtrib.ToString("#0.0000").Replace(",", ".")) : null),
                                                   (det.belProd.Vuntrib != null ? new XElement(pf + "vUnTrib", det.belProd.Vuntrib.ToString("#0.0000").Replace(",", ".")) : null),
                                                   ((det.belProd.Vfrete != null) && (det.belProd.Vfrete != 0) ? new XElement(pf + "vFrete", det.belProd.Vfrete.ToString("#0.00").Replace(",", ".")) : null),
                                                   ((det.belProd.Vseg != null) && (det.belProd.Vseg != 0) ? new XElement(pf + "vSeg", det.belProd.Vseg.ToString("#0.00").Replace(",", ".")) : null),
                                                   (((det.belProd.Vdesc != null) && (det.belProd.Vdesc != 0)) ? new XElement(pf + "vDesc", det.belProd.Vdesc.ToString("#0.00").Replace(",", ".")) : null),
                                                   ((det.belProd.VOutro != null) && (det.belProd.VOutro != 0) ? new XElement(pf + "vOutro", det.belProd.VOutro.ToString("#0.00").Replace(",", ".")) : null), //NFe_2.0 
                                                   (det.belProd.IndTot != null) ? new XElement(pf + "indTot", det.belProd.IndTot.ToString()) : null), //NFe_2.0

                                               new XElement(pf + "imposto",

                                                   //---------------ICMS-----------------//

                                                   new XElement(pf + "ICMS",

                                                       //-------------ICMS00-------------//

                                                       (det.belImposto.belIcms.belIcms00 != null ?
                                                       new XElement(pf + "ICMS00",
                                                            (det.belImposto.belIcms.belIcms00.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms00.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms00.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms00.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms00.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms00.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms00.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms00.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms00.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms00.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms00.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms00.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS10-------------//

                                                        (det.belImposto.belIcms.belIcms10 != null ?
                                                        new XElement(pf + "ICMS10",
                                                            (det.belImposto.belIcms.belIcms10.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms10.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms10.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms10.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms10.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms10.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms10.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms10.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms10.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 11/03/2010
                                                            (det.belImposto.belIcms.belIcms10.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms10.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms10.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms10.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms10.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS20-------------//

                                                        (det.belImposto.belIcms.belIcms20 != null ?
                                                        new XElement(pf + "ICMS20",
                                                            (det.belImposto.belIcms.belIcms20.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms20.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms20.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms20.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms20.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms20.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms20.Predbc != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms20.Predbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms20.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms20.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms20.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms20.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms20.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms20.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS30-------------//

                                                        (det.belImposto.belIcms.belIcms30 != null ?
                                                        new XElement(pf + "ICMS30",
                                                            (det.belImposto.belIcms.belIcms30.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms30.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms30.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms30.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms30.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms30.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms30.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms30.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 12/03/2010
                                                            (det.belImposto.belIcms.belIcms30.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms30.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms30.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms30.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms30.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms30.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms30.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms30.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS40-------------//

                                                        (det.belImposto.belIcms.belIcms40 != null ?
                                                        new XElement(pf + "ICMS40",
                                                            (det.belImposto.belIcms.belIcms40.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms40.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms40.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms40.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms40.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms40.Vicms.ToString()) : null), //NFe_2.0
                                                            (det.belImposto.belIcms.belIcms40.motDesICMS != null ? new XElement(pf + "motDesICMS", det.belImposto.belIcms.belIcms40.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS41-------------//

                                                        (det.belImposto.belIcms.belIcms41 != null ?
                                                        new XElement(pf + "ICMS41",
                                                            (det.belImposto.belIcms.belIcms41.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms41.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms41.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms41.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms41.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms41.Vicms.ToString()) : null),//NFe_2.0
                                                            (det.belImposto.belIcms.belIcms41.motDesICMS != null ? new XElement(pf + "motDesICMS", det.belImposto.belIcms.belIcms41.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS50-------------//

                                                        (det.belImposto.belIcms.belIcms50 != null ?
                                                        new XElement(pf + "ICMS50",
                                                            (det.belImposto.belIcms.belIcms50.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms50.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms50.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms50.Cst.ToString()) : null),
                                                                (det.belImposto.belIcms.belIcms50.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms50.Vicms.ToString()) : null),//NFe_2.0
                                                            (det.belImposto.belIcms.belIcms50.motDesICMS != null ? new XElement(pf + "motDesICMS", det.belImposto.belIcms.belIcms50.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS51-------------//

                                                        (det.belImposto.belIcms.belIcms51 != null ?
                                                        new XElement(pf + "ICMS51",
                                                            (det.belImposto.belIcms.belIcms51.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms51.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms51.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms51.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms51.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms51.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms51.Predbc != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms51.Predbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms51.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms51.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms51.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms51.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms51.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms51.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS60-------------//

                                                        (det.belImposto.belIcms.belIcms60 != null ?
                                                        new XElement(pf + "ICMS60",//Danner - o.s. sem - 12/03/2010
                                                            (det.belImposto.belIcms.belIcms60.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms60.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms60.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms60.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms60.Vbcst != null ? new XElement(pf + "vBCSTRet", det.belImposto.belIcms.belIcms60.Vbcst.ToString("#0.00").Replace(",", ".")) : null),//NFe_2.0 - Mudança de nome de Tag
                                                            (det.belImposto.belIcms.belIcms60.Vicmsst != null ? new XElement(pf + "vICMSSTRet", det.belImposto.belIcms.belIcms60.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),//NFe_2.0 Mudança de nome de Tag

                                                        //-------------ICMS70-------------//

                                                        (det.belImposto.belIcms.belIcms70 != null ?
                                                        new XElement(pf + "ICMS70",
                                                            (det.belImposto.belIcms.belIcms70.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms70.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms70.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms70.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Predbc != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms70.Predbc.ToString("#0.00").Replace(',', '.')) : null), //Danner - o.s. 24091 - 06/02/2010
                                                            (det.belImposto.belIcms.belIcms70.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms70.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms70.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms70.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms70.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms70.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 12/03/2010
                                                            (det.belImposto.belIcms.belIcms70.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms70.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms70.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms70.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms70.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS90-------------//

                                                        (det.belImposto.belIcms.belIcms90 != null ?
                                                        new XElement(pf + "ICMS90",
                                                            (det.belImposto.belIcms.belIcms90.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms90.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms90.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms90.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms90.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Predbc != 0 ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms90.Predbc.ToString("#0.00").Replace(',', '.')) : null), //Danner - o.s. 24091 - 06/02/2010 //Claudinei - o.s. sem - 24/02/2010
                                                            (det.belImposto.belIcms.belIcms90.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms90.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms90.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms90.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms90.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. 24076 - 01/02/2010
                                                            (det.belImposto.belIcms.belIcms90.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms90.Predbcst.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. 24076 - 01/02/2010
                                                            (det.belImposto.belIcms.belIcms90.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms90.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms90.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms90.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                       //-------------ICMSSN101--------------//
                                                       (det.belImposto.belIcms.belICMSSN101 != null ?
                                                            new XElement(pf + "ICMSSN101",
                                                               (det.belImposto.belIcms.belICMSSN101.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN101.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN101.CSOSN.ToString()),
                                                               (det.belImposto.belIcms.belICMSSN101.pCredSN != null ? new XElement(pf + "pCredSN", det.belImposto.belIcms.belICMSSN101.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN101.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.belImposto.belIcms.belICMSSN101.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN102--------------//
                                                       (det.belImposto.belIcms.belICMSSN102 != null ?
                                                            new XElement(pf + "ICMSSN102",
                                                               (det.belImposto.belIcms.belICMSSN102.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN102.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN102.CSOSN.ToString())) : null),

                                                      //-------------ICMSSN201--------------//
                                                       (det.belImposto.belIcms.belICMSSN201 != null ?
                                                            new XElement(pf + "ICMSSN201",
                                                               (det.belImposto.belIcms.belICMSSN201.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN201.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN201.CSOSN.ToString()),
                                                               (det.belImposto.belIcms.belICMSSN201.modBCST != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belICMSSN201.modBCST.ToString()) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pMVAST != null ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belICMSSN201.pMVAST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pRedBCST != null ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belICMSSN201.pRedBCST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.vBCST != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belICMSSN201.vBCST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pICMSST != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belICMSSN201.pICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.vICMSST != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belICMSSN201.vICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pCredSN != null ? new XElement(pf + "pCredSN", det.belImposto.belIcms.belICMSSN201.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.belImposto.belIcms.belICMSSN201.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN500--------------//
                                                       (det.belImposto.belIcms.belICMSSN500 != null ?
                                                            new XElement(pf + "ICMSSN500",
                                                               (det.belImposto.belIcms.belICMSSN500.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN500.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN500.CSOSN.ToString()),
                                                               (det.belImposto.belIcms.belICMSSN500.vBCSTRet != null ? new XElement(pf + "vBCSTRet", det.belImposto.belIcms.belICMSSN500.vBCSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN500.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", det.belImposto.belIcms.belICMSSN500.vICMSSTRet.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN900--------------//
                                                       (det.belImposto.belIcms.belICMSSN900 != null ?
                                                            new XElement(pf + "ICMSSN900",
                                                               (det.belImposto.belIcms.belICMSSN900.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN900.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN900.CSOSN.ToString())) : null)),
                                //(det.belImposto.belIcms.belICMSSN900.modBC != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belICMSSN900.modBC.ToString()) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vBC != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belICMSSN900.vBC.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pRedBC != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belICMSSN900.pRedBC.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pICMS != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belICMSSN900.pICMS.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vICMS != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belICMSSN900.vICMS.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.modBCST != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belICMSSN900.modBCST.ToString()) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pMVAST != null ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belICMSSN900.pMVAST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pRedBCST != null ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belICMSSN900.pRedBCST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vBCST != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belICMSSN900.vBCST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pICMSST != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belICMSSN900.pICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vICMSST != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belICMSSN900.vICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vBCSTRet != null ? new XElement(pf + "vBCSTRet", det.belImposto.belIcms.belICMSSN900.vBCSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", det.belImposto.belIcms.belICMSSN900.vICMSSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pCredSN != null ? new XElement(pf + "pCredSN", det.belImposto.belIcms.belICMSSN900.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.belImposto.belIcms.belICMSSN900.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null)),

                                        //---------------IPI-------------//
                                            (det.belImposto.belIpi != null ?
                                            new XElement(pf + "IPI",
                                                (det.belImposto.belIpi.Clenq != null ?
                                                    new XElement(pf + "clEnq", det.belImposto.belIpi.Clenq.ToString()) : null),
                                                (det.belImposto.belIpi.Cnpjprod != null ?
                                                    new XElement(pf + "CNPJProd", det.belImposto.belIpi.Cnpjprod.ToString()) : null),
                                                (det.belImposto.belIpi.Cselo != null ?
                                                    new XElement(pf + "cSelo", det.belImposto.belIpi.Cselo.ToString()) : null),
                                                (det.belImposto.belIpi.Qselo != null ?
                                                    new XElement(pf + "qSelo", det.belImposto.belIpi.Qselo.ToString()) : null),
                                                (det.belImposto.belIpi.Cenq != null ?
                                                    new XElement(pf + "cEnq", det.belImposto.belIpi.Cenq.ToString()) : null),

                                                //-----------IPITrib-----------//    

                                                (det.belImposto.belIpi.belIpitrib != null ?
                                                new XElement(pf + "IPITrib",
                                                    (det.belImposto.belIpi.belIpitrib.Cst != null ? new XElement(pf + "CST", det.belImposto.belIpi.belIpitrib.Cst.ToString()) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIpi.belIpitrib.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Qunid != null ? new XElement(pf + "qUnid", det.belImposto.belIpi.belIpitrib.Qunid.ToString()) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Vunid != 0 ? new XElement(pf + "vUnid", det.belImposto.belIpi.belIpitrib.Vunid.ToString("#0.0000").Replace(",", ".")) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Pipi != null ? new XElement(pf + "pIPI", det.belImposto.belIpi.belIpitrib.Pipi.ToString("#0.00").Replace(",", ".")) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Vipi != null ? new XElement(pf + "vIPI", det.belImposto.belIpi.belIpitrib.Vipi.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                //-----------IPINT-----------//

                                                (det.belImposto.belIpi.belIpint != null ?
                                                new XElement(pf + "IPINT",
                                                    (det.belImposto.belIpi.belIpint != null ? new XElement(pf + "CST", det.belImposto.belIpi.belIpint.Cst.ToString()) : null)) : null)) : null),




                                       //--------------II--------------//             
                                       (det.belImposto.belIi != null ?
                                       new XElement(pf + "II",
                                           (det.belImposto.belIi.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIi.Vbc.ToString("#0.00").Replace(',', '.')) : null),
                                           (det.belImposto.belIi.Vdespadu != null ? new XElement(pf + "vDespAdu", det.belImposto.belIi.Vdespadu.ToString()) : null),
                                           (det.belImposto.belIi.Vii != null ? new XElement(pf + "vII", det.belImposto.belIi.Vii.ToString("0.00").Replace(',', '.')) : null),
                                           (det.belImposto.belIi.Viof != null ? new XElement(pf + "vIOF", det.belImposto.belIi.Viof.ToString("#0.00").Replace(',', '.')) : null)) : null),



                               //----------------PIS------------//

                                 (det.belImposto.belPis != null ?
                                 new XElement(pf + "PIS",

                                     //-----------PISAliq----------//

                                     (det.belImposto.belPis.belPisaliq != null ?
                                     new XElement(pf + "PISAliq",
                                         (det.belImposto.belPis.belPisaliq.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisaliq.Cst.ToString()) : null),
                                         (det.belImposto.belPis.belPisaliq.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belPis.belPisaliq.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisaliq.Ppis != null ? new XElement(pf + "pPIS", det.belImposto.belPis.belPisaliq.Ppis.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisaliq.Vpis != null ? new XElement(pf + "vPIS", det.belImposto.belPis.belPisaliq.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                    //-----------PISQtde-----------//                               
                                     (det.belImposto.belPis.belPisqtde != null ?
                                     new XElement(pf + "PISQtde",
                                         (det.belImposto.belPis.belPisqtde.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisqtde.Cst.ToString()) : null),
                                         (det.belImposto.belPis.belPisqtde.Qbcprod != 0 ? new XElement(pf + "qBCProd", det.belImposto.belPis.belPisqtde.Qbcprod.ToString()) : null),
                                         (det.belImposto.belPis.belPisqtde.Valiqprod != null ? new XElement(pf + "vAliqProd", det.belImposto.belPis.belPisqtde.Valiqprod.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisqtde.Vpis != null ? new XElement(pf + "vPIS", det.belImposto.belPis.belPisqtde.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                     //----------PISNT------------//

                                     (det.belImposto.belPis.belPisnt != null ?
                                     new XElement(pf + "PISNT",
                                         (det.belImposto.belPis.belPisnt.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisnt.Cst.ToString()) : null)) : null),

                                     //----------PISOutr-----------//

                                     (det.belImposto.belPis.belPisoutr != null ?
                                     new XElement(pf + "PISOutr",
                                         (det.belImposto.belPis.belPisoutr.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisoutr.Cst.ToString()) : null),
                                         (det.belImposto.belPis.belPisoutr.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belPis.belPisoutr.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisoutr.Ppis != null ? new XElement(pf + "pPIS", det.belImposto.belPis.belPisoutr.Ppis.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisoutr.Qbcprod != 0 ? new XElement(pf + "qBCProd", det.belImposto.belPis.belPisoutr.Qbcprod.ToString()) : null),
                                         (det.belImposto.belPis.belPisoutr.Valiqprod != 0 ? new XElement(pf + "vAliqProd", det.belImposto.belPis.belPisoutr.Valiqprod.ToString("#0.0000").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisoutr.Vpis != null ? new XElement(pf + "vPIS", det.belImposto.belPis.belPisoutr.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null)) : null),







                                 //---------------COFINS---------------//
                                 (det.belImposto.belCofins != null ?
                                 new XElement(pf + "COFINS",

                                     //-----------COFINSAliq------------//

                                     (det.belImposto.belCofins.belCofinsaliq != null ?
                                     new XElement(pf + "COFINSAliq",
                                         new XElement(pf + "CST", det.belImposto.belCofins.belCofinsaliq.Cst.ToString()),
                                         new XElement(pf + "vBC", det.belImposto.belCofins.belCofinsaliq.Vbc.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "pCOFINS", det.belImposto.belCofins.belCofinsaliq.Pcofins.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.belImposto.belCofins.belCofinsaliq.Vcofins.ToString("#0.00").Replace(",", "."))) : null),

                                     //------------COFINSQtde------------//

                                     (det.belImposto.belCofins.belCofinsqtde != null ?
                                     new XElement(pf + "COFINSQtde",
                                         new XElement(pf + "CST", det.belImposto.belCofins.belCofinsqtde.Cst.ToString()),
                                         new XElement(pf + "pBCProd", det.belImposto.belCofins.belCofinsqtde.Qbcprod.ToString()),
                                         new XElement(pf + "vAliqProd", det.belImposto.belCofins.belCofinsqtde.Valiqprod.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.belImposto.belCofins.belCofinsqtde.Vcofins.ToString("#0.00").Replace(",", "."))) : null),

                                     //------------COFINSNT--------------//

                                     (det.belImposto.belCofins.belCofinsnt != null ?
                                     new XElement(pf + "COFINSNT",
                                         (det.belImposto.belCofins.belCofinsnt.Cst != null ? new XElement(pf + "CST", det.belImposto.belCofins.belCofinsnt.Cst.ToString()) : null)) : null),

                                     //------------COFINSOutr--------------//

                                     (det.belImposto.belCofins.belCofinsoutr != null ?
                                     new XElement(pf + "COFINSOutr",
                                         new XElement(pf + "CST", det.belImposto.belCofins.belCofinsoutr.Cst.ToString()),
                                         new XElement(pf + "vBC", det.belImposto.belCofins.belCofinsoutr.Vbc.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "pCOFINS", det.belImposto.belCofins.belCofinsoutr.Pcofins.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.belImposto.belCofins.belCofinsoutr.Vcofins.ToString("#0.00").Replace(",", "."))) : null)) : null),


                                 //----------------ISSQN-----------------//

                                 (det.belImposto.belIss != null ?
                                 new XElement(pf + "ISSQN",
                                     (det.belImposto.belIss.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIss.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.belImposto.belIss.Valiq != null ? new XElement(pf + "vAliq", det.belImposto.belIss.Valiq.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.belImposto.belIss.Vissqn != null ? new XElement(pf + "vISSQN", det.belImposto.belIss.Vissqn.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.belImposto.belIss.Cmunfg != null ? new XElement(pf + "cMunFG", det.belImposto.belIss.Cmunfg.ToString()) : null),
                                     (det.belImposto.belIss.Clistserv != null ? new XElement(pf + "cListServ", det.belImposto.belIss.Clistserv.ToString()) : null),
                                     (det.belImposto.belIss.cSitTrib != null ? new XElement(pf + "cSitTrib", det.belImposto.belIss.cSitTrib.ToString()) : null)) : null)), // NFe_2.0 Tratar item


                                 //-----------INFADPROD-------------//

                                 (det.belInfadprod != null ?
                                 (det.belInfadprod.Infadprid != null ? new XElement(pf + "infAdProd", det.belInfadprod.Infadprid.ToString()) : null) : null))); //Danner - o.s. sem -21/12/2009

                            lcondet.Add(condet);
                        }
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Detalhes - " + x.Message);
                    }
                    #endregion

                    //Total
                    XContainer contotal;

                    try
                    {
                        belTotal objtotal = i[5] as belTotal;
                        #region XML_Total

                        contotal = (new XElement(pf + "total",
                                                (objtotal.belIcmstot != null ? new XElement(pf + "ICMSTot",
                                                      new XElement(pf + "vBC", objtotal.belIcmstot.Vbc.ToString("#0.00").Replace(",", ".")),//Danner - o.s. 24271 - 15/03/2010
                                                      new XElement(pf + "vICMS", objtotal.belIcmstot.Vicms.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vBCST", objtotal.belIcmstot.Vbcst.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vST", objtotal.belIcmstot.Vst.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vProd", objtotal.belIcmstot.Vprod.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vFrete", objtotal.belIcmstot.Vfrete.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vSeg", objtotal.belIcmstot.Vseg.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vDesc", objtotal.belIcmstot.Vdesc.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vII", objtotal.belIcmstot.Vii.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vIPI", objtotal.belIcmstot.Vipi.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vPIS", objtotal.belIcmstot.Vpis.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vCOFINS", objtotal.belIcmstot.Vcofins.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vOutro", objtotal.belIcmstot.Voutro.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vNF", objtotal.belIcmstot.Vnf.ToString("#0.00").Replace(",", "."))) : null),



                                                (objtotal.belIssqntot != null ? new XElement(pf + "ISSQNtot",
                                                    new XElement(pf + "vServ", objtotal.belIssqntot.Vserv.ToString("#0.00").Replace(",", ".")),
                                                    new XElement(pf + "vBC", objtotal.belIssqntot.Vbc.ToString("#0.00").Replace(",", ".")),
                                                    new XElement(pf + "vISS", objtotal.belIssqntot.Viss.ToString("#0.00").Replace(",", ".")),
                                                    (objtotal.belIssqntot.Vpis != 0 ? new XElement(pf + "vPIS", objtotal.belIssqntot.Vpis.ToString("#0.00").Replace(",", ".")) : null),
                                                    (objtotal.belIssqntot.Vcofins != 0 ? new XElement(pf + "vCOFINS", objtotal.belIssqntot.Vcofins.ToString("#0.00").Replace(",", ".")) : null)) : null)));
                        //(objtotal.belRetTrib != null ? new XElement(pf + "retTrib",
                        //                                   new XElement(pf + "vRetPIS", objtotal.belRetTrib.Vretpis.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vRetCOFINS", objtotal.belRetTrib.Vretcofins.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vRetCSLL", objtotal.belRetTrib.Vretcsll.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vBCIRRF", objtotal.belRetTrib.Vbcirrf.ToString("#0.00"),
                        //                                   new XElement(pf + "vIRRF", objtotal.belRetTrib.Virrf.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vBCRetPrev", objtotal.belRetTrib.Vbcretprev.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vRetPrev", objtotal.belRetTrib.Vretprev.ToString("#0.00").Replace(",", "."))) : null)));

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Total - " + x.Message);
                    }
                    //Fim - Total

                    //Frete
                    XContainer contransp;
                    belTransp objtransp;
                    try
                    {
                        objtransp = i[6] as belTransp;
                        #region XML_Transporte

                        contransp = (new XElement(pf + "transp",
                                                   new XElement(pf + "modFrete", objtransp.Modfrete.ToString()),
                                                   new XElement(pf + "transporta",
                                                       (objtransp.belTransportadora.Cnpj != null ? new XElement(pf + "CNPJ", objtransp.belTransportadora.Cnpj.ToString()) :
                                                                                                   (objtransp.belTransportadora.Cpf != null ? new XElement(pf + "CPF", objtransp.belTransportadora.Cpf.ToString()) : null)),
                                                       (objtransp.belTransportadora.Xnome != null ? new XElement(pf + "xNome", objtransp.belTransportadora.Xnome.ToString()) : null),
                                                       (objtransp.belTransportadora.Ie != null ? new XElement(pf + "IE", objtransp.belTransportadora.Ie.ToString()) : null),
                                                       (objtransp.belTransportadora.Xender != null ? new XElement(pf + "xEnder", objtransp.belTransportadora.Xender.ToString()) : null),
                                                       (objtransp.belTransportadora.Xmun != null ? new XElement(pf + "xMun", objtransp.belTransportadora.Xmun.ToString()) : null),
                                                       (objtransp.belTransportadora.Uf != null ? new XElement(pf + "UF", objtransp.belTransportadora.Uf.ToString()) : null)),
                (objtransp.belRetTransp != null ? (new XElement(pf + "retTransp",
                                                       new XElement(pf + "vServ", objtransp.belRetTransp.Vserv.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "vBCRet", objtransp.belRetTransp.Vbvret.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "pICMSRet", objtransp.belRetTransp.Picmsret.ToString()),
                                                       new XElement(pf + "vICMSRet", objtransp.belRetTransp.Vicmsret.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "CFOP", objtransp.belRetTransp.Cfop.ToString()),
                                                       new XElement(pf + "cMunFG", objtransp.belRetTransp.Cmunfg.ToString()))) : null),
                (objtransp.belVeicTransp != null ? (new XElement(pf + "veicTransp",
                                                       (objtransp.belVeicTransp.Placa != null ? new XElement(pf + "placa", objtransp.belVeicTransp.Placa.ToString()) : null),
                                                       (objtransp.belVeicTransp.Uf != null ? new XElement(pf + "UF", objtransp.belVeicTransp.Uf.ToString()) : null),
                                                       (objtransp.belVeicTransp.Rntc != null ? new XElement(pf + "RNTC", objtransp.belVeicTransp.Rntc.ToString()) : null))) : null),
                   (objtransp.belReboque != null ? new XElement(pf + "reboque",
                                                       (objtransp.belReboque.Placa != null ? new XElement(pf + "placa", objtransp.belReboque.Placa.ToString()) : null),
                                                       (objtransp.belReboque.Uf != null ? new XElement(pf + "UF", objtransp.belReboque.Uf.ToString()) : null),
                                                       (objtransp.belReboque.Rntc != null ? new XElement(pf + "RNTC", objtransp.belReboque.Rntc.ToString()) : null)) : null),
                       (objtransp.belVol != null ? new XElement(pf + "vol",
                                                       new XElement(pf + "qVol", objtransp.belVol.Qvol.ToString("#")),
                                                       new XElement(pf + "esp", objtransp.belVol.Esp.ToString()),
                                                       new XElement(pf + "marca", objtransp.belVol.Marca.ToString()),
                                                       (objtransp.belVol.Nvol != null ? new XElement(pf + "nVol", objtransp.belVol.Nvol.ToString()) : null),//Danner - o.s. 24385 - 26/04/2010
                                                       new XElement(pf + "pesoL", objtransp.belVol.PesoL.ToString("#0.000").Replace(",", ".")),
                                                       new XElement(pf + "pesoB", objtransp.belVol.PesoB.ToString("#0.000").Replace(",", "."))) : null),
                   (objtransp.belLacres != null ? new XElement(pf + "lacres",
                                                       new XElement(pf + "nLacre", "")) : null)));



                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Transporte - " + x.Message);
                    }
                    //Fim - Frete

                    //Duplicata
                    XContainer concobr;
                    belCobr objcob;
                    try
                    {
                        objcob = i[7] as belCobr;
                        belGerarXML BuscaConexao = new belGerarXML();
                        #region XML_Cobrança
                        concobr = (new XElement(pf + "cobr",
                                                  new XElement(pf + "fat",
                                                      new XElement(pf + "nFat", objcob.belFat.Nfat.ToString()),
                                                      (objcob.belFat.Vorig != 0 ? new XElement(pf + "vOrig", objcob.belFat.Vorig.ToString("#0.00").Replace(",", ".")) : null),
                                                      (objcob.belFat.Vdesc != null && objcob.belFat.Vdesc != 0 ? new XElement(pf + "vDesc", objcob.belFat.Vdesc.ToString("#0.00").Replace(",", ".")) : null),
                                                      (objcob.belFat.Vliq != 0 ? new XElement(pf + "vLiq", objcob.belFat.Vliq.ToString("#0.00").Replace(",", ".")) : null)),
                                                      (objcob.belFat.belDup != null ? from dup in objcob.belFat.belDup
                                                                                      select new XElement(pf + "dup", new XElement(pf + "nDup", dup.Ndup.ToString()),
                                                                                             new XElement(pf + "dVenc", dup.Dvenc.ToString("yyyy-MM-dd")),
                                                                                             (BuscaConexao.nm_Cliente != "LORENZON" ? new XElement(pf + "vDup", dup.Vdup.ToString("#0.00").Replace(",", ".")) : null)) : null)));

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Region XML_Cobrança - " + x.Message);
                    }
                    //Fim - Duplicata

                    //Obs
                    XContainer conobs;
                    belInfAdic objobs;
                    try
                    {
                        objobs = i[8] as belInfAdic;
                        #region XML_Obs
                        conobs = new XElement(pf + "infAdic",
                                                    (objobs.Infcpl != null ?
                                                    new XElement(pf + "infCpl", objobs.Infcpl.ToString()) : null));
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML,Regiao XML_Obs - " + x.Message);
                    }
                    //Fim - Obs

                    //Uniao dos Containers
                    try
                    {
                        concabec.Add(coninfnfe);
                        coninfnfe.Add(conide);
                        conide.AddAfterSelf(conemit);
                        conemit.AddAfterSelf(condest);
                        condest.AddAfterSelf(contotal);
                        contotal.AddAfterSelf(contransp);

                        if (concobr != null)
                        {
                            contransp.AddAfterSelf(concobr);
                            if (objobs.Infcpl != null)
                                concobr.AddAfterSelf(conobs);
                        }
                        else
                        {
                            if (objobs.Infcpl != null)
                                contransp.AddAfterSelf(conobs);
                        }

                        foreach (XElement x in lcondet)
                        {
                            contotal.AddBeforeSelf(x);
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na montagem do XML, União dos Containers - " + x.Message);
                    }
                    try
                    {
                        AssinaNFeXml Assinatura = new AssinaNFeXml();
                        string nfe = Assinatura.ConfigurarArquivo(concabec.ToString(), "infNFe", cert);
                        nfes.Add(nfe);
                        XElement xnfe = XElement.Parse(nfe);
                        XDocument xdocsalvanfesemlot = new XDocument(xnfe);

                        DirectoryInfo dPastaData = new DirectoryInfo(glob.LeRegConfig("PastaXmlEnvio").ToString() + "\\" + sNFe.Substring(5, 4));
                        if (!dPastaData.Exists) { dPastaData.Create(); }
                        if (sFormaEmiNFe.Equals("2"))
                        {
                            xdocsalvanfesemlot.Save(glob.LeRegConfig("PastaContingencia").ToString() + "\\" + sNFe.Replace("NFe", "") + "-nfe.xml");
                        }
                        else
                        {
                            xdocsalvanfesemlot.Save(glob.LeRegConfig("PastaXmlEnvio").ToString() + "\\" + sNFe.Substring(5, 4) + "\\" + sNFe.Replace("NFe", "") + "-nfe.xml"); // OS_25024
                        }
                        //StreamWriter swnfe = new StreamWriter(glob.LeRegWin("PastaXmlEnvio").ToString() + "\\" + sNFe.Replace("NFe", "") + "-nfe.xml");
                        //swnfe.Write(nfe);
                        //swnfe.Close();
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro ao assinar a nfe de sequencia " + sNota + x.Message);
                    }

                    iCount++;
                }

                //Concatenando as NFes.
                string sXmlComp = "";
                //Junta todos os XML's em texto 
                foreach (var i in nfes)
                {
                    sXmlComp = sXmlComp + i;
                }
                //Estou inserindo o enviNFe pois se eu transformar o arquivo xml assinado em xml e depois em testo usando um toString,
                //a assinatura se torna invalida. Então depois de assinado do trabalho em forma de testo como xml ateh envia-lo pra fazenda.                        
                sXmlfull = "<?xml version=\"1.0\" encoding=\"utf-8\"?><enviNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"2.00\"><idLote>" +
                            sNomeArq.Substring(7, 15) + "</idLote>" + sXmlComp + "</enviNFe>";

                //Grava
                StreamWriter sw = new StreamWriter(sPath);
                sw.Write(sXmlfull);
                sw.Close();

                #region Valida_Xml

                Globais getschema = new Globais();

                //string sXml = File.OpenText(sPath).ReadToEnd();

                XmlSchemaCollection myschema = new XmlSchemaCollection();

                XmlValidatingReader reader;

                //Danner - o.s. 23732 - 06/11/2009

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

                    reader = new XmlValidatingReader(sXmlfull, XmlNodeType.Element, context);

                    myschema.Add("http://www.portalfiscal.inf.br/nfe", getschema.LeRegConfig("PastaSchema") + "\\enviNFe_v2.00.xsd");

                    reader.ValidationType = ValidationType.Schema;

                    reader.Schemas.Add(myschema);

                    while (reader.Read())
                    {

                    }

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

                //Fim - Danner - o.s. 23732 - 06/11/2009

                #endregion

            }
            catch (Exception ex)
            {
                Conn.Close();
                throw ex;
            }
            finally { Conn.Close(); }
        }
コード例 #13
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public List<belDet> BuscaItem(string sEmp,
                                   string sNF, belDest objdest)
        {
            // belGerarXML BuscaConexao = new belGerarXML();
            //FbConnection Conn = BuscaConexao.Conn;
            List<belDet> dets = new List<belDet>();
            Globais LeRegWin = new Globais();
            string psNM_Banco = LeRegWin.LeRegConfig("BancoDados");

            bool bAgrupa = VerificaSeAgrupaItens(Conn);

            try
            {
                StringBuilder sSql = new StringBuilder();

                #region Campos do Select

                sSql.Append("Select ");
                sSql.Append("coalesce(opereve.st_servico,'') st_servico,movitem.cd_oper, coalesce(nf.st_soma_dev_tot_nf,'N') st_soma_dev_tot_nf, ");//NFe_2.0
                if ((psNM_Banco.ToUpper().IndexOf("COMERCIOC") == -1) && (psNM_Banco.ToUpper().IndexOf("CERAMICAC") == -1)) //SE FOR INDUSTRIA
                {
                    sSql.Append("coalesce(tpdoc.st_compoe_vl_totprod_nf,'A') st_compoe_vl_totprod_nf, ");
                }

                if (objbelGeraXml.nm_Cliente != "MOGPLAST")
                {
                    sSql.Append("case when empresa.st_codprodnfe = 'C' then ");
                    sSql.Append("produto.cd_prod else ");
                    sSql.Append("produto.cd_alter end cProd, ");
                }
                else
                {
                    sSql.Append("case when empresa.nm_empresa containing 'MOGPLAST' then ");
                    sSql.Append("produto.ds_detalhe ");
                    sSql.Append("else ");
                    sSql.Append("case when empresa.st_codprodnfe = 'C' then ");
                    sSql.Append("movitem.cd_prod else ");
                    sSql.Append("movitem.cd_alter end ");
                    sSql.Append("End cProd, ");
                }
                if (LeRegWin.LeRegConfig("CodBarrasXml") == "True")
                {
                    sSql.Append("produto.cd_barras cEAN, ");
                }
                else
                {
                    sSql.Append("produto.cd_alter cEAN, ");
                }
                if (objbelGeraXml.nm_Cliente != "NAVE_THERM")
                {
                    sSql.Append("movitem.ds_prod xProd, ");
                }
                else
                {
                    sSql.Append("case when produto.ds_prod_compl is not null then ");
                    sSql.Append("substring(produto.ds_prod_compl from 1 for 120) ");
                    sSql.Append("else ");
                    sSql.Append("produto.ds_prod ");
                    sSql.Append("end xProd, ");
                }
                sSql.Append("substring(clas_fis.ds_clasfis from 1 for 8) NCM, ");// Diego - 21/10 Lorenzon
                sSql.Append("movitem.cd_cfop CFOP, ");
                sSql.Append("unidades.cd_unfat uCom, "); // Diego - OS_ 25/08/10
                sSql.Append((bAgrupa == false ? "movitem.qt_prod qCom, " : "sum (movitem.qt_prod) qCom, "));   // Diego - OS_24870  
                sSql.Append((bAgrupa == false ? "movitem.vl_comprimento vl_comprimento, " : "sum (movitem.vl_comprimento) vl_comprimento, "));   // Diego - OS_25550  

                if (objdest.Uf != "EX")
                {
                    sSql.Append("movitem.vl_uniprod vUnCom, ");
                }
                else
                {
                    sSql.Append("(case when movitem.vl_uniprod_ii = 0 then movitem.vl_uniprod else movitem.vl_uniprod_ii end) vUnCom, ");
                }
                if (objdest.Uf == "EX") //DIEGO - OS_24730
                {
                    sSql.Append("(case when movitem.vl_uniprod_ii = 0 then movitem.vl_totbruto else (movitem.vl_uniprod_ii * movitem.qt_prod) end) vProd, ");
                }
                else
                {
                    sSql.Append(bAgrupa == false ? "movitem.vl_totbruto vProd, " : " sum(movitem.vl_totbruto) vProd, ");
                }//DIEGO - OS_24730 - FIM                
                sSql.Append(bAgrupa == false ? "movitem.vl_totliq vl_totliq," : "sum(movitem.vl_totliq) vl_totliq, "); // Diego 0S_24595                

                if (LeRegWin.LeRegConfig("CodBarrasXml") == "True")
                {
                    sSql.Append("produto.cd_barras cEANTrib, ");
                }
                else
                {
                    sSql.Append("produto.cd_alter cEANTrib, ");
                }
                if (bAgrupa)
                {
                    sSql.Append("sum(coalesce(movitem.vl_descsuframa,0) + coalesce(movitem.vl_desccofinssuframa,0) + coalesce(movitem.vl_descpissuframa,0)) vDescSuframa ,");
                }
                else
                {
                    sSql.Append("coalesce(movitem.vl_descsuframa,0) + coalesce(movitem.vl_desccofinssuframa,0) + coalesce(movitem.vl_descpissuframa,0) vDescSuframa ,");
                }
                sSql.Append("coalesce(nf.st_desc,'U')st_desc , ");
                sSql.Append("movitem.cd_tpunid uTrib, ");
                sSql.Append(bAgrupa == false ? "movitem.qt_prod qTrib, " : "sum(movitem.qt_prod) qTrib, ");
                sSql.Append(objdest.Uf != "EX" ? (bAgrupa == false ? "movitem.vl_uniprod vUnTrib, " : "sum(movitem.vl_uniprod) vUnTrib, ") : "movitem.vl_uniprod_ii vUnTrib, "); // DIEGO OS_24730
                sSql.Append("movitem.cd_sittrib CST, ");
                sSql.Append("movitem.vl_alicredicms pCredSN, ");//NFe_2.0
                sSql.Append("movitem.vl_credicms vCredICMSSN, ");//NFe_2.0
                if (objdest.Uf != "EX")
                {
                    sSql.Append(bAgrupa == false ? "" : "sum(");
                    sSql.Append("case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then ");

                    if (objbelGeraXml.nm_Cliente != "TORCETEX")
                    {
                        sSql.Append("case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then ");
                        sSql.Append("case when coalesce(nf.st_ipi,'N') = 'N' then ");
                        sSql.Append("CASE when (SELECT SUM(VL_TOTBRUTO) FROM movitem where ((movitem.cd_empresa = nf.cd_empresa) and (MOVITEM.cd_nfseq = NF.cd_nfseq))) > NF.vl_totprod then ");
                        sSql.Append("((case when movitem.vl_redbase <> 0 then ");
                        sSql.Append("case when  coalesce((select first 1 ST_ESTTERC from opereve where ((ST_ESTTERC = 'S') and ((TPDOC.cd_operval) containing cd_oper  ))),'N') = 'N' then ");
                        sSql.Append("(movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where ((movitem.cd_empresa = nf.cd_empresa) and (MOVITEM.cd_nfseq = NF.cd_nfseq))) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) )) - (((100-coalesce(movitem.vl_redbase, 100)) * (movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where ((movitem.cd_empresa = nf.cd_empresa) and (MOVITEM.cd_nfseq = NF.cd_nfseq))) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) )))/ 100) ");
                        sSql.Append("else ");
                        sSql.Append("movitem.vl_totliq - (((100-coalesce(movitem.vl_redbase, 100)) * movitem.vl_totliq)/ 100) ");
                        sSql.Append("end ");
                        sSql.Append("else ");
                        sSql.Append("movitem.vl_totliq ");
                        sSql.Append("end + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) "); //OS 25385
                        sSql.Append("else ");
                        sSql.Append("((case when movitem.vl_redbase <> 0 then ");
                        sSql.Append("movitem.vl_totliq - (((100-coalesce(movitem.vl_redbase, 100)) * movitem.vl_totliq)/ 100) ");
                        sSql.Append("else ");
                        sSql.Append("movitem.vl_totliq ");
                        sSql.Append("end + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) ");//25385
                        sSql.Append("end ");
                        sSql.Append("else ");
                        sSql.Append("(CASE when (SELECT SUM(VL_TOTBRUTO) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) > NF.vl_totprod then ");
                        sSql.Append("((case when movitem.vl_redbase <> 0 then ");
                        sSql.Append("(movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) )) - (((100-coalesce(movitem.vl_redbase, 100)) * (movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_EMPRESA = NF.CD_EMPRESA AND MOVITEM.cd_nfseq = NF.cd_nfseq) )))/ 100) ");
                        sSql.Append("else ");
                        sSql.Append("movitem.vl_totliq ");
                        sSql.Append("end + ");
                        sSql.Append("(case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) "); //25385
                        sSql.Append("else ");
                        sSql.Append("((case when movitem.vl_redbase <> 0 then ");
                        sSql.Append("movitem.vl_totliq - (((100-coalesce(movitem.vl_redbase, 100)) * movitem.vl_totliq)/ 100) ");
                        sSql.Append("else ");
                        sSql.Append("movitem.vl_totliq ");
                        sSql.Append("end + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) "); //25385
                        sSql.Append("end) + movitem.vl_ipi ");
                        sSql.Append("end ");
                    }
                    else
                    {
                        sSql.Append("case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then ");
                        sSql.Append("case when coalesce(nf.st_ipi,'N') = 'N' then ");
                        sSql.Append("((movitem.vl_totliq * movitem.vl_coefdesc) + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))  "); //25385
                        sSql.Append("else ");
                        sSql.Append("((movitem.vl_totliq * movitem.vl_coefdesc) + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end)) + movitem.vl_ipi "); //25385
                        sSql.Append("end ");
                    }

                    sSql.Append("end ");
                    sSql.Append("else ");
                    sSql.Append("nf.vl_baseicm ");
                    sSql.Append("end ");
                    sSql.Append(bAgrupa == false ? "" : ")");
                    sSql.Append("vBC, ");
                }
                else
                {
                    sSql.Append("movitem.vl_baseicms_ii vBC, "); // Diego OS_24730
                }
                sSql.Append("movitem.vl_bicmproprio_subst vBCProp, "); // Diego OS_25278

                sSql.Append("movitem.vl_aliicms pICMS, ");//Diego - OS_24730
                if (objbelGeraXml.nm_Cliente == "PAVAX")
                {
                    if (objdest.Uf == "EX")
                    {
                        sSql.Append("coalesce(MOVITEM.vl_icms_ii,0) vICMS, ");
                    }
                    else
                    {
                        sSql.Append("movitem.vl_icms vICMS, ");
                    }
                }
                else
                {
                    sSql.Append(bAgrupa == false ? "(movitem.vl_icms + movitem.vl_icmproprio_subst)vICMS, " : "sum(movitem.vl_icms + movitem.vl_icmproprio_subst) vICMS, ");
                } //Diego - OS_24730 - FIM
                sSql.Append((bAgrupa == false ? "" : "sum("));
                sSql.Append("case when tpdoc.st_nfcompl = 'N' then ");
                sSql.Append("coalesce(movitem.vl_bicmssubst, 0) ");
                sSql.Append("else ");
                sSql.Append("nf.VL_BICMSSU ");
                sSql.Append("end ");
                sSql.Append((bAgrupa == false ? "" : ")"));
                sSql.Append("vBCST, ");
                //sSql.Append("coalesce(movitem.vl_aliicms_subst, 0) pICMSST, ");
                sSql.Append("coalesce(icm.vl_aliinte, 0) pICMSST, ");
                sSql.Append((bAgrupa == false ? "" : "sum("));
                sSql.Append("case when tpdoc.st_nfcompl = 'N' then ");
                sSql.Append("coalesce(movitem.vl_icmretsubst, 0) ");
                sSql.Append("else ");
                sSql.Append("nf.VL_ICMSSUB ");
                sSql.Append("end ");
                sSql.Append((bAgrupa == false ? "" : ")"));
                sSql.Append("vICMSST, ");
                sSql.Append(bAgrupa == false ? "(100-coalesce(movitem.vl_redbase, 0)) pRedBC, " : "sum((100-coalesce(movitem.vl_redbase, 0))) pRedBC, ");
                sSql.Append("coalesce(icm.vl_alisubs, 0) pMVAST, ");
                sSql.Append("(100-coalesce(movitem.vl_redbase, 0)) pRedBCST, ");
                sSql.Append("coalesce(movitem.vl_aliipi, 0) pIPI, ");
                sSql.Append(bAgrupa == false ? "coalesce(movitem.vl_ipi, 0) vIPI, " : "sum(coalesce(movitem.vl_ipi, 0)) vIPI, ");
                sSql.Append(bAgrupa == false ? "nf.ds_anota infAdProd, " : "'' infAdProd, ");
                sSql.Append("coalesce(clas_fis.st_tributacao, '1') Tributa_ipi, ");
                sSql.Append("tpdoc.tp_doc, ");
                sSql.Append("case when tpdoc.tp_doc = 'NS' then ");
                sSql.Append("opereve.ST_CALCIPI_FA ");
                sSql.Append("else ");
                sSql.Append("opereve.st_ipi ");
                sSql.Append("end Calcula_IPI, ");
                sSql.Append("coalesce(opereve.st_hefrete, 'N') st_hefrete, ");
                sSql.Append("opereve.st_piscofins, ");
                sSql.Append(bAgrupa == false ? "coalesce(movitem.vl_frete, 0) vFrete, " : "sum(coalesce(movitem.vl_frete, 0)) vFrete, ");
                sSql.Append((bAgrupa == false ? "movitem.nr_lanc, " : "'' nr_lanc, "));
                if (objdest.Uf.Equals("EX"))
                {
                    sSql.Append("coalesce(movitem.vl_aliqcofins_cif , 0) vl_aliqcofins_suframa, "); //DIEGO - 24730 - 02/08
                    sSql.Append("coalesce(movitem.VL_ALIQPIS_CIF  , 0) vl_aliqpis_suframa ");
                }
                else
                {
                    sSql.Append("coalesce(empresa.vl_aliqcofins_suframa, 0) vl_aliqcofins_suframa, ");
                    sSql.Append("coalesce(empresa.vl_aliqpis_suframa, 0) vl_aliqpis_suframa ");
                }
                sSql.Append(", endentr.ds_endent xLgr, ");
                sSql.Append("endentr.nr_endent nro, ");
                sSql.Append("endentr.nm_bairroent xBairro, ");
                sSql.Append("endentr.nm_cident cMun, ");
                sSql.Append("endentr.cd_ufent UF ");
                sSql.Append(", listaserv.ds_codigo cListserv, ");
                sSql.Append("coalesce(movitem.vl_aliqserv, 0) vAliqISS, ");
                if (bAgrupa == false)
                {
                    sSql.Append("(movitem.vl_totliq * coalesce(movitem.vl_aliqserv, 0))/100 vIssqn, ");
                }
                else
                {
                    sSql.Append("sum((movitem.vl_totliq * coalesce(movitem.vl_aliqserv, 0))/100) vIssqn, ");
                }
                sSql.Append((bAgrupa == false ? "movitem.vl_totliq vBCISS, " : "sum(movitem.vl_totliq) vBCISS, "));
                sSql.Append("cidades.cd_municipio cMunFG ");
                if (objbelGeraXml.nm_Cliente == "TORCETEX")
                {
                    sSql.Append(", movitem.VL_COEFDESC, ");
                    sSql.Append("movitem.vl_cofins  vl_cofins, ");
                    sSql.Append("movitem.vl_pis vl_pis , ");
                    sSql.Append("((cast((cast((movitem.qt_prod * movitem.vl_uniprod) as numeric(15,4)) * movitem.vl_coefdesc) as numeric(15,2)) + movitem.vl_frete)) vl_basePisCofins ");
                }
                else
                {
                    sSql.Append(", movitem.VL_COEF,  ");
                    if (objdest.Uf.Equals("EX"))  //Diego - 02/08 - 24730
                    {
                        sSql.Append("movitem.VL_COFINS_CIF  vl_cofins, ");
                        sSql.Append("movitem.VL_PIS_CIF vl_pis , ");
                        sSql.Append("coalesce(movitem.VL_BASECOFINS_CIF, 0) vl_basePisCofins ");
                    }
                    else
                    {
                        sSql.Append((bAgrupa == false ? "movitem.vl_cofins  vl_cofins, " : " sum(movitem.vl_cofins)  vl_cofins, "));
                        sSql.Append((bAgrupa == false ? "movitem.vl_pis vl_pis , " : "sum(movitem.vl_pis) vl_pis , "));
                        if (bAgrupa == false)
                        {
                            sSql.Append("((cast((cast((movitem.qt_prod * movitem.vl_uniprod) as numeric(15,2))*movitem.vl_coef) as numeric(15,2)) + movitem.vl_frete)) vl_basePisCofins "); // Diego - 24730 - 02/08                            
                        }
                        else
                        {
                            sSql.Append("sum(((cast((cast((movitem.qt_prod * movitem.vl_uniprod) as numeric(15,2))* movitem.vl_coef) as numeric(15,2)) + movitem.vl_frete))) vl_basePisCofins "); // Diego - 24730 - 02/08
                        }
                    }
                }
                string sBanco = LeRegWin.LeRegConfig("BancoDados");
                if ((sBanco.ToUpper().IndexOf("COMERCIOC") == -1) && (sBanco.ToUpper().IndexOf("CERAMICAC") == -1)) //Claudinei - o.s. - 25/09/2009
                {
                    sSql.Append(", case when empresa.ST_RASTREABILIDADE = '1' ");
                    sSql.Append("then ");
                    sSql.Append("movitem.nr_lote ");
                    sSql.Append("else null ");
                    sSql.Append("end nr_lote ");
                    sSql.Append(", movitem.cd_pedcli, ");
                    sSql.Append("movitem.cd_prodcli ");
                }
                if (objbelGeraXml.nm_Cliente == "MARPA")
                {
                    sSql.Append(", nf.vl_desccomer Desconto_Valor ");
                    sSql.Append(", ((nf.vl_desccomer / nf.vl_totnf)*100) Desconto_Percentual ");
                }
                sSql.Append(", movitem.CD_SITTRIBCOF ");
                sSql.Append(", movitem.CD_SITTRIBIPI ");
                sSql.Append(", movitem.CD_SITTRIBPIS ");
                sSql.Append(", coalesce(movitem.vl_outras,'0') vOutro"); // NFe_2.0
                //sSql.Append(", case when opereve.st_tpoper = '0' then 1 else 0 end indTot "); // NFe_2.0
                //sSql.Append(", coalesce(tpdoc.st_compoe_vl_totprod_nf,'A')st_compoe_vl_totpro "); //NFe_2.0 OS_25346
                sSql.Append(", coalesce(opereve.st_tpoper,'0')st_tpoper ");//NFe_2.0 OS_25346
                sSql.Append(", coalesce(opereve.ST_ESTTERC,'N')ST_ESTTERC ");//NFe_2.0 OS_25346
                sSql.Append(", tpdoc.cd_operval ");
                sSql.Append(", coalesce(Empresa.st_imp_cdpedcli, 'N') st_imp_cdpedcli ");
                sSql.Append(",transpor.nm_trans Redespacho, ");
                sSql.Append("transpor.ds_endnor xLgrRedes, ");
                sSql.Append("transpor.nr_endnor nroRedes, ");
                sSql.Append("transpor.ds_bairronor xBairroRedes, ");
                sSql.Append("transpor.nm_cidnor cmunRedes, ");
                sSql.Append("transpor.cd_ufnor UFRedes ");
                #endregion

                #region Tabelas
                //Tabelas
                sSql.Append("From MOVITEM ");

                //Relacionamentos
                sSql.Append("inner join nf on (nf.cd_empresa = movitem.cd_empresa)");
                sSql.Append(" and ");
                sSql.Append("(nf.cd_nfseq = movitem.cd_nfseq) ");
                sSql.Append("inner join empresa on (empresa.cd_empresa = movitem.cd_empresa) ");
                sSql.Append("inner join unidades on (movitem.cd_tpunid = unidades.cd_tpunid) "); // Diego - OS_ 25/08/10
                sSql.Append("left join clas_fis on (clas_fis.cd_empresa = movitem.cd_empresa)");
                sSql.Append(" and ");
                sSql.Append("(clas_fis.cd_cf = movitem.cd_cf) ");
                sSql.Append("left join icm on (icm.cd_ufnor = nf.cd_ufnor) ");
                sSql.Append("And ");
                sSql.Append("(icm.cd_aliicms = movitem.cd_aliicms) ");
                sSql.Append("left join opereve on (opereve.cd_oper = movitem.cd_oper) ");
                sSql.Append("left join tpdoc on (tpdoc.cd_tipodoc = nf.cd_tipodoc) ");
                sSql.Append("left join produto ");
                sSql.Append("on (produto.cd_empresa = movitem.cd_empresa) ");
                sSql.Append("and ");
                sSql.Append("(produto.cd_prod = movitem.cd_prod) ");
                sSql.Append("left join linhapro ");
                sSql.Append("on (linhapro.cd_empresa = produto.cd_empresa) ");
                sSql.Append("and ");
                sSql.Append("(linhapro.cd_linha = produto.cd_linha) ");
                sSql.Append("left join listaserv ");
                sSql.Append("on (listaserv.nr_lanc = linhapro.nr_lanclistaserv) ");
                sSql.Append("inner join clifor ");
                sSql.Append("on (clifor.cd_clifor = nf.cd_clifor) ");
                sSql.Append("left join cidades ");
                sSql.Append("on (cidades.nm_cidnor = clifor.nm_cidnor) ");
                sSql.Append("and ");
                sSql.Append("(cidades.cd_ufnor = clifor.cd_ufnor) ");
                sSql.Append("inner join uf on (clifor.cd_ufnor = uf.cd_uf) ");//25385
                sSql.Append("left join endentr on (endentr.cd_cliente = nf.cd_clifor) ");
                sSql.Append("and ");
                sSql.Append(" (endentr.cd_endent = nf.cd_endent) ");
                if ((objbelGeraXml.nm_Cliente == "NAVE_THERM") || (objbelGeraXml.nm_Cliente == "MOGPLAST"))
                {
                    sSql.Append("left join produto on (produto.cd_empresa = movitem.cd_empresa) ");
                    sSql.Append("And ");
                    sSql.Append("(produto.cd_prod = movitem.cd_prod)");
                }
                sSql.Append("left join transpor on (transpor.cd_trans = nf.cd_redes) ");
                #endregion

                #region Where
                sSql.Append("Where ");
                sSql.Append("(movitem.cd_empresa ='");
                sSql.Append(sEmp);
                sSql.Append("')");
                sSql.Append(" and ");
                sSql.Append("(nf.cd_nfseq = '");
                sSql.Append(sNF);
                sSql.Append("') ");
                sSql.Append((bAgrupa == false ? "Order by movitem.nr_lanc" : ""));
                #endregion

                #region GrouBy

                if (bAgrupa)
                {
                    sSql.Append(" group by ");
                    sSql.Append("coalesce(opereve.st_servico,''), "); //NFe_2.0
                    sSql.Append("movitem.cd_oper, "); //NFe_2.0
                    sSql.Append("coalesce(nf.st_soma_dev_tot_nf,'N'), ");//NFe_2.0
                    if ((psNM_Banco.ToUpper().IndexOf("COMERCIOC") == -1) && (psNM_Banco.ToUpper().IndexOf("CERAMICAC") == -1)) //SE FOR INDUSTRIA
                    {
                        sSql.Append("coalesce(tpdoc.st_compoe_vl_totprod_nf,'A'), ");  //NFe_2.0
                    }
                    sSql.Append(" case when empresa.st_codprodnfe = 'C' then produto.cd_prod else produto.cd_alter end ");
                    if (LeRegWin.LeRegConfig("CodBarrasXml") == "True")
                    {
                        sSql.Append(" ,produto.cd_barras ");
                    }
                    else
                    {
                        sSql.Append(" ,produto.cd_alter ");
                    }
                    sSql.Append(" ,movitem.ds_prod ");
                    sSql.Append(" ,substring(clas_fis.ds_clasfis from 1 for 8)");
                    sSql.Append(" ,movitem.cd_cfop ");
                    sSql.Append(" ,unidades.cd_unfat ");
                    sSql.Append(" ,movitem.vl_uniprod");
                    if (LeRegWin.LeRegConfig("CodBarrasXml") == "True")
                    {
                        sSql.Append(" ,produto.cd_barras ");
                    }
                    else
                    {
                        sSql.Append(" ,produto.cd_alter ");
                    }
                    sSql.Append(" ,coalesce(nf.st_desc,'U') ");
                    sSql.Append(" ,movitem.cd_tpunid ");
                    sSql.Append(" ,movitem.cd_sittrib ");
                    sSql.Append(" ,movitem.vl_alicredicms "); //NFe_2.0
                    sSql.Append(" ,movitem.vl_credicms "); //NFe_2.0
                    sSql.Append(" ,movitem.vl_bicmproprio_subst ");
                    sSql.Append(" ,movitem.vl_aliicms ");
                    sSql.Append(" ,coalesce(icm.vl_aliinte, 0) ");//NFe_2.0
                    sSql.Append(" ,coalesce(icm.vl_alisubs, 0) ");
                    sSql.Append(" ,movitem.vl_redbase ");
                    sSql.Append(" ,coalesce(movitem.vl_aliipi, 0) ");
                    sSql.Append(" ,coalesce(clas_fis.st_tributacao, '1')");
                    sSql.Append(" ,tpdoc.tp_doc");
                    sSql.Append(" ,case when tpdoc.tp_doc = 'NS' then opereve.ST_CALCIPI_FA else opereve.st_ipi end ");
                    sSql.Append(" ,coalesce(opereve.st_hefrete, 'N')");
                    sSql.Append(" ,opereve.st_piscofins");
                    sSql.Append(" ,coalesce(empresa.vl_aliqcofins_suframa, 0) ");
                    sSql.Append(" ,coalesce(empresa.vl_aliqpis_suframa, 0) ");
                    sSql.Append(" ,endentr.ds_endent ");
                    sSql.Append(" ,endentr.nr_endent ");
                    sSql.Append(" ,endentr.nm_bairroent ");
                    sSql.Append(" ,endentr.nm_cident ");
                    sSql.Append(" ,endentr.cd_ufent");
                    sSql.Append(" ,listaserv.ds_codigo ");
                    sSql.Append(" ,coalesce(movitem.vl_aliqserv, 0) ");
                    sSql.Append(" ,cidades.cd_municipio ");
                    sSql.Append(" ,movitem.VL_COEF");
                    sSql.Append(" ,movitem.CD_SITTRIBCOF ");
                    sSql.Append(" ,movitem.CD_SITTRIBIPI ");
                    sSql.Append(" ,movitem.CD_SITTRIBPIS ");
                    sSql.Append(" ,coalesce(movitem.vl_outras,'0') "); //NFe_2.0
                    //sSql.Append(" ,case when opereve.st_tpoper = '0' then 1 else 0 end ");//NFe_2.0
                    sSql.Append(", coalesce(opereve.st_tpoper,'0')");//NFe_2.0 OS_25346
                    sSql.Append(", coalesce(opereve.ST_ESTTERC,'N')");//NFe_2.0 OS_25346
                    sSql.Append(" ,tpdoc.cd_operval ");
                    sSql.Append(" ,coalesce(Empresa.st_imp_cdpedcli, 'N')");
                    sSql.Append(" ,transpor.nm_trans ");
                    sSql.Append(" ,transpor.ds_endnor ");
                    sSql.Append(" ,transpor.nr_endnor ");
                    sSql.Append(" ,transpor.ds_bairronor ");
                    sSql.Append(" ,transpor.nm_cidnor ");
                    sSql.Append(" ,transpor.cd_ufnor");
                    sSql.Append((bAgrupa == false ? "" : " order by case when empresa.st_codprodnfe = 'C' then produto.cd_prod else produto.cd_alter end"));
                }
                #endregion

                string sNr_Lanc;
                string sql = "select max(nr_lanc) from movitem where (movitem.cd_empresa ='" +
                                                     sEmp +
                                                     "') and " +
                                                     "(movitem.cd_nfseq = '" +
                                                     sNF +
                                                     "') ";
                using (FbCommand cmd = new FbCommand(sql, Conn))
                {
                    //Conn.Open();
                    sNr_Lanc = cmd.ExecuteScalar().ToString();
                    //Conn.Close();
                }
                //Conn.Open();
                FbCommand cmdItem = new FbCommand(sSql.ToString(), Conn);
                cmdItem.ExecuteNonQuery();
                FbDataReader drIItem = cmdItem.ExecuteReader();
                int iSeqItem = 0;
                dTotPis = 0;
                dTotCofins = 0;

                //ITem
                while (drIItem.Read())
                {
                    int indTot = 1;
                    //indTot = (VerificaItemEntraTotalNf(drIItem["st_servico"].ToString(),
                    //                                  drIItem["cd_oper"].ToString(),
                    //                                  drIItem["st_soma_dev_tot_nf"].ToString(),
                    //                                  drIItem["st_compoe_vl_totprod_nf"].ToString(),
                    //                                  Conn) == true ? 1 : 0);

                    //OS_25346 INICIO

                    if ((psNM_Banco.ToUpper().IndexOf("COMERCIOC") == -1) && (psNM_Banco.ToUpper().IndexOf("CERAMICAC") == -1))
                    {
                        if (drIItem["st_compoe_vl_totprod_nf"].ToString().Equals("A")) //Verifica se ambos os produtos vão entrar no total da nota
                        {
                            indTot = 1;
                        }
                        else if (drIItem["st_compoe_vl_totprod_nf"].ToString().Equals("D")) // verifica se movimenta estoque terceiro!! S - SIM / N-NÃO
                        {
                            indTot = (drIItem["ST_ESTTERC"].ToString().Equals("S") ? 1 : 0);
                        }
                        else if (drIItem["st_compoe_vl_totprod_nf"].ToString().Equals("P"))
                        {
                            indTot = (drIItem["st_tpoper"].ToString().Equals("0") ? 1 : 0);  // verifica se representa faturamento!! 0- SIM - 1 -NÃO
                        }
                        //OS_25346 INICIO - FIM 
                    }
                    else
                    {
                        indTot = 1;
                    }

                    belDet objdet = new belDet();
                    belImposto objimp = new belImposto();
                    belProd objprod = new belProd();
                    iSeqItem++;
                    objdet.Nitem = Convert.ToDecimal(iSeqItem.ToString().Trim());
                    objprod.Cprod = TiraSimbolo(drIItem["cProd"].ToString().Trim(), "");
                    objprod.Cean = (Util.Util.IsNumeric(drIItem["cEAN"].ToString()) ? (Util.Util.ValidacEAN(drIItem["cEAN"].ToString()) ? drIItem["cEAN"].ToString() : "00000000") : "00000000");
                    if (objbelGeraXml.nm_Cliente == "ESTACAHC")
                    {
                        objprod.Xprod = drIItem["qCom"].ToString() + "  " + drIItem["xProd"].ToString().Trim();
                    }
                    else
                    {
                        objprod.Xprod = drIItem["xProd"].ToString().Trim();
                    }
                    if (drIItem["NCM"].ToString() != "")
                    {
                        objprod.Ncm = TiraSimbolo(drIItem["NCM"].ToString(), "");
                    }
                    objprod.Cfop = drIItem["CFOP"].ToString();
                    objprod.Ucom = TiraSimbolo(drIItem["uCom"].ToString(), "");
                    if (objbelGeraXml.nm_Cliente.Equals("ESTACAHC"))
                    {
                        decimal dqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()) * Convert.ToDecimal(drIItem["vl_coef"].ToString()), 4);
                        objprod.Qcom = dqCom;
                    }
                    else if (objbelGeraXml.nm_Cliente.Equals("MAD_STA_RITA"))
                    {
                        decimal dqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()), 4);
                        decimal dComprimento = Math.Round(Convert.ToDecimal(drIItem["vl_comprimento"].ToString()), 4);
                        if (dComprimento == 0)
                        {
                            objprod.Qcom = dqCom;
                        }
                        else
                        {
                            objprod.Qcom = dqCom * dComprimento;
                        }
                    }
                    else
                    {
                        decimal dqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()), 4); //Claudinei - o.s. 24248 - 26/03/2010
                        objprod.Qcom = dqCom;
                    }
                    decimal dvUnCom = Math.Round(Convert.ToDecimal(drIItem["vUnCom"].ToString()), 4); //Claudinei - o.s. 24248 - 26/03/2010
                    objprod.Vuncom = dvUnCom;
                    decimal dvProd = 0;
                    decimal vl_prodDesc = 0;
                    decimal vl_desconto = 0;
                    if (objbelGeraXml.nm_Cliente == "ESTACAHC") // Diego - OS_24595
                    {
                        dvProd = Math.Round(Convert.ToDecimal(drIItem["vl_totliq"].ToString()), 2);
                    }
                    else
                    {
                        dvProd = Math.Round(Convert.ToDecimal(drIItem["vProd"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                    }
                    if (objbelGeraXml.nm_Cliente == "TORCETEX")
                    {
                        vl_prodDesc = Math.Round(dvProd * Convert.ToDecimal(drIItem["VL_COEFDESC"].ToString()), 2); //OS_25339 - DIEGO
                    }
                    else
                    {
                        if (drIItem["st_desc"].ToString().Equals("U")) //25368
                        {
                            vl_prodDesc = dvProd; // Math.Round(dvProd * Convert.ToDecimal(drIItem["VL_COEF"].ToString()), 2);
                        }
                        else
                        {
                            vl_prodDesc = Math.Round(dvProd * Convert.ToDecimal(drIItem["VL_COEF"].ToString()), 2);
                        }
                    }
                    pbIndustri = bIndustrializacao(drIItem["cd_operval"].ToString(), Conn);

                    if (VerificaNotaComSuframa(sEmp, sNF, Conn)) //NFe_2.0
                    {
                        vl_desconto = Convert.ToDecimal(drIItem["vDescSuframa"].ToString());
                    }
                    else
                    {
                        vl_desconto = dvProd - vl_prodDesc;
                    }

                    if (vl_desconto == 0)
                    {
                        vl_desconto = BuscaDescTotal(sEmp, sNF, Conn);
                    }
                    if (drIItem["st_hefrete"].ToString() == "S")
                    {
                        dvProd = 0;
                    }
                    if (objbelGeraXml.nm_Cliente == "TORCETEX")//OS_25339 - DIEGO
                    {
                        objprod.Vprod = vl_prodDesc;
                    }
                    else
                    {
                        objprod.Vprod = dvProd;
                    }
                    objprod.Ceantrib = objprod.Cean;
                    objprod.Utrib = TiraSimbolo(drIItem["uTrib"].ToString(), "");
                    objprod.VOutro = Convert.ToDecimal(drIItem["vOutro"].ToString()); // NFe_2.0



                    objprod.IndTot = indTot;//Convert.ToInt16(drIItem["indTot"].ToString()); // NFe_2.0
                    if (objbelGeraXml.nm_Cliente.Equals("ESTACAHC"))
                    {
                        decimal dvqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()) * Convert.ToDecimal(drIItem["vl_coef"].ToString()), 4);
                        objprod.Qtrib = dvqCom;
                    }
                    else if (objbelGeraXml.nm_Cliente.Equals("MAD_STA_RITA"))
                    {
                        objprod.Qtrib = objprod.Qcom;
                    }
                    else
                    {
                        decimal dvqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()), 4); // o.s. 24248 - 26/03/2010
                        objprod.Qtrib = dvqCom;
                    }
                    decimal dvUnTrib = Math.Round(Convert.ToDecimal(drIItem["vUnTrib"].ToString()), 4); //o.s. 24248 - 26/03/2010
                    objprod.Vuntrib = dvUnTrib;
                    if (drIItem["vFrete"].ToString() != "0")
                    {
                        decimal dvFrete = Math.Round(Convert.ToDecimal(drIItem["vFrete"].ToString()), 2); // o.s. 24248 - 26/03/2010
                        objprod.Vfrete = dvFrete;
                    }
                    if (vl_desconto > 0)
                    {
                        objprod.Vdesc = vl_desconto;
                    }
                    objdet.belProd = objprod;

                    //Impostos

                    #region ICMS
                    belIcms objicms = new belIcms();
                    string sCST = drIItem["CST"].ToString();
                    string sSimplesNac = VerificaEmpresaSimplesNac(sEmp, Conn); // Diego - OS 24918 - 14/09/2010
                    decimal dvBC = 0;
                    decimal dvBCProp = 0; //25278
                    if (sSimplesNac == "N" || sSimplesNac == "")
                    {
                        dvBC = Math.Round(Convert.ToDecimal(drIItem["vBC"].ToString()), 2);
                        dvBCProp = Math.Round(Convert.ToDecimal(drIItem["vBCProp"].ToString()), 2); //25278

                    }
                    decimal dvICMS = Math.Round(Convert.ToDecimal(drIItem["vICMS"].ToString()), 2); //o.s. 24248 - 26/03/2010




                    if (!Util.Util.VerificaNovaST(sCST))
                    {
                        #region CST_ANTIGAS
                        switch (sCST.Substring(1, 2))
                        {
                            case "00":
                                {
                                    #region 00
                                    belIcms00 obj00 = new belIcms00();
                                    obj00.Orig = sCST.ToString().Substring(0, 1);
                                    obj00.Cst = sCST.ToString().Substring(1, 2);
                                    obj00.Modbc = "3";
                                    obj00.Vbc = dvBC;
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); //o.s. 24248 - 26/03/2010
                                    obj00.Picms = dpICMS;
                                    obj00.Vicms = dvICMS;
                                    objicms.belIcms00 = obj00;
                                    #endregion

                                    break;
                                }
                            case "10":
                                {
                                    #region 010
                                    belIcms10 obj10 = new belIcms10();
                                    obj10.Orig = sCST.ToString().Substring(0, 1);
                                    obj10.Cst = sCST.ToString().Substring(1, 2);
                                    obj10.Modbc = "0";
                                    obj10.Vbc = (dvBCProp == 0 ? (dvICMS == 0 ? 0 : dvBC) : dvBCProp); // 25278
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2);
                                    obj10.Picms = dpICMS;
                                    //dvICMS = (dvBC * dpICMS) / 100;
                                    obj10.Vicms = dvICMS;
                                    obj10.Modbcst = 4;
                                    decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2);
                                    obj10.Pmvast = dpMVAST;
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    obj10.Vbcst = dvBCST;
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2);
                                    obj10.Picmsst = dpICMSST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    obj10.Vicmsst = dvICMSST;
                                    objicms.belIcms10 = obj10;
                                    break;
                                    #endregion
                                }
                            case "20":
                                {
                                    #region 020
                                    belIcms20 obj20 = new belIcms20();
                                    obj20.Orig = sCST.ToString().Substring(0, 1);
                                    obj20.Cst = sCST.ToString().Substring(1, 2);
                                    obj20.Modbc = "3";
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBC == 100)
                                    {
                                        dpRedBC = 0;
                                    }
                                    obj20.Predbc = dpRedBC;
                                    obj20.Vbc = dvBC;
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj20.Picms = dpICMS;
                                    obj20.Vicms = dvICMS;
                                    objicms.belIcms20 = obj20;
                                    break;
                                    #endregion
                                }
                            case "30":
                                {
                                    #region 030
                                    belIcms30 obj30 = new belIcms30();
                                    obj30.Orig = sCST.ToString().Substring(0, 1);
                                    obj30.Cst = sCST.ToString().Substring(1, 2);
                                    obj30.Modbcst = 3;
                                    decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Pmvast = dpMVAST;
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Predbcst = dpRedBCST;
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Vbcst = dvBCST;
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Picmsst = dpICMSST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Vicmsst = dvICMSST;
                                    objicms.belIcms30 = obj30;
                                    break;
                                    #endregion
                                }
                            case "40":
                                {
                                    #region 040
                                    belIcms40 obj40 = new belIcms40();
                                    obj40.Orig = sCST.ToString().Substring(0, 1);
                                    obj40.Cst = sCST.ToString().Substring(1, 2);
                                    obj40.Vicms = dvICMS; // NFe_2.0
                                    obj40.motDesICMS = (VerificaNotaComSuframa(sEmp, sNF, Conn) == false ? 9 : 7); // NFe_2.0
                                    dvBC = 0;
                                    objicms.belIcms40 = obj40;
                                    break;
                                    #endregion
                                }
                            case "41":
                                {
                                    #region 041
                                    belIcms40 obj40 = new belIcms40();
                                    obj40.Orig = sCST.ToString().Substring(0, 1);
                                    obj40.Cst = sCST.ToString().Substring(1, 2);
                                    obj40.Vicms = dvICMS; // NFe_2.0
                                    obj40.motDesICMS = (VerificaNotaComSuframa(sEmp, sNF, Conn) == false ? 9 : 7); // NFe_2.0
                                    dvBC = 0;
                                    objicms.belIcms40 = obj40;
                                    break;
                                    #endregion
                                }
                            case "50":
                                {
                                    #region 050
                                    belIcms40 obj40 = new belIcms40();
                                    obj40.Orig = sCST.ToString().Substring(0, 1);
                                    obj40.Cst = sCST.ToString().Substring(1, 2);
                                    obj40.Vicms = dvICMS; // NFe_2.0
                                    obj40.motDesICMS = (VerificaNotaComSuframa(sEmp, sNF, Conn) == false ? 9 : 7); // NFe_2.0
                                    dvBC = 0;
                                    objicms.belIcms40 = obj40;
                                    break;
                                    #endregion
                                }
                            case "51":
                                {
                                    #region 051
                                    belIcms51 obj51 = new belIcms51();
                                    obj51.Orig = sCST.ToString().Substring(0, 1);
                                    obj51.Cst = sCST.ToString().Substring(1, 2);
                                    obj51.Modbc = "3";
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBC == 100)
                                    {
                                        dpRedBC = 0;
                                    }
                                    obj51.Predbc = dpRedBC;
                                    obj51.Vbc = 0;// Math.Round(Convert.ToDecimal(drIItem["vBC"].ToString()), 2); // DIEGO- OS_24591 - 26/06/2010 INICIO E FIM
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj51.Picms = dpICMS;
                                    obj51.Vicms = dvICMS;
                                    objicms.belIcms51 = obj51;
                                    break;
                                    #endregion
                                }
                            case "60":
                                {
                                    #region 060
                                    belIcms60 obj60 = new belIcms60();
                                    obj60.Orig = sCST.ToString().Substring(0, 1);
                                    obj60.Cst = sCST.ToString().Substring(1, 2);
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj60.Vbcst = dvBCST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj60.Vicmsst = dvICMSST;
                                    objicms.belIcms60 = obj60;
                                    break;
                                    #endregion
                                }

                            case "70":
                                {
                                    #region 070
                                    belIcms70 obj70 = new belIcms70();
                                    obj70.Orig = sCST.ToString().Substring(0, 1);
                                    obj70.Cst = sCST.ToString().Substring(1, 2);
                                    obj70.Modbc = "3";
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBC == 100)
                                    {
                                        dpRedBC = 0;
                                    }
                                    obj70.Predbc = dpRedBC;
                                    obj70.Vbc = dvBC;
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); // o.s. 24248 - 26/03/20103
                                    obj70.Picms = dpICMS;
                                    dvICMS = (dvBC * dpICMS) / 100;
                                    obj70.Vicms = dvICMS;
                                    obj70.Modbcst = 0;
                                    decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj70.Pmvast = dpMVAST;
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBCST == 100)
                                    {
                                        dpRedBCST = 0;
                                    }
                                    obj70.Predbcst = dpRedBCST;
                                    if (!drIItem["vBCST"].Equals(string.Empty))
                                    {
                                        decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                        obj70.Vbcst = dvBCST;
                                    }
                                    if (!drIItem["pICMSST"].Equals(string.Empty))
                                    {
                                        decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                        obj70.Picmsst = dpICMSST;
                                    }
                                    if (!drIItem["vICMSST"].Equals(string.Empty))
                                    {
                                        decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2); ;
                                        obj70.Vicmsst = dvICMSST;
                                    }
                                    objicms.belIcms70 = obj70;
                                    break;
                                    #endregion
                                }

                            case "90":
                                {
                                    #region 090
                                    belIcms90 obj90 = new belIcms90();
                                    obj90.Orig = sCST.ToString().Substring(0, 1);
                                    obj90.Cst = sCST.ToString().Substring(1, 2);
                                    obj90.Modbc = "3";
                                    dvBC = 0;
                                    obj90.Vbc = dvBC;
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpRedBC = 0;
                                    if (dpRedBC != 0)
                                    {
                                        obj90.Predbc = dpRedBC;
                                    }
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpICMS = 0;
                                    obj90.Picms = dpICMS;

                                    dvICMS = 0;
                                    obj90.Vicms = dvICMS;
                                    obj90.Modbcst = 3;
                                    if (drIItem["pMVAST"].ToString() != "0")
                                    {
                                        decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                        dpMVAST = 0;
                                        obj90.Pmvast = dpMVAST;
                                    }
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpRedBCST = 0;
                                    if (dpRedBCST != 0)
                                    {
                                        obj90.Predbcst = dpRedBCST;
                                    }
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dvBCST = 0;
                                    obj90.Vbcst = dvICMS;
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpICMSST = 0;
                                    obj90.Picmsst = dpICMSST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    dvICMSST = 0;
                                    obj90.Vicmsst = dvICMSST;
                                    objicms.belIcms90 = obj90;
                                    break;
                                    #endregion
                                }
                        }

                        #endregion
                    }
                    else
                    {
                        #region CTS_NOVAS
                        switch ((Util.Util.RetornaSTnovaAserUsada(sCST)))
                        {
                            case "101":
                                {
                                    #region 101
                                    belICMSSN101 obj101 = new belICMSSN101();
                                    obj101.orig = (objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj101.CSOSN = sCST.ToString();
                                    obj101.pCredSN = Math.Round(Convert.ToDecimal(drIItem["pCredSN"].ToString()), 2);//NFe_2.0
                                    obj101.vCredICMSSN = Math.Round(Convert.ToDecimal(drIItem["vCredICMSSN"].ToString()), 2); //NFe_2.0
                                    objicms.belICMSSN101 = obj101;
                                    #endregion
                                }
                                break;

                            case "102":
                                {
                                    #region 102
                                    belICMSSN102 obj102 = new belICMSSN102();
                                    obj102.orig = (objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj102.CSOSN = sCST.ToString();
                                    objicms.belICMSSN102 = obj102;
                                    #endregion

                                }
                                break;
                            case "201":
                                {
                                    #region 201
                                    belICMSSN201 obj201 = new belICMSSN201();
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2);
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2);
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);

                                    obj201.orig = (objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj201.CSOSN = sCST.ToString();
                                    obj201.modBCST = 3;
                                    obj201.pMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2);
                                    obj201.vBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    obj201.pICMSST = dpICMSST;
                                    obj201.vICMSST = dvICMSST;
                                    obj201.pCredSN = Math.Round(Convert.ToDecimal(drIItem["pCredSN"].ToString()), 2);//NFe_2.0
                                    obj201.vCredICMSSN = Math.Round(Convert.ToDecimal(drIItem["vCredICMSSN"].ToString()), 2); //NFe_2.0                                   
                                    if (dpRedBCST != 0)
                                    {
                                        obj201.pRedBCST = dpRedBCST;
                                    }
                                    objicms.belICMSSN201 = obj201;
                                    #endregion
                                }
                                break;
                            case "500":
                                {
                                    #region 500
                                    belICMSSN500 obj500 = new belICMSSN500();
                                    obj500.orig = (objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj500.CSOSN = sCST.ToString();
                                    decimal dvBCSTRet = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    decimal dvICMSSTRet = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    obj500.vBCSTRet = dvBCSTRet;
                                    obj500.vICMSSTRet = dvICMSSTRet;
                                    objicms.belICMSSN500 = obj500;
                                    #endregion
                                }
                                break;
                            case "900":
                                {
                                    #region 900
                                    belICMSSN900 obj900 = new belICMSSN900();
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2);
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2);
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    decimal dvBCSTRet = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    decimal dvICMSSTRet = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);

                                    obj900.orig = (objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj900.CSOSN = sCST.ToString();
                                    obj900.modBC = 3;
                                    obj900.vBC = dvBC;
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2);
                                    if (dpRedBC != 0)
                                    {
                                        obj900.pRedBC = dpRedBC;
                                    }
                                    obj900.vICMS = dvICMS;
                                    obj900.modBCST = 3;
                                    obj900.pMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2);
                                    if (dpRedBCST != 0)
                                    {
                                        obj900.pRedBCST = dpRedBCST;
                                    }
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    obj900.vBCST = dvBCST;
                                    obj900.pICMSST = dpICMSST;
                                    obj900.vICMSST = dvICMSST;
                                    obj900.vBCSTRet = dvBCSTRet;
                                    obj900.vICMSSTRet = dvICMSSTRet;
                                    obj900.pCredSN = Math.Round(Convert.ToDecimal(drIItem["pCredSN"].ToString()), 2);//NFe_2.0
                                    obj900.vCredICMSSN = Math.Round(Convert.ToDecimal(drIItem["vCredICMSSN"].ToString()), 2); //NFe_2.0                                    

                                    // Alteração feita por motivo de NFe para a Lorenzon

                                    obj900.modBC = null;
                                    obj900.vBC = null;
                                    obj900.pRedBC = null;
                                    obj900.pICMS = null;
                                    obj900.vICMS = null;
                                    obj900.modBCST = null;
                                    obj900.pMVAST = null;
                                    obj900.pRedBCST = null;
                                    obj900.vBCST = null;
                                    obj900.pICMSST = null;
                                    obj900.vICMSST = null;
                                    obj900.vBCSTRet = null;
                                    obj900.vICMSSTRet = null;
                                    obj900.pCredSN = null;//NFe_2.0
                                    obj900.vCredICMSSN = null; //NFe_2.0                                    

                                    objicms.belICMSSN900 = obj900;
                                    #endregion
                                }
                                break;
                        }
                        #endregion
                    }

                    if ((dvBC != 0) && (Convert.ToDecimal(drIItem["pICMS"].ToString()) != 0))
                    {
                        dTotbaseICMS += dvBC;
                        dTotValorICMS += dvICMS;
                    }
                    objimp.belIcms = objicms;
                    #endregion

                    #region IPI
                    belIpi objipi = new belIpi();
                    if (drIItem["CD_SITTRIBIPI"].ToString() == "")
                        throw new Exception("Situação Tributária do IPI está vazia na NF");

                    string sTributaIPI = drIItem["cd_sittribipi"].ToString().PadLeft(2, '0');
                    if ((sTributaIPI == "49") || (sTributaIPI == "00") || (sTributaIPI == "50") || (sTributaIPI == "99"))
                    {
                        belIpitrib objipitrib = new belIpitrib();
                        objipi.Cenq = "999";
                        objipitrib.Cst = sTributaIPI;
                        if (!drIItem["vBC"].Equals(string.Empty))
                        {
                            if (objdest.Uf.Equals("EX"))
                            {
                                decimal ddvBC = Math.Round((Convert.ToDecimal(drIItem["vUnCom"]) + Convert.ToDecimal(drIItem["pIPI"])) * (Convert.ToDecimal(drIItem["qCom"])), 2);  // Diego - 24730 - 02/08/10
                                objipitrib.Vbc = ddvBC;
                            }
                            else
                            {
                                decimal ddvBC = Math.Round(Convert.ToDecimal(drIItem["vBC"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                objipitrib.Vbc = ddvBC;
                            }
                        }
                        if (!drIItem["pIPI"].Equals(string.Empty))
                        {
                            decimal dpIPI = Math.Round(Convert.ToDecimal(drIItem["pIPI"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                            objipitrib.Pipi = dpIPI;
                        }
                        if (!drIItem["vIPI"].Equals(string.Empty))
                        {
                            decimal dvIPI = Math.Round(Convert.ToDecimal(drIItem["vIPI"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                            objipitrib.Vipi = dvIPI;
                        }
                        objipi.belIpitrib = objipitrib;
                    }
                    else
                    {
                        belIpint objipint = new belIpint();
                        objipi.Cenq = "999";
                        objipint.Cst = sTributaIPI;
                        objipi.belIpint = objipint;
                    }
                    objimp.belIpi = objipi;
                    #endregion

                    #region II
                    //Imposto de importação
                    belIi objii = new belIi();
                    objii.Vbc = 0;
                    objii.Vdespadu = 0;
                    objii.Vii = 0;
                    objii.Viof = 0;
                    objimp.belIi = objii;
                    //Fim - II                    
                    #endregion

                    #region PIS

                    if (drIItem["CD_SITTRIBPIS"].ToString() == "")
                    {
                        throw new Exception("Situação Tributária do PIS está vazia na NF");
                    }
                    string sCst = drIItem["CD_SITTRIBPIS"].ToString().PadLeft(2, '0'); //o.s. 23672 - 10/09/2009
                    //PIS
                    belPis objpis = new belPis();
                    if ((drIItem["st_piscofins"].ToString() == "S") && (Convert.ToInt16(sCst) < 4))
                    {
                        belPisaliq objpisaliq = new belPisaliq();
                        objpisaliq.Cst = sCst;
                        decimal dvlBasepis = Math.Round(Convert.ToDecimal(drIItem["vl_basePisCofins"].ToString()), 2);
                        objpisaliq.Vbc = (objpisaliq.Cst.ToString().Equals("99") ? 0 : dvlBasepis); //24872
                        decimal dpPis = Math.Round(Convert.ToDecimal(drIItem["vl_aliqpis_suframa"].ToString()), 2);
                        objpisaliq.Ppis = (objpisaliq.Cst.ToString().Equals("99") ? 0 : dpPis);
                        decimal dvPIS = Math.Round(Convert.ToDecimal(drIItem["vl_pis"].ToString()), 2);
                        dTotPis += dvPIS;
                        objpisaliq.Vpis = (objpisaliq.Cst.ToString().Equals("99") ? 0 : dvPIS);
                        objpis.belPisaliq = objpisaliq;
                    }
                    else if (Convert.ToInt16(sCst) == 99)
                    {
                        // Diego - OS_24585 - 25/06/2010
                        sSimplesNac = VerificaEmpresaSimplesNac(sEmp, Conn);
                        belPisoutr objpisoutr = new belPisoutr();
                        objpisoutr.Cst = sCst;
                        objpisoutr.Vbc = 0;
                        objpisoutr.Ppis = 0;
                        dTotPis += 0;
                        objpisoutr.Vpis = 0;
                        objpis.belPisoutr = objpisoutr;
                        // Diego - OS_24585 - 25/06/2010 - FIM
                    }
                    else
                    {
                        belPisnt objpisnt = new belPisnt();
                        objpisnt.Cst = sCst;
                        objpis.belPisnt = objpisnt;
                    }
                    objimp.belPis = objpis;
                    //Fim PIS

                    #endregion

                    #region Cofins
                    //Cofins
                    if (drIItem["cd_sittribcof"].ToString() == "")
                    {
                        throw new Exception("Situação Tributária do COFINS está vazia na NF");
                    }
                    belCofins objcofins = new belCofins();
                    if ((drIItem["st_piscofins"].ToString() == "S") && (Convert.ToInt16(drIItem["cd_sittribcof"].ToString()) < 4))
                    {
                        belCofinsaliq objcofinsaliq = new belCofinsaliq();
                        objcofinsaliq.Cst = drIItem["cd_sittribcof"].ToString().PadLeft(2, '0');
                        decimal dvlBaseCofins = Math.Round(Convert.ToDecimal(drIItem["vl_basePisCofins"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objcofinsaliq.Vbc = dvlBaseCofins;
                        decimal dpCofins = Math.Round(Convert.ToDecimal(drIItem["vl_aliqcofins_suframa"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objcofinsaliq.Pcofins = dpCofins;
                        decimal dvCofins = Math.Round(Convert.ToDecimal(drIItem["vl_cofins"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objcofinsaliq.Vcofins = dvCofins;
                        objcofins.belCofinsaliq = objcofinsaliq;
                    }
                    else if (Convert.ToInt16(drIItem["cd_sittribcof"].ToString()) == 99)
                    {
                        belCofinsoutr objcofinsoutr = new belCofinsoutr();
                        objcofinsoutr.Cst = drIItem["cd_sittribcof"].ToString().PadLeft(2, '0');
                        objcofinsoutr.Vbc = 0;
                        objcofinsoutr.Pcofins = 0;
                        dTotCofins += 0;
                        objcofinsoutr.Vcofins = dTotCofins;
                        objcofins.belCofinsoutr = objcofinsoutr;
                    } // Diego - OS_24585 - 25/06/2010 - FIM
                    else
                    {
                        belCofinsnt objcofinsnt = new belCofinsnt();
                        objcofinsnt.Cst = drIItem["cd_sittribcof"].ToString().PadLeft(2, '0');
                        objcofins.belCofinsnt = objcofinsnt;
                    }
                    objimp.belCofins = objcofins;
                    //Fim - Cofins                    
                    #endregion

                    #region ISS
                    if ((drIItem["vAliqISS"].ToString() != "") && (drIItem["vAliqISS"].ToString() != "0"))
                    {
                        belIss objiss = new belIss();
                        decimal dvBCISS = Math.Round(Convert.ToDecimal(drIItem["vBCISS"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        dTotServ += dvBCISS;
                        dTotBCISS = dTotServ;
                        objiss.Vbc = dvBCISS;
                        decimal dvAliqISS = Math.Round(Convert.ToDecimal(drIItem["vAliqISS"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objiss.Valiq = dvAliqISS;
                        decimal dvISSQN = Convert.ToDecimal(drIItem["vIssqn"].ToString());
                        dTotISS += dvISSQN;
                        dTotPisISS += Math.Round(Convert.ToDecimal(drIItem["vl_pis"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        dTotCofinsISS += Math.Round(Convert.ToDecimal(drIItem["vl_cofins"].ToString()), 2);  //o.s. 24248 - 26/03/2010
                        objiss.Vissqn = dvISSQN;
                        objiss.Cmunfg = drIItem["cMunFG"].ToString();
                        if (drIItem["cListserv"].ToString() != "")
                        {
                            Int64 icListServ = Convert.ToInt64(drIItem["cListserv"].ToString());
                            objiss.Clistserv = icListServ;
                        }
                        objimp.belIss = objiss;
                    }
                    #endregion

                    #region Obs
                    //Obs
                    belInfadprod objinf = new belInfadprod();
                    string sObsItem = "";
                    if (objbelGeraXml.nm_Cliente == "HELENGE")
                    {
                        sObsItem += (BuscaContratoOBS(sEmp, drIItem["nr_lanc"].ToString(), Conn)).Replace(Environment.NewLine, "-");
                    }
                    if (objbelGeraXml.nm_Cliente == "FORMINGP")                    //Diego - O.S 24028 - 22/01/2010
                    {
                        sObsItem += BuscaSerieProd(sEmp, drIItem["nr_lanc"].ToString(), Conn);
                    }//Fim - Diego - O.S 24028 - 22/01/2010

                    sObsItem = BuscaObsItemSimples(sEmp, drIItem["nr_lanc"].ToString(), Conn) + sObsItem; //17/11/2010


                    if ((sBanco.ToUpper().IndexOf("COMERCIOC") == -1) && (sBanco.ToUpper().IndexOf("CERAMICAC") == -1))
                    {
                        if (drIItem["st_imp_cdpedcli"].ToString() != "N") //Claudinei - o.s. sem - 21/12/2009
                        {
                            if (drIItem["nr_lote"].ToString() != "")
                            {
                                sObsItem = string.Format("Lote: {0}", drIItem["nr_lote"].ToString());
                            }
                            if (drIItem["cd_pedcli"].ToString() != "")
                            {
                                if (sObsItem == "")
                                {
                                    sObsItem += string.Format("SEU PEDIDO.: {0}",
                                                              drIItem["cd_pedcli"].ToString().Trim());
                                }
                                else
                                {
                                    sObsItem += string.Format(" SEU PEDIDO.: {0}",
                                                              drIItem["cd_pedcli"].ToString().Trim());
                                }
                            }

                            if (drIItem["cd_prodcli"].ToString() != "")
                            {
                                if (sObsItem == "")
                                {
                                    sObsItem += string.Format("PRD_CLI.: {0}",
                                                              drIItem["cd_prodcli"].ToString().Trim());

                                }
                                else
                                {
                                    sObsItem += string.Format(" PRD_CLI.: {0}",
                                                              drIItem["cd_prodcli"].ToString().Trim());
                                }
                            }

                        }
                    }

                    if (objbelGeraXml.nm_Cliente == "MARPA")
                    {
                        sObsItem = MontaObsItem(sEmp, drIItem["nr_lanc"].ToString(), Conn);
                        if (sObsItem != "")
                        {
                            if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                            {
                                if (drIItem["xLgr"].ToString().Trim() != "")
                                {
                                    sObsItem += string.Format(" - Endereco de Entrega.: {0} {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                              TiraSimbolo(drIItem["xLgr"].ToString().Trim(), ""),
                                                              TiraSimbolo(drIItem["nro"].ToString().Trim(), ""),
                                                              TiraSimbolo(drIItem["xBairro"].ToString().Trim(), ""),
                                                              RetiraCaracterEsquerda(TiraSimbolo(drIItem["cMun"].ToString().Trim(), ""), '0'),
                                                              TiraSimbolo(drIItem["UF"].ToString().Trim(), ""));
                                }
                                if (drIItem["Desconto_Valor"].ToString() != "0")
                                {
                                    decimal dDesconto_Valor = Convert.ToDecimal(drIItem["Desconto_Valor"].ToString());
                                    decimal dDesconto_Percentual = (Convert.ToDecimal(drIItem["Desconto_Percentual"].ToString()) / 100);

                                    sObsItem += string.Format(" - Desconto.: ({0:p2}) {1:f2}",
                                                              dDesconto_Percentual,
                                                              dDesconto_Valor);


                                }
                            }
                            objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "");
                        }
                        else
                        {
                            if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                            {
                                if (drIItem["Desconto_Valor"].ToString() != "0")
                                {
                                    decimal dDesconto_Valor = Convert.ToDecimal(drIItem["Desconto_Valor"].ToString());
                                    decimal dDesconto_Percentual = (Convert.ToDecimal(drIItem["Desconto_Percentual"].ToString()) / 100);

                                    sObsItem = string.Format("Desconto.: ({0:p2}) {1:f2}",
                                                              dDesconto_Percentual,
                                                              dDesconto_Valor);
                                    objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "");
                                }
                            }
                        }
                    }
                    else
                    {
                        if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                        {
                            if (drIItem["xLgr"].ToString().Trim() != "")
                            {
                                if (sObsItem == "")
                                {

                                    sObsItem = string.Format("Endereco de Entrega.: {0}, {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                             drIItem["xLgr"].ToString().Trim(),
                                                             drIItem["nro"].ToString().Trim(),
                                                             drIItem["xBairro"].ToString().Trim(),
                                                             RetiraCaracterEsquerda(drIItem["cMun"].ToString().Trim(), '0'),
                                                             drIItem["UF"].ToString().Trim());
                                }
                                else
                                {
                                    sObsItem += string.Format(" - Endereco de Entrega.: {0}, {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                             drIItem["xLgr"].ToString().Trim(),
                                                             drIItem["nro"].ToString().Trim(),
                                                             drIItem["xBairro"].ToString().Trim(),
                                                             RetiraCaracterEsquerda(drIItem["cMun"].ToString().Trim(), '0'),
                                                             drIItem["UF"].ToString().Trim());

                                }
                                if (sObsItem != "")
                                {
                                    objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "");
                                }
                            }
                            else
                            {
                                if (sObsItem != "")
                                {
                                    objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "").Replace(Environment.NewLine, "-");
                                }
                            }
                        }
                        else
                        {
                            if (sObsItem != "")
                            {
                                objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "");
                            }

                        }
                    }
                    if (sObsItem != "")
                    {
                        if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                        {
                            if (drIItem["xLgrRedes"].ToString().Trim() != "")
                            {
                                sObsItem += string.Format(" - Redespacho.: {5} - {0} {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                          TiraSimbolo(drIItem["xLgrRedes"].ToString().Trim(), ""),
                                                          TiraSimbolo(drIItem["nroRedes"].ToString().Trim(), ""),
                                                          TiraSimbolo(drIItem["xBairroRedes"].ToString().Trim(), ""),
                                                          RetiraCaracterEsquerda(TiraSimbolo(drIItem["cmunRedes"].ToString().Trim(), ""), '0'),
                                                          TiraSimbolo(drIItem["UFRedes"].ToString().Trim(), ""),
                                                          TiraSimbolo(drIItem["redespacho"].ToString().Trim(), ""));
                                objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "-");
                            }
                        }
                    }
                    else
                    {
                        if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                        {
                            if ((drIItem["xLgrRedes"].ToString().Trim() != "-") && (drIItem["xLgrRedes"].ToString().Trim() != "")) //Claudinei - o.s. sem - 01/03/2010
                            {
                                sObsItem += string.Format("Redespacho.: {5} - {0} {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                          TiraSimbolo(drIItem["xLgrRedes"].ToString().Trim(), ""),
                                                          TiraSimbolo(drIItem["nroRedes"].ToString().Trim(), ""),
                                                          TiraSimbolo(drIItem["xBairroRedes"].ToString().Trim(), ""),
                                                          RetiraCaracterEsquerda(TiraSimbolo(drIItem["cmunRedes"].ToString().Trim(), ""), '0'),
                                                          TiraSimbolo(drIItem["UFRedes"].ToString().Trim(), ""),
                                                          TiraSimbolo(drIItem["redespacho"].ToString().Trim(), ""));
                                objinf.Infadprid = TiraSimbolo(sObsItem.Trim(), "-");
                            }
                        }
                    }
                    if (objinf.Infadprid != null)
                    {
                        if (objinf.Infadprid.Length > 500)
                        {
                            objinf.Infadprid = objinf.Infadprid.Substring(0, 500);
                        }
                    }
                    //Fim - Obs                    
                    #endregion

                    objdet.belImposto = objimp;
                    objdet.belInfadprod = objinf;
                    dets.Add(objdet);
                    //Fim - Impostos
                }
            }
            catch (Exception Ex)
            {
                sExecao = " - Problemas ao tentar gerar os Itens da Nota de Seq.: " + sNF;  //OS 24738
                throw new Exception(Ex.Message + sExecao);
            }
            //finally
            //{
            //    Conn.Close();
            //}
            return dets;
        }
コード例 #14
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public belIde BuscaIde(string sEmp,
                                    string sNF,
                                    string sForDanfe,
                                    string sFormaEmissao,
                                    Version versao)
        {
            //belGerarXML BuscaConexao = new belGerarXML();
            // FbConnection Conn = BuscaConexao.Conn;
            belIde objide = new belIde();
            try
            {
                StringBuilder sSql = new StringBuilder();
                //Campos do Select
                sSql.Append("Select First 1 ");
                sSql.Append("coalesce(tpdoc.st_ref_outra_nf,'S') st_ref_outra_nf, ");
                sSql.Append("uf.nr_ufnfe cUF, ");
                sSql.Append("nf.cd_nfseq cNF, ");
                sSql.Append("nf.ds_cfop natop, ");
                sSql.Append("coalesce(PRAZOS.ST_FPAGNFE,0) indPag, ");
                sSql.Append("'55' mod, ");
                sSql.Append("coalesce(nf.cd_serie, 1) serie, ");
                sSql.Append("nf.cd_notafis nNF, ");
                sSql.Append("nf.dt_emi dEmi, ");
                sSql.Append("nf.dt_sainf dSaiEnt, ");
                sSql.Append("case when tpdoc.tp_doc = 'NS' then '1' else '0' end tpNF, ");
                sSql.Append("cidades.cd_municipio cMunFg, ");
                sSql.Append("'1' tpImp, ");
                sSql.Append("'1' tpEmis, ");
                sSql.Append("coalesce(empresa.st_ambiente, '2') tpAmb, ");
                sSql.Append("case when tpdoc.st_nfcompl = 'S' then '2' else '1' end finNFe, ");
                sSql.Append("'0' procEmi, ");
                Globais LeRegWin = new Globais();
                string versionstring = versao.Major + "." + versao.Minor + "." + versao.Build + "." + versao.Revision;
                sSql.Append("'");
                sSql.Append(versionstring);
                sSql.Append("' verProc ");
                //Tabela
                sSql.Append("From NF ");
                //Relacionamentos
                sSql.Append("left join tpdoc on (tpdoc.cd_tipodoc = nf.cd_tipodoc)");
                sSql.Append("inner join empresa on (empresa.cd_empresa = nf.cd_empresa)");
                sSql.Append("left join uf on (uf.cd_uf = empresa.cd_ufnor)");
                sSql.Append("left join cidades on (cidades.nm_cidnor = empresa.nm_cidnor)");
                sSql.Append(" and ");
                sSql.Append("(cidades.cd_ufnor = empresa.cd_ufnor) ");
                sSql.Append("left join  prazos on (prazos.cd_prazo = nf.cd_prazo)");
                //Where
                sSql.Append("Where ");
                sSql.Append("(nf.cd_empresa ='");
                sSql.Append(sEmp);
                sSql.Append("')");
                sSql.Append(" and ");
                sSql.Append("(nf.cd_nfseq = '");
                sSql.Append(sNF);
                sSql.Append("') ");
                // Conn.Open();

                FbCommand cmdIde = new FbCommand(sSql.ToString(), Conn);
                cmdIde.ExecuteNonQuery();
                FbDataReader drIde = cmdIde.ExecuteReader();
                drIde.Read();
                string sNFe = "NFe" + GeraChave(sEmp, sNF, Conn);
                objide = new belIde();
                objide.Cuf = drIde["cUF"].ToString();
                Int32 icNF = Convert.ToInt32(drIde["cNF"].ToString());
                objide.Cnf = icNF.ToString().PadLeft(8, '0'); // NFe_2.0
                string snatop = drIde["natop"].ToString();
                if (snatop.Length > 60)
                {
                    snatop = snatop.Substring(0, 59);
                }
                objide.Natop = TiraSimbolo(snatop, "");
                objide.Indpag = drIde["indPag"].ToString();
                objide.Mod = drIde["mod"].ToString();
                objide.Serie = (bModoSCAN == false ? drIde["serie"].ToString().Replace("U", "1") : iSerieSCAN.ToString());//Diego - OS_24580
                Int32 inNF = Convert.ToInt32(drIde["nNF"].ToString()); //Claudinei - o.s. 23630
                objide.Nnf = inNF.ToString().Trim();
                objide.Demi = System.DateTime.Parse(drIde["dEmi"].ToString());
                if (drIde["dSaiEnt"].ToString() != "")
                {
                    objide.Dsaient = System.DateTime.Parse(drIde["dSaiEnt"].ToString());
                    objide.HSaiEnt = System.DateTime.Parse(drIde["dSaiEnt"].ToString());
                }
                else
                {
                    objide.Dsaient = DateTime.Now;
                    objide.HSaiEnt = DateTime.Now;
                }
                objide.Tpnf = drIde["tpNF"].ToString();
                objide.Cmunfg = TiraSimbolo(drIde["cMunFG"].ToString(), "");
                objide.Tpimp = sForDanfe;
                objide.Tpemis = sFormaEmissao;
                objide.Cdv = sNFe.Substring((sNFe.Length - 1), 1);
                objide.Tpamb = drIde["tpAmb"].ToString();
                objide.Finnfe = drIde["finNFe"].ToString();
                objide.Procemi = drIde["procEmi"].ToString();
                objide.Verproc = drIde["verProc"].ToString();
                objide.bReferenciaNF = (drIde["st_ref_outra_nf"].ToString().Equals("N") ? false : true); //25360
            }
            catch (Exception Ex)
            {
                sExecao = " - Problemas na Identificação da Nota";
                throw new Exception(Ex.Message + sExecao);
            }
            //finally
            //{
            //    Conn.Close();
            //}
            return objide;


        }
コード例 #15
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        /// <summary>
        /// Popula as Classes da NF-e.
        /// </summary>
        /// <param name="sEmp"></param>
        /// <param name="sNF"></param>
        /// <param name="sNomeArq"></param>
        /// <param name="sFormaEmissao"></param>
        /// <param name="sForDanfe"></param>
        /// <param name="tp_amb"></param>
        /// <param name="cd_ufnro"></param>
        /// <param name="cert"></param>
        public void NFe(string sEmp, List<string> sNF, string sNomeArq, string sFormaEmissao, string sForDanfe, int tp_amb, string cd_ufnro, X509Certificate2 cert, bool bModoSCAN, int iSerieSCAN, string sFormaEmiNFe, Version versao)
        {
            objbelGeraXml = new belGerarXML();
            Conn = objbelGeraXml.Conn;
            Conn.Open();
            this.sFormaEmiNFe = sFormaEmiNFe;
            this.bModoSCAN = bModoSCAN;
            this.iSerieSCAN = iSerieSCAN;

            string sPath = "";
            sPath = CarregarDadosXml("PastaXmlEnvio").ToString() + @sNomeArq;
            sTipoIndustrializacao = CarregarDadosXml("Industrializacao")[0].ToString();
            if (File.Exists(sPath))
            {
                File.Delete(sPath);
            }

            XDocument xdoc = new XDocument();
            #region XML_Principal
            XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
            XContainer conenv = new XElement(pf + "enviNFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                     new XAttribute("versao", "1.10"),
                                                     new XElement(pf + "idLote", sNomeArq.Substring(7, 15)));
            #endregion


            Globais glob = new Globais();
            int notaindex = 0;

            try
            {
                foreach (string sNota in sNF)
                {
                    List<object> lObjNotas = new List<object>();
                    notaindex++;
                    string sNFe = "NFe" + GeraChave(sEmp, sNota, Conn);


                    // infNFE                
                    // Começa a Popular as Classes
                    //Ide               
                    belIde objide;
                    objide = BuscaIde(sEmp, sNota, sForDanfe, sFormaEmissao, versao);
                    lObjNotas.Add(objide);//Danner - o.s. 24092 - 03/02/2010
                    //Fim - Ide

                    //Emit
                    belEmit objemit;
                    objemit = BuscaEmit(sEmp, sForDanfe, sForDanfe, sNota); //Claudinei - o.s. 24222 - 08/03/2010
                    lObjNotas.Add(objemit);//Danner - o.s. 24092 - 03/02/2010
                    //Fim - Emit

                    //dest
                    belDest objdest;
                    objdest = BuscaDest(sEmp, sNota);
                    lObjNotas.Add(objdest);//Danner - o.s. 24092 - 03/02/2010
                    //Fim - dest

                    //Endereço de entrega
                    belEndEnt objendent;
                    objendent = BuscaEndEnt(sEmp, sNota);
                    lObjNotas.Add(objendent);
                    //Fim - Endereço de entrega

                    //Itens da NFe
                    List<belDet> objdet = new List<belDet>();
                    objdet = BuscaItem(sEmp, sNota, objdest);
                    lObjNotas.Add(objdet);

                    //Totais               
                    belTotal objtotal;
                    objtotal = BuscaTotais(sEmp, sNota, objdest);
                    lObjNotas.Add(objtotal);
                    //Fim - Totais

                    //Frete
                    belTransp objtransp;
                    objtransp = BuscaFrete(sEmp, sNota);
                    lObjNotas.Add(objtransp);

                    //Fim - Frete

                    //Duplicatas                
                    belCobr objcob;
                    objcob = BuscaFat(sEmp, sNota);
                    lObjNotas.Add(objcob);
                    //Fim - Duplicatas


                    //OBS
                    belInfAdic objobs;
                    objobs = BuscaObs(sEmp, sNota, objdest, objdet, objbelGeraXml);
                    lObjNotas.Add(objobs);
                    //Fim - OBS

                    lTotNota.Add(lObjNotas);

                    // Update para Gravar o numero da Chave no banco da nota corrente.
                    for (int i = 0; i < 4; i++)
                    {
                        if (GravaNumeroChaveNota(sEmp, sNota, sNFe))
                        {
                            break;
                        }
                    }
                }
                Conn.Close();
            }
            catch (Exception ex)
            {
                Conn.Close();
                throw ex;
            }
        }
コード例 #16
0
        private XmlNode MontaMsg()
        {
            try
            {
                XmlSchemaCollection myschema = new XmlSchemaCollection();
                XmlValidatingReader reader;
                XNamespace pf = "http://www.portalfiscal.inf.br/nfe";

                XDocument xdoc = new XDocument(new XElement(pf + "ConsCad", new XAttribute("versao", "2.00"),
                                                    new XElement(pf + "infCons",
                                                               new XElement(pf + "xServ", "CONS-CAD"),
                                                               new XElement(pf + "UF", sUF),
                                                               (sIE != "" ? new XElement(pf + "IE", Util.Util.TiraSimbolo(sIE, "")) : null),
                                                               ((sCNPJ != "" && sIE == "") ? new XElement(pf + "CNPJ", Util.Util.TiraSimbolo(sCNPJ, "")) : null),
                                                               ((sCPF != "" && sIE == "" && sCNPJ == "") ? new XElement(pf + "CPF", Util.Util.TiraSimbolo(sCPF, "")) : null))));

              

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

                reader = new XmlValidatingReader(xdoc.ToString(), XmlNodeType.Element, context);

                Globais Gb = new Globais();

                myschema.Add("http://www.portalfiscal.inf.br/nfe", Gb.LeRegConfig("PastaSchema").ToString() + "\\consCad_v2.00.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                { }
                string sDados = xdoc.ToString();
                XmlDocument Xmldoc = new XmlDocument();
                Xmldoc.LoadXml(sDados);
                XmlNode xNode = Xmldoc.DocumentElement;
                return xNode;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
コード例 #17
0
ファイル: belBusca.cs プロジェクト: dramosti/GeraXml_2.0
        /// <summary>
        /// Utilizado na Classe belGeraXML.
        /// </summary>
        /// <param name="sEmp"></param>
        /// <param name="sSeqs"></param>
        /// <param name="sNfes"></param>
        /// <param name="tp_amb"></param>
        /// <param name="cd_ufnro"></param>
        /// <param name="sRecibo"></param>
        /// <param name="xCert"></param>
        /// <param name="glob"></param>
        /// <param name="pf"></param>
        public belBusRetFazenda(string sEmp, List<string> sSeqs, List<string> sNfes, int tp_amb, string cd_ufnro,
                                string sRecibo, X509Certificate2 xCert, Globais glob, string UF_Empresa, bool bModoSCAN)
        {
            XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
            this.bModoSCAN = bModoSCAN;
            _emp = sEmp;
            _seqs = sSeqs;
            _nfes = sNfes;

            string qtdeTentativas = glob.LeRegConfig("QtdeTentativas").ToString();

            bool parar = false;

            //Retorno da fazenda com Status de cada NFe do lote.
            belNfeRetRecepcao objnferetrecepcao = new belNfeRetRecepcao("1.02", tp_amb, sRecibo, "2.00", xCert, bModoSCAN);
            XmlDocument xret = new XmlDocument();

            //Variavel pra sabe se alguma nota possui erro
            XmlNodeList nodescStat;
            XmlNodeList nodesxMotivo;
            //contador de tentativas
            int countTentativas = 0;

            for (; ; )
            {

                xret = objnferetrecepcao.Retornaxml(UF_Empresa);
                nodescStat = xret.GetElementsByTagName("cStat");
                nodesxMotivo = xret.GetElementsByTagName("xMotivo");

                if (nodescStat[0].InnerText == "105")
                {

                    //Verifica o status do serviço e faz o calculo de tempo médio para a próxima tentativa do recebimento do retorno da fazenda
                    belnfeStatusServicoNF objstausserv = new belnfeStatusServicoNF("2.00", tp_amb, cd_ufnro, xCert, UF_Empresa);
                    System.Threading.Thread.Sleep((objstausserv.Tmed * 1000) + 3000);

                    

                    //Compara se a quantidade de tentativas dadas é igual ao limite estipuládo na configuração do sistema.
                    if (qtdeTentativas == countTentativas.ToString())
                    {

                        throw new Exception("Quantidade de Tentativas de Conexão com a secretaria da Fazenta Excedidas " + Environment.NewLine + 
                                            "Lote foi enviado a fazenda, porem esta em processamento" + Environment.NewLine +
                                            "Espere alguns segundos antes de tentar buscar o retorno novamente");

                    }

                    //Conta tentativa
                    countTentativas++;

                    continue;

                }
                else if (nodescStat[0].InnerText != "104")
                {
                    throw new Exception("Erro " + nodescStat[0].InnerText + " - " + nodesxMotivo[0].InnerText);
                }
                else
                {
                    for (int i = 0; i < sNfes.Count ; i++)
                    {
                        //Variavel que gera a mensagem de resposta
                        _loteres = _loteres + "Nota de número de sequência: " + sSeqs[i] + " - " + nodesxMotivo[i + 1].InnerText + " Cod. " + nodescStat[i + 1].InnerText + Environment.NewLine;

                        if (nodescStat[i + 1].InnerText == "100")
                        {
                            _nfeautorizadas.Add(sSeqs[i]);
                            XmlNode xRetUni = xret.GetElementsByTagName("infProt")[i]; // Diego OS_24777

                            //Método responsavel por gravar o nProt e chNfe no banco e gerar o xml protocolado.
                            geraProcNFe(sEmp, sSeqs[i], sNfes[i], xret.GetElementsByTagName("chNFe")[i].InnerText,
                                                xRetUni["nProt"].InnerText, xret.GetElementsByTagName("protNFe")[i], glob, pf);

                            xret.Save(glob.LeRegConfig("PastaProtocolos").ToString() + "\\" + sRecibo + "-pro-rec.xml");                           

                        }
                        else
                        {
                            if (nodescStat[i + 1].InnerText != "105")
                            {
                                LimpaCampoRecibo(_emp, sSeqs[i]);
                            }
                        }

                        parar = true;

                    }
                }
                if (parar == true)
                    break;
            }


        }
コード例 #18
0
ファイル: belBusca.cs プロジェクト: dramosti/GeraXml_2.0
        /// <summary>
        /// Método Responsavel por gravar os campos cd_chavenferet e cd_nprotnfe. Responsavel tambem montar o TAG procNFe no xml e
        /// grava-lo na pasta de xml enviados.
        /// </summary>
        /// <param name="sEmp"></param>
        /// <param name="sSeq"></param>
        /// <param name="sNFe"></param>
        /// <param name="sChNfe"></param>
        /// <param name="sNProt"></param>
        /// <param name="xret"></param>
        /// <param name="glob"></param>
        /// <param name="pf"></param>
        public void geraProcNFe(string sEmp, string sSeq, string sNFe, string sChNfe, string sNProt,
                                XmlNode xret, Globais glob, XNamespace pf)//Danner - o.s. 24435 - 05/05/2010
        {
            XDocument retcab = new XDocument();
             
            try
            {
                //String sql que dará o update no campos: cd_recibonfe, cd_chavenferet, cd_nprotnfe.
                StringBuilder sSql = new StringBuilder();

                sSql.Append("update nf set cd_chavenferet ='");
                sSql.Append(sChNfe);
                sSql.Append("', ");
                sSql.Append("cd_nprotnfe ='");
                sSql.Append(sNProt);
                sSql.Append("' ");
                sSql.Append("Where nf.cd_empresa = '");
                sSql.Append(sEmp);
                sSql.Append("' and ");
                sSql.Append("nf.cd_nfseq ='");
                sSql.Append(sSeq);
                sSql.Append("'");


                belGerarXML BuscaConexao = new belGerarXML();

                using (FbConnection Conn = BuscaConexao.Conn)
                {
                    using (FbCommand cmdUpdate = new FbCommand(sSql.ToString(), Conn))
                    {
                        Conn.Open();
                        cmdUpdate.ExecuteNonQuery();
                    }
                }
                //Geração do Xml da nfe Autorizado, incluindo a TAG infProc onde consta as informaçoes de retorno da nfe.
                XContainer retproc = new XElement(pf + "nfeProc", new XAttribute("versao", "2.00"),
                                                                     new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"));
                XElement retxml = XElement.Parse(sNFe);
                string sCodificacao = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                string sRetProc = "<nfeProc versao=\"2.00\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">";
                XmlDocument xmldocteste = new XmlDocument();
                xmldocteste.LoadXml(sNFe);                
                DirectoryInfo dPastaData = new DirectoryInfo(glob.LeRegConfig("PastaXmlEnviado") + "\\" + sChNfe.Substring(2, 4));
                if (!dPastaData.Exists) { dPastaData.Create(); }


                StreamWriter sw = new StreamWriter(glob.LeRegConfig("PastaXmlEnviado") +"\\"+ sChNfe.Substring(2,4)+ "\\" + sChNfe + "-nfe.xml"); //OS_25024
       
                if (@xmldocteste.FirstChild.Name.Equals("xml"))
                {
                    sw.Write(@sRetProc + @xmldocteste.OuterXml.Remove(0, 38) + @xret.OuterXml.ToString() + @"</nfeProc>");
                }
                else
                {
                    sw.Write(@sCodificacao + @sRetProc + @xmldocteste.OuterXml + @xret.OuterXml.ToString() + @"</nfeProc>");
                }                
                sw.Close();

                //Geração do Xml da nfe Autorizado, incluindo a TAG infProc onde consta as informaçoes de retorno da nfe.
            }
            catch (Exception x)
            {
                throw new Exception("Erro na geração do XML Protocolado, infProt - " + x.Message);
            }

        }
コード例 #19
0
ファイル: belBusca.cs プロジェクト: dramosti/GeraXml_2.0
        //Fim - Danner - o.s. 24191 - 26/02/2010
        /// <summary>
        /// Utilizado no botão para buscar o retorno da fazenda
        /// </summary>
        /// <param name="sEmp"></param>
        /// <param name="sSeq"></param>
        /// <param name="sVersao"></param>
        /// <param name="sVersaoAplic"></param>
        /// <param name="sNFe"></param>
        /// <param name="sRecibo"></param>
        /// <param name="tp_amb"></param>
        /// <param name="glob"></param>
        /// <param name="?"></param>
        public belBusRetFazenda(string sEmp, string sSeq, string sVersao, string sVersaoAplic, string sNFe,
                                string sRecibo, string sChnfe, int tp_amb, Globais glob, X509Certificate2 xCert, string UF_Empresa)
            
        {
            XNamespace pf = "http://www.portalfiscal.inf.br/nfe";

            //Retorno da fazenda com Status de cada NFe do lote.
            belNfeRetRecepcao objnferetrecepcao = new belNfeRetRecepcao("1.02", tp_amb, sRecibo, "2.00", xCert, bModoSCAN);
            XmlDocument xret = new XmlDocument();
            xret = objnferetrecepcao.Retornaxml(UF_Empresa);

            //Variavel pra sabe se alguma nota possui erro
            XmlNodeList nodeschNFe = xret.GetElementsByTagName("chNFe");
            XmlNodeList nodecStat = xret.GetElementsByTagName("cStat");
            XmlNodeList nodesxMotivo = xret.GetElementsByTagName("xMotivo");

            try
            {
                if (nodecStat[0].InnerText != "104")
                {
                    throw new Exception("Cód. do Status: " + nodecStat[0].InnerText + " " +
                                                  "Status: " + nodesxMotivo[0].InnerText);
                }
                else
                {
                    for (int i = 0; i < nodeschNFe.Count; i++)
                    {
                        if (nodeschNFe[i].InnerText == sChnfe)
                        {
                            //Monta a mensagem de resposta
                            _loteres = _loteres + "Cód. do Status: " + nodecStat[i + 1].InnerText + " " +
                                                  "Status: " + nodesxMotivo[i + 1].InnerText;

                            if (nodecStat[i + 1].InnerText == "100")
                            {
                                _nfeunicaAut = sSeq;
                             
                                geraProcNFe(sEmp, sSeq, sNFe, xret.GetElementsByTagName("chNFe")[i].InnerText,
                                    xret.GetElementsByTagName("nProt")[i].InnerText, xret.GetElementsByTagName("protNFe")[i], glob, pf); //Claudinei - o.s. 24126 - 11/02/2010
                              
                                xret.Save(glob.LeRegConfig("PastaProtocolos").ToString() + "\\" + sRecibo + "-pro-rec.xml");                        
                            }//Diego - OS_24610
                            else if (nodecStat[i + 1].InnerText == "101")
                            {
                                _nfeunicaAut = sSeq;

                                geraProcNFe(sEmp, sSeq, sNFe, xret.GetElementsByTagName("chNFe")[i].InnerText,
                                            xret.GetElementsByTagName("nProt")[i].InnerText, xret.GetElementsByTagName("protNFe")[i], glob, pf);
                                DirectoryInfo dinfo = new DirectoryInfo(glob.LeRegConfig("PastaXmlEnviado") + "\\" + xret.GetElementsByTagName("chNFe")[i].InnerText.Substring(2,4));
                                string path = "";
                                string nome = "";
                                FileInfo[] finfo = dinfo.GetFiles();
                                foreach (var item in finfo)
                                {
                                    if (item.Name.Contains(sChnfe))
                                    {
                                        path = item.FullName;
                                        nome = item.Name;
                                        break;
                                    }
                                }
                                File.Move(path, glob.LeRegConfig("PastaXmlCancelados").ToString() + "\\" + nome.Replace("nfe", "can"));//+ ".xml");
                            }//Diego - OS_24610 - FIM
                        }
                    }
                }
            }
            catch (Exception x)
            {
                throw new Exception("Erro na BuscaRetFazenda, contrutor para o botão - " + x.Message);
            }

            

        }
コード例 #20
0
ファイル: Util.cs プロジェクト: dramosti/GeraXml_2.0
        public static bool ValidacEAN(string scodigo)
        {
            try
            {
                Globais objGlobais = new Globais();
                if (((scodigo.Length == 8) || (scodigo.Length == 12) || (scodigo.Length == 13) || (scodigo.Length == 14))
                    && (Convert.ToBoolean(objGlobais.LeRegConfig("CodBarrasXml"))))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #21
0
        private void NfeDadosMsg()
        {
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;
            Globais sPath = new Globais();
            try
            {
                StringBuilder sSql = new StringBuilder();
                sSql.Append("select ");
                sSql.Append("cd_chavenferet, ");
                sSql.Append("cd_nprotnfe ");
                sSql.Append("from nf ");
                sSql.Append("where ");
                sSql.Append("(cd_empresa = '");
                sSql.Append(_emp);
                sSql.Append("') ");
                sSql.Append("and ");
                sSql.Append("(cd_nfseq = '");
                sSql.Append(_seq);
                sSql.Append("')");

                belGerarXML BuscaConexao = new belGerarXML();

                FbConnection Conn = BuscaConexao.Conn;

                Conn.Open();

                FbCommand cmdcanc = new FbCommand(sSql.ToString(), Conn);
                cmdcanc.ExecuteNonQuery();



                FbDataReader drCanc = cmdcanc.ExecuteReader();
                drCanc.Read();

                _chnfe = drCanc["cd_chavenferet"].ToString();
                _nprot = drCanc["cd_nprotnfe"].ToString();
                _id = "ID" + _chnfe;

                XNamespace xname = "http://www.portalfiscal.inf.br/nfe";
                XDocument xdoc = new XDocument(new XElement(xname + "cancNFe", new XAttribute("versao", _versaodados),
                                                   new XElement(xname + "infCanc", new XAttribute("Id", _id),
                                                       new XElement(xname + "tpAmb", _tpamb),
                                                       new XElement(xname + "xServ", _xserv),
                                                       new XElement(xname + "chNFe", _chnfe),
                                                       new XElement(xname + "nProt", _nprot),
                                                       new XElement(xname + "xJust", _xjust))));
                xdoc.Save(_spath + "\\" + _seq + "_ped-can.xml");

                AssinaNFeCancXml assinaCanc = new AssinaNFeCancXml();
                assinaCanc.ConfigurarArquivo(_spath + "\\" + _seq + "_ped-can.xml", "infCanc", _cert);

                StreamReader ler;

                ler = File.OpenText(_spath + "\\" + _seq + "_ped-can.xml");



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

                reader = new XmlValidatingReader(ler.ReadToEnd().ToString(), XmlNodeType.Element, context);

                myschema.Add("http://www.portalfiscal.inf.br/nfe", _sschema + "\\cancNFe_v2.00.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                { }

            }
            catch (Exception x)
            {

                throw new Exception(x.Message);
            }
        }
コード例 #22
0
        private string NfeDadosMsg()
        {
            XNamespace xnome = "http://www.portalfiscal.inf.br/nfe";
            XmlSchemaCollection myschema = new XmlSchemaCollection();
            XmlValidatingReader reader;
            try
            {
                XNamespace xname = "http://www.portalfiscal.inf.br/nfe";
                XDocument xdoc = new XDocument(new XElement(xname + "consReciNFe", new XAttribute("versao", "2.00"),
                                               new XElement(xname + "tpAmb", _tpamb.ToString()),
                                               new XElement(xname + "nRec", _nrec)));

                //Danner -  o.s. sem - 04/11/2009
                Globais xml = new Globais();

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

                reader = new XmlValidatingReader(xdoc.ToString(), XmlNodeType.Element, context);

                myschema.Add("http://www.portalfiscal.inf.br/nfe", xml.LeRegConfig("PastaSchema") + "\\consReciNFe_v2.00.xsd");

                reader.ValidationType = ValidationType.Schema;

                reader.Schemas.Add(myschema);

                while (reader.Read())
                { }
                return xdoc.ToString();
            }
            catch (XmlException x)
            {
                throw new Exception(x.Message.ToString());
            }
            catch (XmlSchemaException x)
            {
                throw new Exception(x.Message.ToString());
            }
            //Fim - Danner -  o.s. sem - 04/11/2009
        }
コード例 #23
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public string GeraChave(string sEmp, string sNF, FbConnection Conn)
        {

            StringBuilder sSql = new StringBuilder();
            sSql.Append("Select ");
            sSql.Append("uf.nr_ufnfe, ");
            sSql.Append("coalesce(nf.cd_serie, 1) serie, ");
            sSql.Append("nf.cd_notafis nNF, ");
            sSql.Append("nf.dt_emi dEmi, ");
            sSql.Append("empresa.cd_cgc CNPJ, ");
            sSql.Append("nf.cd_nfseq cNF ");
            sSql.Append(" From ");
            sSql.Append("NF ");
            sSql.Append("inner join empresa on (empresa.cd_empresa = nf.cd_empresa) ");
            sSql.Append("left join uf on (uf.cd_uf = empresa.cd_ufnor) ");
            sSql.Append("where ");
            sSql.Append("(nf.cd_empresa ='");
            sSql.Append(sEmp);
            sSql.Append("')");
            sSql.Append(" and ");
            sSql.Append("(nf.cd_nfseq = '");
            sSql.Append(sNF);
            sSql.Append("')");

            FbCommand sqlConsulta = new FbCommand(sSql.ToString(), Conn);
            sqlConsulta.ExecuteNonQuery();
            FbDataReader drChave = sqlConsulta.ExecuteReader();
            drChave.Read();

            string scUF, sAAmM, sCNPJ, sMod, sSerie, snNF, tpemis, scNF;
            scUF = drChave["nr_ufnfe"].ToString().PadLeft(2, '0');
            sAAmM = drChave["demi"].ToString().Replace("/", "").Substring(6, 2).ToString() +
                    drChave["demi"].ToString().Replace("/", "").Substring(2, 2).ToString();
            sCNPJ = TiraSimbolo(drChave["cnpj"].ToString(), "");
            sCNPJ = sCNPJ.PadLeft(14, '0');
            sMod = "55";
            tpemis = this.sFormaEmiNFe;

            Globais objGlobais = new Globais();

            if (Convert.ToBoolean(objGlobais.LeRegConfig("AtivaModuloScan")))
            {
                sSerie = objGlobais.LeRegConfig("SerieScan");
            }
            else if (IsNumeric(drChave["serie"].ToString()))
            {
                sSerie = drChave["serie"].ToString().PadLeft(3, '0');
            }
            else
            {
                sSerie = "001";
            }
            snNF = drChave["nNF"].ToString().PadLeft(9, '0');
            scNF = drChave["cNF"].ToString().PadLeft(8, '0');

            string sChaveantDig = "";
            string sChave = "";
            string sDig = "";

            sChaveantDig = scUF.Trim() + sAAmM.Trim() + sCNPJ.Trim() + sMod.Trim() + sSerie.Trim() + snNF.Trim() + iStatusAtualSistema + scNF.Trim();//+ tpemis + scNF.Trim();
            sDig = CalculaDig11(sChaveantDig).ToString();

            sChave = sChaveantDig + sDig;

            return sChave;
        }
コード例 #24
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        public List<belDet> BuscaItem(string sEmp,
                                   string sNF, belDest objdest)
        {
            List<belDet> dets = new List<belDet>();
            Globais LeRegWin = new Globais();
            string psNM_Banco = LeRegWin.LeRegConfig("BancoDados");
            List<CamposSelect> lCampos = new List<CamposSelect>();
            bool bAgrupaCampos = VerificaSeAgrupaItens(Conn);
            StringBuilder sCampos = new StringBuilder();
            StringBuilder sInnerJoin = new StringBuilder();
            StringBuilder sWhere = new StringBuilder();
            StringBuilder sGroup = new StringBuilder();

            try
            {

                #region Campos do Select

                //sSql.Append("Select ");
                lCampos.Add(new CamposSelect { sCampo = "coalesce(tpdoc.st_pauta,'N')", sAlias = "st_pauta" });//OS_25969
                lCampos.Add(new CamposSelect { sCampo = "coalesce(tpdoc.st_frete_entra_ipi_s,'N')", sAlias = "st_frete_entra_ipi_s" });//OS_26866
                lCampos.Add(new CamposSelect { sCampo = "coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N')", sAlias = "ST_FRETE_ENTRA_ICMS_S" });//OS_26866
                lCampos.Add(new CamposSelect { sCampo = "coalesce(MOVITEM.vl_baseicm,0)", sAlias = "vBC_Pauta" });//OS_25969
                lCampos.Add(new CamposSelect { sCampo = "coalesce(MOVITEM.vl_icms,0)", sAlias = "vl_icms_Pauta" });//OS_25969
                lCampos.Add(new CamposSelect { sCampo = "coalesce(MOVITEM.cd_pedcli,'')", sAlias = "xPed" });//OS_25977
                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.nr_item_ped_compra,'')", sAlias = "nItemPed" });//OS_25977
                lCampos.Add(new CamposSelect { sCampo = "coalesce(opereve.st_servico,'')", sAlias = "st_servico" });
                lCampos.Add(new CamposSelect { sCampo = "movitem.cd_oper", sAlias = "cd_oper" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(nf.st_soma_dev_tot_nf,'N')", sAlias = "st_soma_dev_tot_nf" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(produto.cd_orig_sittrib,0)", sAlias = "Orig" }); //os_26467
                lCampos.Add(new CamposSelect { sCampo = "coalesce(EMPRESA.ST_SUPERSIMPLES,'N')", sAlias = "ST_SUPERSIMPLES" }); //os_26817
                //ST_SUPERSIMPLES
                if (objdest.Uf.Equals("EX"))
                {
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(MOVITEM.VL_BASEIPI,0)", sAlias = "VL_BASEIPI" }); //os_26467
                }

                if ((psNM_Banco.ToUpper().IndexOf("COMERCIOC") == -1) && (psNM_Banco.ToUpper().IndexOf("CERAMICAC") == -1)) //SE FOR INDUSTRIA
                {
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(tpdoc.st_compoe_vl_totprod_nf,'A')", sAlias = "st_compoe_vl_totprod_nf" });
                }

                if (objbelGeraXml.nm_Cliente != "MOGPLAST")
                {
                    lCampos.Add(new CamposSelect { sCampo = "case when empresa.st_codprodnfe = 'C' then produto.cd_prod else produto.cd_alter end", sAlias = "cProd" });
                }
                else
                {
                    lCampos.Add(new CamposSelect
                    {
                        sCampo = "case when empresa.nm_empresa containing 'MOGPLAST' then " +
                                 "produto.ds_detalhe " +
                                 "else " +
                                 "case when empresa.st_codprodnfe = 'C' then " +
                                 "movitem.cd_prod else " +
                                 "movitem.cd_alter end " +
                                 "End ",
                        sAlias = "cProd"
                    });
                }
                if (belStaticPastas.CBARRAS == "True")
                {
                    lCampos.Add(new CamposSelect { sCampo = "produto.cd_barras", sAlias = "cEAN" });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "produto.cd_alter", sAlias = "cEAN" });
                }
                if (objbelGeraXml.nm_Cliente != "NAVE_THERM")
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.ds_prod", sAlias = "xProd" });
                }
                else
                {
                    lCampos.Add(new CamposSelect
                    {
                        sCampo = "case when produto.ds_prod_compl is not null then " +
                            "substring(produto.ds_prod_compl from 1 for 120) " +
                            "else " +
                            "produto.ds_prod end ",
                        sAlias = "xProd"
                    });
                }
                lCampos.Add(new CamposSelect { sCampo = "substring(clas_fis.ds_clasfis from 1 for 15)", sAlias = "NCM" });// Diego - 21/10 Lorenzon

                lCampos.Add(new CamposSelect { sCampo = "movitem.cd_cfop", sAlias = "CFOP" });
                lCampos.Add(new CamposSelect { sCampo = "unidades.cd_unfat", sAlias = "uCom" });//Diego - OS_ 25/08/10

                lCampos.Add(new CamposSelect { sCampo = "movitem.qt_prod", sAlias = "qCom", bAgrupa = bAgrupaCampos });


                if (objbelGeraXml.nm_Cliente.Equals("MAD_STA_RITA"))
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_comprimento", sAlias = "vl_comprimento", bAgrupa = bAgrupaCampos }); // Diego - OS_25550  
                }
                if (objbelGeraXml.nm_Cliente.Equals("ZINCOBRIL")) //os_25787                
                {
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(opereve.tp_industrializacao,'')", sAlias = "tp_industrializacao" });
                }

                lCampos.Add(new CamposSelect { sCampo = "coalesce(vl_uniprod_sem_desc,0)", sAlias = "vl_uniprod_sem_desc" });//cast -> OS_25771 // 6 casas decimais

                if (objdest.Uf != "EX")
                {
                    //cast(movitem.vl_uniprod as numeric (15,5))
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_uniprod", sAlias = "vUnCom" });//cast -> OS_25771 // 6 casas decimais
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "(case when movitem.vl_uniprod_ii = 0 then movitem.vl_uniprod else movitem.vl_uniprod_ii end)", sAlias = "vUnCom", bAgrupa = bAgrupaCampos });
                }
                if (objdest.Uf == "EX") //DIEGO - OS_24730
                {
                    lCampos.Add(new CamposSelect { sCampo = "(case when movitem.vl_uniprod_ii = 0 then movitem.vl_totbruto else (movitem.vl_uniprod_ii * movitem.qt_prod) end)", sAlias = "vProd", bAgrupa = bAgrupaCampos });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_totbruto", sAlias = "vProd", bAgrupa = bAgrupaCampos }); //2 casas decimais
                }//DIEGO - OS_24730 - FIM         

                lCampos.Add(new CamposSelect { sCampo = "movitem.vl_totliq", sAlias = "vl_totliq", bAgrupa = bAgrupaCampos });// Diego 0S_24595                

                if (LeRegWin.LeRegConfig("CodBarrasXml") == "True")
                {
                    lCampos.Add(new CamposSelect { sCampo = "produto.cd_barras", sAlias = "cEANTrib" });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "produto.cd_alter", sAlias = "cEANTrib" });
                }

                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_descsuframa,0) + coalesce(movitem.vl_desccofinssuframa,0) + coalesce(movitem.vl_descpissuframa,0)", sAlias = "vDescSuframa", bAgrupa = bAgrupaCampos });

                lCampos.Add(new CamposSelect { sCampo = "coalesce(nf.st_desc,'U')", sAlias = "st_desc" });
                lCampos.Add(new CamposSelect { sCampo = "movitem.cd_tpunid", sAlias = "uTrib" });
                lCampos.Add(new CamposSelect { sCampo = "movitem.qt_prod", sAlias = "qTrib", bAgrupa = bAgrupaCampos });


                if (objdest.Uf != "EX")
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_uniprod", sAlias = "vUnTrib" });
                }
                else
                {
                    // lCampos.Add(new CamposSelect { sCampo = "movitem.vl_uniprod_ii", sAlias = "vUnTrib" });
                    lCampos.Add(new CamposSelect { sCampo = "(case when movitem.vl_uniprod_ii = 0 then movitem.vl_uniprod else movitem.vl_uniprod_ii end)", sAlias = "vUnTrib", bAgrupa = bAgrupaCampos });
                }
                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_ii,0)", sAlias = "vl_ii" });
                lCampos.Add(new CamposSelect { sCampo = "movitem.cd_sittrib", sAlias = "CST" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(nf.st_ipi,'S')", sAlias = "st_ipi" }); //OS_25673
                lCampos.Add(new CamposSelect { sCampo = "movitem.vl_alicredicms", sAlias = "pCredSN" });//NFe_2.0
                lCampos.Add(new CamposSelect { sCampo = "movitem.vl_credicms", sAlias = "vCredICMSSN", bAgrupa = bAgrupaCampos }); // **

                #region BC_ICMS
                if (objdest.Uf != "EX")
                {
                    //if (objbelGeraXml.nm_Cliente != "TORCETEX") OS_27040
                    //{
                    lCampos.Add(new CamposSelect
                    {
                        sCampo = "case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then " +
                                 "case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then " +
                                 "case when coalesce(nf.st_ipi,'N') = 'N' then " +
                                 "CASE when (SELECT SUM(VL_TOTBRUTO) FROM movitem where ((movitem.cd_empresa = nf.cd_empresa) and (MOVITEM.cd_nfseq = NF.cd_nfseq))) > NF.vl_totprod then " +
                                 "((case when movitem.vl_redbase <> 0 then " +
                                 "case when  coalesce((select first 1 ST_ESTTERC from opereve where ((ST_ESTTERC = 'S') and ((TPDOC.cd_operval) containing cd_oper  ))),'N') = 'N' then " +
                                 "(movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where ((movitem.cd_empresa = nf.cd_empresa) and (MOVITEM.cd_nfseq = NF.cd_nfseq))) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) )) - (((100-coalesce(movitem.vl_redbase, 100)) * (movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where ((movitem.cd_empresa = nf.cd_empresa) and (MOVITEM.cd_nfseq = NF.cd_nfseq))) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) )))/ 100) " +
                                 "else " +
                                 "movitem.vl_totliq - (((100-coalesce(movitem.vl_redbase, 100)) * movitem.vl_totliq)/ 100) " +
                                 "end " +
                                 "else " +
                                 "movitem.vl_totliq " +
                                 "end +(case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) " +//OS 25385
                                 "else " +
                                 "((case when movitem.vl_redbase <> 0 then  " +
                                 "movitem.vl_totliq - (((100-coalesce(movitem.vl_redbase, 100)) * movitem.vl_totliq)/ 100) " +
                                 "else " +
                                 "movitem.vl_totliq " +
                                 "end + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) " +
                                 "end " +
                                 "else " +
                                 "(CASE when (SELECT SUM(VL_TOTBRUTO) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) > NF.vl_totprod then " +
                                 "((case when movitem.vl_redbase <> 0 then " +
                                 "(movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) )) - (((100-coalesce(movitem.vl_redbase, 100)) * (movitem.vl_totliq - ((SELECT SUM(VL_TOTBRUTO) FROM movitem where movitem.cd_empresa = nf.cd_empresa and MOVITEM.cd_nfseq = NF.cd_nfseq) - NF.vl_totprod) / ((SELECT COUNT(NR_LANC) FROM movitem where movitem.cd_EMPRESA = NF.CD_EMPRESA AND MOVITEM.cd_nfseq = NF.cd_nfseq) )))/ 100) " +
                                 "else " +
                                 "movitem.vl_totliq " +
                                 "end " +
                                 " + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) " +
                                 "else " +
                                 "((case when movitem.vl_redbase <> 0 then " +
                                 "movitem.vl_totliq - (((100-coalesce(movitem.vl_redbase, 100)) * movitem.vl_totliq)/ 100) " +
                                 "else " +
                                 "movitem.vl_totliq " +
                                 "end + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end))) " +
                                 "end) + movitem.vl_ipi " +
                                 " end " +
                                 " end " +
                                 " else " +
                                 " nf.vl_baseicm " +
                                 " end ",
                        sAlias = " vBC",
                        bAgrupa = bAgrupaCampos
                    });
                    //    }
                    //    else
                    //    {
                    //        lCampos.Add(new CamposSelect
                    //        {
                    //            sCampo =
                    //            "case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then " +
                    //            "case when coalesce(tpdoc.st_nfcompl, 'N') = 'N' then " +
                    //            "case when coalesce(nf.st_ipi,'N') = 'N' then" +
                    //            "((movitem.vl_totliq * movitem.vl_coefdesc) + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end)) " +
                    //            "else " +
                    //            "((movitem.vl_totliq * movitem.vl_coefdesc) + (case when (coalesce(tpdoc.ST_FRETE_ENTRA_ICMS_S,'N') <> 'N') AND (UF.VL_ALIICMSFRETE > 0) then  movitem.vl_frete else 0 end)) + movitem.vl_ipi " +
                    //            "end " +
                    //            "end " +
                    //            "else " +
                    //            "nf.vl_baseicm " +
                    //            "end ",
                    //            sAlias = "vBC",
                    //            bAgrupa = bAgrupaCampos
                    //        });
                    //    }
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_baseicm", sAlias = "vBC", bAgrupa = bAgrupaCampos });
                }
                #endregion

                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_bicmproprio_subst,0) ", sAlias = "vBCProp", bAgrupa = bAgrupaCampos });// Diego OS_25278
                lCampos.Add(new CamposSelect { sCampo = "movitem.vl_aliicms", sAlias = "pICMS" });//Diego - OS_24730


                //CODIGO COMENTADO OS_26817

                //if (objbelGeraXml.nm_Cliente == "PAVAX")
                //{
                //    if (objdest.Uf == "EX")
                //    {
                //        lCampos.Add(new CamposSelect { sCampo = "coalesce(MOVITEM.vl_icms_ii,0)", sAlias = "vICMS" });
                //    }
                //    else
                //    {
                //        lCampos.Add(new CamposSelect { sCampo = "movitem.vl_icms", sAlias = "vICMS" });
                //    }
                //}
                //else if (objbelGeraXml.nm_Cliente == "EMEB")
                //{
                //    if (objdest.Uf == "EX")
                //    {
                //        lCampos.Add(new CamposSelect { sCampo = "coalesce(MOVITEM.vl_icms_ii,0)", sAlias = "vICMS", bAgrupa = bAgrupaCampos });
                //    }
                //    else
                //    {
                //        lCampos.Add(new CamposSelect { sCampo = "(movitem.vl_icms + movitem.vl_icmproprio_subst)", sAlias = "vICMS", bAgrupa = bAgrupaCampos });
                //    }
                //}
                //else
                //{                
                //lCampos.Add(new CamposSelect { sCampo = "(movitem.vl_icms + movitem.vl_icmproprio_subst)", sAlias = "vICMS", bAgrupa = bAgrupaCampos });
                //} //Diego - OS_24730 - FIM     

                //os_26817 - inicio
                if (objdest.Uf == "EX")
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_icms", sAlias = "vICMS", bAgrupa = bAgrupaCampos });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "(movitem.vl_icms + movitem.vl_icmproprio_subst)", sAlias = "vICMS", bAgrupa = bAgrupaCampos });
                }
                //os_26817 - FIM

                lCampos.Add(new CamposSelect
                {
                    sCampo = "case when tpdoc.st_nfcompl = 'N' then " +
                        "coalesce(movitem.vl_bicmssubst, 0) " +
                        "else " +
                        "nf.VL_BICMSSU " +
                        "end ",
                    sAlias = "vBCST",
                    bAgrupa = bAgrupaCampos
                });

                lCampos.Add(new CamposSelect { sCampo = "coalesce(icm.vl_aliinte, 0)", sAlias = "pICMSST" });

                lCampos.Add(new CamposSelect
                {
                    sCampo = "case when tpdoc.st_nfcompl = 'N' then "
                        + "coalesce(movitem.vl_icmretsubst, 0) "
                        + "else "
                        + "nf.VL_ICMSSUB "
                        + "end ",
                    sAlias = "vICMSST",
                    bAgrupa = bAgrupaCampos
                });

                lCampos.Add(new CamposSelect { sCampo = "(100-coalesce(movitem.vl_redbase, 0)) ", sAlias = "pRedBC", bAgrupa = bAgrupaCampos });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(icm.vl_alisubs, 0)", sAlias = "pMVAST" });
                lCampos.Add(new CamposSelect { sCampo = "(100-coalesce(movitem.vl_redbase, 0))", sAlias = "pRedBCST", bAgrupa = bAgrupaCampos });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_aliipi, 0)", sAlias = "pIPI" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_ipi, 0)", sAlias = "vIPI", bAgrupa = bAgrupaCampos });
                if (bAgrupaCampos == false)
                {
                    lCampos.Add(new CamposSelect { sCampo = "nf.ds_anota", sAlias = "infAdProd", });
                }

                lCampos.Add(new CamposSelect { sCampo = "coalesce(clas_fis.st_tributacao, '1')", sAlias = "Tributa_ipi" });
                lCampos.Add(new CamposSelect { sCampo = "tpdoc.tp_doc", sAlias = "tp_doc" });

                lCampos.Add(new CamposSelect
                {
                    sCampo = "case when tpdoc.tp_doc = 'NS' then "
                        + "opereve.ST_CALCIPI_FA "
                        + "else "
                        + "opereve.st_ipi "
                        + "end ",
                    sAlias = "Calcula_IPI"
                });

                lCampos.Add(new CamposSelect { sCampo = "coalesce(opereve.st_hefrete, 'N')", sAlias = "st_hefrete" });

                lCampos.Add(new CamposSelect { sCampo = "opereve.st_piscofins", sAlias = "st_piscofins" });

                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_frete, 0)", sAlias = "vFrete", bAgrupa = bAgrupaCampos });

                if (bAgrupaCampos == false)
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.nr_lanc", sAlias = "nr_lanc" });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "0", sAlias = "nr_lanc", bAgrupa = bAgrupaCampos });
                }
                if (objdest.Uf.Equals("EX"))
                {
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_aliqcofins_cif , 0)", sAlias = "vl_aliqcofins_suframa", bAgrupa = bAgrupaCampos });//DIEGO - 24730 - 02/08
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.VL_ALIQPIS_CIF  , 0)", sAlias = "vl_aliqpis_suframa", bAgrupa = bAgrupaCampos });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(empresa.vl_aliqcofins_suframa, 0)", sAlias = "vl_aliqcofins_suframa", bAgrupa = bAgrupaCampos });
                    lCampos.Add(new CamposSelect { sCampo = "coalesce(empresa.vl_aliqpis_suframa, 0)", sAlias = "vl_aliqpis_suframa", bAgrupa = bAgrupaCampos });
                }
                lCampos.Add(new CamposSelect { sCampo = "endentr.ds_endent", sAlias = "xLgr" });
                lCampos.Add(new CamposSelect { sCampo = "endentr.ds_endent", sAlias = "xLgr" });
                lCampos.Add(new CamposSelect { sCampo = "endentr.nr_endent", sAlias = "nro" });
                lCampos.Add(new CamposSelect { sCampo = "endentr.nm_bairroent", sAlias = "xBairro" });
                lCampos.Add(new CamposSelect { sCampo = "endentr.nm_cident", sAlias = "cMun" });
                lCampos.Add(new CamposSelect { sCampo = "endentr.cd_ufent", sAlias = "UF" });
                lCampos.Add(new CamposSelect { sCampo = "listaserv.ds_codigo", sAlias = "cListserv" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_aliqserv, 0)", sAlias = "vAliqISS", bAgrupa = bAgrupaCampos });

                lCampos.Add(new CamposSelect { sCampo = "(movitem.vl_totliq * coalesce(movitem.vl_aliqserv, 0))/100", sAlias = "vIssqn", bAgrupa = bAgrupaCampos });

                lCampos.Add(new CamposSelect { sCampo = "movitem.vl_totliq", sAlias = "vBCISS", bAgrupa = bAgrupaCampos });

                lCampos.Add(new CamposSelect { sCampo = "cidades.cd_municipio", sAlias = "cMunFG" });


                //if (objbelGeraXml.nm_Cliente == "TORCETEX") OS_27040
                //{
                //    lCampos.Add(new CamposSelect { sCampo = "movitem.VL_COEFDESC", sAlias = "VL_COEFDESC" });
                //    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_cofins", sAlias = "vl_cofins" });
                //    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_pis", sAlias = "vl_pis" });
                //    lCampos.Add(new CamposSelect { sCampo = "((cast((cast((movitem.qt_prod * movitem.vl_uniprod) as numeric(15,4)) * movitem.vl_coefdesc) as numeric(15,2)) + movitem.vl_frete))", sAlias = "vl_basePisCofins", bAgrupa = bAgrupaCampos });
                //}
                //else
                //{
                lCampos.Add(new CamposSelect { sCampo = "movitem.VL_COEF", sAlias = "VL_COEF" });
                if (objdest.Uf.Equals("EX"))  //Diego - 02/08 - 24730
                {
                    //lCampos.Add(new CamposSelect { sCampo = "movitem.VL_COFINS_CIF", sAlias = "vl_cofins" });
                    //lCampos.Add(new CamposSelect { sCampo = "movitem.VL_PIS_CIF", sAlias = "vl_pis" });
                    lCampos.Add(new CamposSelect { sCampo = "movitem.VL_COFINS", sAlias = "vl_cofins", bAgrupa = bAgrupaCampos });
                    lCampos.Add(new CamposSelect { sCampo = "movitem.VL_PIS", sAlias = "vl_pis", bAgrupa = bAgrupaCampos });
                    //lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.VL_BASECOFINS_CIF, 0)", sAlias = "vl_basePisCofins" });
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_basecofins", sAlias = "vl_basecofins", bAgrupa = bAgrupaCampos });
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_basepis", sAlias = "vl_basepis", bAgrupa = bAgrupaCampos });
                }
                else
                {
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_cofins", sAlias = "vl_cofins", bAgrupa = bAgrupaCampos });
                    lCampos.Add(new CamposSelect { sCampo = "movitem.vl_pis", sAlias = "vl_pis", bAgrupa = bAgrupaCampos });
                    //lCampos.Add(new CamposSelect { sCampo = "((cast((cast((movitem.qt_prod * movitem.vl_uniprod) as numeric(15,2))*movitem.vl_coef) as numeric(15,2)) + movitem.vl_frete))", sAlias = "vl_basePisCofins", bAgrupa = bAgrupaCampos }); // Conceito alterado pela OS_26866, de acordo com Hamilton
                    lCampos.Add(new CamposSelect { sCampo = "(cast((movitem.qt_prod * movitem.vl_uniprod) as numeric(15,2)))", sAlias = "vl_basePisCofins", bAgrupa = bAgrupaCampos });

                }
                //}
                string sBanco = LeRegWin.LeRegConfig("BancoDados");
                if ((sBanco.ToUpper().IndexOf("COMERCIOC") == -1) && (sBanco.ToUpper().IndexOf("CERAMICAC") == -1)) //Claudinei - o.s. - 25/09/2009
                {
                    lCampos.Add(new CamposSelect
                    {
                        sCampo = "case when empresa.ST_RASTREABILIDADE = '1' " +
                            "then " +
                            "coalesce(movitem.nr_lote,'') " +
                            "else '' " +
                            "end",
                        sAlias = "nr_lote"
                    });
                    lCampos.Add(new CamposSelect { sCampo = "movitem.cd_prodcli", sAlias = "cd_prodcli" });
                }

                if (objbelGeraXml.nm_Cliente == "MARPA")
                {

                    lCampos.Add(new CamposSelect { sCampo = "nf.vl_desccomer ", sAlias = "Desconto_Valor" });
                    lCampos.Add(new CamposSelect { sCampo = "((nf.vl_desccomer / nf.vl_totnf)*100)", sAlias = "Desconto_Percentual" });
                }
                lCampos.Add(new CamposSelect { sCampo = "movitem.CD_SITTRIBCOF", sAlias = "CD_SITTRIBCOF" });
                lCampos.Add(new CamposSelect { sCampo = "movitem.CD_SITTRIBIPI", sAlias = "CD_SITTRIBIPI" });
                lCampos.Add(new CamposSelect { sCampo = "movitem.CD_SITTRIBPIS", sAlias = "CD_SITTRIBPIS" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(movitem.vl_outras,'0') ", sAlias = "vOutro" });
                //lCampos.Add(new strucCamposSelect { sCampo = "", sAlias = "" });

                lCampos.Add(new CamposSelect { sCampo = "coalesce(opereve.st_tpoper,'0')", sAlias = "st_tpoper" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(opereve.ST_ESTTERC,'N')", sAlias = "ST_ESTTERC" });//NFe_2.0 OS_25346
                lCampos.Add(new CamposSelect { sCampo = "tpdoc.cd_operval", sAlias = "cd_operval" });
                lCampos.Add(new CamposSelect { sCampo = "coalesce(Empresa.st_imp_cdpedcli, 'N')", sAlias = "st_imp_cdpedcli" });
                lCampos.Add(new CamposSelect { sCampo = "transpor.nm_trans", sAlias = "Redespacho" });
                lCampos.Add(new CamposSelect { sCampo = "transpor.ds_endnor", sAlias = "xLgrRedes" });
                lCampos.Add(new CamposSelect { sCampo = "transpor.nr_endnor", sAlias = "nroRedes" });
                lCampos.Add(new CamposSelect { sCampo = "transpor.ds_bairronor", sAlias = "xBairroRedes" });
                lCampos.Add(new CamposSelect { sCampo = "transpor.nm_cidnor", sAlias = "cmunRedes" });
                lCampos.Add(new CamposSelect { sCampo = "transpor.cd_ufnor", sAlias = "UFRedes" });

                #endregion

                sCampos.Append(Environment.NewLine + "Select " + Environment.NewLine);
                lCampos = lCampos.OrderBy(c => c.bAgrupa).ToList();
                for (int i = 0; i < lCampos.Count; i++)
                {
                    CamposSelect camp = lCampos[i];
                    string sFormat = "Sum({0}) ";
                    sCampos.Append((camp.bAgrupa ? string.Format(sFormat, camp.sCampo) : camp.sCampo) + " " + camp.sAlias + ((i + 1) != lCampos.Count() ? "," : "") + Environment.NewLine);
                }


                #region Inner Join
                //Tabelas
                sInnerJoin.Append("From MOVITEM ");

                //Relacionamentos
                sInnerJoin.Append("inner join nf on (nf.cd_empresa = movitem.cd_empresa)");
                sInnerJoin.Append(" and ");
                sInnerJoin.Append("(nf.cd_nfseq = movitem.cd_nfseq) ");
                sInnerJoin.Append("inner join empresa on (empresa.cd_empresa = movitem.cd_empresa) ");
                sInnerJoin.Append("inner join unidades on (movitem.cd_tpunid = unidades.cd_tpunid) "); // Diego - OS_ 25/08/10
                sInnerJoin.Append("left join clas_fis on (clas_fis.cd_empresa = movitem.cd_empresa)");
                sInnerJoin.Append(" and ");
                sInnerJoin.Append("(clas_fis.cd_cf = movitem.cd_cf) ");
                sInnerJoin.Append("left join icm on (icm.cd_ufnor = nf.cd_ufnor) ");
                sInnerJoin.Append("And ");
                sInnerJoin.Append("(icm.cd_aliicms = movitem.cd_aliicms) ");
                sInnerJoin.Append("left join opereve on (opereve.cd_oper = movitem.cd_oper) ");
                sInnerJoin.Append("left join tpdoc on (tpdoc.cd_tipodoc = nf.cd_tipodoc) ");
                sInnerJoin.Append("left join produto ");
                sInnerJoin.Append("on (produto.cd_empresa = movitem.cd_empresa) ");
                sInnerJoin.Append("and ");
                sInnerJoin.Append("(produto.cd_prod = movitem.cd_prod) ");
                sInnerJoin.Append("left join linhapro ");
                sInnerJoin.Append("on (linhapro.cd_empresa = produto.cd_empresa) ");
                sInnerJoin.Append("and ");
                sInnerJoin.Append("(linhapro.cd_linha = produto.cd_linha) ");
                sInnerJoin.Append("left join listaserv ");
                sInnerJoin.Append("on (listaserv.nr_lanc = linhapro.nr_lanclistaserv) ");
                sInnerJoin.Append("inner join clifor ");
                sInnerJoin.Append("on (clifor.cd_clifor = nf.cd_clifor) ");
                sInnerJoin.Append("left join cidades ");
                sInnerJoin.Append("on (cidades.nm_cidnor = clifor.nm_cidnor) ");
                sInnerJoin.Append("and ");
                sInnerJoin.Append("(cidades.cd_ufnor = clifor.cd_ufnor) ");
                sInnerJoin.Append("inner join uf on (clifor.cd_ufnor = uf.cd_uf) ");//25385
                sInnerJoin.Append("left join endentr on (endentr.cd_cliente = nf.cd_clifor) ");
                sInnerJoin.Append("and ");
                sInnerJoin.Append(" (endentr.cd_endent = nf.cd_endent) ");
                if ((objbelGeraXml.nm_Cliente == "NAVE_THERM") || (objbelGeraXml.nm_Cliente == "MOGPLAST"))
                {
                    sInnerJoin.Append("left join produto on (produto.cd_empresa = movitem.cd_empresa) ");
                    sInnerJoin.Append("And ");
                    sInnerJoin.Append("(produto.cd_prod = movitem.cd_prod)");
                }
                sInnerJoin.Append("left join transpor on (transpor.cd_trans = nf.cd_redes) ");
                #endregion

                #region Where
                sWhere.Append("Where ");
                sWhere.Append("(movitem.cd_empresa ='");
                sWhere.Append(sEmp);
                sWhere.Append("')");
                sWhere.Append(" and ");
                sWhere.Append("(nf.cd_nfseq = '");
                sWhere.Append(sNF);
                sWhere.Append("') ");
                sWhere.Append((bAgrupaCampos == false ? "Order by movitem.nr_lanc" : ""));
                #endregion

                if (bAgrupaCampos)
                {
                    sGroup.Append(Environment.NewLine + " Group by " + Environment.NewLine);
                    lCampos = lCampos.Where(c => c.bAgrupa == false).ToList();
                    for (int i = 0; i < lCampos.Count; i++)
                    {
                        CamposSelect camp = lCampos[i];
                        sGroup.Append((camp.bAgrupa == false ? camp.sCampo + ((i + 1) < lCampos.Count() ? ", " : "") + Environment.NewLine : ""));
                    }
                }
                string sNr_Lanc;
                string sql = "select max(nr_lanc) from movitem where (movitem.cd_empresa ='" +
                                                     sEmp +
                                                     "') and " +
                                                     "(movitem.cd_nfseq = '" +
                                                     sNF +
                                                     "') ";
                using (FbCommand cmd = new FbCommand(sql, Conn))
                {
                    sNr_Lanc = cmd.ExecuteScalar().ToString();
                }
                System.Globalization.Calendar bla;

                string sQueryItens = sCampos.ToString() + sInnerJoin + sWhere + (bAgrupaCampos ? sGroup.ToString() : "");

                FbCommand cmdItem = new FbCommand(sQueryItens, Conn);
                cmdItem.ExecuteNonQuery();
                FbDataReader drIItem = cmdItem.ExecuteReader();
                int iSeqItem = 0;
                dTotPis = 0;
                dTotCofins = 0;

                //ITem
                while (drIItem.Read())
                {
                    int indTot = 1;
                    //indTot = (VerificaItemEntraTotalNf(drIItem["st_servico"].ToString(),
                    //                                  drIItem["cd_oper"].ToString(),
                    //                                  drIItem["st_soma_dev_tot_nf"].ToString(),
                    //                                  drIItem["st_compoe_vl_totprod_nf"].ToString(),
                    //                                  Conn) == true ? 1 : 0);

                    //OS_25346 INICIO



                    if ((psNM_Banco.ToUpper().IndexOf("COMERCIOC") == -1) && (psNM_Banco.ToUpper().IndexOf("CERAMICAC") == -1))
                    {
                        if (drIItem["st_compoe_vl_totprod_nf"].ToString().Equals("A")) //Verifica se ambos os produtos vão entrar no total da nota
                        {
                            indTot = 1;
                        }
                        else if (drIItem["st_compoe_vl_totprod_nf"].ToString().Equals("D")) // verifica se movimenta estoque terceiro!! S - SIM / N-NÃO
                        {
                            indTot = (drIItem["ST_ESTTERC"].ToString().Equals("S") ? 1 : 0);
                        }
                        else if (drIItem["st_compoe_vl_totprod_nf"].ToString().Equals("P"))
                        {
                            indTot = (drIItem["st_tpoper"].ToString().Equals("0") ? 1 : 0);  // verifica se representa faturamento!! 0- SIM - 1 -NÃO
                        }
                        //OS_25346 INICIO - FIM 
                    }
                    else
                    {
                        indTot = 1;
                    }

                    belDet objdet = new belDet();
                    belImposto objimp = new belImposto();
                    belProd objprod = new belProd();
                    iSeqItem++;
                    objdet.Nitem = Convert.ToDecimal(iSeqItem.ToString().Trim());
                    objprod.Cprod = belUtil.TiraSimbolo(drIItem["cProd"].ToString().Trim(), "");

                    if (objbelGeraXml.nm_Cliente == "ZINCOBRIL") // OS_25787
                    {
                        objdet.tp_industrializacao = drIItem["tp_industrializacao"].ToString();
                    }
                    if (objbelGeraXml.nm_Cliente == "ESTACAHC")
                    {
                        objprod.Xprod = drIItem["qCom"].ToString() + "  " + drIItem["xProd"].ToString().Trim();
                    }
                    else
                    {
                        objprod.Xprod = drIItem["xProd"].ToString().Trim();
                    }
                    if (drIItem["NCM"].ToString() != "")
                    {
                        objprod.Ncm = ((belUtil.TiraSimbolo(drIItem["NCM"].ToString(), "")).PadRight(8, '0')).Substring(0, 8);
                    }
                    objprod.Cean = (Util.Util.IsNumeric(drIItem["cEAN"].ToString()) ? (Util.Util.ValidacEAN(drIItem["cEAN"].ToString()) ? drIItem["cEAN"].ToString() : "") : "");

                    if (!belUtil.ValidaCean13(objprod.Cean))
                    {
                        throw new Exception(string.Format("Código de Barras inválido!!{3}{3}Produto: {1}{3}Codigo: {2}{3}Codigo de barras: {0}.{3}Favor acertar o cadastro.{3}",
                                    objprod.Xprod, objprod.Cprod, objprod.Cean, Environment.NewLine));
                    }

                    objprod.Cfop = drIItem["CFOP"].ToString();
                    objprod.Ucom = belUtil.TiraSimbolo(drIItem["uCom"].ToString(), "");
                    if (objbelGeraXml.nm_Cliente.Equals("ESTACAHC"))
                    {
                        decimal dqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()) * Convert.ToDecimal(drIItem["vl_coef"].ToString()), 4);
                        objprod.Qcom = dqCom;
                    }
                    else if (objbelGeraXml.nm_Cliente.Equals("MAD_STA_RITA"))
                    {
                        decimal dqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()), 4);
                        decimal dComprimento = Math.Round(Convert.ToDecimal(drIItem["vl_comprimento"].ToString()), 4);
                        if (dComprimento == 0)
                        {
                            objprod.Qcom = dqCom;
                        }
                        else
                        {
                            objprod.Qcom = dqCom * dComprimento;
                        }
                    }
                    else
                    {
                        decimal dqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()), 4); //Claudinei - o.s. 24248 - 26/03/2010
                        objprod.Qcom = dqCom;
                    }
                    string sCasasVlUnit = LeRegWin.LeRegConfig("QtdeCasasVlUnit");
                    sCasasVlUnit = (sCasasVlUnit == "" ? "1" : sCasasVlUnit);

                    decimal dvUnCom = Math.Round(Convert.ToDecimal(drIItem["vUnCom"].ToString()), Convert.ToInt32(sCasasVlUnit)); //Claudinei - o.s. 24248 - 26/03/2010
                    objprod.Vuncom = dvUnCom;
                    ///Campo vl_totliq
                    decimal dvProd = 0;
                    decimal vl_prodDesc = 0;
                    decimal vl_desconto = 0;
                    if (objbelGeraXml.nm_Cliente == "ESTACAHC") // Diego - OS_24595
                    {
                        dvProd = Math.Round(Convert.ToDecimal(drIItem["vl_totliq"].ToString()), 2);
                    }
                    else
                    {
                        dvProd = Math.Round(Convert.ToDecimal(drIItem["vProd"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                    }
                    //if (objbelGeraXml.nm_Cliente == "TORCETEX") os_27040
                    //{
                    //    vl_prodDesc = Math.Round(dvProd * Convert.ToDecimal(drIItem["VL_COEFDESC"].ToString()), 2); //OS_25339 - DIEGO
                    //}
                    //else
                    //{
                    if (drIItem["st_desc"].ToString().Equals("U")) //25368
                    {
                        vl_prodDesc = dvProd; // Math.Round(dvProd * Convert.ToDecimal(drIItem["VL_COEF"].ToString()), 2);
                    }
                    else
                    {
                        if (objbelGeraXml.nm_Cliente == "LORENZON")
                        {

                            decimal dValorProdSemDesc;

                            if (drIItem["vl_uniprod_sem_desc"].ToString() != "0")
                            {
                                dValorProdSemDesc = Convert.ToDecimal(drIItem["vl_uniprod_sem_desc"].ToString()) * objprod.Qcom;
                            }
                            else
                            {
                                dValorProdSemDesc = dvProd;
                            }
                            vl_prodDesc = Math.Round((dValorProdSemDesc) * Convert.ToDecimal(drIItem["VL_COEF"].ToString()), 2);
                        }
                        else
                        {
                            vl_prodDesc = Math.Round(dvProd * Convert.ToDecimal(drIItem["VL_COEF"].ToString()), 2);
                        }
                    }
                    // }
                    pbIndustri = bIndustrializacao(drIItem["cd_operval"].ToString(), Conn);

                    if (VerificaNotaComSuframa(sEmp, sNF, Conn)) //NFe_2.0
                    {
                        vl_desconto = Convert.ToDecimal(drIItem["vDescSuframa"].ToString());
                    }
                    else
                    {
                        vl_desconto = dvProd - vl_prodDesc;
                    }

                    if (vl_desconto == 0)
                    {
                        vl_desconto = BuscaDescTotal(sEmp, sNF, Conn);
                    }
                    if (drIItem["st_hefrete"].ToString() == "S")
                    {
                        dvProd = 0;
                    }
                    //if (objbelGeraXml.nm_Cliente == "TORCETEX")//OS_25339 - DIEGO
                    //{
                    //    objprod.Vprod = vl_prodDesc;
                    //}
                    //else
                    {
                        objprod.Vprod = dvProd;
                    }
                    objprod.Ceantrib = objprod.Cean;
                    objprod.Utrib = belUtil.TiraSimbolo(drIItem["uTrib"].ToString(), "");
                    objprod.VOutro = Convert.ToDecimal(drIItem["vOutro"].ToString()); // NFe_2.0



                    objprod.IndTot = indTot;//Convert.ToInt16(drIItem["indTot"].ToString()); // NFe_2.0
                    if (objbelGeraXml.nm_Cliente.Equals("ESTACAHC"))
                    {
                        decimal dvqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()) * Convert.ToDecimal(drIItem["vl_coef"].ToString()), 4);
                        objprod.Qtrib = dvqCom;
                    }
                    else if (objbelGeraXml.nm_Cliente.Equals("MAD_STA_RITA"))
                    {
                        objprod.Qtrib = objprod.Qcom;
                    }
                    else
                    {
                        decimal dvqCom = Math.Round(Convert.ToDecimal(drIItem["qCom"].ToString()), 4); // o.s. 24248 - 26/03/2010
                        objprod.Qtrib = dvqCom;
                    }
                    decimal dvUnTrib = Math.Round(Convert.ToDecimal(drIItem["vUnTrib"].ToString()), Convert.ToInt32(sCasasVlUnit)); //o.s. 24248 - 26/03/2010

                    objprod.Vuntrib = dvUnTrib;
                    if (drIItem["vFrete"].ToString() != "0")
                    {
                        decimal dvFrete = Math.Round(Convert.ToDecimal(drIItem["vFrete"].ToString()), 2); // o.s. 24248 - 26/03/2010
                        objprod.Vfrete = dvFrete;
                    }
                    if (vl_desconto > 0)
                    {
                        objprod.Vdesc = vl_desconto;
                    }

                    if (objdest.Uf.Equals("EX"))
                    {
                        objprod.belDI = new List<belDI>();
                        StringBuilder sQuery = new StringBuilder();
                        sQuery.Append("select ");
                        sQuery.Append("impdecla.nr_lanc, ");
                        sQuery.Append("impdecla.cd_empresa, ");
                        sQuery.Append("coalesce(impdecla.nr_di,'') nDI, ");
                        sQuery.Append("coalesce(impdecla.dt_di,'') dDI, ");
                        sQuery.Append("coalesce(impdecla.ds_localdesemb ,'') xLocDesemb, ");
                        sQuery.Append("coalesce(impdecla.uf_desemb,'') UFDesemb, ");
                        sQuery.Append("coalesce(impdecla.dt_desemb ,'')dDesemb, ");
                        sQuery.Append("coalesce(impdecla.cd_exportador,'') cExportador ");
                        sQuery.Append("from impdecla ");
                        sQuery.Append("where impdecla.nr_lancmovitem = '" + drIItem["nr_lanc"].ToString() + "' ");
                        sQuery.Append("and impdecla.cd_empresa = '" + sEmp + "' ");

                        using (FbCommand cmd = new FbCommand(sQuery.ToString(), Conn))
                        {
                            if (Conn.State == ConnectionState.Closed)
                            {
                                Conn.Open();
                            }
                            FbDataReader dr = cmd.ExecuteReader();

                            while (dr.Read())
                            {
                                belDI objDI = new belDI();
                                objDI.nDI = dr["nDI"].ToString();
                                objDI.DDI = (dr["dDI"].ToString() != "" ? Convert.ToDateTime(dr["dDI"].ToString()) : HLP.Util.Util.GetDateServidor().Date);
                                objDI.xLocDesemb = dr["xLocDesemb"].ToString();
                                objDI.UFDesemb = dr["UFDesemb"].ToString();
                                objDI.dDesemb = (dr["dDesemb"].ToString() != "" ? Convert.ToDateTime(dr["dDesemb"].ToString()) : HLP.Util.Util.GetDateServidor().Date);
                                objDI.cExportador = dr["cExportador"].ToString();

                                sQuery = new StringBuilder();
                                sQuery.Append("select ");
                                sQuery.Append("impadica.nr_lanc, ");
                                sQuery.Append("impadica.nr_lancimpdecla,");
                                sQuery.Append("coalesce(impadica.nr_adicao,'0') nAdicao, ");
                                sQuery.Append("coalesce(impadica.nr_lanc,'0') nSeqAdic, ");
                                sQuery.Append(" coalesce(impadica.cd_fabricante,'') cFabricante, ");
                                sQuery.Append(" coalesce(impadica.vl_descdi,'0') vDescDI ");
                                sQuery.Append("from impadica ");
                                sQuery.Append("where impadica.nr_lancimpdecla = '" + dr["nr_lanc"].ToString() + "' ");
                                sQuery.Append("and impadica.cd_empresa = '" + sEmp + "' ");

                                using (FbCommand cmd2 = new FbCommand(sQuery.ToString(), Conn))
                                {
                                    FbDataReader dr2 = cmd2.ExecuteReader();
                                    objDI.adi = new List<beladi>();
                                    while (dr2.Read())
                                    {
                                        beladi objadi = new beladi();

                                        objadi.cFabricante = dr2["cFabricante"].ToString();
                                        objadi.nAdicao = Convert.ToInt32(dr2["nAdicao"].ToString());
                                        objadi.nSeqAdic = Convert.ToInt32(dr2["nSeqAdic"].ToString().Replace("0", ""));
                                        objadi.vDescDI = Convert.ToDecimal(dr2["vDescDI"].ToString().Replace(".", ","));
                                        objDI.adi.Add(objadi);
                                    }
                                }
                                objprod.belDI.Add(objDI);
                            }

                        }
                    }
                    objprod.XPed = drIItem["xPed"].ToString();
                    objprod.NItemPed = drIItem["nItemPed"].ToString();

                    objdet.belProd = objprod;

                    //Impostos

                    #region ICMS
                    belIcms objicms = new belIcms();
                    string sCST = drIItem["CST"].ToString();
                    string sSimplesNac = VerificaEmpresaSimplesNac(sEmp, Conn); // Diego - OS 24918 - 14/09/2010
                    decimal dvBC = 0;
                    decimal dvBCProp = 0; //25278
                    if (sSimplesNac == "N" || sSimplesNac == "")
                    {
                        dvBC = Math.Round(Convert.ToDecimal(drIItem["vBC"].ToString()), 2);
                        if (drIItem["vBCProp"].ToString() != "0")
                        {
                            dvBCProp = Math.Round(Convert.ToDecimal(drIItem["vBCProp"].ToString().Replace(".", ",")), 2); //25278
                        }
                    }
                    bool bPauta = drIItem["st_pauta"].ToString().Equals("N") ? false : true; //OS_25969
                    decimal dvICMS = (bPauta == false ? (drIItem["vICMS"].ToString() != "" ? Math.Round(Convert.ToDecimal(drIItem["vICMS"].ToString()), 2) : 0) : Math.Round(Convert.ToDecimal(drIItem["vl_icms_Pauta"].ToString()), 2)); //o.s. 24248 - 26/03/2010  - //OS_25969
                    decimal dvBC_pauta = Convert.ToDecimal(drIItem["vBC_Pauta"].ToString()); //OS_25969


                    //cst novas - > SUPER SIMPLES

                    if (!Util.Util.VerificaNovaST(sCST))
                    {
                        #region CST_ANTIGAS
                        switch (sCST.Substring(1, 2))
                        {
                            case "00":
                                {
                                    #region 00
                                    belIcms00 obj00 = new belIcms00();
                                    obj00.Orig = sCST.ToString().Substring(0, 1);
                                    obj00.Cst = sCST.ToString().Substring(1, 2);
                                    obj00.Modbc = "3";
                                    obj00.Vbc = (bPauta ? dvBC_pauta : dvBC);
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); //o.s. 24248 - 26/03/2010
                                    obj00.Picms = dpICMS;
                                    obj00.Vicms = dvICMS;
                                    objicms.belIcms00 = obj00;
                                    #endregion

                                    break;
                                }
                            case "10":
                                {
                                    #region 010
                                    belIcms10 obj10 = new belIcms10();
                                    obj10.Orig = sCST.ToString().Substring(0, 1);
                                    obj10.Cst = sCST.ToString().Substring(1, 2);
                                    obj10.Modbc = "0";
                                    obj10.Vbc = (bPauta ? dvBC_pauta : (dvBCProp == 0 ? (dvICMS == 0 ? 0 : dvBC) : dvBCProp)); // 25278
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2);
                                    obj10.Picms = dpICMS;
                                    //dvICMS = (dvBC * dpICMS) / 100;
                                    obj10.Vicms = dvICMS;
                                    obj10.Modbcst = 4;
                                    decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2);
                                    obj10.Pmvast = dpMVAST;
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    obj10.Vbcst = dvBCST;
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2);
                                    obj10.Picmsst = dpICMSST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    obj10.Vicmsst = dvICMSST;
                                    objicms.belIcms10 = obj10;
                                    break;
                                    #endregion
                                }
                            case "20":
                                {
                                    #region 020
                                    belIcms20 obj20 = new belIcms20();
                                    obj20.Orig = sCST.ToString().Substring(0, 1);
                                    obj20.Cst = sCST.ToString().Substring(1, 2);
                                    obj20.Modbc = "3";
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBC == 100)
                                    {
                                        dpRedBC = 0;
                                    }
                                    obj20.Predbc = dpRedBC;
                                    obj20.Vbc = (bPauta ? dvBC_pauta : dvBC);
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj20.Picms = dpICMS;
                                    obj20.Vicms = dvICMS;
                                    objicms.belIcms20 = obj20;
                                    break;
                                    #endregion
                                }
                            case "30":
                                {
                                    #region 030
                                    belIcms30 obj30 = new belIcms30();
                                    obj30.Orig = sCST.ToString().Substring(0, 1);
                                    obj30.Cst = sCST.ToString().Substring(1, 2);
                                    obj30.Modbcst = 3;
                                    decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Pmvast = dpMVAST;
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Predbcst = dpRedBCST;
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Vbcst = dvBCST;
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Picmsst = dpICMSST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj30.Vicmsst = dvICMSST;
                                    objicms.belIcms30 = obj30;
                                    break;
                                    #endregion
                                }
                            case "40":
                                {
                                    #region 040
                                    belIcms40 obj40 = new belIcms40();
                                    obj40.Orig = sCST.ToString().Substring(0, 1);
                                    obj40.Cst = sCST.ToString().Substring(1, 2);
                                    obj40.Vicms = dvICMS; // NFe_2.0
                                    obj40.motDesICMS = (dvICMS > 0 ? (VerificaNotaComSuframa(sEmp, sNF, Conn) == false ? 9 : 7) : 0); // NFe_2.0
                                    dvBC = (bPauta ? dvBC_pauta : 0);
                                    objicms.belIcms40 = obj40;
                                    break;
                                    #endregion
                                }
                            case "41":
                                {
                                    #region 041
                                    belIcms40 obj40 = new belIcms40();
                                    obj40.Orig = sCST.ToString().Substring(0, 1);
                                    obj40.Cst = sCST.ToString().Substring(1, 2);
                                    obj40.Vicms = dvICMS; // NFe_2.0
                                    obj40.motDesICMS = (dvICMS > 0 ? (VerificaNotaComSuframa(sEmp, sNF, Conn) == false ? 9 : 7) : 0); // NFe_2.0
                                    dvBC = (bPauta ? dvBC_pauta : 0);
                                    objicms.belIcms40 = obj40;
                                    break;
                                    #endregion
                                }
                            case "50":
                                {
                                    #region 050
                                    belIcms40 obj40 = new belIcms40();
                                    obj40.Orig = sCST.ToString().Substring(0, 1);
                                    obj40.Cst = sCST.ToString().Substring(1, 2);
                                    obj40.Vicms = dvICMS; // NFe_2.0
                                    obj40.motDesICMS = (dvICMS > 0 ? (VerificaNotaComSuframa(sEmp, sNF, Conn) == false ? 9 : 7) : 0); // NFe_2.0
                                    dvBC = (bPauta ? dvBC_pauta : 0);
                                    objicms.belIcms40 = obj40;
                                    break;
                                    #endregion
                                }
                            case "51":
                                {
                                    #region 051
                                    belIcms51 obj51 = new belIcms51();
                                    obj51.Orig = sCST.ToString().Substring(0, 1);
                                    obj51.Cst = sCST.ToString().Substring(1, 2);
                                    obj51.Modbc = "3";
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBC == 100)
                                    {
                                        dpRedBC = 0;
                                    }
                                    obj51.Predbc = dpRedBC;
                                    obj51.Vbc = (bPauta ? dvBC_pauta : 0);// Math.Round(Convert.ToDecimal(drIItem["vBC"].ToString()), 2); // DIEGO- OS_24591 - 26/06/2010 INICIO E FIM
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj51.Picms = dpICMS;
                                    obj51.Vicms = dvICMS;
                                    objicms.belIcms51 = obj51;
                                    break;
                                    #endregion
                                }
                            case "60":
                                {
                                    #region 060
                                    belIcms60 obj60 = new belIcms60();
                                    obj60.Orig = sCST.ToString().Substring(0, 1);
                                    obj60.Cst = sCST.ToString().Substring(1, 2);
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj60.Vbcst = dvBCST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj60.Vicmsst = dvICMSST;
                                    objicms.belIcms60 = obj60;
                                    break;
                                    #endregion
                                }

                            case "70":
                                {
                                    #region 070
                                    belIcms70 obj70 = new belIcms70();
                                    obj70.Orig = sCST.ToString().Substring(0, 1);
                                    obj70.Cst = sCST.ToString().Substring(1, 2);
                                    obj70.Modbc = "3";
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBC == 100)
                                    {
                                        dpRedBC = 0;
                                    }
                                    obj70.Predbc = dpRedBC;
                                    obj70.Vbc = (bPauta ? dvBC_pauta : dvBC);
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); // o.s. 24248 - 26/03/20103
                                    obj70.Picms = dpICMS;
                                    //dvICMS = (dvBC * dpICMS) / 100; //OS_25856

                                    obj70.Vicms = dvICMS;
                                    obj70.Modbcst = 0;
                                    decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    obj70.Pmvast = dpMVAST;
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                    if (dpRedBCST == 100)
                                    {
                                        dpRedBCST = 0;
                                    }
                                    obj70.Predbcst = dpRedBCST;
                                    if (!drIItem["vBCST"].Equals(string.Empty))
                                    {
                                        decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                        obj70.Vbcst = dvBCST;
                                    }
                                    if (!drIItem["pICMSST"].Equals(string.Empty))
                                    {
                                        decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2); // o.s. 24248 - 26/03/2010
                                        obj70.Picmsst = dpICMSST;
                                    }
                                    if (!drIItem["vICMSST"].Equals(string.Empty))
                                    {
                                        decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2); ;
                                        obj70.Vicmsst = dvICMSST;
                                    }
                                    objicms.belIcms70 = obj70;
                                    break;
                                    #endregion
                                }

                            case "90":
                                {
                                    #region 090
                                    belIcms90 obj90 = new belIcms90();
                                    obj90.Orig = sCST.ToString().Substring(0, 1);
                                    obj90.Cst = sCST.ToString().Substring(1, 2);
                                    obj90.Modbc = "3";
                                    dvBC = 0;
                                    obj90.Vbc = (bPauta ? dvBC_pauta : dvBC);
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpRedBC = 0;
                                    if (dpRedBC != 0)
                                    {
                                        obj90.Predbc = dpRedBC;
                                    }
                                    decimal dpICMS = Math.Round(Convert.ToDecimal(drIItem["pICMS"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpICMS = 0;
                                    obj90.Picms = dpICMS;

                                    dvICMS = 0;
                                    obj90.Vicms = dvICMS;
                                    obj90.Modbcst = 3;
                                    if (drIItem["pMVAST"].ToString() != "0")
                                    {
                                        decimal dpMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                        dpMVAST = 0;
                                        obj90.Pmvast = dpMVAST;
                                    }
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpRedBCST = 0;
                                    if (dpRedBCST != 0)
                                    {
                                        obj90.Predbcst = dpRedBCST;
                                    }
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dvBCST = 0;
                                    obj90.Vbcst = dvICMS;
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                    dpICMSST = 0;
                                    obj90.Picmsst = dpICMSST;
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    dvICMSST = 0;
                                    obj90.Vicmsst = dvICMSST;
                                    objicms.belIcms90 = obj90;
                                    break;
                                    #endregion
                                }
                        }

                        #endregion
                    }
                    else
                    {
                        string sOrig = drIItem["Orig"].ToString();

                        #region CTS_NOVAS
                        switch ((Util.Util.RetornaSTnovaAserUsada(sCST)))
                        {
                            case "101":
                                {
                                    #region 101
                                    belICMSSN101 obj101 = new belICMSSN101();
                                    obj101.orig = sOrig;//(objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj101.CSOSN = sCST.ToString();
                                    obj101.pCredSN = Math.Round(Convert.ToDecimal(drIItem["pCredSN"].ToString()), 2);//NFe_2.0
                                    obj101.vCredICMSSN = Math.Round(Convert.ToDecimal(drIItem["vCredICMSSN"].ToString()), 2); //NFe_2.0
                                    objicms.belICMSSN101 = obj101;
                                    #endregion
                                }
                                break;

                            case "102":
                                {
                                    #region 102
                                    belICMSSN102 obj102 = new belICMSSN102();
                                    obj102.orig = sOrig; //(objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj102.CSOSN = sCST.ToString();
                                    objicms.belICMSSN102 = obj102;
                                    #endregion

                                }
                                break;
                            case "201":
                                {
                                    #region 201
                                    belICMSSN201 obj201 = new belICMSSN201();
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2);
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2);
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);

                                    obj201.orig = sOrig; //(objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj201.CSOSN = sCST.ToString();
                                    obj201.modBCST = 3;
                                    obj201.pMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2);
                                    obj201.vBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    obj201.pICMSST = dpICMSST;
                                    obj201.vICMSST = dvICMSST;
                                    obj201.pCredSN = Math.Round(Convert.ToDecimal(drIItem["pCredSN"].ToString()), 2);//NFe_2.0
                                    obj201.vCredICMSSN = Math.Round(Convert.ToDecimal(drIItem["vCredICMSSN"].ToString()), 2); //NFe_2.0                                   
                                    if (dpRedBCST != 0)
                                    {
                                        obj201.pRedBCST = dpRedBCST;
                                    }
                                    objicms.belICMSSN201 = obj201;
                                    #endregion
                                }
                                break;
                            case "500":
                                {
                                    #region 500
                                    belICMSSN500 obj500 = new belICMSSN500();
                                    obj500.orig = sOrig; //(objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj500.CSOSN = sCST.ToString();
                                    decimal dvBCSTRet = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    decimal dvICMSSTRet = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    obj500.vBCSTRet = dvBCSTRet;
                                    obj500.vICMSSTRet = dvICMSSTRet;
                                    objicms.belICMSSN500 = obj500;
                                    #endregion
                                }
                                break;
                            case "900":
                                {
                                    #region 900
                                    belICMSSN900 obj900 = new belICMSSN900();
                                    decimal dpRedBCST = Math.Round(Convert.ToDecimal(drIItem["pRedBCST"].ToString()), 2);
                                    decimal dpICMSST = Math.Round(Convert.ToDecimal(drIItem["pICMSST"].ToString()), 2);
                                    decimal dvICMSST = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);
                                    decimal dvBCSTRet = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    decimal dvICMSSTRet = Math.Round(Convert.ToDecimal(drIItem["vICMSST"].ToString()), 2);

                                    obj900.orig = sOrig; //(objdest.Uf.Equals("EX") ? "1" : "0");
                                    obj900.CSOSN = sCST.ToString();
                                    obj900.modBC = 3;
                                    obj900.vBC = (bPauta ? dvBC_pauta : dvBC);
                                    decimal dpRedBC = Math.Round(Convert.ToDecimal(drIItem["pRedBC"].ToString()), 2);
                                    if (dpRedBC != 0)
                                    {
                                        obj900.pRedBC = dpRedBC;
                                    }
                                    obj900.vICMS = dvICMS;
                                    obj900.modBCST = 3;
                                    obj900.pMVAST = Math.Round(Convert.ToDecimal(drIItem["pMVAST"].ToString()), 2);
                                    if (dpRedBCST != 0)
                                    {
                                        obj900.pRedBCST = dpRedBCST;
                                    }
                                    decimal dvBCST = Math.Round(Convert.ToDecimal(drIItem["vBCST"].ToString()), 2);
                                    obj900.vBCST = dvBCST;
                                    obj900.pICMSST = dpICMSST;
                                    obj900.vICMSST = dvICMSST;
                                    obj900.vBCSTRet = dvBCSTRet;
                                    obj900.vICMSSTRet = dvICMSSTRet;
                                    obj900.pCredSN = Math.Round(Convert.ToDecimal(drIItem["pCredSN"].ToString()), 2);//NFe_2.0
                                    obj900.vCredICMSSN = Math.Round(Convert.ToDecimal(drIItem["vCredICMSSN"].ToString()), 2); //NFe_2.0                                    

                                    // Alteração feita por motivo de NFe para a Lorenzon

                                    obj900.modBC = null;
                                    obj900.vBC = null;
                                    obj900.pRedBC = null;
                                    obj900.pICMS = null;
                                    obj900.vICMS = null;
                                    obj900.modBCST = null;
                                    obj900.pMVAST = null;
                                    obj900.pRedBCST = null;
                                    obj900.vBCST = null;
                                    obj900.pICMSST = null;
                                    obj900.vICMSST = null;
                                    obj900.vBCSTRet = null;
                                    obj900.vICMSSTRet = null;
                                    obj900.pCredSN = null;//NFe_2.0
                                    obj900.vCredICMSSN = null; //NFe_2.0                                    

                                    objicms.belICMSSN900 = obj900;
                                    #endregion
                                }
                                break;
                        }
                        #endregion
                    }

                    if ((dvBC != 0) && (Convert.ToDecimal(drIItem["pICMS"].ToString()) != 0))
                    {
                        dTotbaseICMS += dvBC;
                        dTotValorICMS += dvICMS;
                    }
                    objimp.belIcms = objicms;
                    #endregion

                    #region IPI
                    belIpi objipi = new belIpi();
                    if (drIItem["CD_SITTRIBIPI"].ToString() == "")
                        throw new Exception("Situação Tributária do IPI está vazia na NF");

                    string sTributaIPI = drIItem["cd_sittribipi"].ToString().PadLeft(2, '0');
                    if ((sTributaIPI == "49") || (sTributaIPI == "00") || (sTributaIPI == "50") || (sTributaIPI == "99"))
                    {
                        belIpitrib objipitrib = new belIpitrib();
                        objipi.Cenq = "999";
                        objipitrib.Cst = sTributaIPI;
                        if (drIItem["ST_SUPERSIMPLES"].ToString() == "N")
                        {
                            if (!drIItem["vBC"].Equals(string.Empty))
                            {
                                if (objdest.Uf.Equals("EX"))
                                {
                                    //decimal ddvBC = Math.Round((Convert.ToDecimal(drIItem["vUnCom"]) + Convert.ToDecimal(drIItem["pIPI"])) * (Convert.ToDecimal(drIItem["qCom"])), 2);  // Diego - 24730 - 02/08/10
                                    //VL_BASEIPI
                                    decimal ddvBC = Convert.ToDecimal(drIItem["VL_BASEIPI"].ToString());
                                    objipitrib.Vbc = ddvBC;
                                }
                                else
                                {
                                    decimal ddvBC = Math.Round(Convert.ToDecimal(drIItem["vBC"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010

                                    if (drIItem["st_frete_entra_ipi_s"].ToString().Equals("S") && drIItem["ST_FRETE_ENTRA_ICMS_S"].ToString().Equals("N")) //OS_26866
                                    {
                                        ddvBC = ddvBC + objprod.Vfrete;
                                    }
                                    else if (drIItem["st_frete_entra_ipi_s"].ToString().Equals("N") && drIItem["ST_FRETE_ENTRA_ICMS_S"].ToString().Equals("S")) //OS_26866
                                    {
                                        ddvBC = ddvBC - objprod.Vfrete;
                                    }

                                    objipitrib.Vbc = ddvBC;
                                }
                            }
                            if (!drIItem["pIPI"].Equals(string.Empty))
                            {
                                decimal dpIPI = Math.Round(Convert.ToDecimal(drIItem["pIPI"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                objipitrib.Pipi = dpIPI;
                            }
                            if (!drIItem["vIPI"].Equals(string.Empty))
                            {
                                decimal dvIPI = Math.Round(Convert.ToDecimal(drIItem["vIPI"].ToString()), 2); //Claudinei - o.s. 24248 - 26/03/2010
                                objipitrib.Vipi = dvIPI;
                            }

                            if ((objbelGeraXml.nm_Cliente.Equals("EMBALATEC")) && (drIItem["st_ipi"].ToString().Equals("S")))//OS_25673
                            {
                                objipitrib.Vbc = objipitrib.Vbc - objipitrib.Vipi;
                            }
                        }
                        objipi.belIpitrib = objipitrib;
                    }
                    else
                    {
                        belIpint objipint = new belIpint();
                        objipi.Cenq = "999";
                        objipint.Cst = sTributaIPI;
                        objipi.belIpint = objipint;
                    }
                    objimp.belIpi = objipi;
                    #endregion

                    #region II
                    //Imposto de importação
                    belIi objii = new belIi();
                    objii.Vbc = (objdest.Uf == "EX" ? Convert.ToDecimal(drIItem["vUnTrib"].ToString()) : 0);
                    objii.Vdespadu = 0;
                    objii.Vii = (objdest.Uf == "EX" ? Convert.ToDecimal(drIItem["VL_II"].ToString()) : 0); ;
                    objii.Viof = 0;
                    objimp.belIi = objii;
                    //Fim - II                    
                    #endregion

                    #region PIS

                    if (drIItem["CD_SITTRIBPIS"].ToString() == "")
                    {
                        throw new Exception("Situação Tributária do PIS está vazia na NF");
                    }
                    string sCst = drIItem["CD_SITTRIBPIS"].ToString().PadLeft(2, '0'); //o.s. 23672 - 10/09/2009
                    //PIS
                    belPis objpis = new belPis();
                    //opereve.st_piscofins
                    if ((drIItem["st_piscofins"].ToString() == "S") && (Convert.ToInt16(sCst) < 4))
                    {
                        belPisaliq objpisaliq = new belPisaliq();
                        objpisaliq.Cst = sCst;
                        decimal dvlBasepis = (objdest.Uf != "EX" ? Math.Round(Convert.ToDecimal(drIItem["vl_basePisCofins"].ToString()), 2) : Math.Round(Convert.ToDecimal(drIItem["vl_basepis"].ToString()), 2));
                        objpisaliq.Vbc = (objpisaliq.Cst.ToString().Equals("99") ? 0 : dvlBasepis); //24872
                        decimal dpPis = Math.Round(Convert.ToDecimal(drIItem["vl_aliqpis_suframa"].ToString()), 2);
                        objpisaliq.Ppis = (objpisaliq.Cst.ToString().Equals("99") ? 0 : dpPis);
                        decimal dvPIS = Math.Round(Convert.ToDecimal(drIItem["vl_pis"].ToString()), 2);
                        dTotPis += dvPIS;
                        objpisaliq.Vpis = (objpisaliq.Cst.ToString().Equals("99") ? 0 : dvPIS);
                        objpis.belPisaliq = objpisaliq;
                    }
                    else if (Convert.ToInt16(sCst) == 99)
                    {
                        // Diego - OS_24585 - 25/06/2010
                        sSimplesNac = VerificaEmpresaSimplesNac(sEmp, Conn);
                        belPisoutr objpisoutr = new belPisoutr();
                        objpisoutr.Cst = sCst;
                        objpisoutr.Vbc = 0;
                        objpisoutr.Ppis = 0;
                        dTotPis += 0;
                        objpisoutr.Vpis = 0;
                        objpis.belPisoutr = objpisoutr;
                        // Diego - OS_24585 - 25/06/2010 - FIM
                    }
                    else
                    {
                        belPisnt objpisnt = new belPisnt();
                        objpisnt.Cst = sCst;
                        objpis.belPisnt = objpisnt;
                    }
                    objimp.belPis = objpis;
                    //Fim PIS

                    #endregion

                    #region Cofins
                    //Cofins
                    if (drIItem["cd_sittribcof"].ToString() == "")
                    {
                        throw new Exception("Situação Tributária do COFINS está vazia na NF");
                    }
                    belCofins objcofins = new belCofins();
                    if ((drIItem["st_piscofins"].ToString() == "S") && (Convert.ToInt16(drIItem["cd_sittribcof"].ToString()) < 4))
                    {
                        belCofinsaliq objcofinsaliq = new belCofinsaliq();
                        objcofinsaliq.Cst = drIItem["cd_sittribcof"].ToString().PadLeft(2, '0');
                        decimal dvlBaseCofins = (objdest.Uf != "EX" ? Math.Round(Convert.ToDecimal(drIItem["vl_basePisCofins"].ToString()), 2) : Math.Round(Convert.ToDecimal(drIItem["vl_basecofins"].ToString()), 2)); //o.s. 24248 - 26/03/2010
                        objcofinsaliq.Vbc = dvlBaseCofins;
                        decimal dpCofins = Math.Round(Convert.ToDecimal(drIItem["vl_aliqcofins_suframa"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objcofinsaliq.Pcofins = dpCofins;
                        decimal dvCofins = Math.Round(Convert.ToDecimal(drIItem["vl_cofins"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objcofinsaliq.Vcofins = dvCofins;
                        objcofins.belCofinsaliq = objcofinsaliq;
                    }
                    else if ((drIItem["cd_sittribcof"].ToString()) == "04"
                        || (drIItem["cd_sittribcof"].ToString()) == "06"
                        || (drIItem["cd_sittribcof"].ToString()) == "07"
                        || (drIItem["cd_sittribcof"].ToString()) == "08"
                        || (drIItem["cd_sittribcof"].ToString()) == "09")
                    {
                        belCofinsnt objcofinsnt = new belCofinsnt();
                        objcofinsnt.Cst = drIItem["cd_sittribcof"].ToString().PadLeft(2, '0');
                        objcofins.belCofinsnt = objcofinsnt;
                    }
                    else //if (Convert.ToInt16(drIItem["cd_sittribcof"].ToString()) == 99)
                    {
                        belCofinsoutr objcofinsoutr = new belCofinsoutr();
                        objcofinsoutr.Cst = drIItem["cd_sittribcof"].ToString().PadLeft(2, '0');
                        objcofinsoutr.Vbc = 0;
                        objcofinsoutr.Pcofins = 0;
                        dTotCofins += 0;
                        objcofinsoutr.Vcofins = dTotCofins;
                        objcofins.belCofinsoutr = objcofinsoutr;
                    } // Diego - OS_24585 - 25/06/2010 - FIM
                    objimp.belCofins = objcofins;
                    //Fim - Cofins                    
                    #endregion

                    #region ISS
                    if ((drIItem["vAliqISS"].ToString() != "") && (drIItem["vAliqISS"].ToString() != "0"))
                    {
                        belIss objiss = new belIss();
                        decimal dvBCISS = Math.Round(Convert.ToDecimal(drIItem["vBCISS"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        dTotServ += dvBCISS;
                        dTotBCISS = dTotServ;
                        objiss.Vbc = dvBCISS;
                        decimal dvAliqISS = Math.Round(Convert.ToDecimal(drIItem["vAliqISS"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        objiss.Valiq = dvAliqISS;
                        decimal dvISSQN = Convert.ToDecimal(drIItem["vIssqn"].ToString());
                        dTotISS += dvISSQN;
                        dTotPisISS += Math.Round(Convert.ToDecimal(drIItem["vl_pis"].ToString()), 2); //o.s. 24248 - 26/03/2010
                        dTotCofinsISS += Math.Round(Convert.ToDecimal(drIItem["vl_cofins"].ToString()), 2);  //o.s. 24248 - 26/03/2010
                        objiss.Vissqn = dvISSQN;
                        objiss.Cmunfg = drIItem["cMunFG"].ToString();
                        if (drIItem["cListserv"].ToString() != "")
                        {
                            Int64 icListServ = Convert.ToInt64(drIItem["cListserv"].ToString());
                            objiss.Clistserv = icListServ;
                        }
                        objimp.belIss = objiss;
                    }
                    #endregion

                    #region Obs
                    //Obs
                    belInfadprod objinf = new belInfadprod();
                    string sObsItem = "";
                    if (objbelGeraXml.nm_Cliente == "HELENGE")
                    {
                        sObsItem += (BuscaContratoOBS(sEmp, drIItem["nr_lanc"].ToString(), Conn)).Replace(Environment.NewLine, "-");
                    }
                    if (objbelGeraXml.nm_Cliente == "FORMINGP")                    //Diego - O.S 24028 - 22/01/2010
                    {
                        sObsItem += BuscaSerieProd(sEmp, drIItem["nr_lanc"].ToString(), Conn);
                    }//Fim - Diego - O.S 24028 - 22/01/2010

                    if (objbelGeraXml.nm_Cliente == "JAMAICA")
                    {
                        sObsItem += BuscaInformacoesLote(drIItem["nr_lanc"].ToString(), Conn);
                    }
                    sObsItem = BuscaObsItemSimples(sEmp, drIItem["nr_lanc"].ToString(), Conn) + sObsItem; //17/11/2010

                    //if ((sBanco.ToUpper().IndexOf("COMERCIOC") == -1) && (sBanco.ToUpper().IndexOf("CERAMICAC") == -1))
                    //{
                    if (drIItem["st_imp_cdpedcli"].ToString() != "N") //Claudinei - o.s. sem - 21/12/2009
                    {

                        if (drIItem["xPed"].ToString() != "")
                        {
                            sObsItem += string.Format("SEU PEDIDO.: {0}",
                                                          drIItem["xPed"].ToString().Trim());
                        }
                        if (drIItem["nItemPed"].ToString() != "")
                        {
                            sObsItem += string.Format("ITEM NUMERO.: {0}",
                                                          drIItem["nItemPed"].ToString().Trim());
                        }

                        if ((sBanco.ToUpper().IndexOf("COMERCIOC") == -1) && (sBanco.ToUpper().IndexOf("CERAMICAC") == -1)) //Claudinei - o.s. - 25/09/2009
                        {
                            if (drIItem["nr_lote"].ToString() != "")
                            {
                                sObsItem += " " + string.Format("Lote: {0}", drIItem["nr_lote"].ToString());
                            }

                            if (drIItem["cd_prodcli"].ToString() != "")
                            {
                                if (sObsItem == "")
                                {
                                    sObsItem += string.Format("PRD_CLI.: {0}",
                                                              drIItem["cd_prodcli"].ToString().Trim());

                                }
                                else
                                {
                                    sObsItem += string.Format(" PRD_CLI.: {0}",
                                                              drIItem["cd_prodcli"].ToString().Trim());
                                }
                            }
                        }
                    }
                    // }

                    if (objbelGeraXml.nm_Cliente == "MARPA")
                    {
                        sObsItem = MontaObsItem(sEmp, drIItem["nr_lanc"].ToString(), Conn);
                        if (sObsItem != "")
                        {
                            if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                            {
                                if (drIItem["xLgr"].ToString().Trim() != "")
                                {
                                    sObsItem += string.Format(" - Endereco de Entrega.: {0} {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                              belUtil.TiraSimbolo(drIItem["xLgr"].ToString().Trim(), ""),
                                                              belUtil.TiraSimbolo(drIItem["nro"].ToString().Trim(), ""),
                                                              belUtil.TiraSimbolo(drIItem["xBairro"].ToString().Trim(), ""),
                                                              RetiraCaracterEsquerda(belUtil.TiraSimbolo(drIItem["cMun"].ToString().Trim(), ""), '0'),
                                                              belUtil.TiraSimbolo(drIItem["UF"].ToString().Trim(), ""));
                                }
                                if (drIItem["Desconto_Valor"].ToString() != "0")
                                {
                                    decimal dDesconto_Valor = Convert.ToDecimal(drIItem["Desconto_Valor"].ToString());
                                    decimal dDesconto_Percentual = (Convert.ToDecimal(drIItem["Desconto_Percentual"].ToString()) / 100);

                                    sObsItem += string.Format(" - Desconto.: ({0:p2}) {1:f2}",
                                                              dDesconto_Percentual,
                                                              dDesconto_Valor);


                                }
                            }
                            objinf.Infadprid = belUtil.TiraSimbolo(sObsItem.Trim(), "");
                        }
                        else
                        {
                            if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                            {
                                if (drIItem["Desconto_Valor"].ToString() != "0")
                                {
                                    decimal dDesconto_Valor = Convert.ToDecimal(drIItem["Desconto_Valor"].ToString());
                                    decimal dDesconto_Percentual = (Convert.ToDecimal(drIItem["Desconto_Percentual"].ToString()) / 100);

                                    sObsItem = string.Format("Desconto.: ({0:p2}) {1:f2}",
                                                              dDesconto_Percentual,
                                                              dDesconto_Valor);

                                    objinf.Infadprid = belUtil.TiraSimbolo(sObsItem.Trim(), "");
                                }
                            }
                        }
                    }
                    else
                    {

                        if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                        {
                            if (drIItem["xLgr"].ToString().Trim() != "")
                            {
                                sObsItem += string.Format("Endereco de Entrega.: {0}, {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                             drIItem["xLgr"].ToString().Trim(),
                                                             drIItem["nro"].ToString().Trim(),
                                                             drIItem["xBairro"].ToString().Trim(),
                                                             RetiraCaracterEsquerda(drIItem["cMun"].ToString().Trim(), '0'),
                                                             drIItem["UF"].ToString().Trim());

                                if (sObsItem != "")
                                {
                                    objinf.Infadprid = belUtil.TiraSimbolo(sObsItem.Trim(), "");
                                }
                            }
                            else
                            {
                                if (sObsItem != "")
                                {
                                    objinf.Infadprid = belUtil.TiraSimbolo(sObsItem.Trim(), "").Replace(Environment.NewLine, "-");
                                }
                            }
                        }
                        else
                        {
                            if (sObsItem != "")
                            {
                                objinf.Infadprid = belUtil.TiraSimbolo(sObsItem.Trim(), "");
                            }

                        }

                    }

                    if (drIItem["nr_lanc"].ToString() == sNr_Lanc)
                    {
                        if (drIItem["xLgrRedes"].ToString().Trim() != "")
                        {
                            string sTransportadora = "";

                            sTransportadora = string.Format((objbelGeraXml.nm_Cliente == "TORCETEX" ? "FRETE A PAGAR DESTINO - TRANSP . DE REDESPACHO.: " : "Redespacho.:")
                                                      + "{5} - {0} {1} - Bairro.: {2} - Cidade.: {3} - UF.: {4} ",
                                                      belUtil.TiraSimbolo(drIItem["xLgrRedes"].ToString().Trim(), ""),
                                                      belUtil.TiraSimbolo(drIItem["nroRedes"].ToString().Trim(), ""),
                                                      belUtil.TiraSimbolo(drIItem["xBairroRedes"].ToString().Trim(), ""),
                                                      RetiraCaracterEsquerda(belUtil.TiraSimbolo(drIItem["cmunRedes"].ToString().Trim(), ""), '0'),
                                                      belUtil.TiraSimbolo(drIItem["UFRedes"].ToString().Trim(), ""),
                                                      belUtil.TiraSimbolo(drIItem["redespacho"].ToString().Trim(), ""));
                            sTransportadora += ";";
                            sObsItem = sTransportadora + sObsItem;
                            objinf.Infadprid = belUtil.TiraSimbolo(sObsItem.Trim(), "-");
                        }
                    }

                    if ((objbelGeraXml.nm_Cliente.Equals("TORCETEX")))
                    {
                        if (objinf.Infadprid != null)
                        {
                            if ((objinf.Infadprid.Contains("FRETE A PAGAR DESTINO") == false))
                            {
                                objinf.Infadprid = objinf.Infadprid.Replace("REDESPACHO ", "");
                            }
                        }
                    }
                    if (objinf.Infadprid != null)
                    {
                        if (objinf.Infadprid.Length > 500)
                        {
                            objinf.Infadprid = objinf.Infadprid.Substring(0, 500);
                        }
                    }
                    //Fim - Obs                    
                    #endregion

                    objdet.belImposto = objimp;
                    objdet.belInfadprod = objinf;
                    dets.Add(objdet);
                    //Fim - Impostos
                }
            }
            catch (Exception Ex)
            {
                sExecao = " - Problemas ao tentar gerar os Itens da Nota de Seq.: " + sNF;  //OS 24738
                throw new Exception(Ex.Message + sExecao);
            }
            //finally
            //{
            //    Conn.Close();
            //}
            return dets;
        }
コード例 #25
0
        public FbConnection MontaConexaoEscritor()
        {
            FbConnection Conn = new FbConnection();
            try
            {

                Globais MontaStringConexao = new Globais();
                StringBuilder sbConexao = new StringBuilder();

                sbConexao.Append("User ="******"Usuario"));
                sbConexao.Append(";");
                sbConexao.Append("Password="******"Senha"));
                sbConexao.Append(";");
                sbConexao.Append("Database=");
                string sdatabase = MontaStringConexao.LeRegConfig("BancoEscritor");
                sbConexao.Append(sdatabase);
                sbConexao.Append(";");
                sbConexao.Append("DataSource=");
                sbConexao.Append(MontaStringConexao.LeRegConfig("ServidorEscritor"));
                sbConexao.Append(";");
                sbConexao.Append("Port=3050;Dialect=1; Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;");


                Conn = new FbConnection(sbConexao.ToString());
                Conn.Open();


            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Não foi possivel se conectar ao banco de dados do Escritor, Verifique as Configurações do Sistema, Erro.: {0}",
                                                  ex.Message));
            }
            finally
            {
                Conn.Close();
            }

            return Conn;
        }