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