diff --git a/src/app/dre-entidade/analitico.tsx b/src/app/dre-entidade/analitico.tsx index c6092b2..9b3d119 100644 --- a/src/app/dre-entidade/analitico.tsx +++ b/src/app/dre-entidade/analitico.tsx @@ -523,6 +523,62 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) { }; const baseColumns = [ + { + field: "numero_lancamento", + headerName: "ID", + width: 100, + sortable: true, + resizable: true, + renderCell: (params: any) => params.value || "-", + }, + { + field: "data_lancamento", + headerName: "Dt Lanc", + width: 95, + sortable: true, + resizable: true, + renderCell: dateCellRenderer, + }, + { + field: "data_compensacao", + headerName: "Dt Comp", + width: 95, + sortable: true, + resizable: true, + renderCell: dateCellRenderer, + }, + { + field: "data_vencimento", + headerName: "Dt Venc", + width: 95, + sortable: true, + resizable: true, + renderCell: dateCellRenderer, + }, + { + field: "data_caixa", + headerName: "Dt Caixa", + width: 95, + sortable: true, + resizable: true, + renderCell: dateCellRenderer, + }, + { + field: "data_pagto", + headerName: "Dt Pagto", + width: 95, + sortable: true, + resizable: true, + renderCell: dateCellRenderer, + }, + { + field: "entidade", + headerName: "Entidade", + width: 90, + sortable: true, + resizable: true, + renderCell: (params: any) => params.value || "-", + }, { field: "codigo_fornecedor", headerName: "Cod. Fornec", @@ -600,62 +656,6 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) { sortable: true, resizable: true, }, - { - field: "numero_lancamento", - headerName: "ID", - width: 100, - sortable: true, - resizable: true, - renderCell: (params: any) => params.value || "-", - }, - { - field: "data_lancamento", - headerName: "Dt Lanc", - width: 95, - sortable: true, - resizable: true, - renderCell: dateCellRenderer, - }, - { - field: "data_compensacao", - headerName: "Dt Comp", - width: 95, - sortable: true, - resizable: true, - renderCell: dateCellRenderer, - }, - { - field: "data_vencimento", - headerName: "Dt Venc", - width: 95, - sortable: true, - resizable: true, - renderCell: dateCellRenderer, - }, - { - field: "data_caixa", - headerName: "Dt Caixa", - width: 95, - sortable: true, - resizable: true, - renderCell: dateCellRenderer, - }, - { - field: "data_pagto", - headerName: "Dt Pagto", - width: 95, - sortable: true, - resizable: true, - renderCell: dateCellRenderer, - }, - { - field: "entidade", - headerName: "Entidade", - width: 90, - sortable: true, - resizable: true, - renderCell: (params: any) => params.value || "-", - }, { field: "tipo_parceiro", headerName: "Tipo Parc", @@ -764,42 +764,78 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) { } }, [filteredData, columnFilters]); - // Exportação XLSX + // Exportação XLSX - Exporta exatamente as colunas e valores da grid const exportToExcel = () => { if (sortedAndFilteredData.length === 0) return; - const exportData = sortedAndFilteredData.map((item) => ({ - "DTLANC": item.data_lancamento - ? new Date(item.data_lancamento).toLocaleDateString("pt-BR") - : "-", - "DTCOMPENSACAO": item.data_compensacao - ? new Date(item.data_compensacao).toLocaleDateString("pt-BR") - : "-", - "DTVENC": item.data_vencimento - ? new Date(item.data_vencimento).toLocaleDateString("pt-BR") - : "-", - "DTCAIXA": item.data_caixa - ? new Date(item.data_caixa).toLocaleDateString("pt-BR") - : "-", - "DTPAGTO": item.data_pagto - ? new Date(item.data_pagto).toLocaleDateString("pt-BR") - : "-", - "ENTIDADE": item.entidade || "-", - "TIPOPARCEIRO": item.tipo_parceiro || "-", - "CODFORNEC": item.codigo_fornecedor || "-", - "FORNECEDOR": item.nome_fornecedor || "-", - "CODIGOCENTROCUSTO": item.codigo_centrocusto || "-", - "CENTROCUSTO": item.centro_custo || "-", - "CODCONTA": item.codigo_conta || "-", - "CONTA": item.conta || "-", - "VLREALIZADO": typeof item.valor === "string" ? parseFloat(item.valor) : (item.valor || 0), - "VLPREVISTO": typeof item.valor_previsto === "string" ? parseFloat(item.valor_previsto) : (item.valor_previsto || 0), - "VLCONFIRMADO": typeof item.valor_confirmado === "string" ? parseFloat(item.valor_confirmado) : (item.valor_confirmado || 0), - "VLPAGO": typeof item.valor_pago === "string" ? parseFloat(item.valor_pago) : (item.valor_pago || 0), - "HISTORICO": item.historico || "-", - "HISTORICO2": item.historico2 || "-", - "NUMLANC": item.numero_lancamento || "-", - })); + // Funções auxiliares para formatar valores exatamente como na grid + const formatDateValue = (value: any): string => { + if (!value) return "-"; + try { + return new Date(value).toLocaleDateString("pt-BR"); + } catch (error) { + return value; + } + }; + + const formatCurrencyValue = (value: any, showZero: boolean = false): string | number => { + if (value === null || value === undefined || value === "") return "-"; + const numValue = typeof value === "string" ? parseFloat(value) : Number(value); + if (isNaN(numValue)) return "-"; + if (!showZero && numValue === 0) return "-"; + // Para Excel, retornar o número formatado como string (mantém o formato de moeda) + return new Intl.NumberFormat("pt-BR", { + style: "currency", + currency: "BRL", + }).format(numValue); + }; + + const formatCellValue = (column: GridColDef, item: any): any => { + const value = item[column.field]; + + // Se a coluna tem renderCell, aplicar a mesma lógica + if (column.renderCell) { + // Para datas + if (column.field.includes("data_")) { + return formatDateValue(value); + } + + // Para valores monetários + if (column.field === "valor") { + return formatCurrencyValue(value, true); + } + if (column.field === "valor_previsto" || column.field === "valor_confirmado" || column.field === "valor_pago") { + return formatCurrencyValue(value, false); + } + + // Para campos que retornam "-" se vazios + if (column.field === "centro_custo" || column.field === "numero_lancamento" || + column.field === "entidade" || column.field === "tipo_parceiro") { + return value || "-"; + } + } + + // Para datas sem renderCell explícito (mas que são datas) + if (column.field.includes("data_")) { + return formatDateValue(value); + } + + // Valor padrão + return value ?? ""; + }; + + // Criar dados de exportação usando as colunas da grid na ordem exata + const exportData = sortedAndFilteredData.map((item) => { + const row: Record = {}; + + // Iterar sobre as colunas na ordem da grid + columns.forEach((column) => { + const headerName = column.headerName || column.field; + row[headerName] = formatCellValue(column, item); + }); + + return row; + }); const wb = XLSX.utils.book_new(); const ws = XLSX.utils.json_to_sheet(exportData); diff --git a/src/app/dre-entidade/teste.tsx b/src/app/dre-entidade/teste.tsx index ac354e9..1bc9c2b 100644 --- a/src/app/dre-entidade/teste.tsx +++ b/src/app/dre-entidade/teste.tsx @@ -2292,7 +2292,7 @@ export default function Teste() {
-

DRE Gerencial

+

Despesa Entidade

Demonstração do Resultado do Exercício