fix: target literal financial diagnostics and document V09 QA coverage

This commit is contained in:
2026-11-21 21:40:56 +02:00
parent b20feeb72e
commit 77f621b48d
34 changed files with 924 additions and 8 deletions
+75 -2
View File
@@ -82,10 +82,82 @@ export function financialPlanWarningMetrics(
});
}
/** Presentation of already-calculated monthly values; never produces or changes amounts. */
function periodGuidance(
plan: BusinessFinancialPlanInput,
tables: InvestmentReportTable[],
) {
if (plan.reportSource === 'annual_example') return [];
const salesTable = tables.find((t) => t.id === 'sales');
const costsTable = tables.find((t) => t.id === 'profit-loss');
const read = (
table: InvestmentReportTable | undefined,
label: string,
col: number,
) => {
if (!table || table.issues.length) return undefined;
const value = table?.rows.find((row) => row[0] === label)?.[col];
return typeof value === 'string' && /^-?\d+\.\d{2}$/.test(value)
? BigInt(value.replace('.', ''))
: undefined;
};
const noContribution: string[] = [],
missingSales: string[] = [];
for (let col = 1; col <= (plan.periods ?? 12); col++) {
const sales = read(salesTable, 'Vendas totais', col),
cost = read(costsTable, 'Custo directo', col);
const period = (salesTable?.headers[col] ?? `Mês ${col}`).replace(
/^(\d{4})-(\d{2})$/,
'$2/$1',
);
if (
sales !== undefined &&
sales > 0n &&
cost !== undefined &&
cost >= sales
)
noContribution.push(period);
if (
sales === 0n &&
[
'Custo directo',
'Despesas operacionais',
'Pessoal',
'Depreciação',
'Juros',
'Imposto estimado',
].some((label) => {
const value = read(costsTable, label, col);
return value !== undefined && value > 0n;
})
)
missingSales.push(period);
}
return [
...(noContribution.length
? [
{
id: 'period-no-contribution',
message: `Com este custo variável não há margem para cobrir os custos fixos. Reveja o custo variável ou o preço. Períodos a rever: ${noContribution.join(', ')}.`,
},
]
: []),
...(missingSales.length
? [
{
id: 'period-sales-required',
message: `Indique as vendas previstas para calcular a margem e o ponto de equilíbrio. Períodos a rever: ${missingSales.join(', ')}.`,
},
]
: []),
];
}
export function financialPlanWarnings(
plan: BusinessFinancialPlanInput,
tables: InvestmentReportTable[],
) {
const periodWarnings = periodGuidance(plan, tables);
const annualWarnings = financialPlanWarningMetrics(plan, tables).flatMap(
({ year, metrics, count }) => {
// The warning helper compares a full year's sales to a monthly threshold.
@@ -121,7 +193,8 @@ export function financialPlanWarnings(
);
const cashflow = tables.find((table) => table.id === 'cashflow');
const closing = cashflow?.rows.find((row) => row[0] === 'Caixa final');
if (!cashflow || cashflow.issues.length || !closing) return annualWarnings;
if (!cashflow || cashflow.issues.length || !closing)
return [...annualWarnings, ...periodWarnings];
const months = [
'Janeiro',
'Fevereiro',
@@ -154,7 +227,7 @@ export function financialPlanWarnings(
},
];
});
return [...annualWarnings, ...monthlyWarnings];
return [...annualWarnings, ...periodWarnings, ...monthlyWarnings];
}
function inputCreditDays(value: string | undefined) {