Integrate monthly IVA with projected sales in QA

This commit is contained in:
2026-10-21 14:05:46 +02:00
parent 0d7b70459d
commit 3f5430c3ce
18 changed files with 690 additions and 12 deletions
@@ -776,6 +776,23 @@ function multiplyQuantity(
return (quantityMilli * unitPriceCents + 500n) / 1000n;
}
/** Reuses the sales model, without requiring unrelated cost/cash assumptions. Blank is never zero. */
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.');
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}`));
}
return format(total);
});
}
function format(centsValue: bigint): string {
const absolute = centsValue < 0n ? -centsValue : centsValue;
return `${centsValue < 0n ? '-' : ''}${absolute / 100n}.${String(absolute % 100n).padStart(2, '0')}`;