feat(qa): separate startup resources and trace original example rows
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import type { BusinessFinancialPlanInput } from './business-financial-plan.ts';
|
||||
|
||||
const money = (value: string | undefined, label: string) => {
|
||||
if (!value || !/^\d+(?:\.\d{1,2})?$/.test(value))
|
||||
throw Error(
|
||||
`Indique ${label}, com até duas casas decimais; use zero apenas quando conhecido.`,
|
||||
);
|
||||
const [a, b = ''] = value.split('.');
|
||||
return BigInt(a) * 100n + BigInt(b.padEnd(2, '0'));
|
||||
};
|
||||
const positive = (n: bigint) => (n > 0n ? n : 0n);
|
||||
export const startupAmount = (n: bigint) =>
|
||||
`${n / 100n}.${String(n % 100n).padStart(2, '0')}`;
|
||||
|
||||
/** Requirements are targets; the existing cash equation still uses available opening balances and declared financing. */
|
||||
export function calculateStartupAssetRequirements(
|
||||
plan: BusinessFinancialPlanInput,
|
||||
) {
|
||||
const target = plan.startupAssetRequirements;
|
||||
if (!target)
|
||||
throw Error('Indique as necessidades de caixa e activos do arranque.');
|
||||
const cashRequired = money(target.cashRequired, 'a caixa requerida');
|
||||
const currentRequired = money(
|
||||
target.otherCurrentAssetsRequired,
|
||||
'os activos correntes requeridos',
|
||||
);
|
||||
const fixedRequired = money(
|
||||
target.longTermAssetsRequired,
|
||||
'os activos de longo prazo requeridos',
|
||||
);
|
||||
const cashAvailable = money(
|
||||
plan.openingCash,
|
||||
'a caixa disponível antes do financiamento',
|
||||
);
|
||||
const currentAvailable = money(
|
||||
plan.reportAssumptions?.openingOtherAssets,
|
||||
'os outros activos correntes disponíveis',
|
||||
);
|
||||
const fixedAvailable = money(
|
||||
plan.reportAssumptions?.openingFixedAssets,
|
||||
'os activos de longo prazo disponíveis',
|
||||
);
|
||||
// Inventory and receivables are already initial uses in the approved engine, not acquisitions to count twice.
|
||||
const currentAlreadyPlanned =
|
||||
money(plan.inventory, 'as existências iniciais planeadas') +
|
||||
money(plan.receivables, 'os recebíveis iniciais planeados');
|
||||
const currentAcquisition = positive(
|
||||
currentRequired - currentAvailable - currentAlreadyPlanned,
|
||||
);
|
||||
const fixedAcquisition = positive(fixedRequired - fixedAvailable);
|
||||
const startupExpenses = (
|
||||
plan.reportAssumptions?.startupExpenses ?? []
|
||||
).reduce((sum, r) => sum + money(r.amount, r.label), 0n);
|
||||
return {
|
||||
cashRequired,
|
||||
currentRequired,
|
||||
fixedRequired,
|
||||
cashAvailable,
|
||||
currentAvailable,
|
||||
fixedAvailable,
|
||||
currentAlreadyPlanned,
|
||||
currentAcquisition,
|
||||
fixedAcquisition,
|
||||
assetInvestment: currentAcquisition + fixedAcquisition,
|
||||
totalRequiredAssets: cashRequired + currentRequired + fixedRequired,
|
||||
grossRequirements:
|
||||
startupExpenses + cashRequired + currentRequired + fixedRequired,
|
||||
startupExpenses,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user