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