diff --git a/NFe.Danfe.Base/NFe/NFeRetratoRtc.frx b/NFe.Danfe.Base/NFe/NFeRetratoRtc.frx index ed29619e..012057e8 100644 --- a/NFe.Danfe.Base/NFe/NFeRetratoRtc.frx +++ b/NFe.Danfe.Base/NFe/NFeRetratoRtc.frx @@ -1,1569 +1,1643 @@  - using System; -using System.IO; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Windows.Forms; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text.RegularExpressions; -using System.Globalization; -using FastReport; -using FastReport.Data; -using FastReport.Dialog; -using FastReport.Barcode; -using FastReport.Table; -using FastReport.Utils; -using NFe.Classes.Informacoes.Identificacao.Tipos; -using NFe.Classes.Informacoes.Detalhe.Tributacao; -using NFe.Classes.Informacoes.Detalhe.Tributacao.Estadual.Tipos; -using NFe.Classes.Informacoes.Detalhe.Tributacao.Federal.Tipos; -using NFe.Utils.Tributacao.Estadual; -using NFe.Classes.Informacoes.Transporte; -using NFe.Classes.Protocolo; -using NFe.Classes.Informacoes.Emitente; -using NFe.Utils; -using NFe.Danfe.Base.NFe; -using NFe.Classes.Informacoes.Total; - -namespace FastReport -{ - public class ReportScript - { - private void Emitente_BeforePrint(object sender, EventArgs e) - { - var chave = Substring(((String)Report.GetColumnValue("NFe.NFe.infNFe.Id")), 3); - TextChaveAcesso.Text = Regex.Replace(chave, ".{4}", "$0 "); - BarcodeChave.Text = chave; - poEmitLogo.Image = ObterLogo(); - PreencherRegimeTributario(); - PreencherTotaisIBSCBSIS(); - PreencherTotaisICMS(); - PreencherFaturaDuplicatas(); - PreencherTransportador(); - - var contigenciaId = (string)Report.GetParameterValue("ContingenciaID"); - BarCodeContigencia.Visible = !string.IsNullOrEmpty(contigenciaId); - if(BarCodeContigencia.Visible) BarCodeContigencia.Text = contigenciaId; - DadosISSQN.Visible = !(bool)Report.GetParameterValue("ImprimirISSQN"); - DadosRetencoes.Visible = (bool)Report.GetParameterValue("ExibeRetencoes"); - // Leiaute NT 2026.010: as celulas de tributos possuem varias linhas, a linha do item sempre se ajusta - prodRow.AutoSize = true; - prodNomeCell.WordWrap = (bool)Report.GetParameterValue("DuasLinhas"); - - if (Report.GetColumnValue("NFe.NFe.infNFe.emit.enderEmit.fone") != null) { - Text209.Text = FormatarCampo(Report.GetColumnValue("NFe.NFe.infNFe.emit.enderEmit.fone").ToString(), 'f'); - } - - if ((FinalidadeNFe)(Report.GetColumnValue("NFe.NFe.infNFe.ide.finNFe")) == FinalidadeNFe.fnDevolucao) - { - var ipi = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ICMSTot.vIPI")) ?? 0M; - if (ipi <= 0) - { - var ipiDevolvido = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ICMSTot.vIPIDevol")) ?? 0M; - Text163.Text = FormatNumber(ipiDevolvido,2); - } - } - - //Trato o campo de Frete - switch((int)((ModalidadeFrete)Report.GetColumnValue("NFe.NFe.infNFe.transp.modFrete"))) - { - case 0: Memo92.Text = "0 - Emit/Remet"; break; - case 1: Memo92.Text = "1 - Destinatário"; break; - case 2: Memo92.Text = "2 - Terceiros"; break; - case 3: Memo92.Text = "3 - Prop Remetente"; break; - case 4: Memo92.Text = "4 - Prop Destinatário"; break; - case 9: Memo92.Text = "9 - Sem Frete"; break; - } - - var vol = Report.GetDataSource("NFe.NFe.infNFe.transp.vol"); - if(vol != null){ - vol.Init(); - int qtdeVol = 0; - - int volumes = 0; - decimal pesoL = 0; - decimal pesoB = 0; - while (vol.HasMoreRows) - { - Memo106.Text = ((String)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.esp")); - Memo108.Text = ((String)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.marca")); - Memo110.Text = ((String)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.nVol")); - - volumes += ((Nullable<int>)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.qVol")) ?? 0; - pesoL += ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.pesoL")) ?? 0M; - pesoB += ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.pesoB")) ?? 0M; - qtdeVol++; - vol.Next(); - } - - if (qtdeVol >= 1) - { - if (qtdeVol > 1) - { - Memo106.Text = "VOLUMES"; - Memo108.Text = ""; - Memo110.Text = ""; - } - - Memo104.Text = volumes.ToString(); - Memo112.Text = FormatNumber(pesoB,3) + " KG"; - Memo114.Text = FormatNumber(pesoL,3) + " KG"; - } - } - - if(Engine.FinalPass) - { - var vBC = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ISSQNtot.vBC")) ?? 0; - var vISS = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ISSQNtot.vISS")) ?? 0; - var vServ = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ISSQNtot.vServ")) ?? 0; - - DadosISSQN.Visible = !(vBC == 0 && vISS == 0 && vServ == 0); - - var retiradaCPF = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.CPF"); - var retiradaCNPJ = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.CNPJ"); - var retiradaEndereco = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.xLgr"); - var retiradaNro = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.nro"); - var retiradaBairro = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.xBairro"); - var retiradaMunicipio = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.xMun"); - var retiradaUf = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.UF"); - - LocalRetirada.Visible = !string.IsNullOrEmpty(retiradaCPF) || !string.IsNullOrEmpty(retiradaCNPJ) || - !string.IsNullOrEmpty(retiradaEndereco) || !string.IsNullOrEmpty(retiradaNro) || - !string.IsNullOrEmpty(retiradaBairro) || !string.IsNullOrEmpty(retiradaMunicipio) || - !string.IsNullOrEmpty(retiradaUf); - - var entregaCPF = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.CPF"); - var entregaCNPJ = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.CNPJ"); - var entregaEndereco = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.xLgr"); - var entregaNro = (string)Report.GetColumnValue("[NFe.NFe.infNFe.entrega.nro]"); - var entregaBairro = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.xBairro"); - var entregaMunicipio = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.xMun"); - var entregaUf = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.UF"); - - LocalEntrega.Visible = !string.IsNullOrEmpty(entregaCPF) || !string.IsNullOrEmpty(entregaCNPJ) || - !string.IsNullOrEmpty(entregaEndereco) || !string.IsNullOrEmpty(entregaNro) || - !string.IsNullOrEmpty(entregaBairro) || !string.IsNullOrEmpty(entregaMunicipio) || - !string.IsNullOrEmpty(entregaUf); - - - } - - string infAdicionais = (string)Report.GetColumnValue("NFe.NFe.infNFe.infAdic.infCpl") + Environment.NewLine + (string)Report.GetColumnValue("NFe.NFe.infNFe.infAdic.infAdFisco"); - - infAdicionais = infAdicionais.Replace("[","[\"[").Replace("]","]\"]"); - - if((bool)Report.GetParameterValue("QuebrarLinhasObservacao")) - { - infAdicionais = infAdicionais.Replace(";", Environment.NewLine); - } - - TextInfAdicionais.Text = infAdicionais; - //memContInfAdicionais.Text = string.Empty; - - //string[] lines = infAdicionais.Split(new string[]{ Environment.NewLine, "\n" }, StringSplitOptions.None); - //pgContDadosAdicionais.Visible = lines.Length > LINHAS_DADOS_ADICIONAIS; - - //if(pgContDadosAdicionais.Visible) - //{ - // TextInfAdicionais.Text = string.Join(Environment.NewLine, lines.Take(LINHAS_DADOS_ADICIONAIS-1).ToArray()); - // memContInfAdicionais.Text = string.Join(Environment.NewLine, lines.Skip(LINHAS_DADOS_ADICIONAIS-1).ToArray()); - //} - } - - private void _StartReport(object sender, EventArgs e) - { - // Canhoto facultativo (NT 2026.010 - item 4.4) - var exibirCanhoto = Report.GetParameterValue("ExibirCanhoto"); - Canhoto.Visible = exibirCanhoto == null || (bool)exibirCanhoto; - - // QR Code (NT 2026.010 - itens 4.4 e 4.5): exibido somente quando informado no XML - string qrCode = null; - try { qrCode = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFeSupl.qrCode")); } catch { } - var exibirQrCode = !string.IsNullOrEmpty(qrCode); - MemoQrCodeBox.Visible = exibirQrCode; - MemoQrCodeTitulo.Visible = exibirQrCode; - bcoQrCode.Visible = exibirQrCode; - if (exibirQrCode) - { - bcoQrCode.Text = qrCode; - DadosAdicionais.Height = 107.73f; - Text155.Width = 400f; - TextInfAdicionais.Width = 397f; - Text156.Left = 400f; - Text156.Width = 188.9f; - Text37.Left = 402f; - Text37.Width = 184.9f; - } - } - - private void DadosProdutos_BeforePrint(object sender, EventArgs e) - { - var icmsBasico = (ICMSBasico)Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.ICMS.TipoICMS"); - var ipiBasico = (IPIBasico)Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.IPI.TipoIPI"); - var ibsCbs = ObterIBSCBSItem(); - var impostoSeletivo = Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.IS") as global::NFe.Classes.Informacoes.Detalhe.Tributacao.IS; - var crt = (CRT)Report.GetColumnValue("NFe.NFe.infNFe.emit.CRT"); - var simplesNacional = crt == CRT.SimplesNacional || crt == CRT.SimplesNacionalMei; - - // CST / CFOP - var cst = string.Empty; - var rotuloCst = "CST"; - string icmsBc = "", icmsAliq = "", icmsValor = ""; - if (icmsBasico != null) - { - var orig = icmsBasico.GetIcmsOrig(); - var cstIcms = icmsBasico.GetIcmsCst().CsticmsParaString(); - if (simplesNacional && cstIcms != "02" && cstIcms != "15" && cstIcms != "53" && cstIcms != "61") - { - rotuloCst = "CSOSN"; - cst = orig.OrigemMercadoriaParaString() + icmsBasico.GetIcmsCsosn().CsosnicmsParaString(); - } - else - { - cst = orig.OrigemMercadoriaParaString() + cstIcms; - } - icmsBc = FormatarValorRTC(icmsBasico.GetIcmsBcValue()); - icmsAliq = FormatarAliquotaRTC(icmsBasico.GetIcmsPercent()); - icmsValor = FormatarValorRTC(icmsBasico.GetIcmsValue()); - } - var cfop = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.CFOP")); - prodOriginCell.Text = rotuloCst + " " + cst + Environment.NewLine + "CFOP " + cfop; - - // IPI - string ipiBc = "", ipiAliq = "", ipiValor = ""; - var ipiTrib = ipiBasico as global::NFe.Classes.Informacoes.Detalhe.Tributacao.Federal.IPITrib; - if (ipiTrib != null) - { - if (ipiTrib.vBC.HasValue) ipiBc = FormatarValorRTC(ipiTrib.vBC.Value); - if (ipiTrib.pIPI.HasValue) ipiAliq = FormatarAliquotaRTC(ipiTrib.pIPI.Value); - ipiValor = FormatarValorRTC(ipiTrib.vIPI ?? 0M); - } - else if (ipiBasico == null && (FinalidadeNFe)(Report.GetColumnValue("NFe.NFe.infNFe.ide.finNFe")) == FinalidadeNFe.fnDevolucao) - { - var devolvidoPercentual = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.det.impostoDevol.pDevol")) ?? 0M; - var ipiDevolvidoValor = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.det.impostoDevol.IPI.vIPIDevol")) ?? 0M; - if (ipiDevolvidoValor > 0) - { - ipiValor = FormatarValorRTC(ipiDevolvidoValor); - ipiAliq = FormatarAliquotaRTC(devolvidoPercentual); - } - } - - // IBS / CBS (Grupo UB) - com gRed informado imprime a aliquota efetiva (pAliqEfet) - string ibsCbsBc = "", ibsUfAliq = "", ibsUfValor = "", ibsMunAliq = "", ibsMunValor = "", cbsAliq = "", cbsValor = ""; - var gIBSCBS = ibsCbs != null ? ibsCbs.gIBSCBS : null; - if (gIBSCBS != null) - { - ibsCbsBc = FormatarValorRTC(gIBSCBS.vBC); - if (gIBSCBS.gIBSUF != null) - { - ibsUfAliq = FormatarAliquotaRTC(gIBSCBS.gIBSUF.gRed != null ? gIBSCBS.gIBSUF.gRed.pAliqEfet : gIBSCBS.gIBSUF.pIBSUF); - ibsUfValor = FormatarValorRTC(gIBSCBS.gIBSUF.vIBSUF); - } - if (gIBSCBS.gIBSMun != null) - { - ibsMunAliq = FormatarAliquotaRTC(gIBSCBS.gIBSMun.gRed != null ? gIBSCBS.gIBSMun.gRed.pAliqEfet : gIBSCBS.gIBSMun.pIBSMun); - ibsMunValor = FormatarValorRTC(gIBSCBS.gIBSMun.vIBSMun); - } - if (gIBSCBS.gCBS != null) - { - cbsAliq = FormatarAliquotaRTC(gIBSCBS.gCBS.gRed != null ? gIBSCBS.gCBS.gRed.pAliqEfet : gIBSCBS.gCBS.pCBS); - cbsValor = FormatarValorRTC(gIBSCBS.gCBS.vCBS); - } - } - - // Imposto Seletivo - string isBc = "", isAliq = "", isValor = ""; - if (impostoSeletivo != null) - { - isBc = FormatarValorRTC(impostoSeletivo.vBCIS); - isAliq = FormatarAliquotaRTC(impostoSeletivo.pIS); - isValor = FormatarValorRTC(impostoSeletivo.vIS); - } - - var nl = Environment.NewLine; - prodBcLCell.Text = "ICMS" + nl + "IBS / CBS" + nl + "IS" + nl + "IPI"; - prodBcVCell.Text = icmsBc + nl + ibsCbsBc + nl + isBc + nl + ipiBc; - prodAlqLACell.Text = "ICMS" + nl + "IBS UF" + nl + "IBS MUN"; - prodAlqVACell.Text = icmsAliq + nl + ibsUfAliq + nl + ibsMunAliq; - prodAlqLBCell.Text = "/ CBS" + nl + "/ IPI" + nl + "/ IS"; - prodAlqVBCell.Text = cbsAliq + nl + ipiAliq + nl + isAliq; - prodVlrLACell.Text = prodAlqLACell.Text; - prodVlrVACell.Text = icmsValor + nl + ibsUfValor + nl + ibsMunValor; - prodVlrLBCell.Text = prodAlqLBCell.Text; - prodVlrVBCell.Text = cbsValor + nl + ipiValor + nl + isValor; - - // Quantidade / unidade / valores - var imprimirUnidQtdeValor = (ImprimirUnidQtdeValor)Report.GetParameterValue("ImprimirUnidQtdeValor"); - var imprimirTotalLiquido = (bool)Report.GetParameterValue("ImprimirTotalLiquido"); - var uCom = ((String)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.uCom")); - var uTrib = ((String)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.uTrib")); - var qCom = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.qCom")); - var qTrib = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.qTrib")); - var vUnCom = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vUnCom")); - var vUnTrib = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vUnTrib")); - var vProd = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vProd")); - var vDesc = (((Nullable<Decimal>)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vDesc")) ?? 0); - if (imprimirTotalLiquido) vProd = vProd - vDesc; - prodValorCell.Text = FormatNumber(vProd, 2); - var decimaisQuantidadeItem = (int)Report.GetParameterValue("DecimaisQuantidadeItem"); - var decimaisValorUnitario = (int)Report.GetParameterValue("DecimaisValorUnitario"); - switch (imprimirUnidQtdeValor) - { - case ImprimirUnidQtdeValor.Comercial: - prodQtdCell.Text = FormatNumber(qCom, decimaisQuantidadeItem) + nl + "[" + uCom + "]"; - prodValorUnitarioCell.Text = FormatNumber(vUnCom, decimaisValorUnitario); - break; - case ImprimirUnidQtdeValor.Tributavel: - prodQtdCell.Text = FormatNumber(qTrib, decimaisQuantidadeItem) + nl + "[" + uTrib + "]"; - prodValorUnitarioCell.Text = FormatNumber(vUnTrib, decimaisValorUnitario); - break; - case ImprimirUnidQtdeValor.Ambos: - prodQtdCell.Text = FormatNumber(qCom, decimaisQuantidadeItem) + nl + "[" + uCom + "]" + nl + - FormatNumber(qTrib, decimaisQuantidadeItem) + nl + "[" + uTrib + "]"; - prodValorUnitarioCell.Text = FormatNumber(vUnCom, decimaisValorUnitario) + nl + nl + FormatNumber(vUnTrib, decimaisValorUnitario); - break; - } - } - - private static readonly CultureInfo CulturaRTC = new CultureInfo("pt-BR"); - - private string FormatarValorRTC(decimal valor) - { - return valor.ToString("#,##0.00", CulturaRTC); - } - - private string FormatarValorOpcional(Nullable<decimal> valor) - { - return valor.HasValue ? FormatarValorRTC(valor.Value) : string.Empty; - } - - private string FormatarQuantidadeOpcional(Nullable<decimal> valor) - { - return valor.HasValue ? valor.Value.ToString("#,##0.00##", CulturaRTC) : string.Empty; - } - - private string FormatarAliquotaRTC(decimal valor) - { - return valor.ToString("#,##0.00##", CulturaRTC) + "%"; - } - - private static bool Positivo(Nullable<decimal> valor) - { - return valor.HasValue && valor.Value != 0; - } - - private static string EscaparHtml(string texto) - { - if (string.IsNullOrEmpty(texto)) return string.Empty; - return texto.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;"); - } - - private global::NFe.Classes.Informacoes.Detalhe.Tributacao.IBSCBS ObterIBSCBSItem() - { - return Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.IBSCBS") as global::NFe.Classes.Informacoes.Detalhe.Tributacao.IBSCBS; - } - - // Descricao do item: codigo (negrito) + descricao, informacoes adicionais e [NCM] [cClassTrib] (NT 2026.010 - item 4.3) - public string TextoDescricaoItem() - { - var nl = Environment.NewLine; - var texto = "<b>" + EscaparHtml(Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.cProd"))) + "</b> " + - EscaparHtml(Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.xProd"))); - var infAdProd = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.infAdProd")); - if (!string.IsNullOrEmpty(infAdProd)) texto += nl + EscaparHtml(infAdProd); - - var partes = new List<string>(); - var ncm = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.NCM")); - if (!string.IsNullOrEmpty(ncm)) partes.Add("[NCM " + EscaparHtml(ncm) + "]"); - var ibsCbs = ObterIBSCBSItem(); - if (ibsCbs != null && !string.IsNullOrEmpty(ibsCbs.cClassTrib)) partes.Add("[cClassTrib " + EscaparHtml(ibsCbs.cClassTrib) + "]"); - if (partes.Count > 0) texto += nl + string.Join(" ", partes.ToArray()); - return texto; - } - - private void PreencherRegimeTributario() - { - TextCRT.Text = string.Empty; - var crt = Report.GetColumnValue("NFe.NFe.infNFe.emit.CRT"); - if (crt == null) return; - switch ((int)(CRT)crt) - { - case 1: TextCRT.Text = "1 - SIMPLES NACIONAL"; break; - case 2: TextCRT.Text = "2 - SIMPLES NACIONAL - EXCESSO DE SUBLIMITE"; break; - case 3: TextCRT.Text = "3 - REGIME NORMAL"; break; - case 4: TextCRT.Text = "4 - SIMPLES NACIONAL - MEI"; break; - } - // Tipo de Regime de Apuracao do IBS e da CBS: campo reservado (NT 2026.010 - item 4.4), sem conteudo - TextTpRegApurIBSCBS.Text = string.Empty; - } - - // Linhas facultativas do bloco "TOTAL DO ICMS / IPI" (NT 2026.010 - item 4.4) - private void PreencherTotaisICMS() - { - var icmsTot = Report.GetColumnValue("NFe.NFe.infNFe.total.ICMSTot") as ICMSTot; - TotalICMSFCP.Visible = icmsTot != null && - (Positivo(icmsTot.vFCP) || Positivo(icmsTot.vFCPST) || Positivo(icmsTot.vICMSUFDest) || Positivo(icmsTot.vFCPUFDest)); - TotalICMSMono.Visible = icmsTot != null && - (Positivo(icmsTot.qBCMono) || Positivo(icmsTot.vICMSMono) || Positivo(icmsTot.qBCMonoReten) || Positivo(icmsTot.vICMSMonoReten)); - if (icmsTot == null) return; - - TextTotVFCP.Text = FormatarValorOpcional(icmsTot.vFCP); - TextTotVFCPST.Text = FormatarValorOpcional(icmsTot.vFCPST); - TextTotVICMSUFDest.Text = FormatarValorOpcional(icmsTot.vICMSUFDest); - TextTotVFCPUFDest.Text = FormatarValorOpcional(icmsTot.vFCPUFDest); - - TextTotQBCMono.Text = FormatarQuantidadeOpcional(icmsTot.qBCMono); - TextTotVICMSMono.Text = FormatarValorOpcional(icmsTot.vICMSMono); - TextTotQBCMonoReten.Text = FormatarQuantidadeOpcional(icmsTot.qBCMonoReten); - TextTotVICMSMonoReten.Text = FormatarValorOpcional(icmsTot.vICMSMonoReten); - - var exibirIbpt = Report.GetParameterValue("ExibirTotalTributos"); - QuadroVTOTTRIB.Visible = exibirIbpt != null && (bool)exibirIbpt; - txtIbptValor.Visible = QuadroVTOTTRIB.Visible; - } - - // Bloco "TOTAL DO IBS / CBS / IS" (NT 2026.010 - item 4.1) - private void PreencherTotaisIBSCBSIS() - { - var ibsCbsTot = Report.GetColumnValue("NFe.NFe.infNFe.total.IBSCBSTot") as IBSCBSTot; - var isTot = Report.GetColumnValue("NFe.NFe.infNFe.total.ISTot") as ISTot; - - TotalIBSCBSIS.Visible = ibsCbsTot != null || isTot != null; - TotalIBSCBSMono.Visible = ibsCbsTot != null && ibsCbsTot.gMono != null; - if (!TotalIBSCBSIS.Visible) return; - - decimal vCBS = 0, vIBSUF = 0, vIBSMun = 0, vIS = 0; - if (ibsCbsTot != null) - { - if (ibsCbsTot.gCBS != null) vCBS = ibsCbsTot.gCBS.vCBS; - if (ibsCbsTot.gIBS != null) - { - if (ibsCbsTot.gIBS.gIBSUF != null) vIBSUF = ibsCbsTot.gIBS.gIBSUF.vIBSUF; - if (ibsCbsTot.gIBS.gIBSMun != null) vIBSMun = ibsCbsTot.gIBS.gIBSMun.vIBSMun; - } - } - if (isTot != null) vIS = isTot.vIS; - - TextTotVCBS.Text = FormatarValorRTC(vCBS); - TextTotVIBSUF.Text = FormatarValorRTC(vIBSUF); - TextTotVIBSMun.Text = FormatarValorRTC(vIBSMun); - TextTotVIS.Text = isTot != null ? FormatarValorRTC(vIS) : string.Empty; - - if (TotalIBSCBSMono.Visible) - { - var mono = ibsCbsTot.gMono; - TextTotVIBSMono.Text = FormatarValorRTC(mono.vIBSMono); - TextTotVCBSMono.Text = FormatarValorRTC(mono.vCBSMono); - TextTotVIBSMonoReten.Text = FormatarValorRTC(mono.vIBSMonoReten); - TextTotVCBSMonoReten.Text = FormatarValorRTC(mono.vCBSMonoReten); - } - } - - // Quadro "FATURA / DUPLICATAS" (NT 2026.010 - modelo vertical): grade com 3 duplicatas por linha - // (FATURA/DUPLICATA | VENCIMENTO | VALOR). Facultativo - suprimido quando nao houver cobr no XML. - private float _TamanhoDuplicatas = 0; - - private void PreencherFaturaDuplicatas() - { - var cobranca = Report.GetColumnValue("NFe.NFe.infNFe.cobr") as global::NFe.Classes.Informacoes.Cobranca.cobr; - var qtdDuplicatas = 0; - if (cobranca != null && cobranca.dup != null) qtdDuplicatas = cobranca.dup.Count; - FaturaDuplicatas.Visible = qtdDuplicatas > 0; - Duplicatas.Visible = qtdDuplicatas > 0; - _TamanhoDuplicatas = Duplicatas.Visible ? (float)Math.Ceiling(qtdDuplicatas / 3.0) * Duplicatas.Height : 0f; - } - - // Quadro "TRANSPORTADOR / VOLUMES TRANSPORTADOS": exibido somente quando houver dados de transporte/volumes - private void PreencherTransportador() - { - var transporte = Report.GetColumnValue("NFe.NFe.infNFe.transp") as transp; - TransportadorVolumes.Visible = transporte != null && (transporte.transporta != null || transporte.veicTransp != null || - (transporte.vol != null && transporte.vol.Count > 0) || (transporte.reboque != null && transporte.reboque.Count > 0)); - } - - private string FormatarCampo(string valor, char format) - { - if (valor == null) return string.Empty; - - switch (format) - { - case 'c': //CEP - if (valor.Length == 8) - valor = valor.Substring(0,2) + "." + valor.Substring(2,3) + "-" + valor.Substring(5,3); - break; - - case 'f': //Telefone /celular - if (valor.Length == 10) - valor = "(" +valor.Substring(0,2) + ") " + valor.Substring(2,4) + "-" + valor.Substring(6,4); - else if (valor.Length == 11) - valor = "(" +valor.Substring(0,2) + ") " + valor.Substring(2,5) + "-" + valor.Substring(7,4); - break; - - case 'd': //cpf / cnpj - if (valor.Length == 11) - valor = valor.Substring(0,3)+"."+valor.Substring(3,3)+"."+valor.Substring(6,3)+"-"+valor.Substring(9,2); - else if (valor.Length == 14) - valor = valor.Substring(0,2)+"."+valor.Substring(2,3)+"."+valor.Substring(5,3)+"/"+valor.Substring(8,4)+"-"+valor.Substring(12,2); - break; - - case 'p': - if (valor.Length == 7) - valor = valor.Substring(0,3)+"-"+valor.Substring(3,4); - break; - } - return valor; - } - - public Image ObterLogo() - { - byte[] logomarca = Report.GetParameterValue("Logo") as byte[]; - - if (logomarca == null) return null; - - using(var ms = new MemoryStream(logomarca)) - { - var image = Image.FromStream(ms); - return image; - } - } - - private float _Tamanho = 0; - private float _TamanhoItens = 0; - - private void DadosProdutosHeader_BeforePrint(object sender, EventArgs e) - { - - - _Tamanho = 0; - _TamanhoItens = 0; - - if (Canhoto.Visible) - _Tamanho += Canhoto.Height; - - if (Emitente.Visible) - _Tamanho += Emitente.Height; - - if (Destinatario.Visible) - _Tamanho += Destinatario.Height; - - if (LocalRetirada.Visible) - _Tamanho += LocalRetirada.Height; - - if (LocalEntrega.Visible) - _Tamanho += LocalEntrega.Height; - - string[] bandasTopo = { "FaturaDuplicatas", "TotaisNota", "TotalICMSIPI", "TotalICMSFCP", "TotalICMSMono", - "TotalIBSCBSIS", "TotalIBSCBSMono", "DadosISSQN" }; - foreach (var nomeBanda in bandasTopo) - { - var banda = Report.FindObject(nomeBanda) as BandBase; - if (banda != null && banda.Visible && (banda.Parent as BandBase == null || ((BandBase)banda.Parent).Visible)) - _Tamanho += banda.Height; - } - _Tamanho += _TamanhoDuplicatas; - - if (TransportadorVolumes.Visible) - _Tamanho += TransportadorVolumes.Height; - - if (DadosProdutosHeader.Visible) - _Tamanho += DadosProdutosHeader.Height; - - //_Tamanho += _TamanhoItens; - - if (DadosProdutosFooter.Visible) - _Tamanho += DadosProdutosFooter.Height; - - - if (DadosRetencoes.Visible) - _Tamanho += DadosRetencoes.Height; - - DadosAdicionais.CalcHeight(); - - if (DadosAdicionais.Visible) - _Tamanho += DadosAdicionais.Height; - - if (Rodape.Visible) - _Tamanho += Rodape.Height; - - } - - private void DadosProdutos_AfterPrint(object sender, EventArgs e) - { - DadosProdutos.StartNewPage = false; - - if (Engine.PageNo == 1) - { - _TamanhoItens += DadosProdutos.Height; - - if (_Tamanho + _TamanhoItens >= Engine.PageHeight) - { - DadosProdutos.StartNewPage = true; - _TamanhoItens = 0; - } - } - } - - - - } -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + using System; + using System.IO; + using System.Collections; + using System.Collections.Generic; + using System.ComponentModel; + using System.Windows.Forms; + using System.Drawing; + using System.Data; + using System.Linq; + using System.Text.RegularExpressions; + using System.Globalization; + using FastReport; + using FastReport.Data; + using FastReport.Dialog; + using FastReport.Barcode; + using FastReport.Table; + using FastReport.Utils; + using NFe.Classes.Informacoes.Identificacao.Tipos; + using NFe.Classes.Informacoes.Detalhe.Tributacao; + using NFe.Classes.Informacoes.Detalhe.Tributacao.Estadual.Tipos; + using NFe.Classes.Informacoes.Detalhe.Tributacao.Federal.Tipos; + using NFe.Utils.Tributacao.Estadual; + using NFe.Classes.Informacoes.Transporte; + using NFe.Classes.Protocolo; + using NFe.Classes.Informacoes.Emitente; + using NFe.Utils; + using NFe.Danfe.Base.NFe; + using NFe.Classes.Informacoes.Total; + + namespace FastReport + { + public class ReportScript + { + private void Emitente_BeforePrint(object sender, EventArgs e) + { + var chave = Substring(((String)Report.GetColumnValue("NFe.NFe.infNFe.Id")), 3); + TextChaveAcesso.Text = Regex.Replace(chave, ".{4}", "$0 "); + BarcodeChave.Text = chave; + poEmitLogo.Image = ObterLogo(); + PreencherDadosEmitente(poEmitLogo.Image != null); + PreencherRegimeTributario(); + PreencherTotaisIBSCBSIS(); + PreencherTotaisICMS(); + PreencherFaturaDuplicatas(); + PreencherTransportador(); + + var contigenciaId = (string)Report.GetParameterValue("ContingenciaID"); + BarCodeContigencia.Visible = !string.IsNullOrEmpty(contigenciaId); + if(BarCodeContigencia.Visible) BarCodeContigencia.Text = contigenciaId; + DadosISSQN.Visible = !(bool)Report.GetParameterValue("ImprimirISSQN"); + DadosRetencoes.Visible = (bool)Report.GetParameterValue("ExibeRetencoes"); + // Leiaute NT 2026.010: as celulas de tributos possuem varias linhas, a linha do item sempre se ajusta + prodRow.AutoSize = true; + prodNomeCell.WordWrap = (bool)Report.GetParameterValue("DuasLinhas"); + + if ((FinalidadeNFe)(Report.GetColumnValue("NFe.NFe.infNFe.ide.finNFe")) == FinalidadeNFe.fnDevolucao) + { + var ipi = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ICMSTot.vIPI")) ?? 0M; + if (ipi <= 0) + { + var ipiDevolvido = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ICMSTot.vIPIDevol")) ?? 0M; + Text163.Text = FormatNumber(ipiDevolvido,2); + } + } + + //Trato o campo de Frete + switch((int)((ModalidadeFrete)Report.GetColumnValue("NFe.NFe.infNFe.transp.modFrete"))) + { + case 0: Memo92.Text = "0 - Emit/Remet"; break; + case 1: Memo92.Text = "1 - Destinatário"; break; + case 2: Memo92.Text = "2 - Terceiros"; break; + case 3: Memo92.Text = "3 - Prop Remetente"; break; + case 4: Memo92.Text = "4 - Prop Destinatário"; break; + case 9: Memo92.Text = "9 - Sem Frete"; break; + } + + var vol = Report.GetDataSource("NFe.NFe.infNFe.transp.vol"); + if(vol != null){ + vol.Init(); + int qtdeVol = 0; + + int volumes = 0; + decimal pesoL = 0; + decimal pesoB = 0; + while (vol.HasMoreRows) + { + Memo106.Text = ((String)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.esp")); + Memo108.Text = ((String)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.marca")); + Memo110.Text = ((String)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.nVol")); + + volumes += ((Nullable<int>)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.qVol")) ?? 0; + pesoL += ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.pesoL")) ?? 0M; + pesoB += ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.transp.vol.pesoB")) ?? 0M; + qtdeVol++; + vol.Next(); + } + + if (qtdeVol >= 1) + { + if (qtdeVol > 1) + { + Memo106.Text = "VOLUMES"; + Memo108.Text = ""; + Memo110.Text = ""; + } + + Memo104.Text = volumes.ToString(); + Memo112.Text = FormatNumber(pesoB,3) + " KG"; + Memo114.Text = FormatNumber(pesoL,3) + " KG"; + } + } + + if(Engine.FinalPass) + { + var vBC = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ISSQNtot.vBC")) ?? 0; + var vISS = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ISSQNtot.vISS")) ?? 0; + var vServ = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.total.ISSQNtot.vServ")) ?? 0; + + DadosISSQN.Visible = !(vBC == 0 && vISS == 0 && vServ == 0); + + var retiradaCPF = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.CPF"); + var retiradaCNPJ = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.CNPJ"); + var retiradaEndereco = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.xLgr"); + var retiradaNro = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.nro"); + var retiradaBairro = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.xBairro"); + var retiradaMunicipio = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.xMun"); + var retiradaUf = (string)Report.GetColumnValue("NFe.NFe.infNFe.retirada.UF"); + + LocalRetirada.Visible = !string.IsNullOrEmpty(retiradaCPF) || !string.IsNullOrEmpty(retiradaCNPJ) || + !string.IsNullOrEmpty(retiradaEndereco) || !string.IsNullOrEmpty(retiradaNro) || + !string.IsNullOrEmpty(retiradaBairro) || !string.IsNullOrEmpty(retiradaMunicipio) || + !string.IsNullOrEmpty(retiradaUf); + + var entregaCPF = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.CPF"); + var entregaCNPJ = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.CNPJ"); + var entregaEndereco = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.xLgr"); + var entregaNro = (string)Report.GetColumnValue("[NFe.NFe.infNFe.entrega.nro]"); + var entregaBairro = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.xBairro"); + var entregaMunicipio = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.xMun"); + var entregaUf = (string)Report.GetColumnValue("NFe.NFe.infNFe.entrega.UF"); + + LocalEntrega.Visible = !string.IsNullOrEmpty(entregaCPF) || !string.IsNullOrEmpty(entregaCNPJ) || + !string.IsNullOrEmpty(entregaEndereco) || !string.IsNullOrEmpty(entregaNro) || + !string.IsNullOrEmpty(entregaBairro) || !string.IsNullOrEmpty(entregaMunicipio) || + !string.IsNullOrEmpty(entregaUf); + + + } + + string infAdicionais = (string)Report.GetColumnValue("NFe.NFe.infNFe.infAdic.infCpl") + Environment.NewLine + (string)Report.GetColumnValue("NFe.NFe.infNFe.infAdic.infAdFisco"); + + infAdicionais = infAdicionais.Replace("[","[\"[").Replace("]","]\"]"); + + if((bool)Report.GetParameterValue("QuebrarLinhasObservacao")) + { + infAdicionais = infAdicionais.Replace(";", Environment.NewLine); + } + + TextInfAdicionais.Text = infAdicionais; + //memContInfAdicionais.Text = string.Empty; + + //string[] lines = infAdicionais.Split(new string[]{ Environment.NewLine, "\n" }, StringSplitOptions.None); + //pgContDadosAdicionais.Visible = lines.Length > LINHAS_DADOS_ADICIONAIS; + + //if(pgContDadosAdicionais.Visible) + //{ + // TextInfAdicionais.Text = string.Join(Environment.NewLine, lines.Take(LINHAS_DADOS_ADICIONAIS-1).ToArray()); + // memContInfAdicionais.Text = string.Join(Environment.NewLine, lines.Skip(LINHAS_DADOS_ADICIONAIS-1).ToArray()); + //} + } + + private void _StartReport(object sender, EventArgs e) + { + // Canhoto facultativo (NT 2026.010 - item 4.4) + var exibirCanhoto = Report.GetParameterValue("ExibirCanhoto"); + Canhoto.Visible = exibirCanhoto == null || (bool)exibirCanhoto; + + // QR Code (NT 2026.010 - itens 4.4 e 4.5): exibido somente quando informado no XML + string qrCode = null; + try { qrCode = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFeSupl.qrCode")); } catch { } + var exibirQrCode = !string.IsNullOrEmpty(qrCode); + MemoQrCodeBox.Visible = exibirQrCode; + MemoQrCodeTitulo.Visible = exibirQrCode; + bcoQrCode.Visible = exibirQrCode; + if (exibirQrCode) + { + bcoQrCode.Text = qrCode; + DadosAdicionais.Height = 107.73f; + Text155.Width = 400f; + TextInfAdicionais.Width = 397f; + Text156.Left = 400f; + Text156.Width = 188.9f; + Text37.Left = 402f; + Text37.Width = 184.9f; + } + } + + private void DadosProdutos_BeforePrint(object sender, EventArgs e) + { + var icmsBasico = (ICMSBasico)Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.ICMS.TipoICMS"); + var ipiBasico = (IPIBasico)Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.IPI.TipoIPI"); + var ibsCbs = ObterIBSCBSItem(); + var impostoSeletivo = Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.IS") as global::NFe.Classes.Informacoes.Detalhe.Tributacao.IS; + var crt = (CRT)Report.GetColumnValue("NFe.NFe.infNFe.emit.CRT"); + var simplesNacional = crt == CRT.SimplesNacional || crt == CRT.SimplesNacionalMei; + + // CST / CFOP + var cst = string.Empty; + var rotuloCst = "CST"; + string icmsBc = "", icmsAliq = "", icmsValor = ""; + if (icmsBasico != null) + { + var orig = icmsBasico.GetIcmsOrig(); + var cstIcms = icmsBasico.GetIcmsCst().CsticmsParaString(); + if (simplesNacional && cstIcms != "02" && cstIcms != "15" && cstIcms != "53" && cstIcms != "61") + { + rotuloCst = "CSOSN"; + cst = orig.OrigemMercadoriaParaString() + icmsBasico.GetIcmsCsosn().CsosnicmsParaString(); + } + else + { + cst = orig.OrigemMercadoriaParaString() + cstIcms; + } + icmsBc = FormatarValorRTC(icmsBasico.GetIcmsBcValue()); + icmsAliq = FormatarAliquotaRTC(icmsBasico.GetIcmsPercent()); + icmsValor = FormatarValorRTC(icmsBasico.GetIcmsValue()); + } + var cfop = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.CFOP")); + prodOriginCell.Text = rotuloCst + " " + cst + Environment.NewLine + "CFOP " + cfop; + + // IPI + string ipiBc = "", ipiAliq = "", ipiValor = ""; + var ipiTrib = ipiBasico as global::NFe.Classes.Informacoes.Detalhe.Tributacao.Federal.IPITrib; + if (ipiTrib != null) + { + if (ipiTrib.vBC.HasValue) ipiBc = FormatarValorRTC(ipiTrib.vBC.Value); + if (ipiTrib.pIPI.HasValue) ipiAliq = FormatarAliquotaRTC(ipiTrib.pIPI.Value); + ipiValor = FormatarValorRTC(ipiTrib.vIPI ?? 0M); + } + else if (ipiBasico == null && (FinalidadeNFe)(Report.GetColumnValue("NFe.NFe.infNFe.ide.finNFe")) == FinalidadeNFe.fnDevolucao) + { + var devolvidoPercentual = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.det.impostoDevol.pDevol")) ?? 0M; + var ipiDevolvidoValor = ((Nullable<decimal>)Report.GetColumnValue("NFe.NFe.infNFe.det.impostoDevol.IPI.vIPIDevol")) ?? 0M; + if (ipiDevolvidoValor > 0) + { + ipiValor = FormatarValorRTC(ipiDevolvidoValor); + ipiAliq = FormatarAliquotaRTC(devolvidoPercentual); + } + } + + // IBS / CBS (Grupo UB) - com gRed informado imprime a aliquota efetiva (pAliqEfet) + string ibsCbsBc = "", ibsUfAliq = "", ibsUfValor = "", ibsMunAliq = "", ibsMunValor = "", cbsAliq = "", cbsValor = ""; + var gIBSCBS = ibsCbs != null ? ibsCbs.gIBSCBS : null; + if (gIBSCBS != null) + { + ibsCbsBc = FormatarValorRTC(gIBSCBS.vBC); + if (gIBSCBS.gIBSUF != null) + { + ibsUfAliq = FormatarAliquotaRTC(gIBSCBS.gIBSUF.gRed != null ? gIBSCBS.gIBSUF.gRed.pAliqEfet : gIBSCBS.gIBSUF.pIBSUF); + ibsUfValor = FormatarValorRTC(gIBSCBS.gIBSUF.vIBSUF); + } + if (gIBSCBS.gIBSMun != null) + { + ibsMunAliq = FormatarAliquotaRTC(gIBSCBS.gIBSMun.gRed != null ? gIBSCBS.gIBSMun.gRed.pAliqEfet : gIBSCBS.gIBSMun.pIBSMun); + ibsMunValor = FormatarValorRTC(gIBSCBS.gIBSMun.vIBSMun); + } + if (gIBSCBS.gCBS != null) + { + cbsAliq = FormatarAliquotaRTC(gIBSCBS.gCBS.gRed != null ? gIBSCBS.gCBS.gRed.pAliqEfet : gIBSCBS.gCBS.pCBS); + cbsValor = FormatarValorRTC(gIBSCBS.gCBS.vCBS); + } + } + + // Imposto Seletivo + string isBc = "", isAliq = "", isValor = ""; + if (impostoSeletivo != null) + { + isBc = FormatarValorRTC(impostoSeletivo.vBCIS); + isAliq = FormatarAliquotaRTC(impostoSeletivo.pIS); + isValor = FormatarValorRTC(impostoSeletivo.vIS); + } + + var nl = Environment.NewLine; + prodBcLCell.Text = "ICMS" + nl + "IBS / CBS" + nl + "IS" + nl + "IPI"; + prodBcVCell.Text = icmsBc + nl + ibsCbsBc + nl + isBc + nl + ipiBc; + prodAlqLACell.Text = "ICMS" + nl + "IBS UF" + nl + "IBS MUN"; + prodAlqVACell.Text = icmsAliq + nl + ibsUfAliq + nl + ibsMunAliq; + prodAlqLBCell.Text = "/ CBS" + nl + "/ IPI" + nl + "/ IS"; + prodAlqVBCell.Text = cbsAliq + nl + ipiAliq + nl + isAliq; + prodVlrLACell.Text = prodAlqLACell.Text; + prodVlrVACell.Text = icmsValor + nl + ibsUfValor + nl + ibsMunValor; + prodVlrLBCell.Text = prodAlqLBCell.Text; + prodVlrVBCell.Text = cbsValor + nl + ipiValor + nl + isValor; + + // Quantidade / unidade / valores + var imprimirUnidQtdeValor = (ImprimirUnidQtdeValor)Report.GetParameterValue("ImprimirUnidQtdeValor"); + var imprimirTotalLiquido = (bool)Report.GetParameterValue("ImprimirTotalLiquido"); + var uCom = ((String)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.uCom")); + var uTrib = ((String)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.uTrib")); + var qCom = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.qCom")); + var qTrib = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.qTrib")); + var vUnCom = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vUnCom")); + var vUnTrib = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vUnTrib")); + var vProd = ((Decimal)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vProd")); + var vDesc = (((Nullable<Decimal>)Report.GetColumnValue("NFe.NFe.infNFe.det.prod.vDesc")) ?? 0); + if (imprimirTotalLiquido) vProd = vProd - vDesc; + prodValorCell.Text = FormatNumber(vProd, 2); + var decimaisQuantidadeItem = (int)Report.GetParameterValue("DecimaisQuantidadeItem"); + var decimaisValorUnitario = (int)Report.GetParameterValue("DecimaisValorUnitario"); + switch (imprimirUnidQtdeValor) + { + case ImprimirUnidQtdeValor.Comercial: + prodQtdCell.Text = FormatNumber(qCom, decimaisQuantidadeItem) + nl + "[" + uCom + "]"; + prodValorUnitarioCell.Text = FormatNumber(vUnCom, decimaisValorUnitario); + break; + case ImprimirUnidQtdeValor.Tributavel: + prodQtdCell.Text = FormatNumber(qTrib, decimaisQuantidadeItem) + nl + "[" + uTrib + "]"; + prodValorUnitarioCell.Text = FormatNumber(vUnTrib, decimaisValorUnitario); + break; + case ImprimirUnidQtdeValor.Ambos: + prodQtdCell.Text = FormatNumber(qCom, decimaisQuantidadeItem) + nl + "[" + uCom + "]" + nl + + FormatNumber(qTrib, decimaisQuantidadeItem) + nl + "[" + uTrib + "]"; + prodValorUnitarioCell.Text = FormatNumber(vUnCom, decimaisValorUnitario) + nl + nl + FormatNumber(vUnTrib, decimaisValorUnitario); + break; + } + } + + private static readonly CultureInfo CulturaRTC = new CultureInfo("pt-BR"); + + private string FormatarValorRTC(decimal valor) + { + return valor.ToString("#,##0.00", CulturaRTC); + } + + private string FormatarValorOpcional(Nullable<decimal> valor) + { + return valor.HasValue ? FormatarValorRTC(valor.Value) : string.Empty; + } + + private string FormatarQuantidadeOpcional(Nullable<decimal> valor) + { + return valor.HasValue ? valor.Value.ToString("#,##0.00##", CulturaRTC) : string.Empty; + } + + private string FormatarAliquotaRTC(decimal valor) + { + return valor.ToString("#,##0.00##", CulturaRTC) + "%"; + } + + private static bool Positivo(Nullable<decimal> valor) + { + return valor.HasValue && valor.Value != 0; + } + + private static string EscaparHtml(string texto) + { + if (string.IsNullOrEmpty(texto)) return string.Empty; + return texto.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;"); + } + + private global::NFe.Classes.Informacoes.Detalhe.Tributacao.IBSCBS ObterIBSCBSItem() + { + return Report.GetColumnValue("NFe.NFe.infNFe.det.imposto.IBSCBS") as global::NFe.Classes.Informacoes.Detalhe.Tributacao.IBSCBS; + } + + // Descricao do item: codigo (negrito) + descricao, informacoes adicionais e [NCM] [cClassTrib] (NT 2026.010 - item 4.3) + public string TextoDescricaoItem() + { + var nl = Environment.NewLine; + var texto = "<b>" + EscaparHtml(Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.cProd"))) + "</b> " + + EscaparHtml(Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.xProd"))); + var infAdProd = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.infAdProd")); + if (!string.IsNullOrEmpty(infAdProd)) texto += nl + EscaparHtml(infAdProd); + + var partes = new List<string>(); + var ncm = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.det.prod.NCM")); + if (!string.IsNullOrEmpty(ncm)) partes.Add("[NCM " + EscaparHtml(ncm) + "]"); + var ibsCbs = ObterIBSCBSItem(); + if (ibsCbs != null && !string.IsNullOrEmpty(ibsCbs.cClassTrib)) partes.Add("[cClassTrib " + EscaparHtml(ibsCbs.cClassTrib) + "]"); + if (partes.Count > 0) texto += nl + string.Join(" ", partes.ToArray()); + return texto; + } + + private void PreencherRegimeTributario() + { + TextCRT.Text = string.Empty; + var crt = Report.GetColumnValue("NFe.NFe.infNFe.emit.CRT"); + if (crt == null) return; + switch ((int)(CRT)crt) + { + case 1: TextCRT.Text = "1 - SIMPLES NACIONAL"; break; + case 2: TextCRT.Text = "2 - SIMPLES NACIONAL - EXCESSO DE SUBLIMITE"; break; + case 3: TextCRT.Text = "3 - REGIME NORMAL"; break; + case 4: TextCRT.Text = "4 - SIMPLES NACIONAL - MEI"; break; + } + // Tipo de Regime de Apuracao do IBS e da CBS: campo reservado (NT 2026.010 - item 4.4), sem conteudo + TextTpRegApurIBSCBS.Text = string.Empty; + } + + // Linhas facultativas do bloco "TOTAL DO ICMS / IPI" (NT 2026.010 - item 4.4) + private void PreencherTotaisICMS() + { + var icmsTot = Report.GetColumnValue("NFe.NFe.infNFe.total.ICMSTot") as ICMSTot; + TotalICMSFCP.Visible = icmsTot != null && + (Positivo(icmsTot.vFCP) || Positivo(icmsTot.vFCPST) || Positivo(icmsTot.vICMSUFDest) || Positivo(icmsTot.vFCPUFDest)); + TotalICMSMono.Visible = icmsTot != null && + (Positivo(icmsTot.qBCMono) || Positivo(icmsTot.vICMSMono) || Positivo(icmsTot.qBCMonoReten) || Positivo(icmsTot.vICMSMonoReten)); + if (icmsTot == null) return; + + TextTotVFCP.Text = FormatarValorOpcional(icmsTot.vFCP); + TextTotVFCPST.Text = FormatarValorOpcional(icmsTot.vFCPST); + TextTotVICMSUFDest.Text = FormatarValorOpcional(icmsTot.vICMSUFDest); + TextTotVFCPUFDest.Text = FormatarValorOpcional(icmsTot.vFCPUFDest); + + TextTotQBCMono.Text = FormatarQuantidadeOpcional(icmsTot.qBCMono); + TextTotVICMSMono.Text = FormatarValorOpcional(icmsTot.vICMSMono); + TextTotQBCMonoReten.Text = FormatarQuantidadeOpcional(icmsTot.qBCMonoReten); + TextTotVICMSMonoReten.Text = FormatarValorOpcional(icmsTot.vICMSMonoReten); + } + + // Bloco "TOTAL DO IBS / CBS / IS" (NT 2026.010 - item 4.1) + private void PreencherTotaisIBSCBSIS() + { + var ibsCbsTot = Report.GetColumnValue("NFe.NFe.infNFe.total.IBSCBSTot") as IBSCBSTot; + var isTot = Report.GetColumnValue("NFe.NFe.infNFe.total.ISTot") as ISTot; + + // Bloco sempre impresso; campos ausentes no XML saem como 0,00 + TotalIBSCBSIS.Visible = true; + TotalIBSCBSMono.Visible = ibsCbsTot != null && ibsCbsTot.gMono != null; + + decimal vCBS = 0, vIBSUF = 0, vIBSMun = 0, vIS = 0; + if (ibsCbsTot != null) + { + if (ibsCbsTot.gCBS != null) vCBS = ibsCbsTot.gCBS.vCBS; + if (ibsCbsTot.gIBS != null) + { + if (ibsCbsTot.gIBS.gIBSUF != null) vIBSUF = ibsCbsTot.gIBS.gIBSUF.vIBSUF; + if (ibsCbsTot.gIBS.gIBSMun != null) vIBSMun = ibsCbsTot.gIBS.gIBSMun.vIBSMun; + } + } + if (isTot != null) vIS = isTot.vIS; + + TextTotVCBS.Text = FormatarValorRTC(vCBS); + TextTotVIBSUF.Text = FormatarValorRTC(vIBSUF); + TextTotVIBSMun.Text = FormatarValorRTC(vIBSMun); + TextTotVIS.Text = FormatarValorRTC(vIS); + + if (TotalIBSCBSMono.Visible) + { + var mono = ibsCbsTot.gMono; + TextTotVIBSMono.Text = FormatarValorRTC(mono.vIBSMono); + TextTotVCBSMono.Text = FormatarValorRTC(mono.vCBSMono); + TextTotVIBSMonoReten.Text = FormatarValorRTC(mono.vIBSMonoReten); + TextTotVCBSMonoReten.Text = FormatarValorRTC(mono.vCBSMonoReten); + } + } + + // Quadro "FATURA / DUPLICATAS" (NT 2026.010 - modelo vertical): grade com 3 duplicatas por linha + // (FATURA/DUPLICATA | VENCIMENTO | VALOR). Facultativo - suprimido quando nao houver cobr no XML. + private float _TamanhoDuplicatas = 0; + + private void PreencherFaturaDuplicatas() + { + var cobranca = Report.GetColumnValue("NFe.NFe.infNFe.cobr") as global::NFe.Classes.Informacoes.Cobranca.cobr; + var qtdDuplicatas = 0; + if (cobranca != null && cobranca.dup != null) qtdDuplicatas = cobranca.dup.Count; + FaturaDuplicatas.Visible = qtdDuplicatas > 0; + Duplicatas.Visible = qtdDuplicatas > 0; + _TamanhoDuplicatas = Duplicatas.Visible ? (float)Math.Ceiling(qtdDuplicatas / 3.0) * Duplicatas.Height : 0f; + } + + // Quadro "TRANSPORTADOR / VOLUMES TRANSPORTADOS": + // - modFrete 0 a 4: sempre exibido; + // - modFrete 9 (sem frete): exibido somente quando houver dados de transportador ou volumes no XML. + private void PreencherTransportador() + { + var transporte = Report.GetColumnValue("NFe.NFe.infNFe.transp") as transp; + if (transporte == null) + { + TransportadorVolumes.Visible = false; + return; + } + + var possuiDados = transporte.transporta != null || transporte.veicTransp != null || + PossuiVolumePreenchido(transporte.vol) || (transporte.reboque != null && transporte.reboque.Count > 0); + + TransportadorVolumes.Visible = transporte.modFrete != ModalidadeFrete.mfSemFrete || possuiDados; + } + + // A lista "vol" pode vir com 1 item vazio quando o XML não possui volumes; + // considera somente itens com algum dado de fato preenchido. + private bool PossuiVolumePreenchido(List<vol> volumes) + { + if (volumes == null) + return false; + + return volumes.Any(v => v != null && ( + (v.qVol ?? 0) != 0 || + !String.IsNullOrWhiteSpace(v.esp) || + !String.IsNullOrWhiteSpace(v.marca) || + !String.IsNullOrWhiteSpace(v.nVol) || + (v.pesoL ?? 0M) != 0M || + (v.pesoB ?? 0M) != 0M || + (v.lacres != null && v.lacres.Any(l => l != null && !String.IsNullOrWhiteSpace(l.nLacre))))); + } + + // Quadro do emitente (à esquerda de "DANFE"), de cima para baixo: + // razão social / logradouro, número - bairro / cidade/UF - CEP / fone. + // Sem logomarca, os dados ocupam o quadro inteiro. + private void PreencherDadosEmitente(bool temLogo) + { + var ender = Report.GetColumnValue("NFe.NFe.infNFe.emit.enderEmit") as enderEmit; + var nome = Convert.ToString(Report.GetColumnValue("NFe.NFe.infNFe.emit.xNome")); + + var linhas = new List<string>(); + if (ender != null) + { + linhas.Add(JuntarTexto(" – ", JuntarTexto(", ", ender.xLgr, ender.nro, ender.xCpl), ender.xBairro)); + + var cep = ender.CEP == null ? string.Empty : Regex.Replace(ender.CEP, "\\D", ""); + if (cep.Length == 8) cep = cep.Substring(0, 5) + "-" + cep.Substring(5, 3); + linhas.Add(JuntarTexto(" – ", JuntarTexto("/", ender.xMun, ender.ProxyUF), string.IsNullOrEmpty(cep) ? null : "CEP " + cep)); + + if (ender.fone.HasValue) + linhas.Add("Fone: " + FormatarCampo(ender.fone.Value.ToString(), 'f')); + } + linhas.RemoveAll(l => string.IsNullOrWhiteSpace(l)); + + poEmitLogo.Visible = temLogo; + + const float margem = 4f; + float esquerda = temLogo ? poEmitLogo.Left + poEmitLogo.Width + 1f : memEmitente.Left + margem; + float largura = memEmitente.Left + memEmitente.Width - margem - esquerda; + + Text1.Left = esquerda; + Text1.Width = largura; + Text1.Font = new Font("Times New Roman", temLogo ? 9f : 11f, FontStyle.Bold); + Text1.Text = nome; + + memDadosEmitente.Left = esquerda; + memDadosEmitente.Width = largura; + memDadosEmitente.Font = new Font("Times New Roman", temLogo ? 7.5f : 9f, FontStyle.Regular); + memDadosEmitente.Text = string.Join("\r\n", linhas); + + // bloco centralizado verticalmente dentro do quadro do emitente + float disponivel = memEmitente.Height - 4f; + float alturaNome = Math.Min(Text1.CalcHeight(), disponivel); + float alturaDados = Math.Min(memDadosEmitente.CalcHeight(), Math.Max(0f, disponivel - alturaNome)); + float topo = memEmitente.Top + Math.Max(2f, (memEmitente.Height - alturaNome - alturaDados) / 2f); + + Text1.Top = topo; + Text1.Height = alturaNome; + memDadosEmitente.Top = topo + alturaNome; + memDadosEmitente.Height = alturaDados; + } + + private string JuntarTexto(string separador, params string[] partes) + { + return string.Join(separador, partes.Where(p => !string.IsNullOrWhiteSpace(p)).Select(p => p.Trim())); + } + + private string FormatarCampo(string valor, char format) + { + if (valor == null) return string.Empty; + + switch (format) + { + case 'c': //CEP + if (valor.Length == 8) + valor = valor.Substring(0,2) + "." + valor.Substring(2,3) + "-" + valor.Substring(5,3); + break; + + case 'f': //Telefone /celular + if (valor.Length == 10) + valor = "(" +valor.Substring(0,2) + ") " + valor.Substring(2,4) + "-" + valor.Substring(6,4); + else if (valor.Length == 11) + valor = "(" +valor.Substring(0,2) + ") " + valor.Substring(2,5) + "-" + valor.Substring(7,4); + break; + + case 'd': //cpf / cnpj + if (valor.Length == 11) + valor = valor.Substring(0,3)+"."+valor.Substring(3,3)+"."+valor.Substring(6,3)+"-"+valor.Substring(9,2); + else if (valor.Length == 14) + valor = valor.Substring(0,2)+"."+valor.Substring(2,3)+"."+valor.Substring(5,3)+"/"+valor.Substring(8,4)+"-"+valor.Substring(12,2); + break; + + case 'p': + if (valor.Length == 7) + valor = valor.Substring(0,3)+"-"+valor.Substring(3,4); + break; + } + return valor; + } + + public Image ObterLogo() + { + byte[] logomarca = Report.GetParameterValue("Logo") as byte[]; + + if (logomarca == null) return null; + + using(var ms = new MemoryStream(logomarca)) + { + var image = Image.FromStream(ms); + return image; + } + } + + private float _Tamanho = 0; + private float _TamanhoItens = 0; + + private void DadosProdutosHeader_BeforePrint(object sender, EventArgs e) + { + + + _Tamanho = 0; + _TamanhoItens = 0; + + if (Canhoto.Visible) + _Tamanho += Canhoto.Height; + + if (Emitente.Visible) + _Tamanho += Emitente.Height; + + if (Destinatario.Visible) + _Tamanho += Destinatario.Height; + + if (LocalRetirada.Visible) + _Tamanho += LocalRetirada.Height; + + if (LocalEntrega.Visible) + _Tamanho += LocalEntrega.Height; + + string[] bandasTopo = { "FaturaDuplicatas", "TotaisNota", "TotalICMSIPI", "TotalICMSFCP", "TotalICMSMono", + "TotalIBSCBSIS", "TotalIBSCBSMono", "DadosISSQN" }; + foreach (var nomeBanda in bandasTopo) + { + var banda = Report.FindObject(nomeBanda) as BandBase; + if (banda != null && banda.Visible && (banda.Parent as BandBase == null || ((BandBase)banda.Parent).Visible)) + _Tamanho += banda.Height; + } + _Tamanho += _TamanhoDuplicatas; + + if (TransportadorVolumes.Visible) + _Tamanho += TransportadorVolumes.Height; + + if (DadosProdutosHeader.Visible) + _Tamanho += DadosProdutosHeader.Height; + + //_Tamanho += _TamanhoItens; + + if (DadosProdutosFooter.Visible) + _Tamanho += DadosProdutosFooter.Height; + + + if (DadosRetencoes.Visible) + _Tamanho += DadosRetencoes.Height; + + DadosAdicionais.CalcHeight(); + + if (DadosAdicionais.Visible) + _Tamanho += DadosAdicionais.Height; + + if (Rodape.Visible) + _Tamanho += Rodape.Height; + + } + + private void DadosProdutos_AfterPrint(object sender, EventArgs e) + { + DadosProdutos.StartNewPage = false; + + if (Engine.PageNo == 1) + { + _TamanhoItens += DadosProdutos.Height; + + if (_Tamanho + _TamanhoItens >= Engine.PageHeight) + { + DadosProdutos.StartNewPage = true; + _TamanhoItens = 0; + } + } + } + + + + } + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +