@@ -1,10 +1,13 @@
|
||||
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 } from '@inertiajs/react';
|
||||
import { Head, useForm, Link, router, useRemember } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import { InventoryNotice, InventoryPagination } from '@/components/inventory';
|
||||
import {
|
||||
@@ -98,7 +101,18 @@ const statuses: Record<string, string> = {
|
||||
stopped: 'Parado',
|
||||
retired: 'Retirado',
|
||||
};
|
||||
export default function EquipmentPage({
|
||||
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,
|
||||
@@ -113,35 +127,45 @@ export default function EquipmentPage({
|
||||
geographyEnabled = false,
|
||||
equipmentGeography = {},
|
||||
geographyFilter = null,
|
||||
}: Props) {
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const [message, setMessage] = useState('');
|
||||
const category = useForm({
|
||||
const initialCategory = {
|
||||
code: '',
|
||||
name: '',
|
||||
criteria: '',
|
||||
is_critical: '',
|
||||
});
|
||||
const asset = useForm({
|
||||
geography_reference: null as GeographyReference | null,
|
||||
id: '',
|
||||
version: '',
|
||||
code: '',
|
||||
description: '',
|
||||
area: '',
|
||||
serial_number: '',
|
||||
department_id: '',
|
||||
stock_item_id: '',
|
||||
criticality_level_id: '',
|
||||
status: 'active',
|
||||
});
|
||||
const part = useForm({
|
||||
id: '',
|
||||
version: '',
|
||||
stock_item_id: '',
|
||||
criticality_level_id: '',
|
||||
minimum_quantity: '',
|
||||
});
|
||||
const association = useForm({ equipment_id: '', spare_part_id: '' });
|
||||
};
|
||||
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() {
|
||||
@@ -262,28 +286,7 @@ export default function EquipmentPage({
|
||||
className="mt-3"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
asset.setData({
|
||||
geography_reference:
|
||||
e.geography_reference ??
|
||||
null,
|
||||
id: String(e.id),
|
||||
version: String(e.version),
|
||||
code: e.code,
|
||||
description: e.description,
|
||||
area: e.area,
|
||||
serial_number:
|
||||
e.serial_number ?? '',
|
||||
department_id: String(
|
||||
e.department_id,
|
||||
),
|
||||
stock_item_id: e.stock_item_id
|
||||
? String(e.stock_item_id)
|
||||
: '',
|
||||
criticality_level_id: String(
|
||||
e.criticality_level_id,
|
||||
),
|
||||
status: e.status,
|
||||
})
|
||||
setSelectedAssetId(String(e.id))
|
||||
}
|
||||
>
|
||||
Editar ficha
|
||||
@@ -336,19 +339,7 @@ export default function EquipmentPage({
|
||||
className="mt-3"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
part.setData({
|
||||
id: String(p.id),
|
||||
version: String(p.version),
|
||||
stock_item_id: String(
|
||||
p.stock_item_id,
|
||||
),
|
||||
criticality_level_id: String(
|
||||
p.criticality_level_id,
|
||||
),
|
||||
minimum_quantity: (
|
||||
p.minimum_milli / 1000
|
||||
).toFixed(3),
|
||||
})
|
||||
setSelectedPartId(String(p.id))
|
||||
}
|
||||
>
|
||||
Editar mínimo e criticidade
|
||||
@@ -365,288 +356,82 @@ export default function EquipmentPage({
|
||||
<div className="grid gap-6 lg:grid-cols-2">
|
||||
<Panel
|
||||
title={
|
||||
asset.data.id
|
||||
selectedAssetId
|
||||
? 'Editar equipamento'
|
||||
: 'Cadastrar equipamento'
|
||||
}
|
||||
>
|
||||
<form
|
||||
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.reset(),
|
||||
});
|
||||
}}
|
||||
>
|
||||
<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 && (
|
||||
{selectedAssetId && !selectedAsset ? (
|
||||
<p>
|
||||
Ficha seleccionada indisponível no contexto
|
||||
actual.{' '}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => asset.reset()}
|
||||
onClick={() => setSelectedAssetId('')}
|
||||
>
|
||||
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>
|
||||
) : (
|
||||
<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={
|
||||
part.data.id
|
||||
selectedPartId
|
||||
? 'Editar peça'
|
||||
: 'Cadastrar peça sobre artigo existente'
|
||||
}
|
||||
>
|
||||
<form
|
||||
className="grid gap-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
if (demo) {
|
||||
simulate();
|
||||
return;
|
||||
}
|
||||
part.post('/equipamentos/pecas', {
|
||||
onSuccess: () => part.reset(),
|
||||
});
|
||||
}}
|
||||
>
|
||||
<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 && (
|
||||
{selectedPartId && !selectedPart ? (
|
||||
<p>
|
||||
Ficha seleccionada indisponível no
|
||||
contexto actual.{' '}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => part.reset()}
|
||||
onClick={() =>
|
||||
setSelectedPartId('')
|
||||
}
|
||||
>
|
||||
Nova peça
|
||||
Novo cadastro
|
||||
</Button>
|
||||
)}
|
||||
</form>
|
||||
</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();
|
||||
@@ -657,8 +442,20 @@ export default function EquipmentPage({
|
||||
association.post(
|
||||
'/equipamentos/associacoes',
|
||||
{
|
||||
onSuccess: () =>
|
||||
association.reset(),
|
||||
onSuccess: () => {
|
||||
association.setData(
|
||||
initialAssociation,
|
||||
);
|
||||
association.clearErrors();
|
||||
router.remember(
|
||||
initialAssociation,
|
||||
`${identity}:association:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:association:errors`,
|
||||
);
|
||||
},
|
||||
},
|
||||
);
|
||||
}}
|
||||
@@ -734,6 +531,10 @@ export default function EquipmentPage({
|
||||
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();
|
||||
@@ -742,7 +543,18 @@ export default function EquipmentPage({
|
||||
return;
|
||||
}
|
||||
category.post('/equipamentos/categorias', {
|
||||
onSuccess: () => category.reset(),
|
||||
onSuccess: () => {
|
||||
category.setData(initialCategory);
|
||||
category.clearErrors();
|
||||
router.remember(
|
||||
initialCategory,
|
||||
`${identity}:category:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:category:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -951,3 +763,302 @@ export function EquipmentDemo() {
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
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>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user