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