@@ -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>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user