import { forecastBusinessPlan, type BusinessFinancialPlanInput, } from "./business-financial-plan.ts"; /** * Fiscal scenario for the investor's business plan: the yearly IRPC is the * documented rate applied to each year's profit before tax. It guides the * viability reading; it is not a tax assessment. */ export const irpcPlanScenarios = { general: { label: "Regime geral do IRPC — taxa de 32%", rate: 32, source: "Cenário fiscal: IRPC à taxa geral de 32% (Código do IRPC, artigo 61.º, n.º 1) sobre o lucro antes de impostos de cada ano.", }, } as const; export type IrpcPlanScenario = keyof typeof irpcPlanScenarios; const SIMPLIFICATIONS = " Estimativa para avaliar a viabilidade, não é liquidação: o imposto de cada ano é registado no último mês desse ano; um ano com prejuízo tem imposto zero; não inclui reporte de prejuízos, benefícios fiscais, pagamentos por conta nem o calendário de pagamento da Autoridade Tributária."; function cents(value: string): bigint { const match = /^(-?)(\d+)\.(\d{2})$/.exec(value); if (!match) throw new Error("Resultado mensal com formato inesperado."); const amount = BigInt(match[2]) * 100n + BigInt(match[3]); return match[1] ? -amount : amount; } function money(value: bigint): string { return `${value / 100n}.${String(value % 100n).padStart(2, "0")}`; } /** Yearly tax on 12-month blocks, rounded half-up to the cent. */ export function irpcScenarioSchedule( plan: BusinessFinancialPlanInput, scenario: IrpcPlanScenario, ): { expense: string[]; payment: string[]; source: string } { const { rate, source } = irpcPlanScenarios[scenario]; const rows = forecastBusinessPlan(plan).rows; const expense = rows.map(() => "0.00"); for (let start = 0; start < rows.length; start += 12) { const block = rows.slice(start, start + 12); const profit = block.reduce( (total, row) => total + cents(String(row.profitBeforeTax)), 0n, ); if (profit <= 0n) continue; const tax = (profit * BigInt(rate) * 2n + 100n) / 200n; expense[start + block.length - 1] = money(tax); } return { expense, payment: [...expense], source: source + SIMPLIFICATIONS, }; }