@@ -1,3 +1,6 @@
|
||||
import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { InventoryEventActions } from '@/components/inventory-event-actions';
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
@@ -102,13 +105,39 @@ const states: Record<string, string> = {
|
||||
};
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
||||
const capabilities = JSON.stringify(
|
||||
Object.entries(props.capabilities).sort(([a], [b]) =>
|
||||
a.localeCompare(b),
|
||||
),
|
||||
);
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock/ocorrencias:prepare:${old?.record_key ?? 'new'}:${old?.version ?? 0}:${old?.state ?? 'new'}:${capabilities}`,
|
||||
);
|
||||
return (
|
||||
<PrepareFields
|
||||
key={identity}
|
||||
props={props}
|
||||
old={old}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
function PrepareFields({
|
||||
props,
|
||||
old,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
old?: RecordEntry;
|
||||
identity: string;
|
||||
}) {
|
||||
const [receipts, setReceipts] = useState(props.receipts);
|
||||
const formKey = `disposition:${old?.id ?? 'new'}`;
|
||||
useEffect(() => {
|
||||
if (!props.sourceFormKey || props.sourceFormKey === formKey)
|
||||
setReceipts(props.receipts);
|
||||
}, [props.sourceFormKey, props.receipts, formKey]);
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: old?.record_key,
|
||||
version: old?.version ?? 0,
|
||||
@@ -125,7 +154,10 @@ function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
||||
quantity: old ? String(old.data.quantity_milli / 1000) : '',
|
||||
reference: old?.data.reference ?? '',
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const searchSources = (value: string) =>
|
||||
router.get(
|
||||
'/stock/ocorrencias',
|
||||
@@ -158,10 +190,23 @@ function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
||||
);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4 md:grid-cols-2"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/stock/ocorrencias');
|
||||
form.post('/stock/ocorrencias', {
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(next, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -223,6 +268,20 @@ function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
||||
)}
|
||||
</option>
|
||||
))}
|
||||
{form.data.data.receipt_item_id &&
|
||||
!receipts.some(
|
||||
(receipt) =>
|
||||
String(receipt.id) ===
|
||||
form.data.data.receipt_item_id,
|
||||
) && (
|
||||
<option
|
||||
value={form.data.data.receipt_item_id}
|
||||
disabled
|
||||
>
|
||||
Origem seleccionada — pesquisar para
|
||||
conferir disponibilidade
|
||||
</option>
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
|
||||
@@ -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, Link, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { InventoryNotice, InventoryPagination } from '@/components/inventory';
|
||||
import {
|
||||
TableViewport,
|
||||
@@ -25,29 +28,57 @@ import type {
|
||||
Warehouse,
|
||||
} from '@/types/inventory';
|
||||
|
||||
function WarehouseLocation({
|
||||
warehouse,
|
||||
geography,
|
||||
}: {
|
||||
function WarehouseLocation(props: {
|
||||
warehouse: Warehouse;
|
||||
geography: GeographyDescription;
|
||||
}) {
|
||||
const form = useForm({
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock/warehouse-location:${props.warehouse.id}:${JSON.stringify(props.geography?.reference ?? null)}`,
|
||||
);
|
||||
return (
|
||||
<WarehouseLocationFields
|
||||
key={identity}
|
||||
{...props}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
function WarehouseLocationFields({
|
||||
warehouse,
|
||||
geography,
|
||||
identity,
|
||||
}: {
|
||||
identity: string;
|
||||
warehouse: Warehouse;
|
||||
geography: GeographyDescription;
|
||||
}) {
|
||||
const initial = {
|
||||
geography_reference:
|
||||
geography?.reference ?? (null as GeographyReference | null),
|
||||
expected_reference: geography?.reference ?? null,
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<details className="mb-4 rounded-lg border p-3">
|
||||
<summary className="cursor-pointer text-sm font-medium">
|
||||
Localização de {warehouse.code} · {warehouse.name}
|
||||
</summary>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-4 space-y-3"
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
form.post(`/stock/armazens/${warehouse.id}/localizacao`, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.setData(initial);
|
||||
form.clearErrors();
|
||||
router.remember(initial, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -85,7 +116,13 @@ type Props = {
|
||||
filters: { search?: string; warehouse_id?: number; location?: string };
|
||||
canManage: boolean;
|
||||
};
|
||||
export default function Stock({
|
||||
export default function Stock(props: Props) {
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock:catalogue:${props.canManage}:${props.geographyEnabled ?? false}`,
|
||||
);
|
||||
return <StockFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function StockFields({
|
||||
items,
|
||||
warehouses,
|
||||
balances,
|
||||
@@ -95,18 +132,28 @@ export default function Stock({
|
||||
geographyEnabled = false,
|
||||
warehouseGeography = {},
|
||||
geographyFilter = null,
|
||||
}: Props) {
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const search = useForm({
|
||||
location: filters.location ?? '',
|
||||
search: filters.search ?? '',
|
||||
warehouse_id: filters.warehouse_id ? String(filters.warehouse_id) : '',
|
||||
});
|
||||
const item = useForm({ code: '', name: '', unit: '' });
|
||||
const warehouse = useForm({
|
||||
const initialItem = { code: '', name: '', unit: '' };
|
||||
const item = useForm(`${identity}:item`, initialItem);
|
||||
const itemDirty = hasPreparationChanges(initialItem, item.data);
|
||||
const itemRef = usePreparationDirty(itemDirty);
|
||||
const initialWarehouse = {
|
||||
code: '',
|
||||
name: '',
|
||||
geography_reference: null as GeographyReference | null,
|
||||
});
|
||||
};
|
||||
const warehouse = useForm(`${identity}:warehouse`, initialWarehouse);
|
||||
const warehouseDirty = hasPreparationChanges(
|
||||
initialWarehouse,
|
||||
warehouse.data,
|
||||
);
|
||||
const warehouseRef = usePreparationDirty(warehouseDirty);
|
||||
return (
|
||||
<>
|
||||
<Head title="Stock e movimentos" />
|
||||
@@ -309,12 +356,27 @@ export default function Stock({
|
||||
</div>
|
||||
{canManage && (
|
||||
<form
|
||||
ref={itemRef}
|
||||
data-preparation-dirty={
|
||||
itemDirty ? 'true' : 'false'
|
||||
}
|
||||
className="space-y-4 border-t pt-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
item.post('/stock/artigos', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => item.reset(),
|
||||
onSuccess: () => {
|
||||
item.setData(initialItem);
|
||||
item.clearErrors();
|
||||
router.remember(
|
||||
initialItem,
|
||||
`${identity}:item:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:item:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -378,6 +440,10 @@ export default function Stock({
|
||||
))}
|
||||
{canManage && (
|
||||
<form
|
||||
ref={warehouseRef}
|
||||
data-preparation-dirty={
|
||||
warehouseDirty ? 'true' : 'false'
|
||||
}
|
||||
className="space-y-4 border-t pt-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -390,7 +456,18 @@ export default function Stock({
|
||||
});
|
||||
warehouse.post('/stock/armazens', {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => warehouse.reset(),
|
||||
onSuccess: () => {
|
||||
warehouse.setData(initialWarehouse);
|
||||
warehouse.clearErrors();
|
||||
router.remember(
|
||||
initialWarehouse,
|
||||
`${identity}:warehouse:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:warehouse:errors`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { InventoryEventActions } from '@/components/inventory-event-actions';
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import {
|
||||
FormErrors,
|
||||
PageTitle,
|
||||
@@ -139,9 +142,35 @@ const states: Record<string, string> = {
|
||||
};
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
||||
const capabilities = JSON.stringify(
|
||||
Object.entries(props.capabilities).sort(([a], [b]) =>
|
||||
a.localeCompare(b),
|
||||
),
|
||||
);
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock/lotes:prepare:${old?.record_key ?? 'new'}:${old?.version ?? 0}:${old?.state ?? 'new'}:${capabilities}`,
|
||||
);
|
||||
return (
|
||||
<PrepareFields
|
||||
key={identity}
|
||||
props={props}
|
||||
old={old}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
function PrepareFields({
|
||||
props,
|
||||
old,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
old?: RecordEntry;
|
||||
identity: string;
|
||||
}) {
|
||||
const revision =
|
||||
old?.state === 'active' || !!old?.snapshot.previous_setup_id;
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: old?.record_key,
|
||||
version: old?.version ?? 0,
|
||||
@@ -164,13 +193,29 @@ function Prepare({ props, old }: { props: Props; old?: RecordEntry }) {
|
||||
quantity: String(l.quantity_milli / 1000),
|
||||
})) ?? []),
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/stock/lotes');
|
||||
form.post('/stock/lotes', {
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(next, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -445,7 +490,13 @@ function Decision({
|
||||
);
|
||||
}
|
||||
function Identify({ lot }: { lot: Lot }) {
|
||||
const form = useForm({
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock/lotes:identify:${lot.record_key}:${lot.version}`,
|
||||
);
|
||||
return <IdentifyFields key={identity} lot={lot} identity={identity} />;
|
||||
}
|
||||
function IdentifyFields({ lot, identity }: { lot: Lot; identity: string }) {
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: lot.record_key,
|
||||
version: lot.version,
|
||||
@@ -456,17 +507,33 @@ function Identify({ lot }: { lot: Lot }) {
|
||||
expires_on: lot.data.expires_on ?? '',
|
||||
no_expiry: lot.data.no_expiry,
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
return (
|
||||
<details className="mt-3">
|
||||
<summary className="cursor-pointer text-sm">
|
||||
Identificar ou corrigir dados do lote
|
||||
</summary>
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-3 grid gap-3 md:grid-cols-2"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/stock/lotes');
|
||||
form.post('/stock/lotes', {
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(next, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<p className="text-sm text-muted-foreground md:col-span-2">
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Head, Link, useForm } from '@inertiajs/react';
|
||||
import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { Head, Link, router, useForm, useRemember } from '@inertiajs/react';
|
||||
import { useState } from 'react';
|
||||
import { InventoryNotice, InventoryPagination } from '@/components/inventory';
|
||||
import { LotCountFields, countTotal } from '@/components/inventory-lot-count';
|
||||
@@ -105,7 +108,20 @@ const kinds: Record<string, string> = {
|
||||
count: 'Contagem física',
|
||||
adjustment: 'Ajuste de inventário',
|
||||
};
|
||||
export default function Operations({
|
||||
export default function Operations(props: Props) {
|
||||
if (props.demo)
|
||||
return (
|
||||
<OperationFields {...props} identity="inventory-operations:demo" />
|
||||
);
|
||||
return <PrivateOperations {...props} />;
|
||||
}
|
||||
function PrivateOperations(props: Props) {
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock/operacoes:${props.canOperate}:${props.canAdjust ?? false}`,
|
||||
);
|
||||
return <OperationFields key={identity} {...props} identity={identity} />;
|
||||
}
|
||||
function OperationFields({
|
||||
items,
|
||||
warehouses,
|
||||
users,
|
||||
@@ -119,9 +135,10 @@ export default function Operations({
|
||||
lotCounts = [],
|
||||
demo = false,
|
||||
equipment = [],
|
||||
}: Props) {
|
||||
identity,
|
||||
}: Props & { identity: string }) {
|
||||
const [message, setMessage] = useState('');
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
kind: canOperate ? 'issue' : 'adjustment',
|
||||
reference: '',
|
||||
idempotency_key: crypto.randomUUID(),
|
||||
@@ -135,7 +152,8 @@ export default function Operations({
|
||||
count_id: '',
|
||||
ledger_version: 0,
|
||||
equipment_id: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const balance = balances.find(
|
||||
(b) =>
|
||||
String(b.stock_item_id) === form.data.stock_item_id &&
|
||||
@@ -146,9 +164,9 @@ export default function Operations({
|
||||
String(s.stock_item_id) === form.data.stock_item_id &&
|
||||
String(s.warehouse_id) === form.data.warehouse_id,
|
||||
);
|
||||
const [countInputs, setCountInputs] = useState<
|
||||
const [countInputs, setCountInputs] = useRemember<
|
||||
Record<string, CountInput[]>
|
||||
>({});
|
||||
>({}, `${identity}:lot-count-inputs`);
|
||||
const countKey = countSource?.hash ?? '';
|
||||
const countRows =
|
||||
countInputs[countKey] ??
|
||||
@@ -163,6 +181,12 @@ export default function Operations({
|
||||
no_expiry: lot.no_expiry,
|
||||
})) ??
|
||||
[];
|
||||
const dirty =
|
||||
hasPreparationChanges(
|
||||
{ ...initial, idempotency_key: '' },
|
||||
{ ...form.data, idempotency_key: '' },
|
||||
) || Object.keys(countInputs).length > 0;
|
||||
const ref = usePreparationDirty(dirty, !demo && (canOperate || canAdjust));
|
||||
return (
|
||||
<>
|
||||
<Head title="Operações de stock" />
|
||||
@@ -173,7 +197,11 @@ export default function Operations({
|
||||
description="Movimentos com fundamento, contagens físicas e histórico preservado."
|
||||
/>
|
||||
<InventoryNotice />
|
||||
{canViewReturns && <Link href="/stock/devolucoes-internas">Conferir devoluções de excedentes</Link>}
|
||||
{canViewReturns && (
|
||||
<Link href="/stock/devolucoes-internas">
|
||||
Conferir devoluções de excedentes
|
||||
</Link>
|
||||
)}
|
||||
{canOperate && form.data.kind === 'issue' && (
|
||||
<label className="grid gap-2 text-sm">
|
||||
Equipamento de destino (obrigatório para peças
|
||||
@@ -206,6 +234,8 @@ export default function Operations({
|
||||
>
|
||||
{canOperate || canAdjust ? (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4 sm:grid-cols-2"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -230,14 +260,31 @@ export default function Operations({
|
||||
}));
|
||||
form.post('/stock/operacoes', {
|
||||
onSuccess: () => {
|
||||
form.setData({
|
||||
const next = {
|
||||
...form.data,
|
||||
idempotency_key:
|
||||
crypto.randomUUID(),
|
||||
reference: '',
|
||||
quantity: '',
|
||||
reason: '',
|
||||
});
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(
|
||||
next,
|
||||
`${identity}:data`,
|
||||
);
|
||||
router.remember(
|
||||
{},
|
||||
`${identity}:errors`,
|
||||
);
|
||||
const remaining = { ...countInputs };
|
||||
delete remaining[countKey];
|
||||
setCountInputs(remaining);
|
||||
router.remember(
|
||||
remaining,
|
||||
`${identity}:lot-count-inputs`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
@@ -537,7 +584,8 @@ export default function Operations({
|
||||
'Devolução ao fornecedor',
|
||||
write_off: 'Abate físico',
|
||||
reversal: 'Estorno integral',
|
||||
workshop_return: 'Devolução de excedente',
|
||||
workshop_return:
|
||||
'Devolução de excedente',
|
||||
}[o.kind] ??
|
||||
o.kind}
|
||||
</strong>
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { useInventoryPreparationIdentity } from '@/hooks/use-inventory-preparation-identity';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import { InventoryEventActions } from '@/components/inventory-event-actions';
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
@@ -86,6 +89,32 @@ const states: Record<string, string> = {
|
||||
};
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
||||
const capabilities = JSON.stringify(
|
||||
Object.entries(props.capabilities).sort(([a], [b]) =>
|
||||
a.localeCompare(b),
|
||||
),
|
||||
);
|
||||
const identity = useInventoryPreparationIdentity(
|
||||
`/stock/estornos:prepare:${old?.record_key ?? 'new'}:${old?.version ?? 0}:${old?.state ?? 'new'}:${capabilities}`,
|
||||
);
|
||||
return (
|
||||
<PrepareFields
|
||||
key={identity}
|
||||
props={props}
|
||||
old={old}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
function PrepareFields({
|
||||
props,
|
||||
old,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
old?: Entry;
|
||||
identity: string;
|
||||
}) {
|
||||
const [operations, setOperations] = useState(props.operations);
|
||||
const [receipts, setReceipts] = useState(props.receipts);
|
||||
const formKey = `reversal:${old?.id ?? 'new'}`;
|
||||
@@ -95,7 +124,7 @@ function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
||||
setReceipts(props.receipts);
|
||||
}
|
||||
}, [props.sourceFormKey, props.operations, props.receipts, formKey]);
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: old?.record_key,
|
||||
version: old?.version ?? 0,
|
||||
@@ -111,7 +140,10 @@ function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
||||
? String(old.data.receipt_item_id)
|
||||
: '',
|
||||
},
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const searchSources = (value: string) =>
|
||||
router.get(
|
||||
'/stock/estornos',
|
||||
@@ -142,10 +174,23 @@ function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
||||
);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="grid gap-4 md:grid-cols-2"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
form.post('/stock/estornos');
|
||||
form.post('/stock/estornos', {
|
||||
onSuccess: () => {
|
||||
const next = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(next);
|
||||
form.clearErrors();
|
||||
router.remember(next, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
<FormErrors errors={form.errors} />
|
||||
@@ -195,6 +240,20 @@ function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
||||
{op.label}
|
||||
</option>
|
||||
))}
|
||||
{form.data.data.source_operation_id &&
|
||||
!operations.some(
|
||||
(op) =>
|
||||
String(op.id) ===
|
||||
form.data.data.source_operation_id,
|
||||
) && (
|
||||
<option
|
||||
value={form.data.data.source_operation_id}
|
||||
disabled
|
||||
>
|
||||
Origem seleccionada — pesquisar para
|
||||
conferir disponibilidade
|
||||
</option>
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
) : (
|
||||
@@ -217,6 +276,20 @@ function Prepare({ props, old }: { props: Props; old?: Entry }) {
|
||||
{receipt.label}
|
||||
</option>
|
||||
))}
|
||||
{form.data.data.receipt_item_id &&
|
||||
!receipts.some(
|
||||
(receipt) =>
|
||||
String(receipt.id) ===
|
||||
form.data.data.receipt_item_id,
|
||||
) && (
|
||||
<option
|
||||
value={form.data.data.receipt_item_id}
|
||||
disabled
|
||||
>
|
||||
Origem seleccionada — pesquisar para
|
||||
conferir disponibilidade
|
||||
</option>
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user