Plano de negócio: despesas de arranque por categoria com total e margem por produto ou serviço
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
As despesas de arranque passam a oferecer as categorias habituais de uma empresa nova (constituição e despesas legais, mobiliário, seguros, rendas, computadores, outras despesas de início de actividade), acrescentadas só quando o investidor o pede e sem alterar as linhas que já escreveu, e mostram o total, ou o que falta para o calcular. Cada produto ou serviço mostra a margem por unidade (preço menos custo variável, em valor e em percentagem do preço) e avisa quando o custo excede o preço. Os testes que carregam os editores passam a usar um resolvedor comum para o campo decimal e as bibliotecas de totais e margens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { DecimalInput } from '@/components/decimal-input';
|
||||
import { presentDecimal } from '@/lib/decimal-input';
|
||||
import {
|
||||
startupExpensesTotal,
|
||||
withStartupExpenseCategories,
|
||||
} from '@/lib/startup-asset-requirements';
|
||||
import type { BusinessFinancialPlanInput } from '@/lib/business-financial-plan';
|
||||
import {
|
||||
buildIntegralFinancialTables,
|
||||
@@ -212,6 +217,40 @@ export function IntegralFinancialPlan({
|
||||
>
|
||||
Adicionar despesa de arranque
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="ml-2 rounded border p-2"
|
||||
disabled={
|
||||
withStartupExpenseCategories(a.startupExpenses)
|
||||
.length === a.startupExpenses.length ||
|
||||
withStartupExpenseCategories(a.startupExpenses).length >
|
||||
40
|
||||
}
|
||||
onClick={() =>
|
||||
update({
|
||||
startupExpenses: withStartupExpenseCategories(
|
||||
a.startupExpenses,
|
||||
),
|
||||
})
|
||||
}
|
||||
>
|
||||
Acrescentar as categorias habituais
|
||||
</button>
|
||||
<p className="text-sm text-slate-600">
|
||||
Categorias habituais: constituição e despesas legais,
|
||||
mobiliário, seguros, rendas, computadores e outras despesas
|
||||
de início de actividade. Indique zero nas que não se
|
||||
aplicam.
|
||||
</p>
|
||||
{a.startupExpenses.length > 0 && (
|
||||
<p className="text-sm font-medium">
|
||||
{startupExpensesTotal(a.startupExpenses) !== null
|
||||
? `Total das despesas de arranque: ${presentDecimal(
|
||||
startupExpensesTotal(a.startupExpenses)!,
|
||||
)} ${plan.currency}`
|
||||
: 'Total das despesas de arranque por determinar: indique o montante de cada despesa (zero quando não se aplica).'}
|
||||
</p>
|
||||
)}
|
||||
</fieldset>
|
||||
<p className="my-2 text-sm">
|
||||
Os saldos anteriores têm de reconciliar entre si. Não inclua os
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { DecimalInput } from '@/components/decimal-input';
|
||||
import { presentDecimal } from '@/lib/decimal-input';
|
||||
import { productUnitMargin } from '@/lib/product-margin';
|
||||
import { WarningLimitsEditor } from './warning-limits-editor';
|
||||
import { SupplierAdvancesEditor } from '@/components/supplier-advances';
|
||||
import { PeriodInvestmentsEditor } from '@/components/period-investments';
|
||||
@@ -87,6 +89,30 @@ const emptyPlan: BusinessFinancialPlanInput = {
|
||||
periods: 12,
|
||||
};
|
||||
|
||||
/** Price less variable cost of one unit, so each service shows its own margin. */
|
||||
function ProductMarginNote({
|
||||
price,
|
||||
cost,
|
||||
currency,
|
||||
}: {
|
||||
price: string;
|
||||
cost: string;
|
||||
currency: string;
|
||||
}) {
|
||||
const margin = productUnitMargin(price, cost);
|
||||
if (!margin) return null;
|
||||
const negative = margin.amount.startsWith('-');
|
||||
return (
|
||||
<p
|
||||
className={`text-sm ${negative ? 'text-red-800' : 'text-slate-700'}`}
|
||||
>
|
||||
{negative
|
||||
? `O custo variável excede o preço: cada unidade vendida dá um prejuízo de ${presentDecimal(margin.amount.slice(1))} ${currency}. Reveja o preço ou o custo.`
|
||||
: `Margem por unidade: ${presentDecimal(margin.amount)} ${currency} (${margin.percent.replace('.', ',')}% do preço). Só deduz o custo variável; despesas operacionais e pessoal são cobertas pela soma das margens.`}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
function MoneyInput({
|
||||
label,
|
||||
value,
|
||||
@@ -1193,6 +1219,11 @@ export function SimulationBusinessPlan({
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<ProductMarginNote
|
||||
price={product.unitPrice}
|
||||
cost={product.unitVariableCost}
|
||||
currency={currency}
|
||||
/>
|
||||
{plan.useProductInventorySchedule && (
|
||||
<div className="space-y-2 rounded border p-3">
|
||||
<MoneyInput
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const cents = (value: string) => {
|
||||
if (!/^\d+(?:\.\d{1,2})?$/.test(value)) return null;
|
||||
const [a, b = ''] = value.split('.');
|
||||
return BigInt(a) * 100n + BigInt(b.padEnd(2, '0'));
|
||||
};
|
||||
const decimal = (n: bigint) => {
|
||||
const sign = n < 0n ? '-' : '';
|
||||
const v = n < 0n ? -n : n;
|
||||
return `${sign}${v / 100n}.${String(v % 100n).padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gross margin of one unit of a product or service: price less its variable
|
||||
* cost, and that margin as a share of the price (one decimal). Null until both
|
||||
* values are valid amounts and the price is above zero.
|
||||
*/
|
||||
export function productUnitMargin(
|
||||
unitPrice: string,
|
||||
unitVariableCost: string,
|
||||
): { amount: string; percent: string } | null {
|
||||
const price = cents(unitPrice.trim());
|
||||
const cost = cents(unitVariableCost.trim());
|
||||
if (price === null || cost === null || price === 0n) return null;
|
||||
const margin = price - cost;
|
||||
// Tenths of a percent, rounded half away from zero.
|
||||
const tenths =
|
||||
(margin * 2000n + (margin < 0n ? -price : price)) / (2n * price);
|
||||
const sign = tenths < 0n ? '-' : '';
|
||||
const t = tenths < 0n ? -tenths : tenths;
|
||||
return {
|
||||
amount: decimal(margin),
|
||||
percent: `${sign}${t / 10n}.${t % 10n}`,
|
||||
};
|
||||
}
|
||||
@@ -68,3 +68,42 @@ export function calculateStartupAssetRequirements(
|
||||
startupExpenses,
|
||||
};
|
||||
}
|
||||
|
||||
/** Usual start-up expenses of a new company (mapa de erros, ponto 4), offered as a starting list. */
|
||||
export const startupExpenseCategories = [
|
||||
'Constituição da empresa e despesas legais',
|
||||
'Mobiliário',
|
||||
'Seguros',
|
||||
'Rendas',
|
||||
'Computadores e equipamento informático',
|
||||
'Outras despesas de início de actividade',
|
||||
] as const;
|
||||
|
||||
/** Adds the usual categories that are still missing; existing rows and amounts are kept. */
|
||||
export function withStartupExpenseCategories(
|
||||
rows: { label: string; amount: string }[],
|
||||
): { label: string; amount: string }[] {
|
||||
const present = new Set(rows.map((row) => row.label.trim().toLowerCase()));
|
||||
return [
|
||||
...rows,
|
||||
...startupExpenseCategories
|
||||
.filter((label) => !present.has(label.toLowerCase()))
|
||||
.map((label) => ({ label, amount: '' })),
|
||||
];
|
||||
}
|
||||
|
||||
/** Total of the start-up expenses, or null while an amount is missing or invalid. */
|
||||
export function startupExpensesTotal(
|
||||
rows: { label: string; amount: string }[],
|
||||
): string | null {
|
||||
try {
|
||||
return startupAmount(
|
||||
rows.reduce(
|
||||
(sum, row) => sum + money(row.amount, 'o montante'),
|
||||
0n,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user