fix: complete InvesteMoz validation and error corrections

This commit is contained in:
2026-10-29 21:47:33 +02:00
parent 63147312e2
commit daf8623441
718 changed files with 127308 additions and 20959 deletions
+54 -14
View File
@@ -777,33 +777,73 @@ function multiplyQuantity(
}
/** Reuses the sales model, without requiring unrelated cost/cash assumptions. Blank is never zero. */
export function monthlyProjectedSales(input: BusinessFinancialPlanInput): Array<string | null> {
export function monthlyProjectedSales(
input: BusinessFinancialPlanInput,
): Array<string | null> {
const periods = input.periods ?? 12;
if (!Number.isInteger(periods) || periods < 1 || periods > 60) throw new Error('Horizonte de vendas inválido.');
if (!Number.isInteger(periods) || periods < 1 || periods > 60)
throw new Error('Horizonte de vendas inválido.');
return Array.from({ length: periods }, (_, month) => {
if (!input.products.length) return null;
let total = 0n;
for (const product of input.products) {
const units = product.monthlyUnitsByPeriod === undefined ? product.monthlyUnits : product.monthlyUnitsByPeriod[month];
const price = product.unitPriceByPeriod === undefined ? product.unitPrice : product.unitPriceByPeriod[month];
if (typeof units !== 'string' || typeof price !== 'string' || units.trim() === '' || price.trim() === '') return null;
total += multiplyQuantity(quantity(units), cents(price, `Preço de ${product.label}`));
const units =
product.monthlyUnitsByPeriod === undefined
? product.monthlyUnits
: product.monthlyUnitsByPeriod[month];
const price =
product.unitPriceByPeriod === undefined
? product.unitPrice
: product.unitPriceByPeriod[month];
if (
typeof units !== 'string' ||
typeof price !== 'string' ||
units.trim() === '' ||
price.trim() === ''
)
return null;
total += multiplyQuantity(
quantity(units),
cents(price, `Preço de ${product.label}`),
);
}
return format(total);
});
}
/** Existing purchase orders are planning references, never a proxy for deductible VAT or invoice dates. */
export function monthlyProjectedPurchaseOrders(input: BusinessFinancialPlanInput): Array<string | null> {
export function monthlyProjectedPurchaseOrders(
input: BusinessFinancialPlanInput,
): Array<string | null> {
const periods = input.periods ?? 12;
if (!Number.isInteger(periods) || periods < 1 || periods > 60) throw new Error('Horizonte de compras inválido.');
return Array.from({length: periods}, (_, month) => {
if (!Number.isInteger(periods) || periods < 1 || periods > 60)
throw new Error('Horizonte de compras inválido.');
return Array.from({ length: periods }, (_, month) => {
const amounts = input.useProductInventorySchedule
? input.products.map((product) => product.productInventoryPurchasesByPeriod?.[month]?.amount)
: [input.useInventoryPurchaseSchedule
? input.inventoryPurchaseSchedule?.[month]?.amount : input.monthlyInventoryPurchases];
if (!amounts.length || amounts.some((amount) => typeof amount !== 'string' || amount.trim() === '')) return null;
return format(amounts.reduce((total, amount) => total + cents(amount!, 'Compras projectadas'), 0n));
? input.products.map(
(product) =>
product.productInventoryPurchasesByPeriod?.[month]
?.amount,
)
: [
input.useInventoryPurchaseSchedule
? input.inventoryPurchaseSchedule?.[month]?.amount
: input.monthlyInventoryPurchases,
];
if (
!amounts.length ||
amounts.some(
(amount) => typeof amount !== 'string' || amount.trim() === '',
)
)
return null;
return format(
amounts.reduce(
(total, amount) =>
total + cents(amount!, 'Compras projectadas'),
0n,
),
);
});
}