290 lines
12 KiB
TypeScript
290 lines
12 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";
|
|
|
|
type Deadline = {
|
|
state: string;
|
|
step_id: number | null;
|
|
expires_at: string | null;
|
|
started_at?: string;
|
|
reminder_at?: string | null;
|
|
escalation_at?: string | null;
|
|
basis_hash: string | null;
|
|
};
|
|
type Props = {
|
|
requisition: {
|
|
id: number;
|
|
number: string;
|
|
status: string;
|
|
process_version: number;
|
|
approval_cycle: number;
|
|
};
|
|
deadline: Deadline;
|
|
canExpire: boolean;
|
|
canCheck: boolean;
|
|
canEdit: boolean;
|
|
actorId: number;
|
|
tenantId: number;
|
|
history: {
|
|
data: {
|
|
id: number;
|
|
data: { cycle: number; next_cycle: number };
|
|
reason: string;
|
|
request: { number: string };
|
|
steps: {
|
|
id: number;
|
|
position: number;
|
|
role: string;
|
|
decision: string;
|
|
}[];
|
|
}[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
};
|
|
const labels: Record<string, string> = {
|
|
inactive: "Sem etapa pendente",
|
|
unconfigured: "Sem prazo configurado",
|
|
unverified: "Datas por conferir",
|
|
running: "Em prazo",
|
|
expired: "Prazo terminado",
|
|
};
|
|
const time = (value?: string | null) =>
|
|
value ? new Date(value).toLocaleString("pt-MZ") : "Por indicar";
|
|
export default function Expiry(props: Props) {
|
|
const r = props.requisition;
|
|
const check = useForm({
|
|
operation_key: "",
|
|
process_version: r.process_version,
|
|
});
|
|
const form = useForm(`expiry:${props.tenantId}:${props.actorId}:${r.id}`, {
|
|
operation_key: "",
|
|
process_version: r.process_version,
|
|
basis_hash: props.deadline.basis_hash || "",
|
|
comment: "",
|
|
});
|
|
const current =
|
|
form.data.process_version === r.process_version &&
|
|
form.data.basis_hash === (props.deadline.basis_hash || "");
|
|
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]);
|
|
return (
|
|
<div className="mx-auto grid w-full max-w-6xl min-w-0 gap-6 p-5 lg:p-8">
|
|
<Head title="Prazos da aprovação" />
|
|
<PageTitle
|
|
eyebrow={r.number}
|
|
title="Prazos da aprovação"
|
|
description="A política preservada na submissão determina os prazos de cada etapa. A devolução mantém o pedido e o histórico para nova submissão pelo requisitante."
|
|
/>
|
|
<Link className="underline" href={`/requisicoes/${r.id}`}>
|
|
Voltar à requisição
|
|
</Link>
|
|
<Panel
|
|
title={`${labels[props.deadline.state] || "Por conferir"} · ciclo ${r.approval_cycle}`}
|
|
>
|
|
<dl className="grid gap-3 text-sm">
|
|
<div>
|
|
<dt>Início da etapa</dt>
|
|
<dd>{time(props.deadline.started_at)}</dd>
|
|
</div>
|
|
<div>
|
|
<dt>Expiração</dt>
|
|
<dd>{time(props.deadline.expires_at)}</dd>
|
|
</div>
|
|
{props.deadline.reminder_at && (
|
|
<div>
|
|
<dt>Lembrete previsto</dt>
|
|
<dd>{time(props.deadline.reminder_at)}</dd>
|
|
</div>
|
|
)}
|
|
{props.deadline.escalation_at && (
|
|
<div>
|
|
<dt>Escalamento previsto</dt>
|
|
<dd>{time(props.deadline.escalation_at)}</dd>
|
|
</div>
|
|
)}
|
|
</dl>
|
|
<p className="mt-4 text-sm text-muted-foreground">
|
|
Os prazos usam minutos corridos desde a activação da etapa.
|
|
Uma alteração posterior da política não reescreve o ciclo já
|
|
submetido.
|
|
</p>
|
|
<Button
|
|
className="mt-4"
|
|
variant="outline"
|
|
onClick={() => router.reload()}
|
|
>
|
|
Actualizar conferência do prazo
|
|
</Button>
|
|
{props.canCheck && (
|
|
<form
|
|
className="mt-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
check.data.operation_key || crypto.randomUUID();
|
|
check.setData("operation_key", operation);
|
|
check.transform(() => ({
|
|
operation_key: operation,
|
|
process_version: r.process_version,
|
|
}));
|
|
check.post(
|
|
`/requisicoes/${r.id}/prazos/verificar`,
|
|
{
|
|
preserveScroll: true,
|
|
onSuccess: () => check.reset(),
|
|
},
|
|
);
|
|
}}
|
|
>
|
|
<FormErrors errors={check.errors} />
|
|
<Button variant="outline" disabled={check.processing}>
|
|
Verificar e executar acções devidas
|
|
</Button>
|
|
<p className="mt-2 text-sm text-muted-foreground">
|
|
Regista os lembretes e alertas configurados; se o
|
|
prazo terminou, devolve o pedido ao requisitante.
|
|
</p>
|
|
</form>
|
|
)}
|
|
</Panel>
|
|
{props.canExpire && (
|
|
<Panel
|
|
title="Devolver o pedido vencido"
|
|
description="A reserva remanescente deste ciclo é libertada pelo percurso orçamental. As aprovações anteriores ficam no histórico e a nova submissão exige novas etapas."
|
|
>
|
|
<form
|
|
ref={ref}
|
|
className="space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
const operation =
|
|
form.data.operation_key || crypto.randomUUID();
|
|
form.setData("operation_key", operation);
|
|
form.transform((data) => ({
|
|
...data,
|
|
operation_key: operation,
|
|
}));
|
|
form.post(`/requisicoes/${r.id}/expiracao`, {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
form.reset();
|
|
form.clearErrors();
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
{!current && (
|
|
<div className="space-y-2">
|
|
<p>
|
|
A etapa mudou. Reveja a conferência actual
|
|
antes de devolver.
|
|
</p>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
form.setData((data) => ({
|
|
...data,
|
|
process_version: r.process_version,
|
|
basis_hash:
|
|
props.deadline.basis_hash || "",
|
|
operation_key: "",
|
|
}))
|
|
}
|
|
>
|
|
Preparar devolução da etapa actual
|
|
</Button>
|
|
</div>
|
|
)}
|
|
<label className="grid gap-2">
|
|
Comentário adicional, quando necessário
|
|
<textarea
|
|
className={controlClass}
|
|
maxLength={2000}
|
|
value={form.data.comment}
|
|
onChange={(event) =>
|
|
form.setData("comment", event.target.value)
|
|
}
|
|
/>
|
|
</label>
|
|
<Button disabled={form.processing || !current}>
|
|
Devolver ao requisitante
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
{props.canEdit && (
|
|
<Panel title="Pedido devolvido para preparação">
|
|
<p>
|
|
Reveja os dados do rascunho e submeta novamente quando
|
|
estiver preparado. O ciclo anterior permanece no
|
|
histórico.
|
|
</p>
|
|
<Button className="mt-3" asChild>
|
|
<Link href={`/requisicoes/${r.id}/editar`}>
|
|
Rever rascunho
|
|
</Link>
|
|
</Button>
|
|
</Panel>
|
|
)}
|
|
<Panel title="Ciclos anteriores">
|
|
{props.history.data.length === 0 && (
|
|
<p>Ainda não há devoluções por expiração registadas.</p>
|
|
)}
|
|
{props.history.data.map((row) => (
|
|
<article
|
|
key={row.id}
|
|
className="space-y-2 border-b py-4 break-words"
|
|
>
|
|
<h3 className="font-semibold">
|
|
Ciclo {row.data.cycle} · devolvido para o ciclo{" "}
|
|
{row.data.next_cycle}
|
|
</h3>
|
|
<p>{row.reason}</p>
|
|
<ul>
|
|
{row.steps.map((step) => (
|
|
<li key={step.id}>
|
|
Etapa {step.position}:{" "}
|
|
{step.decision === "approved"
|
|
? "Aprovação preservada"
|
|
: "Etapa pendente no momento da devolução"}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</article>
|
|
))}
|
|
<nav className="mt-3 flex gap-4" aria-label="Ciclos anteriores">
|
|
{props.history.prev_page_url && (
|
|
<Link href={props.history.prev_page_url}>Anterior</Link>
|
|
)}
|
|
{props.history.next_page_url && (
|
|
<Link href={props.history.next_page_url}>Seguinte</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
</div>
|
|
);
|
|
}
|