fix: retain editable investment plans and record partial QA continuity evidence

This commit is contained in:
2026-11-20 09:25:49 +02:00
parent bae996e10d
commit 04d8db6c1b
17 changed files with 1011 additions and 290 deletions
+43 -10
View File
@@ -13,6 +13,29 @@ type ProductLine = {
productInventoryPurchasesByPeriod?: InventoryPurchasePeriod[] | null;
};
export const MAX_BUSINESS_PLAN_SNAPSHOT_LENGTH = 60_000;
export function businessPlanNumberError(
value: string,
options: {
allowNegative?: boolean;
integer?: boolean;
percent?: boolean;
} = {},
): string {
if (value.trim() === '') return 'Preencha este campo para continuar.';
if (
!/^-?\d+(?:\.\d+)?$/.test(value.trim()) ||
!Number.isFinite(Number(value))
)
return 'Introduza apenas números (ex.: 25.000).';
if (!options.allowNegative && Number(value) < 0)
return 'Este valor não pode ser negativo.';
if (options.integer && !Number.isSafeInteger(Number(value)))
return 'Introduza um número inteiro de pessoas.';
if (options.percent && (Number(value) < 0 || Number(value) > 100))
return 'Introduza uma percentagem entre 0% e 100%.';
return '';
}
export type ScenarioAdjustment = {
unitVolumePercent: string;
unitPricePercent: string;
@@ -179,6 +202,7 @@ export function serializeBusinessPlanSnapshot(
export function restoreBusinessPlanSnapshot(
snapshot: string | undefined,
options: { editable?: boolean } = {},
): BusinessFinancialPlanInput | null {
if (!snapshot || snapshot.length > MAX_BUSINESS_PLAN_SNAPSHOT_LENGTH)
return null;
@@ -210,6 +234,15 @@ export function restoreBusinessPlanSnapshot(
)
return null;
const plan = decoded.plan as Record<string, unknown>;
// A blank percentage is an unfinished draft, not a zero assumption.
// Editable recovery also retains invalid percentages for correction.
// The zero below is only an upper-bound validation accumulator, never
// a replacement in the returned plan. Calculation remains strict.
const snapshotPercentage = (value: string, label: string): bigint =>
options.editable || value.trim() === ''
? 0n
: percentMicro(value, label);
const textFields = [
'currency',
'openingCash',
@@ -336,7 +369,7 @@ export function restoreBusinessPlanSnapshot(
)
return null;
const collectionPercent = String(plan.salesCollectionPercent ?? '100');
const collectionRate = percentMicro(
const collectionRate = snapshotPercentage(
collectionPercent,
'Percentagem de cobrança',
);
@@ -360,7 +393,7 @@ export function restoreBusinessPlanSnapshot(
!Object.hasOwn(plan, 'openingReceivablesCollectionPercent'))
)
return null;
const openingCollectionRate = percentMicro(
const openingCollectionRate = snapshotPercentage(
String(plan.openingReceivablesCollectionPercent ?? '100'),
'Percentagem de cobrança dos valores a receber de abertura',
);
@@ -391,7 +424,7 @@ export function restoreBusinessPlanSnapshot(
typeof entry.percent !== 'string'
)
return null;
const rate = percentMicro(
const rate = snapshotPercentage(
entry.percent,
'Percentagem da parcela de recebíveis',
);
@@ -419,11 +452,11 @@ export function restoreBusinessPlanSnapshot(
entry.month < 1 ||
entry.month > 60 ||
typeof entry.percent !== 'string' ||
percentMicro(
snapshotPercentage(
entry.percent,
'Percentagem da liquidação de valores a pagar',
) < 0n ||
percentMicro(
snapshotPercentage(
entry.percent,
'Percentagem da liquidação de valores a pagar',
) > 100_000_000n,
@@ -431,7 +464,7 @@ export function restoreBusinessPlanSnapshot(
plan.openingPayablesInstallments.reduce(
(sum, entry) =>
sum +
percentMicro(
snapshotPercentage(
entry.percent,
'Percentagem da liquidação de valores a pagar',
),
@@ -462,7 +495,7 @@ export function restoreBusinessPlanSnapshot(
)
return null;
months.add(entry.month);
const rate = percentMicro(
const rate = snapshotPercentage(
entry.percent,
'Percentagem de recuperação de vendas',
);
@@ -634,11 +667,11 @@ export function restoreBusinessPlanSnapshot(
part.lagMonths > 12 ||
typeof part.percent !==
'string' ||
percentMicro(
snapshotPercentage(
part.percent,
'Percentagem da parcela',
) < 0n ||
percentMicro(
snapshotPercentage(
part.percent,
'Percentagem da parcela',
) > 100_000_000n,
@@ -646,7 +679,7 @@ export function restoreBusinessPlanSnapshot(
entry.paymentInstallments.reduce(
(sum: bigint, part: any) =>
sum +
percentMicro(
snapshotPercentage(
part.percent,
'Percentagem da parcela',
),