@@ -0,0 +1,197 @@
|
||||
import { useState } from 'react';
|
||||
import { useForm } from '@inertiajs/react';
|
||||
import { Send } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog';
|
||||
import { exactMinorAmount } from '@/components/procurement-payable-insights';
|
||||
import { roleLabels } from '@/types/procurement';
|
||||
import { FormErrors } from '@/components/procurement';
|
||||
|
||||
export type SubmissionPreview =
|
||||
| {
|
||||
state: 'planned';
|
||||
process_version: number | null;
|
||||
subtotal_minor: string;
|
||||
total_minor: string;
|
||||
approval_amount_minor: string;
|
||||
includes_tax: boolean;
|
||||
provisional_rules: boolean;
|
||||
import_basis: boolean;
|
||||
steps: { role: string; label: string | null }[];
|
||||
}
|
||||
| { state: 'unavailable' | 'incomplete'; message: string };
|
||||
|
||||
export function SubmissionPreviewDetails({
|
||||
number,
|
||||
preview,
|
||||
}: {
|
||||
number: string;
|
||||
preview: SubmissionPreview | null;
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-4 text-sm">
|
||||
<p className="font-medium">{number}</p>
|
||||
{preview?.state === 'planned' ? (
|
||||
<>
|
||||
<dl className="grid gap-2">
|
||||
<div>
|
||||
<dt>Total do pedido com IVA</dt>
|
||||
<dd className="font-semibold">
|
||||
{exactMinorAmount(preview.total_minor, 'MZN')}
|
||||
</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>
|
||||
Base prevista para aprovação{' '}
|
||||
{preview.includes_tax ? 'com IVA' : 'sem IVA'}
|
||||
</dt>
|
||||
<dd>
|
||||
{exactMinorAmount(
|
||||
preview.approval_amount_minor,
|
||||
'MZN',
|
||||
)}
|
||||
</dd>
|
||||
</div>
|
||||
</dl>
|
||||
{preview.import_basis && (
|
||||
<p>
|
||||
A base de aprovação inclui a estimativa de
|
||||
importação preservada no pedido.
|
||||
</p>
|
||||
)}
|
||||
{preview.provisional_rules && (
|
||||
<p>
|
||||
As regras da empresa estão assinaladas como
|
||||
provisórias.
|
||||
</p>
|
||||
)}
|
||||
<div>
|
||||
<p className="font-medium">Percurso previsto</p>
|
||||
<ol className="list-inside list-decimal">
|
||||
{preview.steps?.map((step) => (
|
||||
<li key={step.role}>
|
||||
{step.label ||
|
||||
roleLabels[step.role] ||
|
||||
'Perfil definido pela empresa'}
|
||||
</li>
|
||||
))}
|
||||
</ol>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<p role="status">
|
||||
{preview?.message ||
|
||||
'O percurso não está disponível nesta apresentação. O servidor verificará os requisitos ao submeter.'}
|
||||
</p>
|
||||
)}
|
||||
<p>
|
||||
Ao confirmar, envia este pedido para aprovação. O servidor volta
|
||||
a verificar os dados, a versão, os acessos e o orçamento; esta
|
||||
conferência não cria reservas nem aprovações.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Parent keys this component by request/version, so a new cycle cannot reuse an old command. */
|
||||
export function RequisitionSubmissionAction({
|
||||
id,
|
||||
number,
|
||||
version,
|
||||
ready,
|
||||
preview,
|
||||
}: {
|
||||
id: number;
|
||||
number: string;
|
||||
version: number | null;
|
||||
ready: boolean;
|
||||
preview: SubmissionPreview | null;
|
||||
}) {
|
||||
const form = useForm({
|
||||
operation_key: ready ? crypto.randomUUID() : null,
|
||||
process_version: version,
|
||||
});
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<FormErrors errors={form.errors} />
|
||||
<RequisitionSubmissionConfirmation
|
||||
number={number}
|
||||
preview={preview}
|
||||
processing={form.processing}
|
||||
errors={Object.values(form.errors)}
|
||||
onConfirm={(close) =>
|
||||
form.post(`/requisicoes/${id}/submeter`, {
|
||||
onSuccess: close,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function RequisitionSubmissionConfirmation({
|
||||
number,
|
||||
preview,
|
||||
processing,
|
||||
errors,
|
||||
onConfirm,
|
||||
}: {
|
||||
number: string;
|
||||
preview: SubmissionPreview | null;
|
||||
processing: boolean;
|
||||
errors: string[];
|
||||
onConfirm: (close: () => void) => void;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button disabled={processing}>
|
||||
<Send className="size-4" />
|
||||
Submeter para aprovação
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-h-[90dvh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Conferir submissão</DialogTitle>
|
||||
<DialogDescription>
|
||||
Reveja o total e o percurso previsto antes de confirmar.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<SubmissionPreviewDetails number={number} preview={preview} />
|
||||
{errors.length > 0 && (
|
||||
<ul
|
||||
role="alert"
|
||||
className="space-y-1 text-sm text-destructive"
|
||||
>
|
||||
{errors.map((error, index) => (
|
||||
<li key={index}>{error}</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setOpen(false)}>
|
||||
Voltar ao pedido
|
||||
</Button>
|
||||
<Button
|
||||
disabled={
|
||||
processing ||
|
||||
(preview !== null && preview.state !== 'planned')
|
||||
}
|
||||
onClick={() => onConfirm(() => setOpen(false))}
|
||||
>
|
||||
{processing ? 'A submeter…' : 'Confirmar e submeter'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user