[PUI] Stock item forms (#6597)

* Implement "new stock item" button

* Fix adjustFilters for StockForm

* Fix imports
This commit is contained in:
Oliver 2024-02-28 16:43:18 +11:00 committed by GitHub
parent 2e81a304d1
commit 38d013ffe2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 22 deletions

View File

@ -10,7 +10,7 @@ export enum UserRoles {
return_order = 'return_order',
sales_order = 'sales_order',
stock = 'stock',
stock_location = 'stocklocation',
stock_location = 'stock_location',
stocktake = 'stocktake'
}

View File

@ -1,7 +1,10 @@
import { t } from '@lingui/macro';
import { useMemo, useState } from 'react';
import { ApiFormFieldSet } from '../components/forms/fields/ApiFormField';
import {
ApiFormAdjustFilterType,
ApiFormFieldSet
} from '../components/forms/fields/ApiFormField';
import { ApiEndpoints } from '../enums/ApiEndpoints';
import { useCreateApiFormModal, useEditApiFormModal } from '../hooks/UseForm';
@ -37,6 +40,13 @@ export function useStockFields({
part_detail: true,
supplier_detail: true,
...(part ? { part } : {})
},
adjustFilters: (value: ApiFormAdjustFilterType) => {
if (value.data.part) {
value.filters['part'] = value.data.part;
}
return value.filters;
}
},
use_pack_size: {

View File

@ -3,12 +3,17 @@ import { Group, Text } from '@mantine/core';
import { ReactNode, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { AddItemButton } from '../../components/buttons/AddItemButton';
import { formatCurrency, renderDate } from '../../defaults/formatters';
import { ApiEndpoints } from '../../enums/ApiEndpoints';
import { ModelType } from '../../enums/ModelType';
import { UserRoles } from '../../enums/Roles';
import { useStockFields } from '../../forms/StockForms';
import { getDetailUrl } from '../../functions/urls';
import { useCreateApiFormModal } from '../../hooks/UseForm';
import { useTable } from '../../hooks/UseTable';
import { apiUrl } from '../../states/ApiState';
import { useUserState } from '../../states/UserState';
import { TableColumn } from '../Column';
import {
DescriptionColumn,
@ -329,10 +334,39 @@ export function StockItemTable({ params = {} }: { params?: any }) {
let tableFilters = useMemo(() => stockItemTableFilters(), []);
const table = useTable('stockitems');
const user = useUserState();
const navigate = useNavigate();
const stockItemFields = useStockFields({ create: true });
const newStockItem = useCreateApiFormModal({
url: ApiEndpoints.stock_item_list,
title: t`Add Stock Item`,
fields: stockItemFields,
initialData: {
part: params.part,
location: params.location
},
onFormSuccess: (data: any) => {
if (data.pk) {
navigate(getDetailUrl(ModelType.stockitem, data.pk));
}
}
});
const tableActions = useMemo(() => {
return [
<AddItemButton
hidden={!user.hasAddRole(UserRoles.stock)}
tooltip={t`Add Stock Item`}
onClick={() => newStockItem.open()}
/>
];
}, [user]);
return (
<>
{newStockItem.modal}
<InvenTreeTable
url={apiUrl(ApiEndpoints.stock_item_list)}
tableState={table}
@ -341,6 +375,7 @@ export function StockItemTable({ params = {} }: { params?: any }) {
enableDownload: true,
enableSelection: false,
tableFilters: tableFilters,
tableActions: tableActions,
onRowClick: (record) =>
navigate(getDetailUrl(ModelType.stockitem, record.pk)),
params: {
@ -351,5 +386,6 @@ export function StockItemTable({ params = {} }: { params?: any }) {
}
}}
/>
</>
);
}

View File

@ -115,7 +115,7 @@ export function StockLocationTable({ parentId }: { parentId?: any }) {
<AddItemButton
tooltip={t`Add Stock Location`}
onClick={() => newLocation.open()}
disabled={!can_add}
hidden={!can_add}
/>
];
}, [user]);