307 lines
13 KiB
TypeScript
307 lines
13 KiB
TypeScript
import { Head, Link, useForm } from '@inertiajs/react';
|
|
import { useEffect, useRef } from 'react';
|
|
import {
|
|
FormErrors,
|
|
PageTitle,
|
|
Panel,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
type Source = {
|
|
id: number;
|
|
version: number;
|
|
data: { code: string; name: string; type: string; reference: string };
|
|
};
|
|
type Assignment = {
|
|
id: number;
|
|
version: number;
|
|
state: string;
|
|
reason: string;
|
|
data: { requisition_item_id: number };
|
|
snapshot: { item: { description: string }; source: Source | null };
|
|
};
|
|
type Page<T> = {
|
|
data: T[];
|
|
prev_page_url: string | null;
|
|
next_page_url: string | null;
|
|
};
|
|
type Props = {
|
|
requisition: { id: number; number: string; status: string };
|
|
items: { id: number; description: string }[];
|
|
current: Assignment[];
|
|
history: Page<Assignment>;
|
|
choices: Page<Source>;
|
|
canWrite: boolean;
|
|
actorId: number;
|
|
tenantId: number;
|
|
};
|
|
export default function CapitalAssignments(props: Props) {
|
|
const url = `/requisicoes/${props.requisition.id}/projectos-imobilizado`;
|
|
const form = useForm(
|
|
`capital-assignment:${props.tenantId}:${props.actorId}:${props.requisition.id}`,
|
|
{
|
|
operation_key: '',
|
|
item_id: '',
|
|
version: 0,
|
|
capital_id: '',
|
|
capital_label: '',
|
|
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 chooseItem = (id: string) => {
|
|
const old = props.current.find(
|
|
(row) => String(row.data.requisition_item_id) === id,
|
|
);
|
|
const source = old?.snapshot.source;
|
|
form.setData({
|
|
operation_key: '',
|
|
item_id: id,
|
|
version: old?.version ?? 0,
|
|
capital_id: source ? String(source.id) : '',
|
|
capital_label: source
|
|
? `${source.data.code} · ${source.data.name} · v${source.version}`
|
|
: '',
|
|
reason: '',
|
|
});
|
|
};
|
|
return (
|
|
<div className="mx-auto grid w-full max-w-6xl min-w-0 gap-6 p-5 lg:p-8">
|
|
<Head title="Destino por projecto ou imobilizado" />
|
|
<PageTitle
|
|
eyebrow={props.requisition.number}
|
|
title="Projecto ou imobilizado por artigo"
|
|
description="Seleccione a referência do cadastro próprio de Compras. A ligação preserva a versão escolhida e acompanha as novas ordens."
|
|
/>
|
|
<Link
|
|
className="underline"
|
|
href={`/requisicoes/${props.requisition.id}`}
|
|
>
|
|
Voltar à requisição
|
|
</Link>
|
|
<Panel title="Ligações actuais">
|
|
{props.current.length === 0 && (
|
|
<p>
|
|
Ainda não há ligações registadas. As origens anteriores
|
|
permanecem preservadas.
|
|
</p>
|
|
)}
|
|
{props.current.map((row) => (
|
|
<article
|
|
key={row.id}
|
|
className="space-y-2 border-b py-4 break-words"
|
|
>
|
|
<h3 className="font-semibold">
|
|
{row.snapshot.item.description} · versão{' '}
|
|
{row.version}
|
|
</h3>
|
|
<p>
|
|
{row.state === 'cleared'
|
|
? 'Ligação retirada explicitamente'
|
|
: `${row.snapshot.source?.data.code} · ${row.snapshot.source?.data.name} · versão ${row.snapshot.source?.version}`}
|
|
</p>
|
|
<p>{row.reason}</p>
|
|
</article>
|
|
))}
|
|
</Panel>
|
|
{props.canWrite && (
|
|
<Panel
|
|
title="Preparar ligação"
|
|
description="A escolha é explícita. As distribuições e decisões orçamentais conservam o seu percurso próprio."
|
|
>
|
|
<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) => ({
|
|
operation_key: operation,
|
|
item_id: data.item_id,
|
|
version: data.version,
|
|
capital_id: data.capital_id || null,
|
|
reason: data.reason,
|
|
}));
|
|
form.post(url, {
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
form.reset();
|
|
form.clearErrors();
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={form.errors} />
|
|
<label className="grid gap-2">
|
|
Artigo
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={form.data.item_id}
|
|
onChange={(event) =>
|
|
chooseItem(event.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccione</option>
|
|
{props.items.map((item) => (
|
|
<option key={item.id} value={item.id}>
|
|
{item.description}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<p>
|
|
Referência seleccionada:{' '}
|
|
{form.data.capital_label || 'Sem ligação'} · versão
|
|
da preparação {form.data.version}
|
|
</p>
|
|
<p className="text-sm text-muted-foreground">
|
|
Reveja o artigo actual antes de guardar. Uma
|
|
referência arquivada conserva-se no histórico e nas
|
|
escolhas já aceites; para uma nova ligação, escolha
|
|
uma versão activa.
|
|
</p>
|
|
{props.choices.data.map((source) => (
|
|
<article
|
|
key={source.id}
|
|
className="min-w-0 space-y-2 border-b py-3 break-words"
|
|
>
|
|
<p>
|
|
{source.data.type === 'project'
|
|
? 'Projecto'
|
|
: 'Imobilizado'}{' '}
|
|
· {source.data.code} · {source.data.name} ·
|
|
versão {source.version}
|
|
</p>
|
|
<p>{source.data.reference}</p>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={!form.data.item_id}
|
|
onClick={() =>
|
|
form.setData((data) => ({
|
|
...data,
|
|
capital_id: String(source.id),
|
|
capital_label: `${source.data.code} · ${source.data.name} · v${source.version}`,
|
|
operation_key: '',
|
|
}))
|
|
}
|
|
>
|
|
Escolher {source.data.code}
|
|
</Button>
|
|
</article>
|
|
))}
|
|
{props.choices.data.length === 0 && (
|
|
<p>
|
|
Não há referências activas no departamento desta
|
|
requisição.
|
|
</p>
|
|
)}
|
|
<nav
|
|
className="flex gap-4"
|
|
aria-label="Referências do cadastro"
|
|
>
|
|
{props.choices.prev_page_url && (
|
|
<Link
|
|
preserveState
|
|
href={props.choices.prev_page_url}
|
|
>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{props.choices.next_page_url && (
|
|
<Link
|
|
preserveState
|
|
href={props.choices.next_page_url}
|
|
>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={!form.data.item_id}
|
|
onClick={() =>
|
|
form.setData((data) => ({
|
|
...data,
|
|
capital_id: '',
|
|
capital_label: '',
|
|
operation_key: '',
|
|
}))
|
|
}
|
|
>
|
|
Retirar ligação desta preparação
|
|
</Button>
|
|
<label className="grid gap-2">
|
|
Motivo
|
|
<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 || !form.data.item_id}
|
|
>
|
|
Guardar ligação
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
)}
|
|
<Panel title="Histórico preservado">
|
|
{props.history.data.map((row) => (
|
|
<article
|
|
key={row.id}
|
|
className="space-y-2 border-b py-3 break-words"
|
|
>
|
|
<p>
|
|
{row.snapshot.item.description} · versão{' '}
|
|
{row.version} ·{' '}
|
|
{row.snapshot.source
|
|
? `${row.snapshot.source.data.code} · v${row.snapshot.source.version}`
|
|
: 'Sem ligação'}
|
|
</p>
|
|
<p>{row.reason}</p>
|
|
</article>
|
|
))}
|
|
<nav className="flex gap-4" aria-label="Histórico das ligações">
|
|
{props.history.prev_page_url && (
|
|
<Link preserveState href={props.history.prev_page_url}>
|
|
Anterior
|
|
</Link>
|
|
)}
|
|
{props.history.next_page_url && (
|
|
<Link preserveState href={props.history.next_page_url}>
|
|
Seguinte
|
|
</Link>
|
|
)}
|
|
</nav>
|
|
</Panel>
|
|
</div>
|
|
);
|
|
}
|