diff --git a/src/app/dre-filial/teste.tsx b/src/app/dre-filial/teste.tsx index 5632a3d..d7b79ff 100644 --- a/src/app/dre-filial/teste.tsx +++ b/src/app/dre-filial/teste.tsx @@ -40,7 +40,7 @@ interface DREItem { } interface HierarchicalRow { - type: "grupo" | "conta"; + type: "grupo" | "conta" | "calculado"; level: number; grupo?: string; codigo_grupo?: string; @@ -51,6 +51,7 @@ interface HierarchicalRow { valoresPorMes?: Record; percentuaisPorMes?: Record; percentualTotal?: number; + isCalculado?: boolean; } // Componente memoizado para linhas da tabela @@ -344,6 +345,12 @@ export default function Teste() { console.log('🖱️ Clique na linha:', row); console.log('📅 Mês selecionado:', mesSelecionado); + // Linhas calculadas não devem abrir o componente analítico + if (row.type === "calculado") { + console.log('⚠️ Linha calculada - não abre componente analítico'); + return; + } + if (!data.length) { console.log('⚠️ Sem dados disponíveis'); return; @@ -469,26 +476,39 @@ export default function Teste() { return valoresPorMes; }; - // Função para calcular percentuais (simplificada - sem faturamento líquido) + // Função para calcular percentuais baseado no CODGRUPO 01 (FATURAMENTO LÍQUIDO) const calcularPercentuaisPorMes = ( - valoresPorMes: Record + valoresPorMes: Record, + codigoGrupo?: string ): Record => { const percentuais: Record = {}; - // Calcular total geral por mês - const totaisPorMes: Record = {}; + // Se for CODGRUPO 01, sempre retornar 100% + if (codigoGrupo === "01") { + mesesDisponiveis.forEach(mes => { + percentuais[mes] = 100; + }); + return percentuais; + } + + // Calcular valores do grupo 01 por mês (base para cálculo) + const valoresGrupo01PorMes: Record = {}; mesesDisponiveis.forEach(mes => { - totaisPorMes[mes] = data - .filter(item => item.data_competencia === mes) + valoresGrupo01PorMes[mes] = data + .filter(item => { + const codgrupo = item.codgrupo || item.codigo_grupo || ""; + return codgrupo === "01" && item.data_competencia === mes; + }) .reduce((sum, item) => sum + parseFloat(item.valor), 0); }); + // Calcular percentuais baseado no grupo 01 Object.keys(valoresPorMes).forEach((mes) => { const valorAtual = valoresPorMes[mes]; - const totalMes = totaisPorMes[mes] || 0; + const valorGrupo01 = valoresGrupo01PorMes[mes] || 0; - if (totalMes !== 0) { - percentuais[mes] = (valorAtual / totalMes) * 100; + if (valorGrupo01 !== 0) { + percentuais[mes] = (valorAtual / valorGrupo01) * 100; } else { percentuais[mes] = 0; } @@ -497,12 +517,23 @@ export default function Teste() { return percentuais; }; - // Função para calcular percentual do total - const calcularPercentualTotal = (total: number): number => { - const totalGeral = data.reduce((sum, item) => sum + parseFloat(item.valor), 0); + // Função para calcular percentual do total baseado no CODGRUPO 01 + const calcularPercentualTotal = (total: number, codigoGrupo?: string): number => { + // Se for CODGRUPO 01, sempre retornar 100% + if (codigoGrupo === "01") { + return 100; + } - if (totalGeral !== 0) { - return (total / totalGeral) * 100; + // Calcular total do grupo 01 (base para cálculo) + const totalGrupo01 = data + .filter(item => { + const codgrupo = item.codgrupo || item.codigo_grupo || ""; + return codgrupo === "01"; + }) + .reduce((sum, item) => sum + parseFloat(item.valor), 0); + + if (totalGrupo01 !== 0) { + return (total / totalGrupo01) * 100; } else { return 0; } @@ -523,6 +554,12 @@ export default function Teste() { return acc; }, {} as Record); + // Calcular valores por grupo para linhas calculadas + const valoresPorGrupo: Record> = {}; + Object.keys(gruposPorCodigo).forEach(codgrupo => { + valoresPorGrupo[codgrupo] = calcularValoresPorMes(gruposPorCodigo[codgrupo]); + }); + // Ordenar por CODGRUPO (numericamente) const sortedGrupos = Object.entries(gruposPorCodigo).sort(([codA], [codB]) => { const numA = parseInt(codA) || 0; @@ -533,7 +570,7 @@ export default function Teste() { return codA.localeCompare(codB); }); - sortedGrupos.forEach(([codgrupo, items]) => { + sortedGrupos.forEach(([codgrupo, items], index) => { // Calcular total do grupo const totalGrupo = items.reduce( (sum, item) => sum + parseFloat(item.valor), @@ -550,8 +587,8 @@ export default function Teste() { total: totalGrupo, isExpanded: expandedGrupos.has(codgrupo), valoresPorMes: valoresGrupoPorMes, - percentuaisPorMes: calcularPercentuaisPorMes(valoresGrupoPorMes), - percentualTotal: calcularPercentualTotal(totalGrupo), + percentuaisPorMes: calcularPercentuaisPorMes(valoresGrupoPorMes, codgrupo), + percentualTotal: calcularPercentualTotal(totalGrupo, codgrupo), }); if (expandedGrupos.has(codgrupo)) { @@ -596,11 +633,47 @@ export default function Teste() { total: totalConta, isExpanded: false, valoresPorMes: valoresContaPorMes, - percentuaisPorMes: calcularPercentuaisPorMes(valoresContaPorMes), - percentualTotal: calcularPercentualTotal(totalConta), + percentuaisPorMes: calcularPercentuaisPorMes(valoresContaPorMes, codgrupo), + percentualTotal: calcularPercentualTotal(totalConta, codgrupo), }); }); } + + // Adicionar linha calculada "MARGEM DE LOJA" após o grupo 02 + // Verificar se é o último grupo ou se o próximo grupo é maior que 02 + const proximoCodigo = sortedGrupos[index + 1]?.[0]; + const proximoNumero = proximoCodigo ? parseInt(proximoCodigo) : 999; + + if (codgrupo === "02" || (parseInt(codgrupo) === 2 && proximoNumero > 2)) { + // Calcular MARGEM DE LOJA = CODGRUPO 01 - CODGRUPO 02 + const valoresGrupo01 = valoresPorGrupo["01"] || {}; + const valoresGrupo02 = valoresPorGrupo["02"] || {}; + + // Calcular valores por mês para MARGEM DE LOJA + const valoresMargemPorMes: Record = {}; + mesesDisponiveis.forEach(mes => { + const valor01 = valoresGrupo01[mes] || 0; + const valor02 = valoresGrupo02[mes] || 0; + valoresMargemPorMes[mes] = valor01 - valor02; + }); + + // Calcular total + const totalMargem = Object.values(valoresMargemPorMes).reduce((sum, val) => sum + val, 0); + + // Adicionar linha calculada + rows.push({ + type: "calculado", + level: 0, + grupo: "MARGEM DE LOJA", + codigo_grupo: "MARGEM", + total: totalMargem, + isExpanded: false, + valoresPorMes: valoresMargemPorMes, + percentuaisPorMes: calcularPercentuaisPorMes(valoresMargemPorMes, "MARGEM"), + percentualTotal: calcularPercentualTotal(totalMargem, "MARGEM"), + isCalculado: true, + }); + } }); return rows; @@ -623,6 +696,8 @@ export default function Teste() { switch (row.type) { case "grupo": return `${style} bg-gradient-to-r from-blue-50/20 to-indigo-50/20 font-bold text-gray-900 border-b-2 border-blue-200`; + case "calculado": + return `${style} bg-gradient-to-r from-purple-50/30 to-pink-50/30 font-bold text-purple-900 border-b-2 border-purple-300 italic`; case "conta": return `${style} bg-white font-normal text-gray-600`; default: @@ -641,6 +716,8 @@ export default function Teste() { switch (row.type) { case "grupo": return "bg-gradient-to-r from-blue-50 to-indigo-50"; + case "calculado": + return "bg-gradient-to-r from-purple-50 to-pink-50"; case "conta": return "bg-white"; default: @@ -684,6 +761,22 @@ export default function Teste() { ); + case "calculado": + return ( +
+
+ {/* = */} + +
+
+
+ + {row.grupo} + +
+
+
+ ); case "conta": return (