fix: reconcile annual example ledger and illustrative months in QA
This commit is contained in:
@@ -17,6 +17,8 @@ const annualLabels = {
|
||||
taxes: 'Impostos estimados do exemplo',
|
||||
cashReceived: 'Caixa recebida',
|
||||
cashSpent: 'Caixa gasta',
|
||||
vatReceived: 'IVA incluído nos recebimentos (montante do exemplo)',
|
||||
vatPaid: 'IVA incluído nos pagamentos (montante do exemplo)',
|
||||
} as const;
|
||||
const openingLabels = {
|
||||
openingCash: 'Caixa necessária',
|
||||
@@ -95,8 +97,9 @@ export function SimulationAnnualExample({ currency }: { currency: string }) {
|
||||
<p className="rounded border border-amber-400 p-3 text-sm">
|
||||
Linhas mensais por reconciliar. Conservam-se os totais anuais
|
||||
definidos pelo proprietário; não se distribuem valores pelos
|
||||
meses nem se completam linhas em falta. Os impostos são
|
||||
montantes do exemplo, sem activar taxas fiscais.
|
||||
meses da fonte nem se completam as suas linhas em falta. A
|
||||
projecção mensal ilustrativa abaixo é um cálculo separado. Os
|
||||
impostos são montantes do exemplo, sem activar taxas fiscais.
|
||||
</p>
|
||||
<details open>
|
||||
<summary className="font-semibold">
|
||||
@@ -268,6 +271,81 @@ export function SimulationAnnualExample({ currency }: { currency: string }) {
|
||||
</div>
|
||||
{result.years.map((year, index) => (
|
||||
<div key={year.year}>
|
||||
<p>
|
||||
Balanço projectado: activos{' '}
|
||||
{amount(year.closingAssets)}, passivos{' '}
|
||||
{amount(year.closingLiabilities)} e capital{' '}
|
||||
{amount(year.closingCapital)}. Diferença:{' '}
|
||||
{amount(year.balanceDifference)} {currency}.
|
||||
Valores a receber: {amount(year.receivables)};
|
||||
obrigações operacionais:{' '}
|
||||
{amount(year.operatingLiabilities)}.
|
||||
</p>
|
||||
<details>
|
||||
<summary>
|
||||
Distribuição mensal ilustrativa —{' '}
|
||||
{year.year}
|
||||
</summary>
|
||||
<p>
|
||||
Esta simulação distribui os totais anuais
|
||||
pelo perfil sazonal agregado do exemplo.
|
||||
Despesas fixas são repartidas por igual;
|
||||
recebimentos e pagamentos sem IVA seguem
|
||||
vendas e despesas, respectivamente. Os
|
||||
centavos remanescentes são distribuídos
|
||||
pelas maiores fracções. Não representa
|
||||
recuperação dos meses da referência.
|
||||
</p>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left text-sm">
|
||||
<caption>
|
||||
Projecção mensal ({currency})
|
||||
</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
{[
|
||||
'Mês',
|
||||
'Vendas',
|
||||
'Custos directos',
|
||||
'Lucro líquido',
|
||||
'Recebimentos',
|
||||
'Pagamentos',
|
||||
'Caixa',
|
||||
'Diferença do balanço',
|
||||
].map((label) => (
|
||||
<th key={label}>{label}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{year.illustrativeMonths.map(
|
||||
(month) => (
|
||||
<tr key={month.month}>
|
||||
<th>{month.month}</th>
|
||||
{(
|
||||
[
|
||||
'sales',
|
||||
'directCosts',
|
||||
'netProfit',
|
||||
'cashReceived',
|
||||
'cashSpent',
|
||||
'closingCash',
|
||||
'balanceDifference',
|
||||
] as const
|
||||
).map((key) => (
|
||||
<td key={key}>
|
||||
{amount(
|
||||
month[key],
|
||||
)}
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
),
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</details>
|
||||
<h5 className="font-semibold">
|
||||
Avisos — {year.year}
|
||||
</h5>
|
||||
@@ -296,6 +374,17 @@ export function SimulationAnnualExample({ currency }: { currency: string }) {
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
<p className="text-sm">
|
||||
O balanço pressupõe mobilizar{' '}
|
||||
{amount(result.capitalToMobilise)} {currency} de capital
|
||||
ainda em falta. Não é financiamento confirmado. Os
|
||||
saldos iniciais a receber e de obrigações operacionais
|
||||
são zero neste cenário; activos e empréstimos mantêm-se
|
||||
constantes. As obrigações incluem custos, despesas,
|
||||
juros e impostos reconhecidos e ainda não pagos. Os
|
||||
montantes de IVA são dados do exemplo, sem inferência de
|
||||
taxas fiscais.
|
||||
</p>
|
||||
<p className="text-sm">
|
||||
Margem bruta = vendas − custo directo. Lucro líquido =
|
||||
margem bruta − despesas operacionais − juros − impostos.
|
||||
|
||||
@@ -131,6 +131,8 @@ export type AnnualReferencePlan = {
|
||||
taxes: string;
|
||||
cashReceived: string;
|
||||
cashSpent: string;
|
||||
vatReceived: string;
|
||||
vatPaid: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
@@ -164,6 +166,8 @@ export function newAnnualReferencePlan(): AnnualReferencePlan {
|
||||
taxes: '0',
|
||||
cashReceived: '16539',
|
||||
cashSpent: '15783',
|
||||
vatReceived: '2471',
|
||||
vatPaid: '2471',
|
||||
},
|
||||
{
|
||||
year: 2026,
|
||||
@@ -176,6 +180,8 @@ export function newAnnualReferencePlan(): AnnualReferencePlan {
|
||||
taxes: '624',
|
||||
cashReceived: '24930',
|
||||
cashSpent: '23923',
|
||||
vatReceived: '3655',
|
||||
vatPaid: '3655',
|
||||
},
|
||||
{
|
||||
year: 2027,
|
||||
@@ -188,6 +194,8 @@ export function newAnnualReferencePlan(): AnnualReferencePlan {
|
||||
taxes: '699',
|
||||
cashReceived: '27857',
|
||||
cashSpent: '26122',
|
||||
vatReceived: '4059',
|
||||
vatPaid: '4059',
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -296,6 +304,31 @@ export function calculateDecisionRatios(
|
||||
};
|
||||
}
|
||||
|
||||
/** Illustrative allocation, in cents; never a reconstruction of historical months. */
|
||||
function allocateMonths(total: bigint, weights: bigint[]) {
|
||||
const denominator = weights.reduce((sum, value) => sum + value, 0n);
|
||||
const effective = denominator === 0n ? weights.map(() => 1n) : weights;
|
||||
const divisor = denominator === 0n ? BigInt(weights.length) : denominator;
|
||||
const rows = effective.map((weight, index) => ({
|
||||
index,
|
||||
amount: (total * weight) / divisor,
|
||||
remainder: (total * weight) % divisor,
|
||||
}));
|
||||
let remaining = total - rows.reduce((sum, row) => sum + row.amount, 0n);
|
||||
for (const row of [...rows].sort((a, b) =>
|
||||
a.remainder === b.remainder
|
||||
? a.index - b.index
|
||||
: a.remainder > b.remainder
|
||||
? -1
|
||||
: 1,
|
||||
)) {
|
||||
if (remaining === 0n) break;
|
||||
row.amount += 1n;
|
||||
remaining -= 1n;
|
||||
}
|
||||
return rows.map((row) => row.amount);
|
||||
}
|
||||
|
||||
export function calculateAnnualReferencePlan(plan: AnnualReferencePlan) {
|
||||
const startupExpenses = plan.startupExpenses.reduce(
|
||||
(sum, row) => sum + money(row.amount),
|
||||
@@ -312,7 +345,19 @@ export function calculateAnnualReferencePlan(plan: AnnualReferencePlan) {
|
||||
money(plan.investorCapital) +
|
||||
money(plan.loans) +
|
||||
money(plan.additionalMoney);
|
||||
let cash = openingCash;
|
||||
// A projection of the required capital is not confirmation of financing.
|
||||
const capitalToMobilise =
|
||||
requiredFunding > availableFunding
|
||||
? requiredFunding - availableFunding
|
||||
: 0n;
|
||||
const projectedCapital =
|
||||
availableFunding + capitalToMobilise - money(plan.loans);
|
||||
let cash =
|
||||
openingCash + availableFunding + capitalToMobilise - requiredFunding;
|
||||
let receivables = 0n;
|
||||
let operatingLiabilities = 0n;
|
||||
let vatBalance = 0n;
|
||||
let accumulatedResults = -startupExpenses;
|
||||
const years = plan.years.map((year, index) => {
|
||||
const sales = money(year.sales),
|
||||
costs = money(year.directCosts);
|
||||
@@ -333,7 +378,131 @@ export function calculateAnnualReferencePlan(plan: AnnualReferencePlan) {
|
||||
const netProfit =
|
||||
operatingProfit - money(year.interest) - money(year.taxes);
|
||||
const cashFlow = money(year.cashReceived) - money(year.cashSpent);
|
||||
const openingYearCash = cash;
|
||||
const openingReceivables = receivables;
|
||||
const openingLiabilities = operatingLiabilities;
|
||||
const openingResults = accumulatedResults;
|
||||
const openingVatBalance = vatBalance;
|
||||
const vatReceived = money(year.vatReceived);
|
||||
const vatPaid = money(year.vatPaid);
|
||||
if (
|
||||
vatReceived > money(year.cashReceived) ||
|
||||
vatPaid > money(year.cashSpent)
|
||||
)
|
||||
throw new Error(
|
||||
'O IVA de caixa não pode exceder os respectivos recebimentos ou pagamentos.',
|
||||
);
|
||||
receivables += sales - (money(year.cashReceived) - vatReceived);
|
||||
operatingLiabilities +=
|
||||
costs +
|
||||
operatingExpenses +
|
||||
money(year.interest) +
|
||||
money(year.taxes) -
|
||||
(money(year.cashSpent) - vatPaid);
|
||||
accumulatedResults += netProfit;
|
||||
vatBalance += vatReceived - vatPaid;
|
||||
cash += cashFlow;
|
||||
const closingAssets =
|
||||
cash +
|
||||
receivables +
|
||||
money(plan.otherCurrentAssets) +
|
||||
money(plan.longTermAssets);
|
||||
const closingCapital = projectedCapital + accumulatedResults;
|
||||
const closingLiabilities =
|
||||
operatingLiabilities + money(plan.loans) + vatBalance;
|
||||
// Explicit model assumptions: repeat the example's aggregate seasonal
|
||||
// profile; allocate cash net of VAT proportionally to accrued amounts.
|
||||
const salesWeights = [
|
||||
680n,
|
||||
748n,
|
||||
824n,
|
||||
905n,
|
||||
990n,
|
||||
1095n,
|
||||
1203n,
|
||||
1324n,
|
||||
1458n,
|
||||
1604n,
|
||||
1765n,
|
||||
1941n,
|
||||
];
|
||||
const costWeights = [
|
||||
586n,
|
||||
644n,
|
||||
710n,
|
||||
781n,
|
||||
860n,
|
||||
945n,
|
||||
1038n,
|
||||
1142n,
|
||||
1257n,
|
||||
1383n,
|
||||
1520n,
|
||||
1672n,
|
||||
];
|
||||
const monthlySales = allocateMonths(sales, salesWeights);
|
||||
const monthlyCosts = allocateMonths(costs, costWeights);
|
||||
const equalWeights = salesWeights.map(() => 1n);
|
||||
const monthlyExpenses = [
|
||||
year.payroll,
|
||||
year.payrollCharges,
|
||||
year.otherOperatingExpenses,
|
||||
year.interest,
|
||||
year.taxes,
|
||||
].map((value) => allocateMonths(money(value), equalWeights));
|
||||
const accrued = monthlyCosts.map(
|
||||
(value, i) =>
|
||||
value + monthlyExpenses.reduce((sum, row) => sum + row[i], 0n),
|
||||
);
|
||||
const receipts = allocateMonths(
|
||||
money(year.cashReceived) - vatReceived,
|
||||
monthlySales,
|
||||
);
|
||||
const payments = allocateMonths(
|
||||
money(year.cashSpent) - vatPaid,
|
||||
accrued,
|
||||
);
|
||||
const vatReceipts = allocateMonths(vatReceived, salesWeights);
|
||||
const vatPayments = allocateMonths(vatPaid, salesWeights);
|
||||
let monthCash = openingYearCash,
|
||||
monthReceivables = openingReceivables,
|
||||
monthLiabilities = openingLiabilities,
|
||||
monthResults = openingResults;
|
||||
let monthVatBalance = openingVatBalance;
|
||||
const illustrativeMonths = monthlySales.map((revenue, i) => {
|
||||
const profit = revenue - accrued[i];
|
||||
const received = receipts[i] + vatReceipts[i];
|
||||
const spent = payments[i] + vatPayments[i];
|
||||
monthCash += received - spent;
|
||||
monthReceivables += revenue - receipts[i];
|
||||
monthLiabilities += accrued[i] - payments[i];
|
||||
monthResults += profit;
|
||||
monthVatBalance += vatReceipts[i] - vatPayments[i];
|
||||
const monthAssets =
|
||||
monthCash +
|
||||
monthReceivables +
|
||||
money(plan.otherCurrentAssets) +
|
||||
money(plan.longTermAssets);
|
||||
return {
|
||||
month: i + 1,
|
||||
sales: decimal(revenue),
|
||||
directCosts: decimal(monthlyCosts[i]),
|
||||
netProfit: decimal(profit),
|
||||
cashReceived: decimal(received),
|
||||
cashSpent: decimal(spent),
|
||||
closingCash: decimal(monthCash),
|
||||
receivables: decimal(monthReceivables),
|
||||
operatingLiabilities: decimal(monthLiabilities),
|
||||
balanceDifference: decimal(
|
||||
monthAssets -
|
||||
monthLiabilities -
|
||||
monthVatBalance -
|
||||
money(plan.loans) -
|
||||
projectedCapital -
|
||||
monthResults,
|
||||
),
|
||||
};
|
||||
});
|
||||
// Keep the unrounded gross-margin fraction in the break-even formula.
|
||||
const monthlyBreakEven =
|
||||
grossMargin > 0n
|
||||
@@ -360,6 +529,16 @@ export function calculateAnnualReferencePlan(plan: AnnualReferencePlan) {
|
||||
sales > 0n ? (Number(netProfit) / Number(sales)) * 100 : null,
|
||||
cashFlow: decimal(cashFlow),
|
||||
closingCash: decimal(cash),
|
||||
receivables: decimal(receivables),
|
||||
operatingLiabilities: decimal(operatingLiabilities),
|
||||
closingAssets: decimal(closingAssets),
|
||||
closingCapital: decimal(closingCapital),
|
||||
closingLiabilities: decimal(closingLiabilities),
|
||||
vatBalance: decimal(vatBalance),
|
||||
balanceDifference: decimal(
|
||||
closingAssets - closingLiabilities - closingCapital,
|
||||
),
|
||||
illustrativeMonths,
|
||||
monthlyFixedCosts: Number(operatingExpenses) / 1200,
|
||||
monthlyBreakEven,
|
||||
growthPercent,
|
||||
@@ -370,6 +549,7 @@ export function calculateAnnualReferencePlan(plan: AnnualReferencePlan) {
|
||||
assets: decimal(assets),
|
||||
requiredFunding: decimal(requiredFunding),
|
||||
availableFunding: decimal(availableFunding),
|
||||
capitalToMobilise: decimal(capitalToMobilise),
|
||||
fundingGap: decimal(
|
||||
requiredFunding > availableFunding
|
||||
? requiredFunding - availableFunding
|
||||
|
||||
Reference in New Issue
Block a user