821 lines
43 KiB
TypeScript
821 lines
43 KiB
TypeScript
import { Head, Link, router, useForm } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import {
|
|
FormErrors,
|
|
PageTitle,
|
|
Panel,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import { date } from '@/types/procurement';
|
|
|
|
type Document = {
|
|
id: number;
|
|
record_key: string;
|
|
version: number;
|
|
data: { type: string; reference: string; issuer_reference: string };
|
|
};
|
|
type Frozen = {
|
|
id: number;
|
|
type: string;
|
|
reference: string;
|
|
version: number;
|
|
issuer_reference: string;
|
|
};
|
|
type BankRecord = {
|
|
id: number;
|
|
record_key: string;
|
|
version: number;
|
|
state: string;
|
|
reason: string;
|
|
issues?: string[];
|
|
snapshot: { documents: Frozen[] };
|
|
data: {
|
|
commitment_document_id: number;
|
|
declaration_document_id: number | null;
|
|
support_document_ids: number[];
|
|
submission_document_id: number | null;
|
|
submission_basis: string | null;
|
|
submitted_on: string | null;
|
|
requirements_known: boolean;
|
|
required_support_types: string[];
|
|
required_support_counts: Record<string, number>;
|
|
requirements_basis: string;
|
|
due_on: string | null;
|
|
deadline_basis: string | null;
|
|
};
|
|
};
|
|
type Page<T> = {
|
|
data: T[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
type Props = {
|
|
importRecord: { id: number; record_key: string; version: number };
|
|
order: { id: number; number: string };
|
|
current: Page<BankRecord>;
|
|
history: Page<BankRecord>;
|
|
documents: Page<Document>;
|
|
types: Record<string, string>;
|
|
canWrite: boolean;
|
|
actorId: number;
|
|
tenantId: number;
|
|
today?: string;
|
|
};
|
|
const stateLabels: Record<string, string> = {
|
|
tracking: 'Documentação incompleta',
|
|
ready: 'Documentos reunidos; entrega por documentar',
|
|
documented: 'Comprovativo de entrega associado',
|
|
};
|
|
function Pages({ page }: { page: Page<unknown> }) {
|
|
return (
|
|
<nav className="mt-3 flex gap-4" aria-label="Páginas">
|
|
{page.prev_page_url && (
|
|
<Link preserveState href={page.prev_page_url}>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{page.next_page_url && (
|
|
<Link preserveState href={page.next_page_url}>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
);
|
|
}
|
|
export default function ImportBankSupport(props: Props) {
|
|
const url = `/compras/importacoes/${props.importRecord.id}/suporte-bancario`;
|
|
const form = useForm(
|
|
`import-bank:${props.tenantId}:${props.actorId}:${props.importRecord.record_key}:${props.importRecord.version}`,
|
|
{
|
|
operation_key: '',
|
|
record_key: null as string | null,
|
|
version: 0,
|
|
commitment_document_id: '',
|
|
commitment_label: '',
|
|
declaration_document_id: '',
|
|
declaration_label: '',
|
|
support: [] as { id: number; reference: string }[],
|
|
submission_document_id: '',
|
|
submission_label: '',
|
|
submission_basis: '',
|
|
submitted_on: '',
|
|
requirements_known: false,
|
|
counts: {} as Record<string, string>,
|
|
requirements_basis: '',
|
|
due_on: '',
|
|
deadline_basis: '',
|
|
reason: '',
|
|
},
|
|
);
|
|
const ref = useRef<HTMLFormElement>(null);
|
|
useEffect(() => {
|
|
const node = ref.current;
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form: node, dirty: form.isDirty },
|
|
}),
|
|
);
|
|
return () => {
|
|
window.dispatchEvent(
|
|
new CustomEvent('sgc:context-dirty', {
|
|
detail: { form: node, dirty: false },
|
|
}),
|
|
);
|
|
};
|
|
}, [form.isDirty]);
|
|
const edit = (record: BankRecord) => {
|
|
const label = (id: number | null) =>
|
|
record.snapshot.documents.find((doc) => doc.id === id)?.reference ??
|
|
'';
|
|
form.setData({
|
|
operation_key: '',
|
|
record_key: record.record_key,
|
|
version: record.version,
|
|
commitment_document_id: String(record.data.commitment_document_id),
|
|
commitment_label: label(record.data.commitment_document_id),
|
|
declaration_document_id:
|
|
record.data.declaration_document_id === null
|
|
? ''
|
|
: String(record.data.declaration_document_id),
|
|
declaration_label: label(record.data.declaration_document_id),
|
|
support: record.data.support_document_ids.map((id) => ({
|
|
id,
|
|
reference: label(id),
|
|
})),
|
|
submission_document_id:
|
|
record.data.submission_document_id === null
|
|
? ''
|
|
: String(record.data.submission_document_id),
|
|
submission_label: label(record.data.submission_document_id),
|
|
submission_basis: record.data.submission_basis ?? '',
|
|
submitted_on: record.data.submitted_on ?? '',
|
|
requirements_known: record.data.requirements_known,
|
|
counts: Object.fromEntries(
|
|
Object.entries(record.data.required_support_counts).map(
|
|
([type, count]) => [type, String(count)],
|
|
),
|
|
),
|
|
requirements_basis: record.data.requirements_basis,
|
|
due_on: record.data.due_on ?? '',
|
|
deadline_basis: record.data.deadline_basis ?? '',
|
|
reason: '',
|
|
});
|
|
form.clearErrors();
|
|
};
|
|
return (
|
|
<>
|
|
<Head title="Suporte documental bancário" />
|
|
<main className="mx-auto max-w-6xl space-y-5 px-5 py-8">
|
|
<PageTitle
|
|
eyebrow="Acompanhamento da importação"
|
|
title="Suporte documental bancário"
|
|
description={`Ordem ${props.order.number}. Ligue o termo de compromisso ao documento único e acompanhe os documentos pedidos pelo banco.`}
|
|
/>
|
|
<div className="flex flex-wrap gap-4">
|
|
<Link
|
|
className="underline"
|
|
href={`/compras/importacoes/${props.importRecord.id}/documentos`}
|
|
>
|
|
Originais do dossiê
|
|
</Link>
|
|
<a className="underline" href={`${url}.csv`}>
|
|
Exportar acompanhamento CSV
|
|
</a>
|
|
</div>
|
|
<Panel
|
|
title="Situação por termo"
|
|
description="Documentos reunidos não demonstram a entrega ao banco. Um comprovativo registado não certifica cumprimento bancário, nem executa transferência ou submissão na JUE."
|
|
>
|
|
{props.current.data.length === 0 && (
|
|
<p>Ainda não há termos acompanhados neste âmbito.</p>
|
|
)}
|
|
{props.current.data.map((record) => (
|
|
<article
|
|
key={record.id}
|
|
className="space-y-2 border-b py-4"
|
|
>
|
|
<p className="font-medium">
|
|
{
|
|
record.snapshot.documents.find(
|
|
(doc) =>
|
|
doc.id ===
|
|
record.data.commitment_document_id,
|
|
)?.reference
|
|
}{' '}
|
|
· versão {record.version}
|
|
</p>
|
|
<p>{stateLabels[record.state] ?? record.state}</p>
|
|
<p>
|
|
Prazo:{' '}
|
|
{record.data.due_on
|
|
? date(record.data.due_on)
|
|
: 'Por indicar'}{' '}
|
|
·{' '}
|
|
{record.data.deadline_basis ??
|
|
'Sem prazo presumido'}
|
|
</p>
|
|
{(record.issues ?? []).map((issue) => (
|
|
<p key={issue} role="status">
|
|
{issue}
|
|
</p>
|
|
))}
|
|
<p>
|
|
Entrega documentada:{' '}
|
|
{record.data.submitted_on
|
|
? date(record.data.submitted_on)
|
|
: 'Por indicar'}{' '}
|
|
· {record.data.submission_basis}
|
|
</p>
|
|
<p>{record.reason}</p>
|
|
{props.canWrite && (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => edit(record)}
|
|
>
|
|
Preparar nova versão
|
|
</Button>
|
|
)}
|
|
</article>
|
|
))}
|
|
<Pages page={props.current} />
|
|
</Panel>
|
|
{props.canWrite && (
|
|
<Panel
|
|
title={
|
|
form.data.record_key
|
|
? `Rever acompanhamento · versão ${form.data.version}`
|
|
: 'Acompanhar um termo existente'
|
|
}
|
|
description="Seleccione os originais já registados. Indique apenas requisitos, prazos e informação novos deste acompanhamento."
|
|
>
|
|
<form
|
|
ref={ref}
|
|
className="space-y-5"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
form.data.operation_key ||
|
|
crypto.randomUUID();
|
|
form.setData('operation_key', operation);
|
|
form.transform((data) => ({
|
|
operation_key: operation,
|
|
record_key: data.record_key,
|
|
version: data.version,
|
|
commitment_document_id:
|
|
data.commitment_document_id,
|
|
declaration_document_id:
|
|
data.declaration_document_id || null,
|
|
support_document_ids: data.support.map(
|
|
(doc) => doc.id,
|
|
),
|
|
submission_document_id:
|
|
data.submission_document_id || null,
|
|
submission_basis:
|
|
data.submission_basis || null,
|
|
submitted_on: data.submission_document_id
|
|
? data.submitted_on || null
|
|
: null,
|
|
requirements_known: data.requirements_known,
|
|
required_support_types:
|
|
data.requirements_known
|
|
? Object.keys(data.counts)
|
|
: [],
|
|
required_support_counts:
|
|
data.requirements_known
|
|
? data.counts
|
|
: {},
|
|
requirements_basis: data.requirements_basis,
|
|
due_on: data.due_on || null,
|
|
deadline_basis: data.deadline_basis || null,
|
|
reason: data.reason,
|
|
}));
|
|
form.post(url, {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
form.reset();
|
|
router.visit(url, {
|
|
preserveState: false,
|
|
preserveScroll: true,
|
|
});
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<div className="grid gap-4 md:grid-cols-3">
|
|
<label className="grid gap-1">
|
|
Termo de compromisso
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.commitment_document_id}
|
|
onChange={(e) => {
|
|
const doc =
|
|
props.documents.data.find(
|
|
(d) =>
|
|
String(d.id) ===
|
|
e.target.value,
|
|
);
|
|
form.setData((data) => ({
|
|
...data,
|
|
commitment_document_id:
|
|
e.target.value,
|
|
commitment_label:
|
|
doc?.data.reference ?? '',
|
|
}));
|
|
}}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{form.data.commitment_document_id &&
|
|
!props.documents.data.some(
|
|
(doc) =>
|
|
String(doc.id) ===
|
|
form.data
|
|
.commitment_document_id,
|
|
) && (
|
|
<option
|
|
value={
|
|
form.data
|
|
.commitment_document_id
|
|
}
|
|
>
|
|
{form.data.commitment_label}{' '}
|
|
· seleccionado noutra página
|
|
</option>
|
|
)}
|
|
{props.documents.data
|
|
.filter(
|
|
(doc) =>
|
|
doc.data.type ===
|
|
'bank_commitment',
|
|
)
|
|
.map((doc) => (
|
|
<option
|
|
key={doc.id}
|
|
value={doc.id}
|
|
>
|
|
{doc.data.reference} · v
|
|
{doc.version}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Documento único / declaração
|
|
<select
|
|
className={controlClass}
|
|
value={
|
|
form.data.declaration_document_id
|
|
}
|
|
onChange={(e) => {
|
|
const doc =
|
|
props.documents.data.find(
|
|
(d) =>
|
|
String(d.id) ===
|
|
e.target.value,
|
|
);
|
|
form.setData((data) => ({
|
|
...data,
|
|
declaration_document_id:
|
|
e.target.value,
|
|
declaration_label:
|
|
doc?.data.reference ?? '',
|
|
}));
|
|
}}
|
|
>
|
|
<option value="">
|
|
Ainda por associar
|
|
</option>
|
|
{form.data.declaration_document_id &&
|
|
!props.documents.data.some(
|
|
(doc) =>
|
|
String(doc.id) ===
|
|
form.data
|
|
.declaration_document_id,
|
|
) && (
|
|
<option
|
|
value={
|
|
form.data
|
|
.declaration_document_id
|
|
}
|
|
>
|
|
{
|
|
form.data
|
|
.declaration_label
|
|
}{' '}
|
|
· seleccionado noutra página
|
|
</option>
|
|
)}
|
|
{props.documents.data
|
|
.filter(
|
|
(doc) =>
|
|
doc.data.type ===
|
|
'customs_declaration',
|
|
)
|
|
.map((doc) => (
|
|
<option
|
|
key={doc.id}
|
|
value={doc.id}
|
|
>
|
|
{doc.data.reference} · v
|
|
{doc.version}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Comprovativo de entrega ao banco
|
|
<select
|
|
className={controlClass}
|
|
value={form.data.submission_document_id}
|
|
onChange={(e) => {
|
|
const doc =
|
|
props.documents.data.find(
|
|
(d) =>
|
|
String(d.id) ===
|
|
e.target.value,
|
|
);
|
|
form.setData((data) => ({
|
|
...data,
|
|
submission_document_id:
|
|
e.target.value,
|
|
submission_label:
|
|
doc?.data.reference ?? '',
|
|
}));
|
|
}}
|
|
>
|
|
<option value="">
|
|
Ainda por associar
|
|
</option>
|
|
{form.data.submission_document_id &&
|
|
!props.documents.data.some(
|
|
(doc) =>
|
|
String(doc.id) ===
|
|
form.data
|
|
.submission_document_id,
|
|
) && (
|
|
<option
|
|
value={
|
|
form.data
|
|
.submission_document_id
|
|
}
|
|
>
|
|
{form.data.submission_label}{' '}
|
|
· seleccionado noutra página
|
|
</option>
|
|
)}
|
|
{props.documents.data
|
|
.filter(
|
|
(doc) =>
|
|
doc.data.type ===
|
|
'bank_support',
|
|
)
|
|
.map((doc) => (
|
|
<option
|
|
key={doc.id}
|
|
value={doc.id}
|
|
>
|
|
{doc.data.reference} · v
|
|
{doc.version}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
</div>
|
|
{form.data.submission_document_id && (
|
|
<label className="grid gap-1">
|
|
Fundamento do comprovativo de entrega
|
|
<textarea
|
|
required
|
|
minLength={3}
|
|
maxLength={1000}
|
|
className={controlClass}
|
|
value={form.data.submission_basis}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'submission_basis',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
)}
|
|
{form.data.submission_document_id && (
|
|
<label className="grid gap-1">
|
|
Data efectiva da entrega documentada
|
|
<input
|
|
type="date"
|
|
required
|
|
max={props.today}
|
|
className={controlClass}
|
|
value={form.data.submitted_on}
|
|
onChange={(event) =>
|
|
form.setData(
|
|
'submitted_on',
|
|
event.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
)}
|
|
<fieldset className="space-y-3 rounded border p-4">
|
|
<legend>
|
|
Documentos de suporte seleccionados
|
|
</legend>
|
|
{form.data.support.map((doc) => (
|
|
<div
|
|
key={doc.id}
|
|
className="flex flex-wrap items-center gap-3"
|
|
>
|
|
<span>{doc.reference}</span>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
onClick={() =>
|
|
form.setData(
|
|
'support',
|
|
form.data.support.filter(
|
|
(d) => d.id !== doc.id,
|
|
),
|
|
)
|
|
}
|
|
>
|
|
Retirar da preparação
|
|
</Button>
|
|
</div>
|
|
))}
|
|
{props.documents.data
|
|
.filter(
|
|
(doc) =>
|
|
doc.data.type !== 'bank_commitment',
|
|
)
|
|
.map((doc) => (
|
|
<label
|
|
key={doc.id}
|
|
className="flex gap-2"
|
|
>
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.support.some(
|
|
(selected) =>
|
|
selected.id === doc.id,
|
|
)}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'support',
|
|
e.target.checked
|
|
? [
|
|
...form.data
|
|
.support,
|
|
{
|
|
id: doc.id,
|
|
reference:
|
|
doc
|
|
.data
|
|
.reference,
|
|
},
|
|
]
|
|
: form.data.support.filter(
|
|
(d) =>
|
|
d.id !==
|
|
doc.id,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
<span>
|
|
{props.types[doc.data.type]} ·{' '}
|
|
{doc.data.reference} · v
|
|
{doc.version}
|
|
</span>
|
|
</label>
|
|
))}
|
|
<Pages page={props.documents} />
|
|
</fieldset>
|
|
<label className="flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.data.requirements_known}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'requirements_known',
|
|
e.target.checked,
|
|
)
|
|
}
|
|
/>
|
|
Os requisitos documentais pedidos pelo banco
|
|
estão identificados
|
|
</label>
|
|
{form.data.requirements_known && (
|
|
<fieldset className="grid gap-3 rounded border p-4 sm:grid-cols-2">
|
|
<legend>
|
|
Tipos e quantidades pedidos pelo banco
|
|
</legend>
|
|
{Object.entries(props.types)
|
|
.filter(
|
|
([type]) =>
|
|
type !== 'bank_commitment',
|
|
)
|
|
.map(([type, label]) => (
|
|
<div
|
|
key={type}
|
|
className="space-y-1"
|
|
>
|
|
<label className="flex gap-2">
|
|
<input
|
|
type="checkbox"
|
|
checked={Object.hasOwn(
|
|
form.data.counts,
|
|
type,
|
|
)}
|
|
onChange={(e) => {
|
|
const counts = {
|
|
...form.data
|
|
.counts,
|
|
};
|
|
if (
|
|
e.target.checked
|
|
)
|
|
counts[type] =
|
|
'';
|
|
else
|
|
delete counts[
|
|
type
|
|
];
|
|
form.setData(
|
|
'counts',
|
|
counts,
|
|
);
|
|
}}
|
|
/>
|
|
{label}
|
|
</label>
|
|
{Object.hasOwn(
|
|
form.data.counts,
|
|
type,
|
|
) && (
|
|
<label className="grid gap-1">
|
|
Quantidade solicitada
|
|
<input
|
|
required
|
|
type="number"
|
|
min={1}
|
|
max={100}
|
|
step={1}
|
|
className={
|
|
controlClass
|
|
}
|
|
value={
|
|
form.data
|
|
.counts[
|
|
type
|
|
]
|
|
}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'counts',
|
|
{
|
|
...form
|
|
.data
|
|
.counts,
|
|
[type]: e
|
|
.target
|
|
.value,
|
|
},
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
)}
|
|
</div>
|
|
))}
|
|
</fieldset>
|
|
)}
|
|
<label className="grid gap-1">
|
|
Fonte dos requisitos ou informação ainda em
|
|
falta
|
|
<textarea
|
|
required
|
|
minLength={3}
|
|
maxLength={1000}
|
|
className={controlClass}
|
|
value={form.data.requirements_basis}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'requirements_basis',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
<div className="grid gap-3 sm:grid-cols-2">
|
|
<label className="grid gap-1">
|
|
Prazo documental indicado
|
|
<input
|
|
type="date"
|
|
className={controlClass}
|
|
value={form.data.due_on}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'due_on',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
<label className="grid gap-1">
|
|
Fonte do prazo
|
|
<textarea
|
|
required={Boolean(form.data.due_on)}
|
|
minLength={3}
|
|
maxLength={1000}
|
|
className={controlClass}
|
|
value={form.data.deadline_basis}
|
|
onChange={(e) =>
|
|
form.setData(
|
|
'deadline_basis',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</label>
|
|
</div>
|
|
<label className="grid gap-1">
|
|
Motivo desta versão
|
|
<textarea
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
className={controlClass}
|
|
value={form.data.reason}
|
|
onChange={(e) =>
|
|
form.setData('reason', e.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing}>
|
|
Guardar acompanhamento documental
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
<Panel title="Histórico preservado">
|
|
{props.history.data.map((record) => (
|
|
<article
|
|
key={record.id}
|
|
className="space-y-1 border-b py-3"
|
|
>
|
|
<p>
|
|
Versão {record.version} ·{' '}
|
|
{stateLabels[record.state] ?? record.state}
|
|
</p>
|
|
<p>{record.reason}</p>
|
|
<p>
|
|
Requisitos:{' '}
|
|
{record.data.requirements_known
|
|
? record.data.required_support_types
|
|
.map(
|
|
(type) =>
|
|
`${props.types[type]}: ${record.data.required_support_counts[type]}`,
|
|
)
|
|
.join('; ') ||
|
|
'Nenhum tipo exigido segundo a fonte registada'
|
|
: 'Ainda desconhecidos'}{' '}
|
|
· {record.data.requirements_basis}
|
|
</p>
|
|
<p>
|
|
Entrega documentada:{' '}
|
|
{record.data.submitted_on
|
|
? date(record.data.submitted_on)
|
|
: 'Por indicar'}{' '}
|
|
· {record.data.submission_basis}
|
|
</p>
|
|
<p>
|
|
Prazo:{' '}
|
|
{record.data.due_on
|
|
? date(record.data.due_on)
|
|
: 'Desconhecido'}{' '}
|
|
· {record.data.deadline_basis}
|
|
</p>
|
|
{record.snapshot.documents.map((doc) => (
|
|
<p key={doc.id}>
|
|
<a
|
|
className="underline"
|
|
href={`/compras/importacoes/${props.importRecord.id}/documentos/${doc.id}`}
|
|
>
|
|
{props.types[doc.type]} ·{' '}
|
|
{doc.reference} · v{doc.version}
|
|
</a>
|
|
</p>
|
|
))}
|
|
</article>
|
|
))}
|
|
<Pages page={props.history} />
|
|
</Panel>
|
|
</main>
|
|
</>
|
|
);
|
|
}
|