499 lines
22 KiB
TypeScript
499 lines
22 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, money, priorityLabels, roleLabels } from '@/types/procurement';
|
|
|
|
type Deadline = {
|
|
timing?: string;
|
|
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;
|
|
created_at?: string | null;
|
|
approval_amount_minor?: number | null;
|
|
approval_uses_separate_basis?: boolean;
|
|
request: {
|
|
number: string;
|
|
priority?: string;
|
|
justification?: string;
|
|
needed_by?: string | null;
|
|
submitted_at?: string | null;
|
|
subtotal_minor?: number | null;
|
|
tax_minor?: number | null;
|
|
total_minor?: number | null;
|
|
};
|
|
rules?: {
|
|
first_limit_minor?: number | null;
|
|
second_limit_minor?: number | null;
|
|
approval_includes_tax?: boolean;
|
|
rules_provisional?: boolean;
|
|
routing_plan_revision?: number;
|
|
};
|
|
items?:
|
|
| {
|
|
id: number;
|
|
description: string;
|
|
quantity: string;
|
|
unit: string;
|
|
unit_price_minor?: number | null;
|
|
tax_rate?: string | null;
|
|
subtotal_minor?: number | null;
|
|
tax_minor?: number | null;
|
|
total_minor?: number | null;
|
|
}[]
|
|
| null;
|
|
steps: {
|
|
id: number;
|
|
position: number;
|
|
role: string;
|
|
decision: string;
|
|
approver_id?: number | null;
|
|
decided_at?: string | null;
|
|
comment?: string | null;
|
|
routing?: { name?: string } | null;
|
|
}[];
|
|
}[];
|
|
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';
|
|
const pastTime = (value?: string | null) =>
|
|
value ? time(value) : 'Não preservada';
|
|
const historicalDecisions: Record<string, string> = {
|
|
approved: 'Aprovação preservada',
|
|
pending: 'Etapa pendente no momento da devolução',
|
|
rejected: 'Rejeição preservada',
|
|
expired: 'Etapa expirada',
|
|
};
|
|
const preservedMoney = (value?: number | null) =>
|
|
value == null ? 'Não preservado' : money(value);
|
|
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}`}
|
|
>
|
|
{props.deadline.timing && (
|
|
<p className="mb-4 text-sm">
|
|
{props.deadline.timing === 'urgent_override'
|
|
? 'Prazos reduzidos para urgência, preservados na submissão.'
|
|
: 'Prazos gerais da etapa, preservados na submissão.'}
|
|
</p>
|
|
)}
|
|
<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>
|
|
<p className="text-sm text-muted-foreground">
|
|
Documento preservado na devolução. As decisões e os
|
|
valores deste ciclo não são as aprovações da nova
|
|
submissão.
|
|
</p>
|
|
<dl className="grid gap-2 text-sm sm:grid-cols-2">
|
|
<div>
|
|
<dt className="text-muted-foreground">
|
|
Submissão original
|
|
</dt>
|
|
<dd>{pastTime(row.request.submitted_at)}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">
|
|
Devolvido em
|
|
</dt>
|
|
<dd>{pastTime(row.created_at)}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">
|
|
Prioridade original
|
|
</dt>
|
|
<dd>
|
|
{row.request.priority
|
|
? (priorityLabels[
|
|
row.request.priority
|
|
] ?? row.request.priority)
|
|
: 'Não preservada'}
|
|
</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">
|
|
Data de necessidade original
|
|
</dt>
|
|
<dd>
|
|
{row.request.needed_by
|
|
? date(row.request.needed_by)
|
|
: 'Não preservada'}
|
|
</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">
|
|
Sem IVA
|
|
</dt>
|
|
<dd>
|
|
{preservedMoney(row.request.subtotal_minor)}
|
|
</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">IVA</dt>
|
|
<dd>{preservedMoney(row.request.tax_minor)}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-muted-foreground">
|
|
Total original (MZN)
|
|
</dt>
|
|
<dd>
|
|
{preservedMoney(row.request.total_minor)}
|
|
</dd>
|
|
</div>
|
|
</dl>
|
|
{row.request.justification && (
|
|
<p className="whitespace-pre-wrap">
|
|
{row.request.justification}
|
|
</p>
|
|
)}
|
|
<details className="rounded border p-3">
|
|
<summary>Artigos e regras preservados</summary>
|
|
<ul className="mt-3 space-y-3">
|
|
{row.items?.map((item) => (
|
|
<li key={item.id} className="break-words">
|
|
<p>
|
|
{item.description} · {item.quantity}{' '}
|
|
{item.unit}
|
|
</p>
|
|
<p className="text-sm">
|
|
Preço unitário:{' '}
|
|
{preservedMoney(
|
|
item.unit_price_minor,
|
|
)}{' '}
|
|
· Taxa de IVA declarada:{' '}
|
|
{item.tax_rate == null
|
|
? 'Não preservada'
|
|
: `${item.tax_rate}%`}
|
|
</p>
|
|
<p className="text-sm">
|
|
Sem IVA:{' '}
|
|
{preservedMoney(
|
|
item.subtotal_minor,
|
|
)}{' '}
|
|
· IVA:{' '}
|
|
{preservedMoney(item.tax_minor)} ·
|
|
Total:{' '}
|
|
{preservedMoney(item.total_minor)}
|
|
</p>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
{!row.items?.length && (
|
|
<p>Artigos não preservados neste registo.</p>
|
|
)}
|
|
<p className="mt-3 text-sm">
|
|
Base de aprovação:{' '}
|
|
{row.rules?.approval_includes_tax === true
|
|
? 'com IVA'
|
|
: row.rules?.approval_includes_tax === false
|
|
? 'sem IVA'
|
|
: 'Não preservada'}
|
|
.
|
|
</p>
|
|
<p className="text-sm">
|
|
Valor usado no encaminhamento:{' '}
|
|
{preservedMoney(row.approval_amount_minor)}.
|
|
</p>
|
|
{row.approval_uses_separate_basis && (
|
|
<p className="text-sm">
|
|
Base adicional preservada na submissão
|
|
original.
|
|
</p>
|
|
)}
|
|
{row.rules?.routing_plan_revision != null && (
|
|
<p className="text-sm">
|
|
Plano de encaminhamento · versão{' '}
|
|
{row.rules.routing_plan_revision}
|
|
</p>
|
|
)}
|
|
<p className="text-sm">
|
|
Limites preservados:{' '}
|
|
{preservedMoney(row.rules?.first_limit_minor)} /{' '}
|
|
{preservedMoney(row.rules?.second_limit_minor)}.
|
|
</p>
|
|
{row.rules?.rules_provisional === true && (
|
|
<p className="text-sm">
|
|
Regras provisórias na submissão original.
|
|
</p>
|
|
)}
|
|
</details>
|
|
<ul>
|
|
{row.steps.map((step) => (
|
|
<li key={step.id} className="my-3">
|
|
Etapa {step.position}:{' '}
|
|
{step.routing?.name ??
|
|
roleLabels[step.role] ??
|
|
step.role}{' '}
|
|
·{' '}
|
|
{historicalDecisions[step.decision] ??
|
|
'Estado não preservado'}
|
|
{step.approver_id != null && (
|
|
<span>
|
|
{' '}
|
|
· Utilizador #{step.approver_id}
|
|
</span>
|
|
)}
|
|
{step.decided_at && (
|
|
<span> · {time(step.decided_at)}</span>
|
|
)}
|
|
{step.comment && (
|
|
<p className="mt-1 whitespace-pre-wrap">
|
|
{step.comment}
|
|
</p>
|
|
)}
|
|
</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>
|
|
);
|
|
}
|