Entrega dashboard e correccoes transversais preservando Ofertas instalado
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,267 @@
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
estimateOperatingCosts,
|
||||
estimateOperatingRevenue,
|
||||
restoreOperatingAssumptions,
|
||||
} from '@/lib/investment-operating-assistance';
|
||||
|
||||
export default function InvestmentOperatingAssistance({
|
||||
snapshot,
|
||||
currency,
|
||||
onChange,
|
||||
}: {
|
||||
snapshot: string;
|
||||
currency: string;
|
||||
onChange: (key: string, value: string) => void;
|
||||
}) {
|
||||
const [data, setData] = useState(() =>
|
||||
restoreOperatingAssumptions(snapshot),
|
||||
);
|
||||
const [message, setMessage] = useState('');
|
||||
const save = (next: typeof data) => {
|
||||
setData(next);
|
||||
setMessage('');
|
||||
onChange(
|
||||
'planning_assumptions_snapshot',
|
||||
JSON.stringify({ version: 1, data: next }),
|
||||
);
|
||||
};
|
||||
const field = (
|
||||
key: 'capacity' | 'price' | 'occupancy' | 'frequency',
|
||||
label: string,
|
||||
) => (
|
||||
<label className="block">
|
||||
{label}
|
||||
<input
|
||||
className="block w-full rounded border p-2"
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.01"
|
||||
value={data[key]}
|
||||
onChange={(e) => save({ ...data, [key]: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
);
|
||||
const adopt = (kind: 'revenue' | 'costs') => {
|
||||
try {
|
||||
if (!currency)
|
||||
throw new Error(
|
||||
'Seleccione a moeda do investimento antes de utilizar a estimativa.',
|
||||
);
|
||||
const value =
|
||||
kind === 'revenue'
|
||||
? estimateOperatingRevenue(data)
|
||||
: estimateOperatingCosts(data);
|
||||
onChange(
|
||||
kind === 'revenue'
|
||||
? 'annual_revenue'
|
||||
: 'annual_operating_costs',
|
||||
value,
|
||||
);
|
||||
setMessage(
|
||||
`Estimativa anual de ${kind === 'revenue' ? 'receitas' : 'custos'} adoptada por sua escolha: ${value} ${currency}. Pode alterá-la nos campos acima.`,
|
||||
);
|
||||
} catch (error) {
|
||||
setMessage(
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'Confira os pressupostos.',
|
||||
);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<section
|
||||
className="space-y-4 rounded border p-4"
|
||||
aria-label="Ajuda para estimar receitas e custos"
|
||||
>
|
||||
<h3 className="font-semibold">
|
||||
Não sei estimar receitas ou custos
|
||||
</h3>
|
||||
<p>
|
||||
Escolha a actividade e introduza os pressupostos que conhece.
|
||||
Não são previsões de procura nem valores recomendados. Nenhuma
|
||||
estimativa substitui os seus valores até clicar em Usar.
|
||||
</p>
|
||||
<label className="block">
|
||||
Modelo para esta estimativa
|
||||
<select
|
||||
className="block w-full rounded border p-2"
|
||||
value={data.model}
|
||||
onChange={(e) =>
|
||||
save({
|
||||
...data,
|
||||
model: e.target.value as typeof data.model,
|
||||
})
|
||||
}
|
||||
>
|
||||
<option value="">Escolher</option>
|
||||
<option value="hotel">Hotel — quartos e ocupação</option>
|
||||
<option value="agriculture">
|
||||
Agronegócio — produção e ciclos
|
||||
</option>
|
||||
<option value="other">
|
||||
Outra actividade — unidades e frequência
|
||||
</option>
|
||||
</select>
|
||||
</label>
|
||||
{data.model && (
|
||||
<>
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
{field(
|
||||
'capacity',
|
||||
data.model === 'hotel'
|
||||
? 'Quartos disponíveis'
|
||||
: 'Unidades vendáveis por ciclo ou período',
|
||||
)}
|
||||
{field(
|
||||
'price',
|
||||
data.model === 'hotel'
|
||||
? 'Preço por quarto/noite'
|
||||
: 'Preço por unidade',
|
||||
)}
|
||||
{data.model === 'hotel' &&
|
||||
field('occupancy', 'Ocupação prevista (%)')}
|
||||
{field(
|
||||
'frequency',
|
||||
data.model === 'hotel'
|
||||
? 'Dias de funcionamento previstos no ano'
|
||||
: 'Ciclos ou períodos no ano',
|
||||
)}
|
||||
</div>
|
||||
<p className="text-sm">
|
||||
{data.model === 'hotel'
|
||||
? 'Receita anual = quartos × dias de funcionamento × ocupação ÷ 100 × preço. Considere sazonalidade ao escolher a ocupação e os dias.'
|
||||
: 'Receita anual = unidades vendáveis × preço × ciclos. Considere perdas e sazonalidade nas unidades e ciclos declarados.'}{' '}
|
||||
A facturação não equivale a recebimentos; a próxima
|
||||
etapa trata a tesouraria.
|
||||
</p>
|
||||
<label className="block">
|
||||
Fundamento ou informação ainda a confirmar
|
||||
<textarea
|
||||
className="block w-full rounded border p-2"
|
||||
maxLength={1000}
|
||||
value={data.source}
|
||||
onChange={(e) =>
|
||||
save({ ...data, source: e.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-3 py-2"
|
||||
onClick={() => adopt('revenue')}
|
||||
>
|
||||
Usar esta estimativa de receitas
|
||||
</button>
|
||||
<details className="rounded border p-3">
|
||||
<summary>Categorias de custos anuais</summary>
|
||||
<p>
|
||||
{data.model === 'hotel'
|
||||
? 'Considere pessoal, limpeza/lavandaria, energia, água, manutenção, consumíveis e distribuição.'
|
||||
: data.model === 'agriculture'
|
||||
? 'Considere insumos, pessoal, água/energia, armazenagem, transformação, transporte e perdas.'
|
||||
: 'Considere pessoal, matérias-primas, serviços, instalações, energia, manutenção e distribuição.'}{' '}
|
||||
Acrescente apenas categorias aplicáveis; a soma não
|
||||
demonstra que todos os custos foram identificados.
|
||||
</p>
|
||||
{data.costs.map((row, index) => (
|
||||
<div
|
||||
className="my-3 grid gap-2 sm:grid-cols-2"
|
||||
key={index}
|
||||
>
|
||||
<label>
|
||||
Categoria
|
||||
<input
|
||||
className="block w-full rounded border p-2"
|
||||
maxLength={120}
|
||||
value={row.label}
|
||||
onChange={(e) =>
|
||||
save({
|
||||
...data,
|
||||
costs: data.costs.map(
|
||||
(item, i) =>
|
||||
i === index
|
||||
? {
|
||||
...item,
|
||||
label: e
|
||||
.target
|
||||
.value,
|
||||
}
|
||||
: item,
|
||||
),
|
||||
})
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Estimativa anual
|
||||
<input
|
||||
className="block w-full rounded border p-2"
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.01"
|
||||
value={row.annual}
|
||||
onChange={(e) =>
|
||||
save({
|
||||
...data,
|
||||
costs: data.costs.map(
|
||||
(item, i) =>
|
||||
i === index
|
||||
? {
|
||||
...item,
|
||||
annual: e
|
||||
.target
|
||||
.value,
|
||||
}
|
||||
: item,
|
||||
),
|
||||
})
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
className="text-left underline"
|
||||
onClick={() =>
|
||||
save({
|
||||
...data,
|
||||
costs: data.costs.filter(
|
||||
(_, i) => i !== index,
|
||||
),
|
||||
})
|
||||
}
|
||||
>
|
||||
Retirar esta categoria
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
<button
|
||||
type="button"
|
||||
className="mr-3 rounded border px-3 py-2"
|
||||
disabled={data.costs.length >= 30}
|
||||
onClick={() =>
|
||||
save({
|
||||
...data,
|
||||
costs: [
|
||||
...data.costs,
|
||||
{ label: '', annual: '' },
|
||||
],
|
||||
})
|
||||
}
|
||||
>
|
||||
Acrescentar categoria
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded border px-3 py-2"
|
||||
onClick={() => adopt('costs')}
|
||||
>
|
||||
Usar esta estimativa de custos
|
||||
</button>
|
||||
</details>
|
||||
</>
|
||||
)}
|
||||
{message && <p role="status">{message}</p>}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user