fix: reconcile annual example ledger and illustrative months in QA

This commit is contained in:
2026-11-20 11:01:28 +02:00
parent e27b354192
commit 65a52aa3c9
10 changed files with 935 additions and 7 deletions
+181 -1
View File
@@ -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