fix: correção da exportação do xlsx por entidade

This commit is contained in:
Alessandro Gonçaalves 2025-12-03 10:47:44 -03:00
parent 5b1f36db14
commit bfdeadac8e
2 changed files with 126 additions and 90 deletions

View File

@ -523,6 +523,62 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
}; };
const baseColumns = [ 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", field: "codigo_fornecedor",
headerName: "Cod. Fornec", headerName: "Cod. Fornec",
@ -600,62 +656,6 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
sortable: true, sortable: true,
resizable: 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", field: "tipo_parceiro",
headerName: "Tipo Parc", headerName: "Tipo Parc",
@ -764,42 +764,78 @@ export default function AnaliticoComponent({ filtros }: AnaliticoProps) {
} }
}, [filteredData, columnFilters]); }, [filteredData, columnFilters]);
// Exportação XLSX // Exportação XLSX - Exporta exatamente as colunas e valores da grid
const exportToExcel = () => { const exportToExcel = () => {
if (sortedAndFilteredData.length === 0) return; if (sortedAndFilteredData.length === 0) return;
const exportData = sortedAndFilteredData.map((item) => ({ // Funções auxiliares para formatar valores exatamente como na grid
"DTLANC": item.data_lancamento const formatDateValue = (value: any): string => {
? new Date(item.data_lancamento).toLocaleDateString("pt-BR") if (!value) return "-";
: "-", try {
"DTCOMPENSACAO": item.data_compensacao return new Date(value).toLocaleDateString("pt-BR");
? new Date(item.data_compensacao).toLocaleDateString("pt-BR") } catch (error) {
: "-", return value;
"DTVENC": item.data_vencimento }
? new Date(item.data_vencimento).toLocaleDateString("pt-BR") };
: "-",
"DTCAIXA": item.data_caixa const formatCurrencyValue = (value: any, showZero: boolean = false): string | number => {
? new Date(item.data_caixa).toLocaleDateString("pt-BR") if (value === null || value === undefined || value === "") return "-";
: "-", const numValue = typeof value === "string" ? parseFloat(value) : Number(value);
"DTPAGTO": item.data_pagto if (isNaN(numValue)) return "-";
? new Date(item.data_pagto).toLocaleDateString("pt-BR") if (!showZero && numValue === 0) return "-";
: "-", // Para Excel, retornar o número formatado como string (mantém o formato de moeda)
"ENTIDADE": item.entidade || "-", return new Intl.NumberFormat("pt-BR", {
"TIPOPARCEIRO": item.tipo_parceiro || "-", style: "currency",
"CODFORNEC": item.codigo_fornecedor || "-", currency: "BRL",
"FORNECEDOR": item.nome_fornecedor || "-", }).format(numValue);
"CODIGOCENTROCUSTO": item.codigo_centrocusto || "-", };
"CENTROCUSTO": item.centro_custo || "-",
"CODCONTA": item.codigo_conta || "-", const formatCellValue = (column: GridColDef, item: any): any => {
"CONTA": item.conta || "-", const value = item[column.field];
"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), // Se a coluna tem renderCell, aplicar a mesma lógica
"VLCONFIRMADO": typeof item.valor_confirmado === "string" ? parseFloat(item.valor_confirmado) : (item.valor_confirmado || 0), if (column.renderCell) {
"VLPAGO": typeof item.valor_pago === "string" ? parseFloat(item.valor_pago) : (item.valor_pago || 0), // Para datas
"HISTORICO": item.historico || "-", if (column.field.includes("data_")) {
"HISTORICO2": item.historico2 || "-", return formatDateValue(value);
"NUMLANC": item.numero_lancamento || "-", }
}));
// 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<string, any> = {};
// 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 wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(exportData); const ws = XLSX.utils.json_to_sheet(exportData);

View File

@ -2292,7 +2292,7 @@ export default function Teste() {
<div className="flex items-center justify-between mb-1"> <div className="flex items-center justify-between mb-1">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<div> <div>
<h1 className="text-2xl font-bold text-gray-900">DRE Gerencial</h1> <h1 className="text-2xl font-bold text-gray-900">Despesa Entidade</h1>
<p className="text-sm text-gray-500"> <p className="text-sm text-gray-500">
Demonstração do Resultado do Exercício Demonstração do Resultado do Exercício
</p> </p>