Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        private void Gravar()
        {
            List<object> lObj = new List<object>();
            try
            {
                #region IDE
                objide = new belIde();

                objide.Cuf = Convert.ToString(cbxUF.SelectedValue).Trim();
                objide.Cnf = txtSeq.Text;
                objide.Natop = txtNatOp.Text;
                objide.Indpag = Convert.ToString(cbxIndPag.SelectedIndex).Trim();
                objide.Mod = txtMod.Text.Trim();
                objide.Serie = txtSerie.Text.Trim();
                objide.Nnf = txtNNF.Text.Trim();
                objide.Demi = Convert.ToDateTime(mtbDEmi.Text);
                objide.Dsaient = Convert.ToDateTime(mtbDSaiEnt.Text);
                objide.Tpnf = Convert.ToString(cbxTpNF.SelectedIndex);
                objide.Cmunfg = txtCMunFG.Text.Trim();
                objide.Tpimp = Convert.ToString((int)cbxtpImp.SelectedIndex + 1);
                objide.Tpemis = Convert.ToString((int)cbcxTpEmis.SelectedIndex + 1);
                objide.Cdv = txtCDV.Text.Trim();
                objide.Tpamb = Convert.ToString((int)cbxTpAmb.SelectedIndex + 1);
                objide.Finnfe = Convert.ToString((int)cbxFinNFe.SelectedIndex + 1);
                objide.Procemi = txtProcEmi.Text.Trim();
                objide.Verproc = txtVerProc.Text.Trim();
                if (belNFrefBindingSource.Count > 0) // 25360
                {
                    List<belNFref> lObjNFref = new List<belNFref>();

                    for (int i = 0; i < belNFrefBindingSource.Count; i++)
                    {
                        lObjNFref.Add((belNFref)belNFrefBindingSource[i]);
                        if (lObjNFref[i].cUF != null)
                        {
                            if (!HLP.Util.Util.IsNumeric(lObjNFref[i].cUF))
                            {
                                belUF objuf = new belUF();
                                lObjNFref[i].cUF = objuf.RetornaCUF((lObjNFref[i].cUF));
                                lObjNFref[i].CNPJ = (lObjNFref[i].CNPJ).Replace(",", "").Replace("/", "").Replace("-", "");
                                lObjNFref[i].nNF = (Convert.ToInt32(lObjNFref[i].nNF)).ToString();
                            }
                        }

                    }
                    objide.belNFref = lObjNFref;
                }
                objide.HSaiEnt = dtpHSaiEnt.Value; //NFe_2.0

                lObj.Add(objide);
                #endregion

                #region Emitente
                belEmit objemit = new belEmit();

                if (cbxPessoaEmit.SelectedIndex == 0)
                {
                    objemit.Cpf = mtbCpfCnpjEmit.Text;
                }
                else
                {
                    objemit.Cnpj = mtbCpfCnpjEmit.Text;
                }
                objemit.Xnome = txtXNomeEmit.Text.Trim();
                objemit.Xfant = txtXFantEmit.Text.Trim();
                if (txtIEEmit.Text != "")
                {
                    objemit.Ie = txtIEEmit.Text.Trim();
                }
                if (txtIESTEmit.Text != "")
                {
                    objemit.Iest = txtIESTEmit.Text.Trim();
                }
                if (txtIM.Text != "")
                {
                    objemit.Im = txtIM.Text.Trim();
                }
                if (txtCNAE.Text != "")
                {
                    objemit.Cnae = txtCNAE.Text;
                }

                //Endereço

                objemit.Xlgr = txtEnderEmitXlgr.Text.Trim();
                objemit.Nro = txtEnderEmitNum.Text.Trim();
                if (txtEnderEmitCompl.Text != "")
                {
                    objemit.Xcpl = txtEnderEmitCompl.Text.Trim();
                }
                objemit.Xbairro = txtEnderEmitXbairro.Text.Trim();
                objemit.Cmun = txtEnderEmitCmun.Text.Trim();
                objemit.Xmun = txtEnderEmitXmun.Text.Trim();
                objemit.Uf = txtEnderEmitUF.Text.Trim();
                objemit.Cpais = txtEnderEmitCpais.Text.Trim();
                objemit.Xpais = txtEnderEmitXpais.Text.Trim();
                objemit.Cep = mtbEnderEmitCep.Text.Trim();
                objemit.Fone = mtbEnderEmitFone.Text.Trim();

                switch (cmbCRT.SelectedIndex) // NFe_2.0
                {
                    case 0: objemit.CRT = 1;
                        break;

                    case 1: objemit.CRT = 2;
                        break;

                    case 2: objemit.CRT = 3;
                        break;
                }

                //Fim - Endereço
                lObj.Add(objemit);
                #endregion

                #region Destinatário

                belDest objdest = new belDest();

                if (mtbCpfCnpjDest.Mask.Equals("00.000.000/0000-00") || mtbCpfCnpjDest.Text.ToString().ToUpper().Equals("EXTERIOR"))
                {
                    objdest.Cnpj = mtbCpfCnpjDest.Text.Trim();
                }
                else
                {
                    objdest.Cpf = mtbCpfCnpjDest.Text.Trim();
                }

                objdest.Xnome = (belStatic.TpAmb == 2 ? "NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL"
                    : txtXnomeDest.Text.Trim());

                objdest.Ie = txtIEDest.Text.Trim();// (belStatic.tpAmb == 2 ? "" : txtIEDest.Text.Trim());
                if (txtISUFDest.Text != "")
                {
                    objdest.Isuf = txtISUFDest.Text.Trim();
                }

                //Endereço

                objdest.Xlgr = txtEnderDestXlgr.Text.Trim();
                objdest.Nro = txtEnderDestNro.Text.Trim();
                objdest.Xcpl = txtEnderDestCpl.Text.Trim(); //OS_26347
                objdest.Xbairro = txtEnderDestXbairro.Text.Trim();
                objdest.Cmun = txtEnderDestCmun.Text.Trim();
                objdest.Xmun = txtEnderDestXmun.Text.Trim();
                objdest.Uf = txtEnderDestUF.Text.Trim();
                objdest.Cpais = txtEnderDestCpais.Text.Trim();
                objdest.Xpais = txtEnderDestXpais.Text.Trim();
                objdest.Cep = mtbEnderDestCEP.Text.Trim();
                if (mtbEnderDestFone.Text.Trim() != "")
                {
                    objdest.Fone = mtbEnderDestFone.Text.Trim();
                }
                objdest.email = txtEmaildest.Text.Trim(); // NFe_2.0

                //Fim - Endereço
                lObj.Add(objdest);
                #endregion

                #region Endereço de Entrega
                belEndEnt objendent = new belEndEnt();
                if (mtbEndEntCNPJ.Text != "")
                {
                    objendent.Cnpj = mtbEndEntCNPJ.Text.Trim();
                    objendent.Xlgr = txtEndEntXlgr.Text.Trim();
                    objendent.Nro = txtEndEntNro.Text.Trim();
                    objendent.Cmun = txtEndEntCmun.Text.Trim();
                    objendent.Xmun = txtEndEntXmun.Text.Trim();
                    objendent.Uf = txtEndEntUF.Text.Trim();
                    objendent.Xbairro = txtEndEntXbairro.Text.Trim(); //0S_25185
                    objendent.Xcpl = txtEndEntCpl.Text.Trim();//0S_25185
                }
                lObj.Add(objendent);

                #endregion

                #region Detatalhes

                List<belDet> lObjDet = new List<belDet>();

                for (int i = 0; i < dgvDet.RowCount; i++)
                {
                    belDet objDet = new belDet();

                    objDet.Nitem = i + 1;

                    #region Prod
                    belProd objprod = new belProd();

                    objprod.Cean = Convert.ToString(dgvDet["Cean", i].Value);
                    objprod.Ceantrib = Convert.ToString(dgvDet["Ceantrib", i].Value);
                    objprod.Cfop = Convert.ToString(dgvDet["Cfop", i].Value);
                    objprod.Cprod = Convert.ToString(dgvDet["Cprod", i].Value);
                    if (dgvDet["Extipi", i].Value != null)
                    {
                        objprod.Extipi = Convert.ToString(dgvDet["Extipi", i].Value);
                    }
                    if (dgvDet["Genero", i].Value != null)
                    {
                        objprod.Genero = Convert.ToString(dgvDet["Genero", i].Value);
                    }
                    if (Convert.ToString(dgvDet["NCM", i].Value) != "")
                    {
                        objprod.Ncm = Convert.ToString(dgvDet["NCM", i].Value);
                    }
                    objprod.Qcom = Convert.ToDecimal(dgvDet["Qcom", i].Value);
                    objprod.Qtrib = Convert.ToDecimal(dgvDet["Qtrib", i].Value);
                    objprod.Ucom = Convert.ToString(dgvDet["Ucom", i].Value);
                    objprod.Utrib = Convert.ToString(dgvDet["Utrib", i].Value);
                    objprod.Vdesc = Convert.ToDecimal(dgvDet["Vdesc", i].Value);
                    objprod.Vfrete = Convert.ToDecimal(dgvDet["Vfrete", i].Value);
                    objprod.Vprod = Convert.ToDecimal(dgvDet["Vprod", i].Value);
                    objprod.Vseg = Convert.ToDecimal(dgvDet["Vseg", i].Value);
                    objprod.Vuncom = Convert.ToDecimal(dgvDet["Vuncom", i].Value);
                    objprod.Vuntrib = Convert.ToDecimal(dgvDet["Vuntrib", i].Value);
                    objprod.Xprod = Convert.ToString(dgvDet["Xprod", i].Value);
                    objprod.VOutro = Convert.ToDecimal(dgvDet["vOutro", i].Value);// NFe_2.0
                    objprod.IndTot = Convert.ToInt16(dgvDet["indTot", i].Value); // NFe_2.0
                    objprod.XPed = dgvDet["xPed", i].Value.ToString();
                    objprod.NItemPed = dgvDet["nItemPed", i].Value.ToString();

                    objDet.belProd = objprod;

                    objDet.belProd.belDI = ((List<belDet>)lObjTotNotas[notAtual - 1][4])[i].belProd.belDI;
                    #endregion

                    #region Imposto

                    belImposto objimposto = new belImposto();


                    #region ICMS

                    belIcms objicms = new belIcms();

                    if (!HLP.Util.Util.VerificaNovaST(Convert.ToString(dgvDet["CstIcms", i].Value)))
                    {
                        #region cst_antigas
                        switch (Convert.ToString(dgvDet["CstIcms", i].Value))
                        {

                            case "00":
                                {
                                    belIcms00 obj00 = new belIcms00();

                                    obj00.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj00.Modbc = Convert.ToString(dgvDet["ModbcIcms", i].Value);
                                    obj00.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj00.Picms = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    obj00.Vbc = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    obj00.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    objicms.belIcms00 = obj00;
                                    break;
                                }
                            case "10":
                                {
                                    belIcms10 obj10 = new belIcms10();

                                    obj10.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj10.Modbc = Convert.ToString(dgvDet["ModbcIcms", i].Value);
                                    obj10.Modbcst = Convert.ToDecimal(dgvDet["ModbcstIcms", i].Value);
                                    obj10.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj10.Picms = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    obj10.Picmsst = Convert.ToDecimal(dgvDet["PicmsstIcms", i].Value);
                                    obj10.Pmvast = Convert.ToDecimal(dgvDet["PmvastIcms", i].Value);
                                    obj10.Predbcst = Convert.ToDecimal(dgvDet["PredbcstIcms", i].Value);
                                    obj10.Vbc = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    obj10.Vbcst = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj10.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    obj10.Vicmsst = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    objicms.belIcms10 = obj10;
                                    break;
                                }
                            case "20":
                                {
                                    belIcms20 obj20 = new belIcms20();
                                    obj20.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj20.Modbc = Convert.ToString(dgvDet["ModbcIcms", i].Value);
                                    obj20.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj20.Picms = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    obj20.Predbc = Convert.ToDecimal(dgvDet["PredbcIcms", i].Value);
                                    obj20.Vbc = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    obj20.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    objicms.belIcms20 = obj20;
                                    break;
                                }
                            case "30":
                                {
                                    belIcms30 obj30 = new belIcms30();

                                    obj30.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj30.Modbcst = Convert.ToDecimal(dgvDet["ModbcstIcms", i].Value);
                                    obj30.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj30.Picmsst = Convert.ToDecimal(dgvDet["PicmsstIcms", i].Value);
                                    obj30.Pmvast = Convert.ToDecimal(dgvDet["PmvastIcms", i].Value);
                                    obj30.Predbcst = Convert.ToDecimal(dgvDet["PredbcstIcms", i].Value);
                                    obj30.Vbcst = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj30.Vicmsst = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    objicms.belIcms30 = obj30;

                                    break;
                                }
                            case "40":
                                {
                                    belIcms40 obj40 = new belIcms40();

                                    obj40.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj40.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);

                                    obj40.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value); //NFe_2.0
                                    obj40.motDesICMS = Convert.ToInt16(dgvDet["motDesICMS", i].Value);//NFe_2.0

                                    objicms.belIcms40 = obj40;
                                    break;
                                }
                            case "41":
                                {
                                    belIcms40 obj40 = new belIcms40();

                                    obj40.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj40.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj40.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value); //NFe_2.0
                                    obj40.motDesICMS = Convert.ToInt16(dgvDet["motDesICMS", i].Value);//NFe_2.0
                                    objicms.belIcms40 = obj40;
                                    break;
                                }
                            case "50":
                                {
                                    belIcms40 obj40 = new belIcms40();

                                    obj40.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj40.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj40.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value); //NFe_2.0
                                    obj40.motDesICMS = Convert.ToInt16(dgvDet["motDesICMS", i].Value);//NFe_2.0
                                    objicms.belIcms40 = obj40;
                                    break;

                                }
                            case "51":
                                {
                                    belIcms51 obj51 = new belIcms51();

                                    obj51.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj51.Modbc = Convert.ToString(dgvDet["ModbcIcms", i].Value);
                                    obj51.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj51.Picms = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    obj51.Predbc = Convert.ToDecimal(dgvDet["PredbcIcms", i].Value);
                                    obj51.Vbc = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    obj51.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    objicms.belIcms51 = obj51;
                                    break;
                                }
                            //Fim - Danner - o.s. 24189 - 26/02/2010
                            case "60":
                                {
                                    belIcms60 obj60 = new belIcms60();
                                    obj60.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj60.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj60.Vbcst = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj60.Vicmsst = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    objicms.belIcms60 = obj60;
                                    break;
                                }
                            case "70":
                                {
                                    belIcms70 obj70 = new belIcms70();
                                    obj70.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj70.Modbc = Convert.ToString(dgvDet["ModbcIcms", i].Value);
                                    obj70.Modbcst = Convert.ToDecimal(dgvDet["ModbcstIcms", i].Value);
                                    obj70.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj70.Picms = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    obj70.Picmsst = Convert.ToDecimal(dgvDet["PicmsstIcms", i].Value);
                                    obj70.Pmvast = Convert.ToDecimal(dgvDet["PmvastIcms", i].Value);
                                    obj70.Predbc = Convert.ToDecimal(dgvDet["PredbcIcms", i].Value);
                                    obj70.Predbcst = Convert.ToDecimal(dgvDet["PredbcstIcms", i].Value);
                                    obj70.Vbc = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    obj70.Vbcst = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj70.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    obj70.Vicmsst = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    objicms.belIcms70 = obj70;
                                    break;
                                }
                            case "90":
                                {
                                    belIcms90 obj90 = new belIcms90();

                                    obj90.Cst = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj90.Modbc = Convert.ToString(dgvDet["ModbcIcms", i].Value);
                                    obj90.Modbcst = Convert.ToDecimal(dgvDet["ModbcstIcms", i].Value);
                                    obj90.Orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj90.Picms = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    obj90.Picmsst = Convert.ToDecimal(dgvDet["PicmsstIcms", i].Value);
                                    obj90.Pmvast = Convert.ToDecimal(dgvDet["PmvastIcms", i].Value);
                                    obj90.Predbc = Convert.ToDecimal(dgvDet["PredbcIcms", i].Value);
                                    obj90.Predbcst = Convert.ToDecimal(dgvDet["PredbcstIcms", i].Value);
                                    obj90.Vbc = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    obj90.Vbcst = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj90.Vicms = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    obj90.Vicmsst = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);

                                    objicms.belIcms90 = obj90;
                                    break;
                                }
                        }
                        #endregion
                    }
                    else
                    {
                        #region cst_novas
                        switch (HLP.Util.Util.RetornaSTnovaAserUsada(Convert.ToString(dgvDet["CstIcms", i].Value)))
                        {
                            case "101":
                                {
                                    belICMSSN101 obj101 = new belICMSSN101();

                                    obj101.CSOSN = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj101.orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj101.pCredSN = Convert.ToDecimal(dgvDet["pCredSN", i].Value);
                                    obj101.vCredICMSSN = Convert.ToDecimal(dgvDet["vCredICMSSN", i].Value);
                                    objicms.belICMSSN101 = obj101;
                                    break;
                                }
                            case "102":
                                {
                                    belICMSSN102 obj102 = new belICMSSN102();
                                    obj102.CSOSN = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj102.orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    objicms.belICMSSN102 = obj102;
                                    break;
                                }
                            case "201":
                                {
                                    belICMSSN201 obj201 = new belICMSSN201();

                                    obj201.CSOSN = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj201.orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj201.modBCST = Convert.ToInt32(dgvDet["ModbcstIcms", i].Value);
                                    obj201.pMVAST = Convert.ToDecimal(dgvDet["PmvastIcms", i].Value);
                                    obj201.pRedBCST = Convert.ToDecimal(dgvDet["PredbcstIcms", i].Value);
                                    obj201.vBCST = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj201.pICMSST = Convert.ToDecimal(dgvDet["PicmsstIcms", i].Value);
                                    obj201.vICMSST = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);

                                    if (Convert.ToString(dgvDet["CstIcms", i].Value).Equals("101"))
                                    {
                                        obj201.pCredSN = Convert.ToDecimal(dgvDet["pCredSN", i].Value);
                                        obj201.vCredICMSSN = Convert.ToDecimal(dgvDet["vCredICMSSN", i].Value);
                                    }
                                    objicms.belICMSSN201 = obj201;
                                    break;
                                }
                            case "500":
                                {
                                    belICMSSN500 obj500 = new belICMSSN500();
                                    obj500.CSOSN = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj500.orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    obj500.vBCSTRet = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    obj500.vICMSSTRet = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    objicms.belICMSSN500 = obj500;
                                    break;
                                }
                            case "900":
                                {
                                    belICMSSN900 obj900 = new belICMSSN900();
                                    obj900.CSOSN = Convert.ToString(dgvDet["CstIcms", i].Value);
                                    obj900.orig = Convert.ToString(dgvDet["OrigIcms", i].Value);
                                    //obj900.modBC = Convert.ToInt32(dgvDet["ModbcIcms", i].Value);
                                    //obj900.vBC = Convert.ToDecimal(dgvDet["VbcIcms", i].Value);
                                    //obj900.pRedBC = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    //obj900.pICMS = Convert.ToDecimal(dgvDet["PicmsIcms", i].Value);
                                    //obj900.vICMS = Convert.ToDecimal(dgvDet["VicmsIcms", i].Value);
                                    //obj900.modBCST = Convert.ToInt32(dgvDet["ModbcstIcms", i].Value);
                                    //obj900.pMVAST = Convert.ToDecimal(dgvDet["PmvastIcms", i].Value);
                                    //obj900.pRedBCST = Convert.ToDecimal(dgvDet["PredbcstIcms", i].Value);
                                    //obj900.vBCST = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    //obj900.pICMSST = Convert.ToDecimal(dgvDet["PicmsstIcms", i].Value);
                                    //obj900.vICMSST = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    //obj900.vBCSTRet = Convert.ToDecimal(dgvDet["VbcstIcms", i].Value);
                                    //obj900.vICMSSTRet = Convert.ToDecimal(dgvDet["VicmsstIcms", i].Value);
                                    //obj900.pCredSN = Convert.ToDecimal(dgvDet["pCredSN", i].Value);
                                    //obj900.vCredICMSSN = Convert.ToDecimal(dgvDet["vCredICMSSN", i].Value);
                                    objicms.belICMSSN900 = obj900;
                                    break;
                                }
                        }
                        #endregion
                    }


                    objimposto.belIcms = objicms;

                    #endregion

                    #region IPI

                    belIpi objipi = new belIpi();
                    objipi.Cenq = Convert.ToString(dgvDet["CenqIpi", i].Value);

                    string sCSTIPI = Convert.ToString(dgvDet["CstIpi", i].Value);


                    if (sCSTIPI == "00" || sCSTIPI == "49" || sCSTIPI == "50" || sCSTIPI == "99")
                    {
                        belIpitrib objipitrib = new belIpitrib();

                        objipitrib.Cst = sCSTIPI;
                        objipitrib.Pipi = Convert.ToDecimal(dgvDet["PipiTrib", i].Value);
                        if (Convert.ToDecimal(dgvDet["QunidIpiTrib", i].Value) != 0)
                        {
                            objipitrib.Qunid = Convert.ToString(dgvDet["QunidIpiTrib", i].Value);
                        }
                        objipitrib.Vbc = Convert.ToDecimal(dgvDet["VbcIpiTrib", i].Value);
                        objipitrib.Vipi = Convert.ToDecimal(dgvDet["VipiTrib", i].Value); //Claudinei - o.s. 24192 - 01/03/2010
                        objipitrib.Vunid = Convert.ToDecimal(dgvDet["VunidTrib", i].Value);
                        objipi.belIpitrib = objipitrib;
                    }
                    else
                    {
                        belIpint objipint = new belIpint();

                        objipint.Cst = sCSTIPI;
                        objipi.belIpint = objipint;
                    }
                    objimposto.belIpi = objipi;



                    #endregion

                    #region II



                    //if (Convert.ToDecimal(dgvDet["VbcIi", i].Value) != 0)
                    if (objdest.Uf.Equals("EX"))
                    {
                        belIi objii = new belIi();
                        objii.Vbc = Convert.ToDecimal(dgvDet["VbcIi", i].Value);
                        objii.Vdespadu = Convert.ToDecimal(dgvDet["VdespaduIi", i].Value);
                        objii.Vii = Convert.ToDecimal(dgvDet["Vii", i].Value);
                        objii.Viof = Convert.ToDecimal(dgvDet["ViofIi", i].Value);
                        objimposto.belIi = objii;
                    }
                    #endregion

                    #region PIS

                    belPis objpis = new belPis();
                    string sCstPis = Convert.ToString(dgvDet["CstPis", i].Value);

                    if (sCstPis == "01" || sCstPis == "02") // aqui
                    {
                        belPisaliq objpisaliq = new belPisaliq();

                        objpisaliq.Cst = sCstPis;
                        objpisaliq.Ppis = Convert.ToDecimal(dgvDet["Ppis", i].Value);
                        objpisaliq.Vbc = Convert.ToDecimal(dgvDet["VbcPis", i].Value);
                        objpisaliq.Vpis = Convert.ToDecimal(dgvDet["Vpis", i].Value);
                        objpis.belPisaliq = objpisaliq;
                    }
                    else if (sCstPis == "03")
                    {
                        belPisqtde objpisqtde = new belPisqtde();
                        objpisqtde.Cst = sCstPis;
                        objpisqtde.Valiqprod = Convert.ToDecimal(dgvDet["ValiqprodPis", i].Value);
                        objpisqtde.Qbcprod = Convert.ToDecimal(dgvDet["QbcprodPis", i].Value);
                        objpisqtde.Vpis = Convert.ToDecimal(dgvDet["Vpis", i].Value);
                        objpis.belPisqtde = objpisqtde;
                    }
                    else if (sCstPis == "04" || sCstPis == "06" || sCstPis == "07" || sCstPis == "08" || sCstPis == "09")
                    {
                        belPisnt objpisnt = new belPisnt();
                        objpisnt.Cst = sCstPis;
                        objpis.belPisnt = objpisnt;
                    }
                    else //if (sCstPis == "99")
                    {
                        belPisoutr objpisoutr = new belPisoutr();

                        objpisoutr.Cst = sCstPis;
                        objpisoutr.Ppis = Convert.ToDecimal(dgvDet["Ppis", i].Value);
                        objpisoutr.Vbc = Convert.ToDecimal(dgvDet["VbcPis", i].Value);
                        if (Convert.ToDecimal(dgvDet["ValiqprodPis", i].Value) != 0)
                        {
                            objpisoutr.Valiqprod = Convert.ToDecimal(dgvDet["ValiqprodPis", i].Value);
                            //Danner - o.s. 24167 - 22/01/2010
                            objpisoutr.Qbcprod = Convert.ToDecimal(dgvDet["QbcprodPis", i].Value);
                            //Fim - Danner - o.s. 24167 - 22/01/2010
                        }
                        //objpisoutr.Vbcprod = Convert.ToString(dgvDet["QbcprodPis", i].Value);//Danner - o.s. 24167 - 22/01/2010
                        objpisoutr.Vpis = Convert.ToDecimal(dgvDet["Vpis", i].Value);
                        objpis.belPisoutr = objpisoutr;

                    }

                    objimposto.belPis = objpis;



                    #endregion

                    #region COFINS

                    belCofins objcofins = new belCofins();
                    string sCstCofins = Convert.ToString(dgvDet["CstCofins", i].Value);

                    if (sCstCofins == "01" || sCstCofins == "02")
                    {
                        belCofinsaliq objconfinsaliq = new belCofinsaliq();

                        objconfinsaliq.Cst = sCstCofins;
                        objconfinsaliq.Pcofins = Convert.ToDecimal(dgvDet["Pcofins", i].Value);
                        objconfinsaliq.Vbc = Convert.ToDecimal(dgvDet["VbcCofins", i].Value);
                        objconfinsaliq.Vcofins = Convert.ToDecimal(dgvDet["Vconfins", i].Value);

                        objcofins.belCofinsaliq = objconfinsaliq;
                    }
                    else if (sCstCofins == "03")
                    {
                        belCofinsqtde objcofinsqtde = new belCofinsqtde();

                        objcofinsqtde.Cst = sCstCofins;
                        objcofinsqtde.Qbcprod = Convert.ToDecimal(dgvDet["QbcprodCofins", i].Value);
                        objcofinsqtde.Valiqprod = Convert.ToDecimal(dgvDet["ValiqprodCofins", i].Value);
                        objcofinsqtde.Vcofins = Convert.ToDecimal(dgvDet["Vconfins", i].Value);
                        objcofins.belCofinsqtde = objcofinsqtde;
                    }
                    else if (sCstCofins == "04" || sCstCofins == "06" || sCstCofins == "07" || sCstCofins == "08" || sCstCofins == "09")
                    {
                        belCofinsnt objcofinsnt = new belCofinsnt();
                        objcofinsnt.Cst = sCstCofins;
                        objcofins.belCofinsnt = objcofinsnt;
                    }
                    else //if (sCstCofins == "99")
                    {
                        belCofinsoutr objcofinsoutr = new belCofinsoutr();
                        objcofinsoutr.Cst = sCstCofins;
                        objcofinsoutr.Pcofins = Convert.ToDecimal(dgvDet["Pcofins", i].Value);
                        objcofinsoutr.Vbc = Convert.ToDecimal(dgvDet["VbcCofins", i].Value);
                        objcofinsoutr.Qbcprod = Convert.ToDecimal(dgvDet["QbcprodCofins", i].Value);
                        objcofinsoutr.Valiqprod = Convert.ToDecimal(dgvDet["ValiqprodCofins", i].Value);
                        objcofinsoutr.Vcofins = Convert.ToDecimal(dgvDet["Vconfins", i].Value);
                        objcofins.belCofinsoutr = objcofinsoutr;
                    }
                    objimposto.belCofins = objcofins;

                    #endregion

                    #region ISSQN

                    if (Convert.ToDecimal(dgvDet["VbcIss", i].Value) != 0)
                    {
                        belIss objiss = new belIss();
                        objiss.Clistserv = Convert.ToInt64(dgvDet["ClistservIss", i].Value);
                        objiss.Cmunfg = Convert.ToString(dgvDet["CmunfgIss", i].Value);
                        objiss.Valiq = Convert.ToDecimal(dgvDet["ValiqIss", i].Value);
                        objiss.Vbc = Convert.ToDecimal(dgvDet["VbcIss", i].Value);
                        objiss.Vissqn = Convert.ToDecimal(dgvDet["VissqnIss", i].Value);
                        objimposto.belIss = objiss;
                    }



                    #endregion

                    objDet.belImposto = objimposto;

                    #region InfadProd
                    belInfadprod objinfadprod = new belInfadprod();
                    if (Convert.ToString(dgvDet["Infcpl", i].Value) != "")
                    {

                        objinfadprod.Infadprid = Convert.ToString(dgvDet["Infcpl", i].Value);
                        objDet.belInfadprod = objinfadprod;
                    }

                    #endregion

                    #endregion
                    lObjDet.Add(objDet);
                }
                lObj.Add(lObjDet);
                #endregion

                #region Totais

                belTotal objtotal = new belTotal();

                //Totais
                belIcmstot objIcmsTot = new belIcmstot();

                objIcmsTot.Vbc = nudVBC.Value;
                objIcmsTot.Vicms = nudVICMS.Value;
                objIcmsTot.Vbcst = nudVBCICMSST.Value;
                objIcmsTot.Vst = nudVST.Value;
                objIcmsTot.Vprod = nudVProd.Value;
                //Danner - o.s. 24154 - 18/02/2010
                objIcmsTot.Vfrete = nudVFrete.Value;
                //Fim - Danner - o.s. 24154 - 18/02/2010
                objIcmsTot.Vseg = nudVSEG.Value;
                objIcmsTot.Vdesc = nudVDesc.Value;
                objIcmsTot.Vii = nudVII.Value;
                objIcmsTot.Vipi = nudVIPI.Value;
                objIcmsTot.Vpis = nudVPIS.Value;
                objIcmsTot.Vcofins = nudVCOFINS.Value;
                objIcmsTot.Voutro = nudVOutro.Value;
                objIcmsTot.Vnf = nudVNF.Value;
                objtotal.belIcmstot = objIcmsTot;

                //Fim - Totais

                //ISSQNtot
                if (flpISSNQ.Enabled != false)
                {
                    belIssqntot objissqnTot = new belIssqntot();

                    objissqnTot.Vserv = nudVServ.Value;
                    objissqnTot.Vbc = nudVBCISS.Value;
                    objissqnTot.Viss = nudVISS.Value;
                    objissqnTot.Vpis = nudVPISISS.Value;
                    objissqnTot.Vcofins = nudVCOFINSISS.Value;
                    objtotal.belIssqntot = objissqnTot;
                }

                //Fin - ISSQNtot;

                //retTrib
                if (flpRetTrib.Enabled != false)
                {
                    belRetTrib objRetTrib = new belRetTrib();

                    objRetTrib.Vretpis = nudVPISRet.Value;
                    objRetTrib.Vretcofins = nudVCOFINSRet.Value;
                    objRetTrib.Vretcsll = nudVCSLLRet.Value;
                    objRetTrib.Vbcretprev = nudVBCIRRFRet.Value;
                    objRetTrib.Virrf = nudVIRRFRet.Value;
                    objRetTrib.Vbcirrf = nudVBCIRRFRet.Value;
                    objRetTrib.Vbcretprev = nudVBCRetPrev.Value;
                    objRetTrib.Vretprev = nudVRetPrev.Value;
                    objtotal.belRetTrib = objRetTrib;

                }

                lObj.Add(objtotal);

                //Fim - retTrib
                #endregion

                #region Transporte

                belTransp objtransp = new belTransp();

                switch (cbxModFrete.SelectedIndex) //Nfe_2.0
                {
                    case 0: objtransp.Modfrete = "0";
                        break;

                    case 1: objtransp.Modfrete = "1";
                        break;

                    case 2: objtransp.Modfrete = "2";
                        break;

                    case 3: objtransp.Modfrete = "9";
                        break;
                }

                //objtransp.Modfrete = Convert.ToString(cbxModFrete.SelectedIndex);

                //Transportadora
                belTransportadora objtransportadora = new belTransportadora();

                if (cbxPessoaTranp.SelectedIndex == 1)
                {
                    if (mtbCPJCNPJTransp.Text.Trim() != "")
                    {
                        objtransportadora.Cnpj = mtbCPJCNPJTransp.Text.Trim();
                    }
                }
                else
                {
                    if (mtbCPJCNPJTransp.Text.Trim() != "")
                    {
                        objtransportadora.Cpf = mtbCPJCNPJTransp.Text.Trim();
                    }
                }

                if (txtXnomeTransp.Text.Trim() != "")
                {
                    objtransportadora.Xnome = txtXnomeTransp.Text.Trim();
                }
                if (txtIETransp.Text.Trim() != "")
                {
                    objtransportadora.Ie = txtIETransp.Text.Trim();
                }
                if (txtEnderTransp.Text.Trim() != "")
                {
                    objtransportadora.Xender = txtEnderTransp.Text.Trim();
                }
                if (txtUFTransp.Text.Trim() != "")
                {
                    objtransportadora.Uf = txtUFTransp.Text.Trim();
                }
                if (txtXmunTransp.Text.Trim() != "")
                {
                    objtransportadora.Xmun = txtXmunTransp.Text.Trim();
                }

                objtransp.belTransportadora = objtransportadora;

                //Fim - Transportadora

                //VeicTransp
                if (flpVeicTransp.Enabled != false)
                {
                    belVeicTransp objVeicTrasnp = new belVeicTransp();
                    objVeicTrasnp.Placa = mtbPlacaVeicTransp.Text.Trim();
                    objVeicTrasnp.Uf = txtUFVeicTransp.Text.Trim();

                    //Danner - o.s. sem - 05/03/2010
                    if (txtRNTCVeicTransp.Text.Trim() != "")
                    {
                        objVeicTrasnp.Rntc = txtRNTCVeicTransp.Text.Trim();
                    }
                    //Fim - Danner - o.s. sem - 05/03/2010
                    objtransp.belVeicTransp = objVeicTrasnp;

                }
                //Fim -  VeicTransp

                //Reboque
                if (flpReboque.Enabled != false)
                {
                    belReboque objReboque = new belReboque();
                    objReboque.Placa = mtbPlacaReboque.Text.Trim();
                    objReboque.Uf = txtUFReboque.Text.Trim();
                    objReboque.Rntc = txtRNTCReboque.Text.Trim();
                    objtransp.belReboque = objReboque;
                }
                //Fim - Reboque 

                //RetTransp
                if (flpRetICMS.Enabled != false)
                {
                    belRetTransp objRetTransp = new belRetTransp();
                    objRetTransp.Vbvret = nudVBCICMSTransp.Value;
                    objRetTransp.Vserv = nudVServTransp.Value;
                    objRetTransp.Picmsret = nudPICMSTRetTransp.Value;
                    objRetTransp.Vicmsret = nudVICMSRet.Value;
                    objRetTransp.Cmunfg = txtCmunFGTransp.Text.Trim();
                    objRetTransp.Cfop = txtCFOPTransp.Text.Trim();
                    objtransp.belRetTransp = objRetTransp;
                }
                //Fim - RetTransp

                if (flpVolumes.Enabled != false)
                {
                    belVol objVol = new belVol();
                    objVol.Esp = txtEsp.Text.Trim();
                    objVol.Marca = txtMarca.Text.Trim();
                    //Danner - o.s. 24385 - 26/04/2010
                    if (txtNVol.Text != "")
                    {
                        objVol.Nvol = txtNVol.Text;//Danner - o.s. 24432 - 04/05/2010
                    }
                    //Fim - Danner - o.s. 24385 - 26/04/2010
                    objVol.PesoB = nudPesoB.Value;
                    objVol.PesoL = nudPesoL.Value;
                    objVol.Qvol = nudQvol.Value;
                    objtransp.belVol = objVol;

                }

                lObj.Add(objtransp);
                #endregion

                #region Cobrança

                belCobr objcobr = new belCobr();

                belFat objFat = new belFat();

                objFat.Nfat = txtNFat.Text.Trim();
                objFat.Vorig = nudVOrigFat.Value;
                objFat.Vdesc = nudVDescFat.Value;
                objFat.Vliq = nudVLiqFat.Value;

                if (dgvDup.RowCount != 0)
                {
                    List<belDup> lObjDup = new List<belDup>();

                    for (int i = 0; i < dgvDup.RowCount; i++)
                    {
                        belDup objdup = new belDup();
                        objdup.Ndup = Convert.ToString(dgvDup[0, i].Value);
                        objdup.Dvenc = Convert.ToDateTime(dgvDup[1, i].Value);
                        objdup.Vdup = Convert.ToDecimal(dgvDup[2, i].Value);
                        lObjDup.Add(objdup);
                    }
                    objFat.belDup = lObjDup;

                }
                objcobr.belFat = objFat;

                lObj.Add(objcobr);
                #endregion

                #region Inf Adicionais
                belInfAdic objinfadic = new belInfAdic();
                if (txtInfAdic.Text != "")
                {


                    objinfadic.Infcpl = txtInfAdic.Text.Trim().Replace(Environment.NewLine, "");
                }
                lObj.Add(objinfadic);

                #endregion

                #region exporta
                belExporta objexporta = new belExporta();
                if (cbxUF_embarque.SelectedIndex > 0)
                {
                    objexporta.Ufembarq = cbxUF_embarque.Text.ToString();
                }
                if (txtLocalEntrega.Text != "")
                {
                    objexporta.Xlocembarq = txtLocalEntrega.Text;
                }

                lObj.Add(objexporta);

                #endregion

                lObjTotNotasFinal[notAtual - 1] = lObj;
                lObjTotNotas[notAtual - 1] = lObjTotNotasFinal[notAtual - 1];

            }

            catch (Exception ex)
            {

                KryptonMessageBox.Show(ex.Message); ;
            }

        }
Ejemplo n.º 3
0
        public belCofins xmlBuscaDetCofins(XmlDocument Xdoc)
        {
            belCofins objCofins = new belCofins();
            try
            {

                XmlDocument xCofinsN = new XmlDocument();


                switch (Xdoc.GetElementsByTagName("COFINS")[0].FirstChild.Name)
                {
                    case "COFINSAliq":
                        {
                            belCofinsaliq objCofinsAliq = new belCofinsaliq();

                            xCofinsN.LoadXml(Xdoc.GetElementsByTagName("COFINSAliq")[0].OuterXml);

                            if (xCofinsN.GetElementsByTagName("CST").Count > 0)
                            {
                                objCofinsAliq.Cst = xCofinsN.GetElementsByTagName("CST")[0].InnerText;
                            }

                            if (xCofinsN.GetElementsByTagName("vBC").Count > 0)
                            {
                                objCofinsAliq.Vbc = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vBC")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("pCOFINS").Count > 0)
                            {
                                objCofinsAliq.Pcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("pCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vCOFINS").Count > 0)
                            {
                                objCofinsAliq.Vcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }


                            objCofins.belCofinsaliq = objCofinsAliq;
                            break;
                        }
                    case "COFINSQtde":
                        {
                            belCofinsqtde objCofinsQtde = new belCofinsqtde();

                            xCofinsN.LoadXml(Xdoc.GetElementsByTagName("COFINSQtde")[0].OuterXml);

                            if (xCofinsN.GetElementsByTagName("CST").Count > 0)
                            {
                                objCofinsQtde.Cst = xCofinsN.GetElementsByTagName("CST")[0].InnerText;
                            }

                            if (xCofinsN.GetElementsByTagName("qBCProd").Count > 0)
                            {
                                objCofinsQtde.Qbcprod = Convert.ToDecimal(xCofinsN.GetElementsByTagName("qBCProd")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vAliqProd").Count > 0)
                            {
                                objCofinsQtde.Valiqprod = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vAliqProd")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vCOFINS").Count > 0)
                            {
                                objCofinsQtde.Vcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }


                            objCofins.belCofinsqtde = objCofinsQtde;

                            break;
                        }
                    case "COFINSNT":
                        {
                            belCofinsnt objCofinsNT = new belCofinsnt();

                            xCofinsN.LoadXml(Xdoc.GetElementsByTagName("COFINSNT")[0].OuterXml);

                            objCofinsNT.Cst = xCofinsN.GetElementsByTagName("CST")[0].InnerText;
                            objCofins.belCofinsnt = objCofinsNT;
                            break;
                        }
                    case "COFINSOutr":
                        {
                            belCofinsoutr objCofinsOutr = new belCofinsoutr();

                            xCofinsN.LoadXml(Xdoc.GetElementsByTagName("COFINSOutr")[0].OuterXml);

                            if (xCofinsN.GetElementsByTagName("CST").Count > 0)
                            {
                                objCofinsOutr.Cst = xCofinsN.GetElementsByTagName("CST")[0].InnerText;
                            }

                            if (xCofinsN.GetElementsByTagName("vBC").Count > 0)
                            {
                                objCofinsOutr.Vbc = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vBC")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("pCOFINS").Count > 0)
                            {
                                objCofinsOutr.Pcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("pCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("qBCProd").Count > 0)
                            {
                                objCofinsOutr.Qbcprod = Convert.ToDecimal(xCofinsN.GetElementsByTagName("qBCProd")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vAliqProd").Count > 0)
                            {
                                objCofinsOutr.Valiqprod = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vAliqProd")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vCOFINS").Count > 0)
                            {
                                objCofinsOutr.Vcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }


                            objCofins.belCofinsoutr = objCofinsOutr;
                            break;
                        }
                    case "COFINSST":
                        {
                            belCofinsst objCofinsST = new belCofinsst();

                            xCofinsN.LoadXml(Xdoc.GetElementsByTagName("COFINSST")[0].OuterXml);

                            if (xCofinsN.GetElementsByTagName("vBC").Count > 0)
                            {
                                objCofinsST.Vbc = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vBC")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("pCOFINS").Count > 0)
                            {
                                objCofinsST.Pcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("pCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("qBCProd").Count > 0)
                            {
                                objCofinsST.Qbcprod = Convert.ToDecimal(xCofinsN.GetElementsByTagName("qBCProd")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vAliqProd").Count > 0)
                            {
                                objCofinsST.Valiqprod = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vAliqProd")[0].InnerText.ToString().Replace(".", ","));
                            }

                            if (xCofinsN.GetElementsByTagName("vCOFINS").Count > 0)
                            {
                                objCofinsST.Vcofins = Convert.ToDecimal(xCofinsN.GetElementsByTagName("vCOFINS")[0].InnerText.ToString().Replace(".", ","));
                            }

                            objCofins.belCofinsst = objCofinsST;
                            break;
                        }


                }


            }
            catch (Exception ex)
            {

                throw new Exception(string.Format("Erro buscando COFINS.: {0}",
                                    ex.Message));
            }
            return objCofins;
        }
Ejemplo n.º 4
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;
        }