1065 lines
45 KiB
TypeScript
1065 lines
45 KiB
TypeScript
import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity';
|
|
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
|
import { hasPreparationChanges } from '@/lib/requisition-history';
|
|
import { AdministrativeLocation } from '@/components/administrative-location';
|
|
import type {
|
|
GeographyDescription,
|
|
GeographyReference,
|
|
} from '@/components/administrative-location';
|
|
import { TerritorialFilter } from '@/components/territorial-filter';
|
|
import { Head, useForm, Link, router, useRemember } from '@inertiajs/react';
|
|
import { useState } from 'react';
|
|
import { InventoryNotice, InventoryPagination } from '@/components/inventory';
|
|
import {
|
|
PageTitle,
|
|
Panel,
|
|
FormErrors,
|
|
controlClass,
|
|
} from '@/components/procurement';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { quantity } from '@/types/inventory';
|
|
import type { StockItem, Page } from '@/types/inventory';
|
|
|
|
type Level = {
|
|
id: number;
|
|
code: string;
|
|
name: string;
|
|
criteria: string;
|
|
is_critical: boolean;
|
|
};
|
|
type Asset = {
|
|
geography_reference?: GeographyReference | null;
|
|
id: number;
|
|
code: string;
|
|
description: string;
|
|
area: string;
|
|
serial_number: string | null;
|
|
status: string;
|
|
department_id: number;
|
|
stock_item_id: number | null;
|
|
criticality_level_id: number;
|
|
version: number;
|
|
};
|
|
type Part = {
|
|
id: number;
|
|
stock_item_id: number;
|
|
criticality_level_id: number;
|
|
minimum_milli: number;
|
|
balance_milli: number;
|
|
below_minimum: boolean;
|
|
version: number;
|
|
};
|
|
type History = {
|
|
id: number;
|
|
reference: string;
|
|
quantity_milli: number;
|
|
snapshot: {
|
|
equipment?: { code: string; description: string };
|
|
stock_item: { name: string; unit: string };
|
|
reason?: string;
|
|
reversal?: {
|
|
source: {
|
|
document: { reference: string };
|
|
rows: { warehouse_name: string; quantity_milli: number }[];
|
|
};
|
|
};
|
|
};
|
|
};
|
|
type Props = {
|
|
capitalRegisterReady?: boolean;
|
|
geographyEnabled?: boolean;
|
|
equipmentGeography?: Record<number, GeographyDescription>;
|
|
geographyFilter?: GeographyDescription;
|
|
equipment: Asset[];
|
|
parts: Part[];
|
|
levels: Level[];
|
|
links: { equipment_id: number; spare_part_id: number }[];
|
|
items: StockItem[];
|
|
departments: { id: number; name: string }[];
|
|
history: Page<History>;
|
|
canManage: boolean;
|
|
canConfigure: boolean;
|
|
demo?: boolean;
|
|
};
|
|
function Field({
|
|
label,
|
|
children,
|
|
}: {
|
|
label: string;
|
|
children: React.ReactNode;
|
|
}) {
|
|
return (
|
|
<label className="grid gap-2 text-sm">
|
|
{label}
|
|
{children}
|
|
</label>
|
|
);
|
|
}
|
|
const statuses: Record<string, string> = {
|
|
active: 'Em serviço',
|
|
stopped: 'Parado',
|
|
retired: 'Retirado',
|
|
};
|
|
export default function EquipmentPage(props: Props) {
|
|
if (props.demo)
|
|
return <EquipmentFields {...props} identity="equipment:demo" />;
|
|
return <PrivateEquipment {...props} />;
|
|
}
|
|
function PrivateEquipment(props: Props) {
|
|
const identity = useInventoryPreparationIdentity(
|
|
`/equipamentos:${props.canManage}:${props.canConfigure}:${props.geographyEnabled ?? false}`,
|
|
);
|
|
return <EquipmentFields key={identity} {...props} identity={identity} />;
|
|
}
|
|
function EquipmentFields({
|
|
equipment,
|
|
parts,
|
|
levels,
|
|
links,
|
|
items,
|
|
departments,
|
|
history,
|
|
canManage,
|
|
canConfigure,
|
|
capitalRegisterReady = false,
|
|
demo = false,
|
|
geographyEnabled = false,
|
|
equipmentGeography = {},
|
|
geographyFilter = null,
|
|
identity,
|
|
}: Props & { identity: string }) {
|
|
const [message, setMessage] = useState('');
|
|
const initialCategory = {
|
|
code: '',
|
|
name: '',
|
|
criteria: '',
|
|
is_critical: '',
|
|
};
|
|
const category = useForm(`${identity}:category`, initialCategory);
|
|
const categoryDirty = hasPreparationChanges(initialCategory, category.data);
|
|
const categoryRef = usePreparationDirty(
|
|
categoryDirty,
|
|
!demo && canConfigure,
|
|
);
|
|
const [selectedAssetId, setSelectedAssetId] = useRemember(
|
|
'',
|
|
`${identity}:selected-asset`,
|
|
);
|
|
const [selectedPartId, setSelectedPartId] = useRemember(
|
|
'',
|
|
`${identity}:selected-part`,
|
|
);
|
|
const selectedAsset = equipment.find(
|
|
(row) => String(row.id) === selectedAssetId,
|
|
);
|
|
const selectedPart = parts.find((row) => String(row.id) === selectedPartId);
|
|
const assetIdentity = `${identity}:asset:${selectedAssetId || 'new'}:${selectedAsset?.version ?? 0}`;
|
|
const partIdentity = `${identity}:part:${selectedPartId || 'new'}:${selectedPart?.version ?? 0}`;
|
|
const initialAssociation = { equipment_id: '', spare_part_id: '' };
|
|
const association = useForm(`${identity}:association`, initialAssociation);
|
|
const associationDirty = hasPreparationChanges(
|
|
initialAssociation,
|
|
association.data,
|
|
);
|
|
const associationRef = usePreparationDirty(
|
|
associationDirty,
|
|
!demo && canManage,
|
|
);
|
|
const stock = (id: number) => items.find((i) => i.id === id);
|
|
const criticality = (id: number) => levels.find((l) => l.id === id);
|
|
function simulate() {
|
|
setMessage(
|
|
'Simulação revista. Nenhum cadastro ou movimento real foi criado.',
|
|
);
|
|
}
|
|
return (
|
|
<>
|
|
<Head title="Equipamentos e peças" />
|
|
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 p-5 lg:p-8">
|
|
<PageTitle
|
|
eyebrow="Produção e armazém"
|
|
title="Equipamentos e peças sobressalentes"
|
|
description="Cadastro, peças compatíveis, mínimos explícitos e rastreabilidade das saídas."
|
|
/>
|
|
<InventoryNotice />
|
|
{capitalRegisterReady && (
|
|
<Link className="underline" href="/projectos-imobilizado">
|
|
Projectos e imobilizado
|
|
</Link>
|
|
)}
|
|
{geographyEnabled && !demo && (
|
|
<Panel title="Localização dos equipamentos">
|
|
<TerritorialFilter
|
|
target="/equipamentos"
|
|
initial={geographyFilter}
|
|
/>
|
|
</Panel>
|
|
)}
|
|
{demo && (
|
|
<p className="rounded-lg border bg-teal-50 p-4 text-sm text-teal-900">
|
|
Dados fictícios para revisão sem login. Formulários
|
|
apenas de simulação.
|
|
</p>
|
|
)}
|
|
<Panel
|
|
title="Regras e validação técnica"
|
|
description="Os critérios de criticidade e mínimos são definidos explicitamente pela empresa, sem valores predefinidos."
|
|
>
|
|
<p className="text-sm">
|
|
O responsável da área (chefe do departamento), distinto
|
|
do solicitante, valida tecnicamente a versão do pedido
|
|
de equipamento. Finanças é obrigatória para qualquer
|
|
valor. A adjudicação e emissão de equipamento exigem
|
|
regra de investimento vigente, com duas aprovações
|
|
administrativas, e reserva em dotação de investimento. A
|
|
validação técnica não substitui estes controlos.
|
|
</p>
|
|
<Link
|
|
className="mt-3 inline-block text-sm font-semibold text-teal-700"
|
|
href={demo ? '/consulta/requisicoes' : '/requisicoes'}
|
|
>
|
|
Consultar requisições →
|
|
</Link>
|
|
</Panel>
|
|
<Panel title="Equipamentos">
|
|
<div className="grid gap-4 lg:grid-cols-2">
|
|
{equipment.map((e) => (
|
|
<article
|
|
className="rounded-lg border p-4 text-sm"
|
|
key={e.id}
|
|
>
|
|
<strong>
|
|
{e.code} · {e.description}
|
|
</strong>
|
|
<Link
|
|
className="mt-2 block underline"
|
|
href={`/equipamentos/${e.id}`}
|
|
>
|
|
Acompanhar compras e movimentos
|
|
</Link>
|
|
<p className="mt-2">
|
|
{e.area} ·{' '}
|
|
{
|
|
departments.find(
|
|
(d) => d.id === e.department_id,
|
|
)?.name
|
|
}{' '}
|
|
· {statuses[e.status]}
|
|
</p>
|
|
<p>
|
|
Série: {e.serial_number || 'Não indicada'} ·
|
|
Criticidade:{' '}
|
|
{criticality(e.criticality_level_id)?.name}
|
|
</p>
|
|
{equipmentGeography[e.id]?.path.length ? (
|
|
<p className="text-muted-foreground">
|
|
Localização actual:{' '}
|
|
{equipmentGeography[e.id]?.path
|
|
.map((node) => node.name)
|
|
.join(' / ')}
|
|
</p>
|
|
) : null}
|
|
{e.stock_item_id && (
|
|
<p>
|
|
Artigo: {stock(e.stock_item_id)?.code}
|
|
</p>
|
|
)}
|
|
<p className="mt-2">
|
|
Peças associadas:{' '}
|
|
{links
|
|
.filter((l) => l.equipment_id === e.id)
|
|
.map(
|
|
(l) =>
|
|
stock(
|
|
parts.find(
|
|
(p) =>
|
|
p.id ===
|
|
l.spare_part_id,
|
|
)?.stock_item_id ?? 0,
|
|
)?.code,
|
|
)
|
|
.join(', ') || 'Nenhuma'}
|
|
</p>
|
|
{canManage && (
|
|
<Button
|
|
className="mt-3"
|
|
variant="outline"
|
|
onClick={() =>
|
|
setSelectedAssetId(String(e.id))
|
|
}
|
|
>
|
|
Editar ficha
|
|
</Button>
|
|
)}
|
|
</article>
|
|
))}
|
|
</div>
|
|
{equipment.length === 0 && (
|
|
<p className="text-sm">
|
|
Sem equipamentos. Configure a criticidade antes do
|
|
cadastro.
|
|
</p>
|
|
)}
|
|
</Panel>
|
|
<Panel
|
|
title="Peças e alertas de mínimo"
|
|
description="Saldo total por artigo nos armazéns da empresa. Cada peça reutiliza um artigo existente; associá-la a várias máquinas não duplica o stock."
|
|
>
|
|
<div className="space-y-3">
|
|
{parts.map((p) => (
|
|
<article
|
|
key={p.id}
|
|
className={`rounded-lg border p-4 text-sm ${p.below_minimum ? 'border-amber-300 bg-amber-50 text-amber-950' : ''}`}
|
|
>
|
|
<strong>
|
|
{stock(p.stock_item_id)?.code} ·{' '}
|
|
{stock(p.stock_item_id)?.name}
|
|
</strong>
|
|
<p className="mt-2">
|
|
Stock: {quantity(p.balance_milli)} · Mínimo
|
|
definido: {quantity(p.minimum_milli)}{' '}
|
|
{stock(p.stock_item_id)?.unit} ·{' '}
|
|
{criticality(p.criticality_level_id)?.name}
|
|
</p>
|
|
<p className="mt-1 font-semibold">
|
|
{p.below_minimum
|
|
? 'Abaixo do mínimo — rever reposição'
|
|
: 'Mínimo assegurado'}
|
|
</p>
|
|
<p>
|
|
Critério:{' '}
|
|
{
|
|
criticality(p.criticality_level_id)
|
|
?.criteria
|
|
}
|
|
</p>
|
|
{canManage && (
|
|
<Button
|
|
className="mt-3"
|
|
variant="outline"
|
|
onClick={() =>
|
|
setSelectedPartId(String(p.id))
|
|
}
|
|
>
|
|
Editar mínimo e criticidade
|
|
</Button>
|
|
)}
|
|
</article>
|
|
))}
|
|
</div>
|
|
{parts.length === 0 && (
|
|
<p className="text-sm">Sem peças registadas.</p>
|
|
)}
|
|
</Panel>
|
|
{canManage && (
|
|
<div className="grid gap-6 lg:grid-cols-2">
|
|
<Panel
|
|
title={
|
|
selectedAssetId
|
|
? 'Editar equipamento'
|
|
: 'Cadastrar equipamento'
|
|
}
|
|
>
|
|
{selectedAssetId && !selectedAsset ? (
|
|
<p>
|
|
Ficha seleccionada indisponível no contexto
|
|
actual.{' '}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => setSelectedAssetId('')}
|
|
>
|
|
Novo cadastro
|
|
</Button>
|
|
</p>
|
|
) : (
|
|
<AssetEditor
|
|
key={assetIdentity}
|
|
identity={assetIdentity}
|
|
source={selectedAsset}
|
|
items={items}
|
|
levels={levels}
|
|
departments={departments}
|
|
geographyEnabled={geographyEnabled}
|
|
equipmentGeography={equipmentGeography}
|
|
demo={demo}
|
|
simulate={simulate}
|
|
onNew={() => setSelectedAssetId('')}
|
|
onSaved={() => setSelectedAssetId('')}
|
|
/>
|
|
)}
|
|
</Panel>
|
|
<div className="space-y-6">
|
|
<Panel
|
|
title={
|
|
selectedPartId
|
|
? 'Editar peça'
|
|
: 'Cadastrar peça sobre artigo existente'
|
|
}
|
|
>
|
|
{selectedPartId && !selectedPart ? (
|
|
<p>
|
|
Ficha seleccionada indisponível no
|
|
contexto actual.{' '}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() =>
|
|
setSelectedPartId('')
|
|
}
|
|
>
|
|
Novo cadastro
|
|
</Button>
|
|
</p>
|
|
) : (
|
|
<PartEditor
|
|
key={partIdentity}
|
|
identity={partIdentity}
|
|
source={selectedPart}
|
|
items={items}
|
|
levels={levels}
|
|
demo={demo}
|
|
simulate={simulate}
|
|
onNew={() => setSelectedPartId('')}
|
|
onSaved={() => setSelectedPartId('')}
|
|
/>
|
|
)}
|
|
</Panel>
|
|
<Panel title="Associar peça a equipamento">
|
|
<form
|
|
ref={associationRef}
|
|
data-preparation-dirty={
|
|
associationDirty ? 'true' : 'false'
|
|
}
|
|
className="grid gap-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
if (demo) {
|
|
simulate();
|
|
return;
|
|
}
|
|
association.post(
|
|
'/equipamentos/associacoes',
|
|
{
|
|
onSuccess: () => {
|
|
association.setData(
|
|
initialAssociation,
|
|
);
|
|
association.clearErrors();
|
|
router.remember(
|
|
initialAssociation,
|
|
`${identity}:association:data`,
|
|
);
|
|
router.remember(
|
|
{},
|
|
`${identity}:association:errors`,
|
|
);
|
|
},
|
|
},
|
|
);
|
|
}}
|
|
>
|
|
<FormErrors errors={association.errors} />
|
|
<Field label="Equipamento">
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={
|
|
association.data.equipment_id
|
|
}
|
|
onChange={(e) =>
|
|
association.setData(
|
|
'equipment_id',
|
|
e.target.value,
|
|
)
|
|
}
|
|
>
|
|
<option value="">
|
|
Seleccionar
|
|
</option>
|
|
{equipment.map((e) => (
|
|
<option key={e.id} value={e.id}>
|
|
{e.code} · {e.description}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Peça">
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={
|
|
association.data.spare_part_id
|
|
}
|
|
onChange={(e) =>
|
|
association.setData(
|
|
'spare_part_id',
|
|
e.target.value,
|
|
)
|
|
}
|
|
>
|
|
<option value="">
|
|
Seleccionar
|
|
</option>
|
|
{parts.map((p) => (
|
|
<option key={p.id} value={p.id}>
|
|
{
|
|
stock(p.stock_item_id)
|
|
?.code
|
|
}{' '}
|
|
·{' '}
|
|
{
|
|
stock(p.stock_item_id)
|
|
?.name
|
|
}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Button disabled={association.processing}>
|
|
Associar sem duplicar stock
|
|
</Button>
|
|
</form>
|
|
</Panel>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{canConfigure && (
|
|
<Panel
|
|
title="Configurar critérios de criticidade"
|
|
description="Sem categorias automáticas. Registe a decisão da empresa; os critérios guardados não são reescritos. Para novos critérios, crie uma nova categoria."
|
|
>
|
|
<form
|
|
ref={categoryRef}
|
|
data-preparation-dirty={
|
|
categoryDirty ? 'true' : 'false'
|
|
}
|
|
className="grid gap-4 sm:grid-cols-2"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
if (demo) {
|
|
simulate();
|
|
return;
|
|
}
|
|
category.post('/equipamentos/categorias', {
|
|
onSuccess: () => {
|
|
category.setData(initialCategory);
|
|
category.clearErrors();
|
|
router.remember(
|
|
initialCategory,
|
|
`${identity}:category:data`,
|
|
);
|
|
router.remember(
|
|
{},
|
|
`${identity}:category:errors`,
|
|
);
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<Field label="Código">
|
|
<Input
|
|
required
|
|
value={category.data.code}
|
|
onChange={(e) =>
|
|
category.setData('code', e.target.value)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Field label="Nome da categoria">
|
|
<Input
|
|
required
|
|
value={category.data.name}
|
|
onChange={(e) =>
|
|
category.setData('name', e.target.value)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Field label="Critério e fundamento da classificação">
|
|
<textarea
|
|
required
|
|
className={controlClass}
|
|
value={category.data.criteria}
|
|
onChange={(e) =>
|
|
category.setData(
|
|
'criteria',
|
|
e.target.value,
|
|
)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Field label="É crítica para produção segundo este critério?">
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={category.data.is_critical}
|
|
onChange={(e) =>
|
|
category.setData(
|
|
'is_critical',
|
|
e.target.value,
|
|
)
|
|
}
|
|
>
|
|
<option value="">Decisão explícita</option>
|
|
<option value="1">Sim</option>
|
|
<option value="0">Não</option>
|
|
</select>
|
|
</Field>
|
|
<FormErrors errors={category.errors} />
|
|
<Button disabled={category.processing}>
|
|
Guardar critério
|
|
</Button>
|
|
</form>
|
|
<div className="mt-4 space-y-2 text-sm">
|
|
{levels.map((l) => (
|
|
<p key={l.id}>
|
|
<strong>
|
|
{l.code} · {l.name}
|
|
</strong>
|
|
: {l.criteria} ·{' '}
|
|
{l.is_critical ? 'Crítica' : 'Não crítica'}
|
|
</p>
|
|
))}
|
|
</div>
|
|
</Panel>
|
|
)}
|
|
<p role="status" className="text-sm">
|
|
{message}
|
|
</p>
|
|
<Panel title="Saídas rastreáveis por equipamento">
|
|
<Link
|
|
href={
|
|
demo
|
|
? '/consulta/operacoes-stock'
|
|
: '/stock/operacoes'
|
|
}
|
|
className="text-sm font-semibold text-teal-700"
|
|
>
|
|
Registar saída com equipamento de destino →
|
|
</Link>
|
|
<div className="mt-4 space-y-3">
|
|
{history.data.map((h) => (
|
|
<article
|
|
key={h.id}
|
|
className="rounded-lg border p-4 text-sm"
|
|
>
|
|
<strong>
|
|
{h.reference} · {h.snapshot.equipment?.code}
|
|
</strong>
|
|
<p>
|
|
{h.snapshot.equipment?.description} ·{' '}
|
|
{h.snapshot.stock_item.name} ·{' '}
|
|
{quantity(h.quantity_milli)}{' '}
|
|
{h.snapshot.stock_item.unit}
|
|
</p>
|
|
<p>{h.snapshot.reason}</p>
|
|
{h.snapshot.reversal && (
|
|
<p className="mt-2 font-medium">
|
|
Estorno de{' '}
|
|
{
|
|
h.snapshot.reversal.source.document
|
|
.reference
|
|
}
|
|
:{' '}
|
|
{h.snapshot.reversal.source.rows
|
|
.map(
|
|
(row) =>
|
|
`${row.warehouse_name}: ${row.quantity_milli > 0 ? '+' : ''}${quantity(row.quantity_milli)}`,
|
|
)
|
|
.join(' · ')}
|
|
. As quantidades repostas exigem a
|
|
conferência de qualidade aplicável.
|
|
</p>
|
|
)}
|
|
</article>
|
|
))}
|
|
{history.data.length === 0 && (
|
|
<p className="text-sm">
|
|
Sem saídas associadas a equipamentos.
|
|
</p>
|
|
)}
|
|
</div>
|
|
<InventoryPagination page={history} />
|
|
</Panel>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
export function EquipmentDemo() {
|
|
return (
|
|
<EquipmentPage
|
|
demo
|
|
canManage
|
|
canConfigure
|
|
equipment={[
|
|
{
|
|
id: 1,
|
|
code: 'EQ-DEMO-01',
|
|
description: 'Compressor fictício',
|
|
area: 'Linha de produção fictícia A',
|
|
serial_number: 'SERIE-DEMO',
|
|
status: 'stopped',
|
|
department_id: 1,
|
|
stock_item_id: null,
|
|
criticality_level_id: 1,
|
|
version: 1,
|
|
},
|
|
]}
|
|
parts={[
|
|
{
|
|
id: 1,
|
|
stock_item_id: 1,
|
|
criticality_level_id: 1,
|
|
minimum_milli: 3000,
|
|
balance_milli: 1000,
|
|
below_minimum: true,
|
|
version: 1,
|
|
},
|
|
]}
|
|
links={[{ equipment_id: 1, spare_part_id: 1 }]}
|
|
levels={[
|
|
{
|
|
id: 1,
|
|
code: 'CRIT-DEMO',
|
|
name: 'Crítica (exemplo fictício)',
|
|
criteria:
|
|
'Exemplo apenas: falha interrompe a linha fictícia. A empresa deve definir os seus critérios.',
|
|
is_critical: true,
|
|
},
|
|
]}
|
|
items={[
|
|
{
|
|
id: 1,
|
|
code: 'ROL-DEMO',
|
|
name: 'Rolamento fictício',
|
|
unit: 'un',
|
|
},
|
|
]}
|
|
departments={[{ id: 1, name: 'Produção fictícia' }]}
|
|
history={{
|
|
data: [
|
|
{
|
|
id: 1,
|
|
reference: 'SAI-DEMO-PECA',
|
|
quantity_milli: 1000,
|
|
snapshot: {
|
|
equipment: {
|
|
code: 'EQ-DEMO-01',
|
|
description:
|
|
'Compressor fictício no momento da saída',
|
|
},
|
|
stock_item: {
|
|
name: 'Rolamento fictício',
|
|
unit: 'un',
|
|
},
|
|
reason: 'Substituição de peça numa paragem fictícia.',
|
|
},
|
|
},
|
|
],
|
|
prev_page_url: null,
|
|
next_page_url: null,
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function AssetEditor({
|
|
identity,
|
|
source,
|
|
items,
|
|
levels,
|
|
demo,
|
|
simulate,
|
|
onNew,
|
|
onSaved,
|
|
departments,
|
|
geographyEnabled,
|
|
equipmentGeography,
|
|
}: Pick<
|
|
Props,
|
|
| 'items'
|
|
| 'levels'
|
|
| 'departments'
|
|
| 'geographyEnabled'
|
|
| 'equipmentGeography'
|
|
> & {
|
|
identity: string;
|
|
source?: Asset;
|
|
demo: boolean;
|
|
simulate: () => void;
|
|
onNew: () => void;
|
|
onSaved: () => void;
|
|
}) {
|
|
const initial = {
|
|
geography_reference:
|
|
source?.geography_reference ?? (null as GeographyReference | null),
|
|
id: source ? String(source.id) : '',
|
|
version: source ? String(source.version) : '',
|
|
code: source?.code ?? '',
|
|
description: source?.description ?? '',
|
|
area: source?.area ?? '',
|
|
serial_number: source?.serial_number ?? '',
|
|
department_id: source ? String(source.department_id) : '',
|
|
stock_item_id: source?.stock_item_id
|
|
? String(source.stock_item_id)
|
|
: '',
|
|
criticality_level_id: source ? String(source.criticality_level_id) : '',
|
|
status: source?.status ?? 'active',
|
|
};
|
|
const asset = useForm(identity, initial);
|
|
const dirty = hasPreparationChanges(initial, asset.data);
|
|
const ref = usePreparationDirty(dirty, !demo);
|
|
return (
|
|
<form
|
|
ref={ref}
|
|
data-preparation-dirty={dirty ? 'true' : 'false'}
|
|
className="grid gap-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
if (demo) {
|
|
simulate();
|
|
return;
|
|
}
|
|
asset.transform((data) => {
|
|
const { geography_reference, ...rest } = data;
|
|
return geographyEnabled
|
|
? { ...rest, geography_reference }
|
|
: rest;
|
|
});
|
|
asset.post('/equipamentos/activos', {
|
|
onSuccess: () => {
|
|
asset.setData(initial);
|
|
asset.clearErrors();
|
|
router.remember(initial, `${identity}:data`);
|
|
router.remember({}, `${identity}:errors`);
|
|
onSaved();
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={asset.errors} />
|
|
{geographyEnabled && !demo && (
|
|
<AdministrativeLocation
|
|
key={asset.data.id || 'new'}
|
|
value={asset.data.geography_reference}
|
|
initial={
|
|
(equipmentGeography ?? {})[Number(asset.data.id)] ??
|
|
null
|
|
}
|
|
onChange={(value) =>
|
|
asset.setData('geography_reference', value)
|
|
}
|
|
/>
|
|
)}
|
|
{(['code', 'description', 'area', 'serial_number'] as const).map(
|
|
(k) => (
|
|
<Field
|
|
key={k}
|
|
label={
|
|
{
|
|
code: 'Código',
|
|
description: 'Descrição',
|
|
area: 'Área / linha de produção',
|
|
serial_number: 'Número de série',
|
|
}[k]
|
|
}
|
|
>
|
|
<Input
|
|
required={k !== 'serial_number'}
|
|
value={asset.data[k]}
|
|
onChange={(e) => asset.setData(k, e.target.value)}
|
|
/>
|
|
</Field>
|
|
),
|
|
)}
|
|
<Field label="Departamento">
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={asset.data.department_id}
|
|
onChange={(e) =>
|
|
asset.setData('department_id', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccionar</option>
|
|
{departments.map((d) => (
|
|
<option key={d.id} value={d.id}>
|
|
{d.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Artigo de stock (opcional)">
|
|
<select
|
|
className={controlClass}
|
|
value={asset.data.stock_item_id}
|
|
onChange={(e) =>
|
|
asset.setData('stock_item_id', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Sem artigo associado</option>
|
|
{items.map((i) => (
|
|
<option key={i.id} value={i.id}>
|
|
{i.code} · {i.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Criticidade definida pela empresa">
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={asset.data.criticality_level_id}
|
|
onChange={(e) =>
|
|
asset.setData('criticality_level_id', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccionar</option>
|
|
{levels.map((l) => (
|
|
<option key={l.id} value={l.id}>
|
|
{l.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Estado">
|
|
<select
|
|
className={controlClass}
|
|
value={asset.data.status}
|
|
onChange={(e) => asset.setData('status', e.target.value)}
|
|
>
|
|
{Object.entries(statuses).map(([k, v]) => (
|
|
<option key={k} value={k}>
|
|
{v}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Button disabled={asset.processing || levels.length === 0}>
|
|
Guardar equipamento
|
|
</Button>
|
|
{asset.data.id && (
|
|
<Button type="button" variant="outline" onClick={onNew}>
|
|
Novo cadastro
|
|
</Button>
|
|
)}
|
|
<p className="text-xs text-muted-foreground">
|
|
O cadastro não cria entradas nem retira existências. Os
|
|
movimentos usam o fluxo de stock.
|
|
</p>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
function PartEditor({
|
|
identity,
|
|
source,
|
|
items,
|
|
levels,
|
|
demo,
|
|
simulate,
|
|
onNew,
|
|
onSaved,
|
|
}: Pick<Props, 'items' | 'levels'> & {
|
|
identity: string;
|
|
source?: Part;
|
|
demo: boolean;
|
|
simulate: () => void;
|
|
onNew: () => void;
|
|
onSaved: () => void;
|
|
}) {
|
|
const initial = {
|
|
id: source ? String(source.id) : '',
|
|
version: source ? String(source.version) : '',
|
|
stock_item_id: source ? String(source.stock_item_id) : '',
|
|
criticality_level_id: source ? String(source.criticality_level_id) : '',
|
|
minimum_quantity: source
|
|
? (source.minimum_milli / 1000).toFixed(3)
|
|
: '',
|
|
};
|
|
const part = useForm(identity, initial);
|
|
const dirty = hasPreparationChanges(initial, part.data);
|
|
const ref = usePreparationDirty(dirty, !demo);
|
|
return (
|
|
<form
|
|
ref={ref}
|
|
data-preparation-dirty={dirty ? 'true' : 'false'}
|
|
className="grid gap-4"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
if (demo) {
|
|
simulate();
|
|
return;
|
|
}
|
|
part.post('/equipamentos/pecas', {
|
|
onSuccess: () => {
|
|
part.setData(initial);
|
|
part.clearErrors();
|
|
router.remember(initial, `${identity}:data`);
|
|
router.remember({}, `${identity}:errors`);
|
|
onSaved();
|
|
},
|
|
});
|
|
}}
|
|
>
|
|
<FormErrors errors={part.errors} />
|
|
<Field label="Artigo / referência">
|
|
<select
|
|
required
|
|
disabled={!!part.data.id}
|
|
className={controlClass}
|
|
value={part.data.stock_item_id}
|
|
onChange={(e) =>
|
|
part.setData('stock_item_id', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccionar</option>
|
|
{items.map((i) => (
|
|
<option key={i.id} value={i.id}>
|
|
{i.code} · {i.name} ({i.unit})
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Criticidade">
|
|
<select
|
|
required
|
|
className={controlClass}
|
|
value={part.data.criticality_level_id}
|
|
onChange={(e) =>
|
|
part.setData('criticality_level_id', e.target.value)
|
|
}
|
|
>
|
|
<option value="">Seleccionar</option>
|
|
{levels.map((l) => (
|
|
<option key={l.id} value={l.id}>
|
|
{l.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Quantidade mínima explícita (pode ser zero)">
|
|
<Input
|
|
required
|
|
type="number"
|
|
min="0"
|
|
step="0.001"
|
|
value={part.data.minimum_quantity}
|
|
onChange={(e) =>
|
|
part.setData('minimum_quantity', e.target.value)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Button disabled={part.processing || levels.length === 0}>
|
|
Guardar peça
|
|
</Button>
|
|
{part.data.id && (
|
|
<Button type="button" variant="outline" onClick={onNew}>
|
|
Nova peça
|
|
</Button>
|
|
)}
|
|
</form>
|
|
);
|
|
}
|