@@ -0,0 +1,416 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import {
|
||||
PageTitle,
|
||||
Panel,
|
||||
FormErrors,
|
||||
controlClass,
|
||||
} from '@/components/procurement';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
|
||||
type Order = {
|
||||
id: number;
|
||||
version: number;
|
||||
created_at: string;
|
||||
reason: string;
|
||||
data: {
|
||||
type: string;
|
||||
reference: string;
|
||||
utilisation_location: string;
|
||||
description: string;
|
||||
department_id: number;
|
||||
};
|
||||
snapshot: {
|
||||
author: { name: string };
|
||||
department: { name: string };
|
||||
policy: { data: { mode: string } };
|
||||
};
|
||||
};
|
||||
type LinkRecord = {
|
||||
id: number;
|
||||
version: number;
|
||||
data: { requisition_item_id: number };
|
||||
snapshot: { source: Order };
|
||||
};
|
||||
type Props = {
|
||||
policy: { id: number; version: number; data: { mode: string } } | null;
|
||||
canConfigure: boolean;
|
||||
allowedTypes: string[];
|
||||
departments: { id: number; name: string }[];
|
||||
orders: {
|
||||
data: Order[];
|
||||
prev_page_url: string | null;
|
||||
next_page_url: string | null;
|
||||
};
|
||||
requisition: { id: number; number: string; status: string } | null;
|
||||
items: { id: number; description: string }[];
|
||||
links: LinkRecord[];
|
||||
canLink: boolean;
|
||||
};
|
||||
const labels: Record<string, string> = {
|
||||
production: 'Produção',
|
||||
maintenance: 'Manutenção',
|
||||
};
|
||||
type Field = {
|
||||
name: string;
|
||||
label: string;
|
||||
choices?: { value: string; label: string }[];
|
||||
long?: boolean;
|
||||
};
|
||||
|
||||
export default function OperationalOrders(props: Props) {
|
||||
const identity = useProcurementPreparationIdentity(
|
||||
`operational-orders:${props.policy?.id ?? 0}:${props.policy?.version ?? 0}:${props.allowedTypes.join(',')}:${props.canConfigure}:${props.requisition?.id ?? 0}:${props.canLink}`,
|
||||
);
|
||||
return <Workspace key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function Workspace(props: Props & { identity: string }) {
|
||||
const base = '/ordens-operacionais';
|
||||
return (
|
||||
<>
|
||||
<Head title="Ordens de produção e manutenção" />
|
||||
<div className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-5 lg:p-8">
|
||||
<PageTitle
|
||||
eyebrow="Compras"
|
||||
title="Ordens de produção e manutenção"
|
||||
description="Registe a necessidade no local de utilização ou através do procurement, conforme a configuração da empresa. A ordem de origem acompanha a compra."
|
||||
/>
|
||||
{props.requisition && (
|
||||
<p>
|
||||
<Link
|
||||
className="underline"
|
||||
href={`/requisicoes/${props.requisition.id}`}
|
||||
>
|
||||
Voltar à {props.requisition.number}
|
||||
</Link>{' '}
|
||||
·{' '}
|
||||
<Link className="underline" href={base}>
|
||||
Cadastro das ordens
|
||||
</Link>
|
||||
</p>
|
||||
)}
|
||||
<Panel
|
||||
title="Origem das ordens"
|
||||
description="A configuração altera quem pode registar novas ordens. Preserva as ordens existentes e não atribui competências a pessoas."
|
||||
>
|
||||
<p className="mb-4 text-sm">
|
||||
{!props.policy
|
||||
? 'A empresa ainda não definiu a origem das ordens.'
|
||||
: props.policy.data.mode === 'utilisation'
|
||||
? 'Nos locais de utilização: responsável da produção ou chefe de oficina, com competência atribuída pela empresa.'
|
||||
: 'No procurement: o agente regista as ordens para os locais ainda não abrangidos pela aplicação.'}
|
||||
</p>
|
||||
{props.canConfigure && (
|
||||
<DraftForm
|
||||
identity={`${props.identity}:policy`}
|
||||
url={`${base}/configuracao`}
|
||||
initial={{
|
||||
version: String(props.policy?.version ?? 0),
|
||||
mode: props.policy?.data.mode ?? '',
|
||||
reason: '',
|
||||
}}
|
||||
fields={[
|
||||
{
|
||||
name: 'mode',
|
||||
label: 'Modo de registo',
|
||||
choices: [
|
||||
{
|
||||
value: 'utilisation',
|
||||
label: 'Nos locais de utilização',
|
||||
},
|
||||
{
|
||||
value: 'procurement',
|
||||
label: 'Pelo agente de procurement',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'reason',
|
||||
label: 'Fundamento da configuração',
|
||||
long: true,
|
||||
},
|
||||
]}
|
||||
submit="Guardar origem das novas ordens"
|
||||
/>
|
||||
)}
|
||||
</Panel>
|
||||
{props.allowedTypes.length > 0 && (
|
||||
<Panel
|
||||
title="Registar ordem"
|
||||
description="O autor e o modo de origem são registados automaticamente. O documento original permanece no histórico."
|
||||
>
|
||||
<DraftForm
|
||||
identity={`${props.identity}:new`}
|
||||
url={base}
|
||||
initial={{
|
||||
type:
|
||||
props.allowedTypes.length === 1
|
||||
? props.allowedTypes[0]
|
||||
: '',
|
||||
reference: '',
|
||||
department_id:
|
||||
props.departments.length === 1
|
||||
? String(props.departments[0].id)
|
||||
: '',
|
||||
utilisation_location: '',
|
||||
description: '',
|
||||
reason: '',
|
||||
policy_version: String(
|
||||
props.policy?.version ?? 0,
|
||||
),
|
||||
}}
|
||||
fields={[
|
||||
{
|
||||
name: 'type',
|
||||
label: 'Tipo de ordem',
|
||||
choices: props.allowedTypes.map((type) => ({
|
||||
value: type,
|
||||
label: labels[type],
|
||||
})),
|
||||
},
|
||||
{
|
||||
name: 'reference',
|
||||
label: 'Referência da ordem',
|
||||
},
|
||||
{
|
||||
name: 'department_id',
|
||||
label: 'Departamento de utilização',
|
||||
choices: props.departments.map(
|
||||
(department) => ({
|
||||
value: String(department.id),
|
||||
label: department.name,
|
||||
}),
|
||||
),
|
||||
},
|
||||
{
|
||||
name: 'utilisation_location',
|
||||
label: 'Local de utilização',
|
||||
},
|
||||
{
|
||||
name: 'description',
|
||||
label: 'Trabalho ou produção a executar',
|
||||
long: true,
|
||||
},
|
||||
{
|
||||
name: 'reason',
|
||||
label: 'Fundamento do registo',
|
||||
long: true,
|
||||
},
|
||||
]}
|
||||
submit="Registar ordem"
|
||||
/>
|
||||
</Panel>
|
||||
)}
|
||||
<Panel
|
||||
title="Ordens registadas"
|
||||
description="A lista respeita a empresa e o departamento aplicável. Uma ligação utiliza a versão original seleccionada."
|
||||
>
|
||||
{props.orders.data.length === 0 && (
|
||||
<p>Não há ordens disponíveis neste âmbito.</p>
|
||||
)}
|
||||
<ul className="space-y-4">
|
||||
{props.orders.data.map((order) => (
|
||||
<li className="rounded border p-4" key={order.id}>
|
||||
<p className="font-semibold">
|
||||
{labels[order.data.type]} ·{' '}
|
||||
{order.data.reference}
|
||||
</p>
|
||||
<p className="text-sm">
|
||||
{order.snapshot.department.name} ·{' '}
|
||||
{order.data.utilisation_location} ·{' '}
|
||||
{order.snapshot.author.name}
|
||||
</p>
|
||||
<p className="mt-2 text-sm whitespace-pre-wrap">
|
||||
{order.data.description}
|
||||
</p>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div className="mt-4 flex gap-4">
|
||||
{props.orders.prev_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
href={props.orders.prev_page_url}
|
||||
>
|
||||
Anterior
|
||||
</Link>
|
||||
)}
|
||||
{props.orders.next_page_url && (
|
||||
<Link
|
||||
preserveState
|
||||
preserveScroll
|
||||
href={props.orders.next_page_url}
|
||||
>
|
||||
Seguinte
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</Panel>
|
||||
{props.requisition &&
|
||||
props.items.map((item) => {
|
||||
const link = props.links.find(
|
||||
(link) => link.data.requisition_item_id === item.id,
|
||||
);
|
||||
return (
|
||||
<Panel
|
||||
key={item.id}
|
||||
title={item.description}
|
||||
description={
|
||||
link
|
||||
? `Ordem ligada: ${link.snapshot.source.data.reference} · ${labels[link.snapshot.source.data.type]}`
|
||||
: 'Sem ordem de origem ligada.'
|
||||
}
|
||||
>
|
||||
{props.canLink && (
|
||||
<DraftForm
|
||||
key={`${item.id}:${link?.version ?? 0}`}
|
||||
identity={`${props.identity}:link:${item.id}:${link?.version ?? 0}`}
|
||||
url={`/requisicoes/${props.requisition?.id}/ordens-operacionais`}
|
||||
initial={{
|
||||
item_id: String(item.id),
|
||||
order_id: '',
|
||||
version: String(link?.version ?? 0),
|
||||
reason: '',
|
||||
}}
|
||||
fields={[
|
||||
{
|
||||
name: 'order_id',
|
||||
label: 'Ordem de origem',
|
||||
choices: props.orders.data.map(
|
||||
(order) => ({
|
||||
value: String(order.id),
|
||||
label: `${labels[order.data.type]} · ${order.data.reference} · ${order.data.utilisation_location}`,
|
||||
}),
|
||||
),
|
||||
},
|
||||
{
|
||||
name: 'reason',
|
||||
label: 'Fundamento da ligação',
|
||||
long: true,
|
||||
},
|
||||
]}
|
||||
submit="Ligar ordem à linha"
|
||||
/>
|
||||
)}
|
||||
</Panel>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
function DraftForm({
|
||||
identity,
|
||||
url,
|
||||
initial,
|
||||
fields,
|
||||
submit,
|
||||
}: {
|
||||
identity: string;
|
||||
url: string;
|
||||
initial: Record<string, string>;
|
||||
fields: Field[];
|
||||
submit: string;
|
||||
}) {
|
||||
const form = useForm<Record<string, string>>(`${identity}:form`, {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
});
|
||||
const dirty = hasPreparationChanges(
|
||||
{ ...initial, operation_key: '' },
|
||||
{ ...form.data, operation_key: '' },
|
||||
);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-4"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(url, {
|
||||
preserveScroll: true,
|
||||
preserveState: true,
|
||||
onSuccess: () => {
|
||||
const cleared = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(cleared);
|
||||
form.clearErrors();
|
||||
router.remember(cleared, `${identity}:form:data`);
|
||||
router.remember({}, `${identity}:form:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
{fields.map((field) => (
|
||||
<label key={field.name} className="block space-y-1 text-sm">
|
||||
{field.label}
|
||||
{field.choices ? (
|
||||
<select
|
||||
className={controlClass}
|
||||
required
|
||||
value={form.data[field.name] ?? ''}
|
||||
onChange={(event) =>
|
||||
form.setData(field.name, event.target.value)
|
||||
}
|
||||
>
|
||||
<option value="">Seleccione</option>
|
||||
{form.data[field.name] &&
|
||||
!field.choices.some(
|
||||
(choice) =>
|
||||
choice.value === form.data[field.name],
|
||||
) && (
|
||||
<option value={form.data[field.name]}>
|
||||
Selecção preservada de outra página —
|
||||
confira a ordem antes de submeter
|
||||
</option>
|
||||
)}
|
||||
{field.choices.map((choice) => (
|
||||
<option key={choice.value} value={choice.value}>
|
||||
{choice.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
) : field.long ? (
|
||||
<textarea
|
||||
className={`${controlClass} h-24 py-3`}
|
||||
required
|
||||
minLength={10}
|
||||
maxLength={4000}
|
||||
value={form.data[field.name] ?? ''}
|
||||
onChange={(event) =>
|
||||
form.setData(field.name, event.target.value)
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<input
|
||||
className={controlClass}
|
||||
required
|
||||
minLength={3}
|
||||
maxLength={field.name === 'reference' ? 120 : 300}
|
||||
value={form.data[field.name] ?? ''}
|
||||
onChange={(event) =>
|
||||
form.setData(field.name, event.target.value)
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</label>
|
||||
))}
|
||||
<Button disabled={form.processing}>{submit}</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
OperationalOrders.layout = {
|
||||
breadcrumbs: [
|
||||
{
|
||||
title: 'Ordens de produção e manutenção',
|
||||
href: '/ordens-operacionais',
|
||||
},
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user