@@ -1,5 +1,15 @@
|
||||
import { Head, Link, router, useForm } from '@inertiajs/react';
|
||||
import {
|
||||
Head,
|
||||
Link,
|
||||
router,
|
||||
useForm,
|
||||
usePage,
|
||||
useRemember,
|
||||
} from '@inertiajs/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { usePreparationDirty } from '@/hooks/use-preparation-dirty';
|
||||
import { hasPreparationChanges } from '@/lib/requisition-history';
|
||||
import type { WorkContextState } from '@/types/work-context';
|
||||
import {
|
||||
ReceiptConversionFields,
|
||||
type ReceiptConversion,
|
||||
@@ -120,13 +130,43 @@ const quantity = (milli: number) =>
|
||||
(milli / 1000).toLocaleString('pt-MZ', { maximumFractionDigits: 3 });
|
||||
|
||||
function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
const [conversion, setConversion] = useState<ReceiptConversion>(
|
||||
old?.snapshot.quantity_conversion ?? {
|
||||
numerator: '',
|
||||
denominator: '',
|
||||
source: '',
|
||||
version: '',
|
||||
},
|
||||
const identity = `${useCoordinationIdentity(props, old)}:prepare`;
|
||||
return (
|
||||
<PrepareForm
|
||||
key={identity}
|
||||
props={props}
|
||||
old={old}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function useCoordinationIdentity(props: Props, record?: RecordVersion) {
|
||||
const page = usePage().props;
|
||||
const actor = page.auth?.user;
|
||||
const context = page.workContext as WorkContextState | null | undefined;
|
||||
return `supply-coordination:${actor?.tenant_id}:${actor?.id}:${props.kind}:${props.ready}:${props.canPrepare}:${props.canReview}:${props.conversionQaEnabled ?? false}:${record?.record_key ?? 'new'}:${record?.version ?? 0}:${record?.state ?? 'new'}:${record ? (props.pending[record.record_key] ?? '') : ''}${context?.active ? `:context:${context.active}` : ''}`;
|
||||
}
|
||||
|
||||
function PrepareForm({
|
||||
props,
|
||||
old,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
old?: RecordVersion;
|
||||
identity: string;
|
||||
}) {
|
||||
const initialConversion: ReceiptConversion = old?.snapshot
|
||||
.quantity_conversion ?? {
|
||||
numerator: '',
|
||||
denominator: '',
|
||||
source: '',
|
||||
version: '',
|
||||
};
|
||||
const [conversion, setConversion] = useRemember<ReceiptConversion>(
|
||||
initialConversion,
|
||||
`${identity}:conversion`,
|
||||
);
|
||||
const [orderLines, setOrderLines] = useState(props.orderLines);
|
||||
const [requestLines, setRequestLines] = useState(props.requestLines);
|
||||
@@ -173,14 +213,20 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
if (old.data.quantity_milli !== undefined)
|
||||
defaults.quantity = Number(old.data.quantity_milli) / 1000;
|
||||
}
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: old?.record_key,
|
||||
version: old?.version ?? 0,
|
||||
action: 'save',
|
||||
reason: '',
|
||||
data: defaults,
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const dirty = hasPreparationChanges(
|
||||
{ ...initial, conversion: initialConversion },
|
||||
{ ...form.data, conversion },
|
||||
);
|
||||
const ref = usePreparationDirty(dirty);
|
||||
const documentUnit =
|
||||
orderLines.find(
|
||||
(line) => line.id === Number(form.data.data.purchase_order_item_id),
|
||||
@@ -270,6 +316,8 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
);
|
||||
return (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="mt-4 space-y-4"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -291,8 +339,21 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
});
|
||||
form.post(`/compras/fornecimento/${props.kind}`, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () =>
|
||||
form.setData('operation_key', crypto.randomUUID()),
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
setConversion(initialConversion);
|
||||
router.remember(clean, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
router.remember(
|
||||
initialConversion,
|
||||
`${identity}:conversion`,
|
||||
);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
@@ -542,14 +603,33 @@ function Prepare({ props, old }: { props: Props; old?: RecordVersion }) {
|
||||
}
|
||||
|
||||
function Actions({ props, record }: { props: Props; record: RecordVersion }) {
|
||||
const [action, setAction] = useState('');
|
||||
const identity = `${useCoordinationIdentity(props, record)}:action:${record.canApprove}:${record.canReturn}:${record.canSuspend}:${record.canEdit}:${record.canFulfil}:${record.canCancel}`;
|
||||
return (
|
||||
<ActionsForm
|
||||
key={identity}
|
||||
props={props}
|
||||
record={record}
|
||||
identity={identity}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function ActionsForm({
|
||||
props,
|
||||
record,
|
||||
identity,
|
||||
}: {
|
||||
props: Props;
|
||||
record: RecordVersion;
|
||||
identity: string;
|
||||
}) {
|
||||
const [issueSearch, setIssueSearch] = useState(props.issueSearch ?? '');
|
||||
const [operationOptions, setOperationOptions] = useState(props.operations);
|
||||
useEffect(() => {
|
||||
if (props.issueRecordId === record.id)
|
||||
setOperationOptions(props.operations);
|
||||
}, [props.issueRecordId, props.operations, record.id]);
|
||||
const form = useForm({
|
||||
const initial = {
|
||||
operation_key: crypto.randomUUID(),
|
||||
record_key: record.record_key,
|
||||
version: record.version,
|
||||
@@ -557,7 +637,11 @@ function Actions({ props, record }: { props: Props; record: RecordVersion }) {
|
||||
reason: '',
|
||||
data: { stock_operation_id: '', quantity_milli: 0 },
|
||||
quantity: '',
|
||||
});
|
||||
};
|
||||
const form = useForm(identity, initial);
|
||||
const action = form.data.action;
|
||||
const dirty = hasPreparationChanges(initial, form.data);
|
||||
const ref = usePreparationDirty(dirty, Boolean(action));
|
||||
const choices =
|
||||
props.kind === 'alternative'
|
||||
? [
|
||||
@@ -620,7 +704,6 @@ function Actions({ props, record }: { props: Props; record: RecordVersion }) {
|
||||
className={controlClass}
|
||||
value={action}
|
||||
onChange={(e) => {
|
||||
setAction(e.target.value);
|
||||
form.setData('action', e.target.value);
|
||||
}}
|
||||
>
|
||||
@@ -635,6 +718,8 @@ function Actions({ props, record }: { props: Props; record: RecordVersion }) {
|
||||
)}
|
||||
{action && (
|
||||
<form
|
||||
ref={ref}
|
||||
data-preparation-dirty={dirty ? 'true' : 'false'}
|
||||
className="space-y-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
@@ -649,6 +734,16 @@ function Actions({ props, record }: { props: Props; record: RecordVersion }) {
|
||||
}));
|
||||
form.post(`/compras/fornecimento/${props.kind}`, {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
const clean = {
|
||||
...initial,
|
||||
operation_key: crypto.randomUUID(),
|
||||
};
|
||||
form.setData(clean);
|
||||
form.clearErrors();
|
||||
router.remember(clean, `${identity}:data`);
|
||||
router.remember({}, `${identity}:errors`);
|
||||
},
|
||||
});
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user