559 lines
23 KiB
TypeScript
559 lines
23 KiB
TypeScript
import { Link } from '@inertiajs/react';
|
|
import { card, input } from '@/pages/investment/shared';
|
|
|
|
export type PreparationDocument = {
|
|
document_id: string;
|
|
file_name: string;
|
|
status: string;
|
|
malware_scan_status: string;
|
|
};
|
|
export type Preparation = {
|
|
version: string;
|
|
enabled: boolean;
|
|
mode: string;
|
|
period_covered: boolean;
|
|
scope_covered: boolean;
|
|
reason: string[];
|
|
missing_fields: string[];
|
|
missing_labels?: string[];
|
|
authority: string;
|
|
next: string;
|
|
calculation_notice: string;
|
|
notice: string;
|
|
source: { url: string; sha256: string };
|
|
authority_source: { reference: string; url: string };
|
|
questions: Array<{
|
|
key: string;
|
|
label: string;
|
|
type?: string;
|
|
options?: Record<string, string>;
|
|
when?: { field: string; equals: string };
|
|
source: string;
|
|
help?: string;
|
|
}>;
|
|
requirements: Array<{
|
|
key: string;
|
|
label: string;
|
|
state: string;
|
|
required: boolean;
|
|
document_id: string | null;
|
|
document: PreparationDocument | null;
|
|
stage: string;
|
|
reason: string;
|
|
source: { reference: string; url: string; period: string };
|
|
}>;
|
|
};
|
|
const states: Record<string, string> = {
|
|
not_applicable: 'Não aplicável neste contexto',
|
|
indeterminate: 'Informação em falta para decidir',
|
|
missing: 'Documento por associar',
|
|
associated: 'Documento associado — conteúdo por conferir',
|
|
};
|
|
export const preparationMode = (mode: string) =>
|
|
({
|
|
authorization: 'Pedido de autorização',
|
|
registration: 'Pedido de mero registo',
|
|
indeterminate: 'Enquadramento por determinar',
|
|
})[mode] ?? mode;
|
|
|
|
export function PreparationSummary({
|
|
data,
|
|
caseId,
|
|
}: {
|
|
data: Preparation | null | undefined;
|
|
caseId?: string;
|
|
}) {
|
|
if (!data) return null;
|
|
const missingDocuments = data.requirements.filter(
|
|
(requirement) =>
|
|
requirement.required && requirement.state === 'missing',
|
|
).length;
|
|
const associatedDocuments = data.requirements.filter(
|
|
(requirement) =>
|
|
requirement.required && requirement.state === 'associated',
|
|
).length;
|
|
const undeterminedDocuments = data.requirements.filter(
|
|
(requirement) => requirement.state === 'indeterminate',
|
|
).length;
|
|
const continueWithDocuments =
|
|
data.enabled &&
|
|
data.period_covered &&
|
|
data.scope_covered &&
|
|
data.missing_fields.length === 0 &&
|
|
missingDocuments > 0;
|
|
return (
|
|
<section
|
|
id="private-investment-preparation"
|
|
className={card + ' space-y-3'}
|
|
aria-label="Preparação adaptada do investimento privado"
|
|
>
|
|
<h2 className="text-xl font-bold">
|
|
Preparar o pedido de investimento privado
|
|
</h2>
|
|
{data.enabled && (
|
|
<div
|
|
className="space-y-2"
|
|
aria-label="Situação e próxima acção"
|
|
>
|
|
<p>
|
|
<strong>Situação:</strong> {preparationMode(data.mode)}.
|
|
</p>
|
|
<p>
|
|
<strong>Próxima acção:</strong> {data.next}.
|
|
</p>
|
|
{data.missing_fields.length > 0 && (
|
|
<p>
|
|
Informação em falta:{' '}
|
|
{(
|
|
data.missing_labels ??
|
|
data.missing_fields.map(
|
|
(key) =>
|
|
data.questions?.find(
|
|
(q) => q.key === key,
|
|
)?.label ??
|
|
(
|
|
{
|
|
currency: 'Moeda do projecto',
|
|
expected_start_date:
|
|
'Data prevista de início',
|
|
entity_stage:
|
|
'Situação da entidade',
|
|
} as Record<string, string>
|
|
)[key] ??
|
|
key,
|
|
)
|
|
).join('; ')}
|
|
.
|
|
</p>
|
|
)}
|
|
{missingDocuments > 0 && (
|
|
<p>Documentos por associar: {missingDocuments}.</p>
|
|
)}
|
|
{associatedDocuments > 0 && (
|
|
<p>
|
|
Documentos associados: {associatedDocuments} —
|
|
conteúdo por conferir.
|
|
</p>
|
|
)}
|
|
{undeterminedDocuments > 0 && (
|
|
<p>
|
|
Exigências documentais por determinar:{' '}
|
|
{undeterminedDocuments}.
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
{caseId && (
|
|
<p>
|
|
<Link
|
|
className="inline-block rounded border px-4 py-2 font-semibold"
|
|
href={`/investimentos/${caseId}/contexto/editar#${continueWithDocuments ? 'preparation-documents' : 'preparation-questions'}`}
|
|
>
|
|
{continueWithDocuments
|
|
? 'Associar documentos do dossier'
|
|
: 'Preencher ou continuar o formulário'}
|
|
</Link>
|
|
</p>
|
|
)}
|
|
<details>
|
|
<summary className="cursor-pointer font-medium">
|
|
Consultar situação, documentos e fundamentos
|
|
</summary>
|
|
{!data.enabled ? (
|
|
<p>
|
|
Use os dados deste dossier para identificar o pedido e
|
|
os documentos aplicáveis. A preparação é opcional e não
|
|
atribui benefícios.
|
|
</p>
|
|
) : (
|
|
<>
|
|
<p>
|
|
<strong>{preparationMode(data.mode)}</strong> ·
|
|
recorte de 2026 ·{' '}
|
|
{data.version.replace(
|
|
'private-investment-preparation-',
|
|
'',
|
|
)}
|
|
</p>
|
|
{!data.period_covered && (
|
|
<p>
|
|
Este incremento cobre pedidos com início
|
|
previsto em 2026. Uma data em falta ou fora
|
|
desse recorte não permite determinar o pedido
|
|
por este lote; os restantes trabalhos do dossier
|
|
continuam disponíveis.
|
|
</p>
|
|
)}
|
|
{!data.scope_covered && (
|
|
<p>
|
|
Declare a actividade efectiva. Prospecção,
|
|
pesquisa/produção extractiva e investimento
|
|
público ou exclusivamente social/sem fins
|
|
lucrativos seguem enquadramentos próprios, fora
|
|
deste lote.
|
|
</p>
|
|
)}
|
|
{data.reason.map((reason) => (
|
|
<p key={reason}>{reason}</p>
|
|
))}
|
|
<p>
|
|
Encaminhamento indicativo: {data.authority}.{' '}
|
|
<a
|
|
className="underline"
|
|
href={data.authority_source.url}
|
|
>
|
|
{data.authority_source.reference}
|
|
</a>
|
|
</p>
|
|
<ul className="space-y-3">
|
|
{data.requirements
|
|
.filter((r) => r.state !== 'not_applicable')
|
|
.map((r) => (
|
|
<li
|
|
key={r.key}
|
|
className="rounded border p-3"
|
|
>
|
|
<strong>{r.label}</strong> —{' '}
|
|
{states[r.state]}.{' '}
|
|
{r.document?.file_name && (
|
|
<span>
|
|
Ficheiro: {r.document.file_name}
|
|
.{' '}
|
|
</span>
|
|
)}
|
|
<p>
|
|
{r.reason}{' '}
|
|
<a
|
|
className="underline"
|
|
href={r.source.url}
|
|
>
|
|
Ver fundamento:{' '}
|
|
{r.source.reference}
|
|
</a>{' '}
|
|
· {r.source.period}.
|
|
</p>
|
|
{caseId && (
|
|
<Link
|
|
className="underline"
|
|
href={`/investimentos/${caseId}/contexto/editar#preparation-documents`}
|
|
>
|
|
Rever dados ou associar
|
|
documento do dossier
|
|
</Link>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<details>
|
|
<summary>
|
|
Exigências que não se aplicam — histórico
|
|
preservado
|
|
</summary>
|
|
{data.requirements
|
|
.filter((r) => r.state === 'not_applicable')
|
|
.map((r) => (
|
|
<p key={r.key}>
|
|
{r.label}: {r.reason}{' '}
|
|
{r.document_id && (
|
|
<>
|
|
Associação anterior mantida
|
|
{r.document?.file_name
|
|
? `: ${r.document.file_name}`
|
|
: ''}
|
|
.
|
|
</>
|
|
)}{' '}
|
|
<a
|
|
className="underline"
|
|
href={r.source.url}
|
|
>
|
|
{r.source.reference}
|
|
</a>
|
|
</p>
|
|
))}
|
|
</details>
|
|
<p>{data.calculation_notice}</p>
|
|
<p>{data.notice}</p>
|
|
</>
|
|
)}
|
|
<a className="underline" href={data.source.url}>
|
|
Consultar o exemplar oficial e os fundamentos
|
|
</a>
|
|
{caseId && (
|
|
<p>
|
|
<Link
|
|
className="underline"
|
|
href={`/investimentos/${caseId}/contexto/editar#preparation-questions`}
|
|
>
|
|
Editar os dados do formulário
|
|
</Link>
|
|
</p>
|
|
)}
|
|
</details>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
export function PreparationEditor({
|
|
data,
|
|
fields,
|
|
changes,
|
|
documents,
|
|
links,
|
|
onChange,
|
|
onLink,
|
|
disabled,
|
|
}: {
|
|
data: Preparation;
|
|
fields: Record<string, string>;
|
|
changes: Record<string, string>;
|
|
documents: PreparationDocument[];
|
|
links: Record<string, string | null>;
|
|
onChange: (key: string, value: string) => void;
|
|
onLink: (key: string, value: string | null) => void;
|
|
disabled: boolean;
|
|
}) {
|
|
const values = { ...fields, ...changes };
|
|
const active = values.investment_preparation === 'yes';
|
|
const excluded = ['extractive', 'public_nonprofit'].includes(
|
|
values.investment_activity_scope,
|
|
);
|
|
const applicable = (q: Preparation['questions'][number]) =>
|
|
!q.when || values[q.when.field] === q.when.equals;
|
|
// Keep a field in place while the user edits it; collapse only recovered answers.
|
|
const known = (q: Preparation['questions'][number]) =>
|
|
!!fields[q.key] && fields[q.key] !== 'unknown';
|
|
const render = (q: Preparation['questions'][number]) => (
|
|
<label className="grid gap-1" key={q.key} htmlFor={'prep-' + q.key}>
|
|
{q.label}
|
|
{q.options ? (
|
|
<select
|
|
id={'prep-' + q.key}
|
|
className={input}
|
|
disabled={disabled}
|
|
value={values[q.key] ?? ''}
|
|
onChange={(e) => onChange(q.key, e.target.value)}
|
|
>
|
|
<option value="">Por indicar</option>
|
|
{Object.entries(q.options).map(([value, text]) => (
|
|
<option key={value} value={value}>
|
|
{text}
|
|
</option>
|
|
))}
|
|
</select>
|
|
) : (
|
|
<input
|
|
id={'prep-' + q.key}
|
|
className={input}
|
|
disabled={disabled}
|
|
type="text"
|
|
inputMode="decimal"
|
|
value={values[q.key] ?? ''}
|
|
onChange={(e) => onChange(q.key, e.target.value)}
|
|
/>
|
|
)}
|
|
<span className="text-sm">
|
|
{q.help} Fundamento: {q.source}.
|
|
</span>
|
|
</label>
|
|
);
|
|
const questions = data.questions.filter(
|
|
(q) =>
|
|
q.key !== 'investment_preparation' &&
|
|
applicable(q) &&
|
|
(!excluded || q.key === 'investment_activity_scope'),
|
|
);
|
|
const needed = questions.filter(
|
|
(q) =>
|
|
!known(q) &&
|
|
(!data.enabled ||
|
|
data.mode !== 'authorization' ||
|
|
data.missing_fields.includes(q.key)),
|
|
);
|
|
const renderDocument = (r: Preparation['requirements'][number]) => (
|
|
<label className="grid gap-1" key={r.key}>
|
|
{r.label}
|
|
<select
|
|
className={input}
|
|
disabled={disabled}
|
|
value={
|
|
Object.hasOwn(links, r.key)
|
|
? (links[r.key] ?? '')
|
|
: (r.document_id ?? '')
|
|
}
|
|
onChange={(e) => onLink(r.key, e.target.value || null)}
|
|
>
|
|
<option value="">Por associar</option>
|
|
{documents.map((doc) => (
|
|
<option key={doc.document_id} value={doc.document_id}>
|
|
{doc.file_name} ·{' '}
|
|
{{
|
|
quarantined: 'Em quarentena',
|
|
active: 'Disponível',
|
|
deleted: 'Removido',
|
|
}[doc.status] ?? 'Estado por conferir'}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<span>
|
|
{r.reason}{' '}
|
|
<a className="underline" href={r.source.url}>
|
|
{r.source.reference}
|
|
</a>
|
|
</span>
|
|
</label>
|
|
);
|
|
return (
|
|
<section
|
|
id="preparation-questions"
|
|
className={card + ' space-y-4'}
|
|
aria-label="Dados do pedido de investimento privado"
|
|
>
|
|
<h2 className="text-xl font-bold">
|
|
Dados em falta para preparar o pedido
|
|
</h2>
|
|
<p>
|
|
Valores conhecidos são reutilizados. As alterações só são
|
|
gravadas depois da comparação de impacto. Nacionalidade e
|
|
residência não determinam a origem do capital nem a modalidade
|
|
da empresa.
|
|
</p>
|
|
{render(data.questions[0])}
|
|
{active && (
|
|
<>
|
|
{needed.map(render)}
|
|
<details>
|
|
<summary>
|
|
Dados já declarados — consultar ou alterar
|
|
</summary>
|
|
<div className="space-y-3">
|
|
{questions.filter(known).map(render)}
|
|
</div>
|
|
</details>
|
|
<p>
|
|
Data, moeda e situação da entidade usam as declarações
|
|
acima.{' '}
|
|
{data.missing_fields.filter((k) =>
|
|
[
|
|
'currency',
|
|
'expected_start_date',
|
|
'entity_stage',
|
|
].includes(k),
|
|
).length > 0 &&
|
|
'Complete esses campos em falta antes de comparar.'}
|
|
</p>
|
|
<div id="preparation-documents" className="space-y-3">
|
|
<h3 className="font-bold">
|
|
Associar documentos já existentes no dossier
|
|
</h3>
|
|
<p>
|
|
Carregue documentos pelo Balcão Virtual do mesmo
|
|
dossier. A associação não aprova o conteúdo nem
|
|
substitui a análise do pedido.
|
|
</p>
|
|
{data.requirements
|
|
.filter(
|
|
(r) => r.required && r.state !== 'associated',
|
|
)
|
|
.map(renderDocument)}
|
|
{data.requirements.some(
|
|
(r) => r.required && r.state === 'associated',
|
|
) && (
|
|
<details>
|
|
<summary>
|
|
Documentos já associados — consultar ou
|
|
alterar
|
|
</summary>
|
|
<div className="space-y-3">
|
|
{data.requirements
|
|
.filter(
|
|
(r) =>
|
|
r.required &&
|
|
r.state === 'associated',
|
|
)
|
|
.map(renderDocument)}
|
|
</div>
|
|
</details>
|
|
)}
|
|
</div>
|
|
</>
|
|
)}
|
|
<p>
|
|
Compare o impacto para actualizar o enquadramento, as perguntas
|
|
e as exigências. Uma exigência que deixe de ser aplicável
|
|
conserva o documento anterior no histórico.
|
|
</p>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
export function PreparationImpact({
|
|
before,
|
|
after,
|
|
}: {
|
|
before: Preparation;
|
|
after: Preparation;
|
|
}) {
|
|
const changedRequirements = after.requirements.filter((requirement) => {
|
|
const previous = before.requirements.find(
|
|
(item) => item.key === requirement.key,
|
|
);
|
|
return (
|
|
previous?.state !== requirement.state ||
|
|
(previous?.document_id ?? null) !== requirement.document_id
|
|
);
|
|
});
|
|
const documentName = (
|
|
requirement: Preparation['requirements'][number] | undefined,
|
|
) =>
|
|
requirement?.document_id
|
|
? (requirement.document?.file_name ??
|
|
'Documento associado — nome indisponível')
|
|
: 'Sem documento associado';
|
|
return (
|
|
<div
|
|
className="space-y-2 rounded border border-amber-400 p-3"
|
|
aria-label="Impacto no pedido de investimento"
|
|
>
|
|
<h3 className="font-bold">Impacto no pedido e nos documentos</h3>
|
|
<p>
|
|
{preparationMode(before.mode)} → {preparationMode(after.mode)}.
|
|
</p>
|
|
<p>Próximo passo: {after.next}.</p>
|
|
{changedRequirements.length === 0 && (
|
|
<p>Sem alterações às exigências ou associações documentais.</p>
|
|
)}
|
|
<ul className="list-disc pl-5">
|
|
{changedRequirements.map((r) => {
|
|
const previous = before.requirements.find(
|
|
(item) => item.key === r.key,
|
|
);
|
|
const documentChanged =
|
|
(previous?.document_id ?? null) !== r.document_id;
|
|
return (
|
|
<li key={r.key} className="space-y-1">
|
|
{r.label}:{' '}
|
|
{states[previous?.state ?? 'indeterminate']} →{' '}
|
|
{states[r.state]}.{' '}
|
|
{documentChanged ? (
|
|
<p>
|
|
{previous?.document_id && r.document_id
|
|
? 'Substituição de documento: '
|
|
: 'Documento: '}
|
|
{documentName(previous)} → {documentName(r)}
|
|
.
|
|
</p>
|
|
) : (
|
|
r.document_id &&
|
|
'Vínculo documental preservado.'
|
|
)}
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
<p>{after.calculation_notice}</p>
|
|
<PreparationSummary data={after} />
|
|
</div>
|
|
);
|
|
}
|