304 lines
12 KiB
TypeScript
304 lines
12 KiB
TypeScript
import { Head, Link, useForm } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import {
|
|
PageTitle,
|
|
Panel,
|
|
FormErrors,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import { exactMinorAmount } from '@/components/procurement-payable-insights';
|
|
import { date, roleLabels } from '@/types/procurement';
|
|
import { urgentOutcomes } from '@/pages/requisitions/urgent-reviews';
|
|
|
|
type Review = {
|
|
id: number;
|
|
version: number;
|
|
data: { notes: string };
|
|
snapshot: { fingerprint: string };
|
|
};
|
|
type Props = {
|
|
source: {
|
|
cycle: number;
|
|
outcome: string;
|
|
fingerprint: string;
|
|
closed_at: string;
|
|
request: {
|
|
id: number;
|
|
number: string;
|
|
justification: string;
|
|
total_minor: string | null;
|
|
};
|
|
items: {
|
|
id: number;
|
|
description: string;
|
|
quantity: string;
|
|
unit: string;
|
|
total_minor: string | null;
|
|
}[];
|
|
steps: {
|
|
id: number;
|
|
role: string;
|
|
decision: string;
|
|
comment: string | null;
|
|
routing?: { name: string } | null;
|
|
}[];
|
|
};
|
|
current: Review | null;
|
|
currentVerified: boolean;
|
|
canReview: boolean;
|
|
actorId: number;
|
|
tenantId: number;
|
|
history: {
|
|
data: Review[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
};
|
|
export default function UrgentReview(props: Props) {
|
|
const source = props.source;
|
|
const url = `/requisicoes/${source.request.id}/ciclos/${source.cycle}/revisao-urgencia`;
|
|
const form = useForm(
|
|
`urgent-review:${props.tenantId}:${props.actorId}:${source.request.id}:${source.cycle}`,
|
|
{
|
|
operation_key: '',
|
|
version: props.current?.version ?? 0,
|
|
source_hash: source.fingerprint,
|
|
notes: '',
|
|
},
|
|
);
|
|
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 fresh =
|
|
form.data.version === (props.current?.version ?? 0) &&
|
|
form.data.source_hash === source.fingerprint;
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-5 lg:p-8">
|
|
<Head title="Revisão posterior de urgência" />
|
|
<PageTitle
|
|
eyebrow={`${source.request.number} · Ciclo ${source.cycle}`}
|
|
title="Revisão posterior de urgência"
|
|
description="A revisão regista a conferência posterior da direcção de Compras. As aprovações, a decisão final e os movimentos conservam o seu percurso próprio."
|
|
/>
|
|
<nav className="flex flex-wrap gap-4">
|
|
<Link
|
|
className="underline"
|
|
href="/requisicoes/urgencias/revisoes"
|
|
>
|
|
Voltar às urgências
|
|
</Link>
|
|
<Link
|
|
className="underline"
|
|
href={`/requisicoes/${source.request.id}`}
|
|
>
|
|
Consultar o pedido actual
|
|
</Link>
|
|
</nav>
|
|
<Panel
|
|
title={`Ciclo concluído · ${urgentOutcomes[source.outcome]}`}
|
|
>
|
|
<p className="whitespace-pre-wrap">
|
|
{source.request.justification}
|
|
</p>
|
|
<p className="mt-3">
|
|
Conclusão registada em: {date(source.closed_at)}
|
|
</p>
|
|
<p className="mt-3">
|
|
Valor da necessidade original:{' '}
|
|
{source.request.total_minor === null
|
|
? 'Indeterminado'
|
|
: exactMinorAmount(source.request.total_minor, 'MZN')}
|
|
</p>
|
|
{source.outcome === 'expired' && (
|
|
<p className="mt-3">
|
|
Estes dados pertencem ao ciclo expirado. O rascunho
|
|
actual e uma nova submissão podem ter valores
|
|
diferentes.
|
|
</p>
|
|
)}
|
|
<ul className="mt-4 space-y-2">
|
|
{source.items.map((line) => (
|
|
<li key={line.id}>
|
|
{line.description} · {line.quantity} {line.unit} ·{' '}
|
|
{line.total_minor === null
|
|
? 'Indeterminado'
|
|
: exactMinorAmount(line.total_minor, 'MZN')}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<h3 className="mt-4 font-semibold">Decisões conservadas</h3>
|
|
<ul className="mt-2 space-y-2">
|
|
{source.steps.map((step) => (
|
|
<li key={step.id}>
|
|
{step.routing?.name ||
|
|
roleLabels[step.role] ||
|
|
'Etapa configurada'}{' '}
|
|
·{' '}
|
|
{step.decision === 'approved'
|
|
? 'Aprovada'
|
|
: step.decision === 'rejected'
|
|
? 'Rejeitada'
|
|
: 'Sem decisão antes da conclusão do ciclo'}
|
|
{step.comment && (
|
|
<p className="whitespace-pre-wrap">
|
|
{step.comment}
|
|
</p>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</Panel>
|
|
<Panel
|
|
title={
|
|
props.current
|
|
? `Revisão actual · versão ${props.current.version}`
|
|
: 'Registar a revisão posterior'
|
|
}
|
|
>
|
|
{!props.currentVerified && (
|
|
<p role="alert">
|
|
A revisão anterior não confirma a origem actualmente
|
|
apresentada. O ciclo não é apresentado como revisto.
|
|
</p>
|
|
)}
|
|
{props.current && (
|
|
<p className="mb-4 whitespace-pre-wrap">
|
|
{props.current.data.notes}
|
|
</p>
|
|
)}
|
|
{props.canReview ? (
|
|
<form
|
|
ref={ref}
|
|
className="space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const data = {
|
|
...form.data,
|
|
operation_key:
|
|
form.data.operation_key ||
|
|
crypto.randomUUID(),
|
|
};
|
|
form.setData(data);
|
|
form.transform(() => data);
|
|
form.post(url, {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
const next = {
|
|
...data,
|
|
operation_key: '',
|
|
version: data.version + 1,
|
|
notes: '',
|
|
};
|
|
form.setData(next);
|
|
form.setDefaults(next);
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
{!fresh && (
|
|
<p role="alert">
|
|
A origem ou a revisão mudou. Confira os dados
|
|
actuais antes de registar outra versão.
|
|
</p>
|
|
)}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => {
|
|
if (
|
|
form.isDirty &&
|
|
!window.confirm(
|
|
'Descartar esta preparação e usar a origem actual?',
|
|
)
|
|
)
|
|
return;
|
|
form.setData({
|
|
operation_key: '',
|
|
version: props.current?.version ?? 0,
|
|
source_hash: source.fingerprint,
|
|
notes: '',
|
|
});
|
|
}}
|
|
>
|
|
Preparar versão actual
|
|
</Button>
|
|
<label className="block">
|
|
Conclusões da revisão e observações
|
|
<textarea
|
|
className={controlClass}
|
|
required
|
|
minLength={10}
|
|
maxLength={4000}
|
|
value={form.data.notes}
|
|
onChange={(event) =>
|
|
form.setData('notes', event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing || !fresh}>
|
|
Registar revisão posterior
|
|
</Button>
|
|
</form>
|
|
) : (
|
|
<p>
|
|
A consulta não confere competência para registar a
|
|
revisão. São exigidos perfil designado, função e
|
|
atribuição actualmente válidos.
|
|
</p>
|
|
)}
|
|
</Panel>
|
|
<Panel title="Histórico das revisões">
|
|
<ul className="space-y-4">
|
|
{props.history.data.map((row) => (
|
|
<li key={row.id}>
|
|
<h3 className="font-semibold">
|
|
Versão {row.version}
|
|
</h3>
|
|
<p className="whitespace-pre-wrap">
|
|
{row.data.notes}
|
|
</p>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<nav className="mt-4 flex gap-4">
|
|
{props.history.prev_page_url && (
|
|
<Link
|
|
preserveScroll
|
|
preserveState
|
|
className="underline"
|
|
href={props.history.prev_page_url}
|
|
>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{props.history.next_page_url && (
|
|
<Link
|
|
preserveScroll
|
|
preserveState
|
|
className="underline"
|
|
href={props.history.next_page_url}
|
|
>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
</main>
|
|
);
|
|
}
|