import React, { useState, useRef, useEffect, useMemo } from "react";
|
import { useWatch, useFormContext } from "react-hook-form";
|
import {
|
CreateBase,
|
useTranslate,
|
TextInput,
|
NumberInput,
|
BooleanInput,
|
DateInput,
|
SaveButton,
|
SelectInput,
|
ReferenceInput,
|
ReferenceArrayInput,
|
AutocompleteInput,
|
Toolbar,
|
required,
|
useDataProvider,
|
useNotify,
|
Form,
|
useCreateController,
|
useListContext,
|
useRefresh,
|
Edit,
|
useRedirect,
|
} from 'react-admin';
|
import {
|
Dialog,
|
DialogActions,
|
DialogContent,
|
DialogTitle,
|
Stack,
|
Grid,
|
TextField,
|
Box,
|
Button,
|
Paper,
|
TableContainer,
|
Table,
|
TableHead,
|
TableBody,
|
TableRow,
|
TableCell,
|
Tooltip,
|
IconButton,
|
styled,
|
Select,
|
MenuItem,
|
Typography,
|
Card,
|
} from '@mui/material';
|
import { EDIT_MODE, REFERENCE_INPUT_PAGESIZE } from '@/config/setting';
|
import ConfirmButton from "../../components/ConfirmButton";
|
import TreeSelectInput from "@/page/components/TreeSelectInput";
|
import { DataGrid, useGridApiRef } from '@mui/x-data-grid';
|
import DictSelect from "../../components/DictSelect";
|
import AddIcon from '@mui/icons-material/Add';
|
import DeleteIcon from '@mui/icons-material/Delete';
|
import request from '@/utils/request';
|
import LocItemInfoModal from "../components/locItemInfoModal";
|
import { Delete } from '@mui/icons-material';
|
import _, { set } from 'lodash';
|
import StaSelect from "../components/StaSelect";
|
import { redirect } from "react-router";
|
import { number } from "prop-types";
|
|
const OutBoundList = () => {
|
|
const [createDialog, setCreateDialog] = useState(false);
|
const [tabelData, setTableData] = useState([]);
|
const [selectedRows, setSelectedRows] = useState([]);
|
const [sta, setSta] = useState("");
|
const notify = useNotify();
|
const tableRef = useRef();
|
tableRef.current = useGridApiRef();
|
const translate = useTranslate();
|
|
const handleDeleteItem = () => {
|
const newTableData = _.filter(tabelData, (item) => !selectedRows.includes(item.matnrId));
|
setTableData(newTableData);
|
}
|
|
// 添加一个处理新数据的函数,设置outQty默认值
|
const handleSetData = (newData) => {
|
// 为新添加的数据设置outQty默认值为anfme的值
|
const dataWithDefaultQty = newData.map(item => ({
|
...item,
|
outQty: item.outQty || item.anfme // 如果outQty已存在则保留,否则使用anfme的值
|
}));
|
setTableData([...tabelData, ...dataWithDefaultQty]);
|
};
|
|
|
|
return (
|
<>
|
<Card sx={{ p: 2, mb: 2, mt: 2 }}>
|
<Grid container spacing={2}>
|
<Grid item xs={12}>
|
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 1 }}>
|
<Typography variant="h6">
|
{translate('table.field.outBound.stockWithdrawal')}
|
</Typography>
|
<Stack direction='row' spacing={2}>
|
<Button
|
variant="contained"
|
color="primary"
|
startIcon={<AddIcon />}
|
onClick={() => setCreateDialog(true)}
|
>
|
{translate('table.field.outBound.withdrawal')}
|
</Button>
|
</Stack>
|
</Box>
|
</Grid>
|
</Grid>
|
</Card>
|
<Card sx={{ p: 2, mb: 2 }}>
|
<Form>
|
<Grid container spacing={2}>
|
<Grid item xs={12}>
|
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 1 }}>
|
<Typography variant="h6" >
|
{translate('table.field.outBound.outSta')}
|
</Typography>
|
<Stack direction='row' spacing={2} minWidth={200}>
|
<StaSelect
|
source="sta"
|
label={translate("table.field.outBound.outSta")}
|
onChange={(e) => {
|
setSta(e.target.value);
|
console.log("站点已选择:", e.target.value);
|
}}
|
size="small"
|
type="[101,103]"
|
/>
|
</Stack>
|
<Stack direction='row' spacing={2} minWidth={200}>
|
<SubmitButton
|
sta={sta}
|
data={tabelData}
|
setTableData={setTableData}
|
/>
|
</Stack>
|
</Box>
|
</Grid>
|
</Grid>
|
</Form>
|
</Card>
|
<Card sx={{ mb: 2 }}>
|
<Box sx={{}}>
|
<ModalTable tabelData={tabelData} setTableData={setTableData} selectedRows={selectedRows} setSelectedRows={setSelectedRows} tableRef={tableRef}></ModalTable>
|
</Box>
|
</Card>
|
<LocItemInfoModal
|
open={createDialog}
|
setOpen={setCreateDialog}
|
data={tabelData}
|
setData={handleSetData}
|
/>
|
|
</>
|
)
|
}
|
|
export default OutBoundList;
|
|
const SubmitButton = (props) => {
|
const translate = useTranslate();
|
const notify = useNotify();
|
const redirect = useRedirect();
|
const refresh = useRefresh();
|
const { sta, data, setTableData } = props;
|
const check = () => {
|
if (sta === "" || sta === undefined || sta === null) {
|
notify("请选择站点");
|
return;
|
}
|
if (data.length === 0) {
|
notify("请选择物料");
|
return;
|
}
|
http(sta, data);
|
}
|
const http = async (sta, items) => {
|
console.log(items);
|
|
const filter = items.filter(item => (item.outQty + item.workQty) > item.anfme);
|
if (filter.length > 0) {
|
notify(translate('toolbar.request.error.out_stock_qty'))
|
return
|
}
|
const { data: { code, data, msg } } = await request.post(`/locItem/generate/task`, { siteNo: sta, items: items });
|
if (code === 200) {
|
notify(msg);
|
refresh()
|
setTableData([])
|
redirect("/task")
|
} else {
|
notify(msg);
|
}
|
}
|
return (
|
<ConfirmButton
|
variant="contained"
|
color="primary"
|
onConfirm={check}
|
label={"table.field.outBound.createTask"}
|
>
|
</ConfirmButton>
|
)
|
|
}
|
|
const ModalTable = ({ tabelData, setTableData, selectedRows, setSelectedRows, tableRef }) => {
|
const translate = useTranslate();
|
const notify = useNotify();
|
|
const [columns, setColumns] = useState([
|
{
|
field: 'outQty',
|
headerName: translate('table.field.outBound.outQty') + "*",
|
width: 100,
|
type: 'number',
|
editable: true,
|
headerClassName: "custom",
|
},
|
{
|
field: 'anfme',
|
headerName: translate('table.field.locItem.anfme'),
|
type: 'number',
|
width: 100,
|
editable: false,
|
},
|
{
|
field: 'workQty',
|
headerName: translate('table.field.locItem.workQty'),
|
width: 100,
|
type: 'number',
|
editable: false,
|
},
|
{
|
field: 'locCode',
|
headerName: translate('table.field.locItem.locCode'),
|
width: 100,
|
editable: false,
|
},
|
{
|
field: 'matnrCode',
|
headerName: translate('table.field.locItem.matnrCode'),
|
width: 130,
|
editable: false,
|
},
|
{
|
field: 'maktx',
|
headerName: translate('table.field.locItem.maktx'),
|
width: 250,
|
editable: false,
|
},
|
{
|
field: 'batch',
|
headerName: translate('table.field.locItem.batch'),
|
width: 250,
|
editable: false,
|
},
|
])
|
|
const action = {
|
field: 'action',
|
headerName: '操作',
|
width: 70,
|
lockPosition: 'left',
|
renderCell: (params) => (
|
<Tooltip title="Delete">
|
<IconButton onClick={() => handleDelete(params.row)}>
|
<Delete />
|
</IconButton>
|
</Tooltip>
|
),
|
|
}
|
|
let cdata = useRef([]);
|
|
useEffect(() => {
|
getDynamicFields();
|
}, []);
|
|
useEffect(() => {
|
cdata.current = tabelData
|
}, [tabelData]);
|
|
const getDynamicFields = async () => {
|
const {
|
data: { code, data, msg },
|
} = await request.get("/fields/enable/list");
|
if (code === 200) {
|
const cols = data.map(el => ({
|
field: el.fields,
|
headerName: el.fieldsAlise,
|
minWidth: 100,
|
flex: 1,
|
editable: false
|
}))
|
setColumns([...columns, ...cols, action])
|
} else {
|
notify(msg);
|
}
|
}
|
|
|
const handleDelete = (row) => {
|
const newData = _.filter(cdata.current, (item) => item.id !== row.id);
|
setTableData(newData);
|
};
|
|
|
const processRowUpdate = (newRow, oldRow) => {
|
const rows = tabelData.map((r) =>
|
r.id === newRow.id ? { ...newRow } : r
|
)
|
setTableData(rows)
|
return newRow;
|
};
|
|
const handleSelectionChange = (ids) => {
|
setSelectedRows(ids)
|
};
|
|
tableRef.current = useGridApiRef();
|
|
return (
|
<div style={{ height: 500, width: '100%' }}>
|
<DataGrid
|
apiRef={tableRef}
|
rows={tabelData}
|
columns={columns}
|
disableRowSelectionOnClick
|
getRowId={(row) => row.matnrId ? row.matnrId : row.id}
|
disableColumnFilter
|
disableColumnSelector
|
disableColumnSorting
|
disableMultipleColumnsSorting
|
processRowUpdate={processRowUpdate}
|
initialState={{
|
pagination: {
|
paginationModel: {
|
pageSize: 25,
|
},
|
},
|
}}
|
pageSizeOptions={[10, 25, 50, 100]}
|
editMode="row"
|
checkboxSelection
|
onRowSelectionModelChange={handleSelectionChange}
|
selectionModel={selectedRows}
|
sx={{
|
'& .MuiDataGrid-cell input': {
|
border: '1px solid #ccc'
|
},
|
}}
|
/>
|
</div>
|
);
|
};
|