import React, { useState, useRef, useEffect, useMemo } from "react";
|
import {
|
CreateBase,
|
useTranslate,
|
TextInput,
|
NumberInput,
|
BooleanInput,
|
DateInput,
|
SaveButton,
|
SelectInput,
|
ReferenceInput,
|
ReferenceArrayInput,
|
AutocompleteInput,
|
Toolbar,
|
required,
|
useDataProvider,
|
useNotify,
|
Form,
|
useCreateController,
|
} from 'react-admin';
|
import {
|
Dialog,
|
DialogActions,
|
DialogContent,
|
DialogTitle,
|
TextField,
|
IconButton,
|
MenuItem,
|
Tooltip,
|
Select,
|
Button,
|
Stack,
|
Grid,
|
Box,
|
} from '@mui/material';
|
import DialogCloseButton from "../../components/DialogCloseButton.jsx";
|
import ConfirmationNumberIcon from '@mui/icons-material/ConfirmationNumber';
|
import CreateBySelectMats from "./CreateBySelectMats.jsx";
|
import { DataGrid, useGridApiRef } from '@mui/x-data-grid';
|
import { Add, Edit, Delete } from '@mui/icons-material';
|
import ConfirmButton from "../../components/ConfirmButton";
|
import DictSelect from "../../components/DictSelect";
|
import { minHeight, padding } from "@mui/system";
|
import SaveIcon from '@mui/icons-material/Save';
|
import request from '@/utils/request';
|
|
const ManualCreate = (props) => {
|
const { open, setOpen, orderId } = props;
|
const tableRef = useRef();
|
const notify = useNotify();
|
const translate = useTranslate();
|
const [tabelData, setTableData] = useState([]);
|
const [disabled, setDisabled] = useState(false);
|
const [selectedRows, setSelectedRows] = useState([]);
|
const [createDialog, setCreateDialog] = useState(false);
|
const [formData, setFormData] = useState({});
|
const handleClose = (event, reason) => {
|
if (reason !== "backdropClick") {
|
setOpen(false);
|
}
|
};
|
|
const handleSubmit = async () => {
|
setFinally()
|
setDisabled(true)
|
|
if (asnId === 0) {
|
const parmas = {
|
"orders": formData,
|
"items": tabelData,
|
}
|
const res = await request.post(`/outStock/items/save`, parmas);
|
if (res?.data?.code === 200) {
|
setOpen(false);
|
refresh();
|
resetData()
|
} else {
|
notify(res.data.msg);
|
}
|
} else {
|
const parmas = {
|
"orders": formData,
|
"items": tabelData,
|
}
|
const res = await request.post(`/outStock/items/update`, parmas);
|
if (res?.data?.code === 200) {
|
setOpen(false);
|
refresh();
|
resetData()
|
} else {
|
notify(res.data.msg);
|
}
|
}
|
setDisabled(false)
|
|
};
|
|
const handleSuccess = async (data) => {
|
setOpen(false);
|
notify('common.response.success');
|
};
|
|
const handleDeleteItem = () => {
|
const newTableData = _.filter(tabelData, (item) => !selectedRows.includes(item.matnrId));
|
setTableData(newTableData);
|
}
|
|
const handleError = async (error) => {
|
notify(error.message || 'common.response.fail', { type: 'error', messageArgs: { _: error.message } });
|
};
|
|
return (
|
<>
|
<Box sx={{ padding: 1 }}>
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
|
<Form defaultValues={formData}>
|
<Grid container spacing={2}>
|
<Grid item md={2}>
|
<DictSelect
|
label={translate("table.field.transfer.type")}
|
value={formData.wkType}
|
variant="filled"
|
group='2'
|
onChange={(e) => handleChange(e.target.value, 'wkType')}
|
dictTypeCode="sys_business_type"
|
required
|
/>
|
</Grid>
|
<Grid item md={2}>
|
<ReferenceInput reference="warehouseAreas" source="name">
|
<AutocompleteInput optionText="name" label="table.field.transfer.orgAreaName" />
|
</ReferenceInput>
|
</Grid>
|
<Grid item md={2}>
|
<ReferenceInput reference="warehouseAreas" source="name">
|
<AutocompleteInput optionText="name" label="table.field.transfer.orgAreaName" />
|
</ReferenceInput>
|
</Grid>
|
</Grid>
|
</Form>
|
</Box>
|
<Box sx={{ mt: 2 }}>
|
<Stack direction="row" spacing={2} sx={{ justifyContent: "flex-end" }}>
|
<Button variant="contained" onClick={() => setCreateDialog(true)} >
|
{translate('common.action.newAddMats')}
|
</Button>
|
<ConfirmButton label={"toolbar.delete"} variant="outlined" color="error" onConfirm={handleDeleteItem} />
|
</Stack>
|
</Box>
|
<Box sx={{ mt: 2 }}>
|
<TransferTableView
|
tabelData={tabelData}
|
setTableData={setTableData}
|
asnId={orderId}
|
selectedRows={selectedRows}
|
setSelectedRows={setSelectedRows}
|
tableRef={tableRef}>
|
</TransferTableView>
|
</Box>
|
<Toolbar sx={{ width: '100%', justifyContent: 'flex-end', bgcolor: 'white' }} >
|
<Button disabled={disabled} onClick={handleSubmit} variant="contained" startIcon={<SaveIcon />}>
|
{translate('toolbar.confirm')}
|
</Button>
|
</Toolbar>
|
<CreateBySelectMats
|
open={createDialog}
|
setOpen={setCreateDialog}
|
data={tabelData}
|
setData={setTableData}
|
/>
|
</Box>
|
</>
|
)
|
}
|
|
const SelectInputSplrNameEditCell = (params) => {
|
const [formData, setFormData] = useState([{}])
|
useEffect(() => {
|
getOptions();
|
}, []);
|
const getOptions = async () => {
|
const parmas = {
|
"type": "supplier"
|
}
|
const {
|
data: { code, data, msg },
|
} = await request.post("companys/page", parmas);
|
if (code === 200) {
|
setFormData(data.records)
|
} else {
|
notify(msg);
|
}
|
}
|
|
return (
|
<Select
|
value={params.value}
|
onChange={(e) => {
|
params.api.setEditCellValue({
|
id: params.id,
|
field: params.field,
|
value: e.target.value,
|
})
|
// 找到选中的供应商记录
|
const selectedSupplier = formData.find(supplier => supplier.name === e.target.value);
|
|
// 如果找到对应的供应商记录,同时更新splrCode字段
|
if (selectedSupplier) {
|
params.api.setEditCellValue({
|
id: params.id,
|
field: 'splrCode',
|
value: selectedSupplier.id,
|
});
|
}
|
}}
|
fullWidth
|
>
|
{formData.map(e => {
|
return (
|
<MenuItem value={e.name} children={e.name} key={e.id} />
|
);
|
|
})}
|
|
</Select>
|
);
|
};
|
|
const SelectInputSplrCodeEditCell = (params) => {
|
const [formData, setFormData] = useState([{}])
|
useEffect(() => {
|
getOptions();
|
}, []);
|
const getOptions = async () => {
|
const parmas = {
|
"type": "supplier"
|
}
|
const {
|
data: { code, data, msg },
|
} = await request.post("companys/page", parmas);
|
if (code === 200) {
|
setFormData(data.records)
|
} else {
|
notify(msg);
|
}
|
}
|
|
return (
|
<Select
|
value={params.value}
|
onChange={(e) => {
|
params.api.setEditCellValue({
|
id: params.id,
|
field: params.field,
|
value: e.target.value,
|
})
|
const selectedSupplier = formData.find(supplier => supplier.id === e.target.value);
|
|
// 如果找到对应的供应商记录,同时更新splrCode字段
|
if (selectedSupplier) {
|
params.api.setEditCellValue({
|
id: params.id,
|
field: 'splrName',
|
value: selectedSupplier.name,
|
});
|
}
|
}}
|
fullWidth
|
|
>
|
{formData.map(e => {
|
return (
|
<MenuItem value={e.id} children={e.name} key={e.id} />
|
);
|
|
})}
|
|
</Select>
|
);
|
};
|
|
|
|
const TransferTableView = ({ tabelData, setTableData, orderId, selectedRows, setSelectedRows, tableRef }) => {
|
const translate = useTranslate();
|
const notify = useNotify();
|
const [extendColumns, setExtendColumns] = useState([]);
|
const [columns, setColumns] = useState([
|
{
|
field: 'maktx',
|
headerName: translate('table.field.outStockItem.maktx'),
|
width: 250,
|
editable: false,
|
},
|
{
|
field: 'matnrCode',
|
headerName: translate('table.field.outStockItem.matnrCode'),
|
width: 130,
|
editable: false,
|
},
|
{
|
field: 'anfme',
|
headerName: translate('table.field.outStockItem.anfme') + "*",
|
type: 'number',
|
minWidth: 100,
|
flex: 1,
|
editable: true,
|
valueFormatter: (val) => val < 0 ? 0 : val,
|
headerClassName: "custom",
|
},
|
{
|
field: 'splrCode',
|
headerName: translate('table.field.outStockItem.splrCode') + "*",
|
minWidth: 100,
|
flex: 1,
|
editable: true,
|
renderEditCell: (params) => (
|
<SelectInputSplrCodeEditCell {...params} />
|
),
|
headerClassName: "custom",
|
},
|
{
|
field: 'splrName',
|
headerName: translate('table.field.outStockItem.splrName') + "*",
|
minWidth: 100,
|
flex: 1,
|
editable: true,
|
renderEditCell: (params) => (
|
<SelectInputSplrNameEditCell {...params} />
|
),
|
headerClassName: "custom",
|
},
|
{
|
field: 'splrBatch',
|
headerName: translate('table.field.outStockItem.splrBatch'),
|
minWidth: 100,
|
flex: 1,
|
editable: true,
|
},
|
{
|
field: 'poCode',
|
headerName: translate('table.field.outStockItem.poDetlCode'),
|
minWidth: 100,
|
flex: 1,
|
editable: true,
|
},
|
{
|
field: 'stockUnit',
|
headerName: translate('table.field.outStockItem.stockUnit'),
|
minWidth: 100,
|
flex: 1,
|
editable: true,
|
},
|
])
|
|
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(() => {
|
if (extendColumns == undefined || extendColumns.length < 1) {
|
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])
|
setExtendColumns(cols);
|
} else {
|
notify(msg);
|
}
|
}
|
|
|
const handleDelete = (row) => {
|
const newData = _.filter(cdata.current, (item) => item.matnrId !== row.matnrId);
|
setTableData(newData);
|
};
|
|
|
const processRowUpdate = (newRow, oldRow) => {
|
const rows = tabelData.map((r) =>
|
r.matnrId === newRow.matnrId ? { ...newRow } : r
|
)
|
setTableData(rows)
|
return newRow;
|
};
|
|
const handleSelectionChange = (ids) => {
|
setSelectedRows(ids)
|
};
|
|
tableRef.current = useGridApiRef();
|
|
return (
|
<Box>
|
<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={[15, 25, 50, 100]}
|
editMode="row"
|
checkboxSelection
|
onRowSelectionModelChange={handleSelectionChange}
|
selectionModel={selectedRows}
|
sx={{
|
height: 610,
|
'& .MuiDataGrid-cell input': {
|
border: '1px solid #ccc'
|
},
|
}}
|
/>
|
{/* <Box sx={{
|
padding: 2,
|
position: 'absolute',
|
right: 1
|
}}>
|
<Button variant="contained" onClick={() => handleSubmit()} startIcon={<SaveIcon/>} >
|
{translate('toolbar.confirm')}
|
</Button>
|
</Box> */}
|
</Box>
|
);
|
};
|
|
|
export default ManualCreate;
|