import { JoDetail, JoDetailPickLine } from "@/app/api/jo"; import { decimalFormatter } from "@/app/utils/formatUtil"; import { GridColDef, GridRenderCellParams, GridValidRowModel } from "@mui/x-data-grid"; import { isEmpty, pick, upperFirst } from "lodash"; import { useCallback, useMemo } from "react"; import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import StyledDataGrid from "../StyledDataGrid/StyledDataGrid"; import { Box, Grid, Icon, IconButton, Stack, Typography } from "@mui/material"; import PendingOutlinedIcon from '@mui/icons-material/PendingOutlined'; import CheckCircleOutlineOutlinedIcon from '@mui/icons-material/CheckCircleOutlineOutlined'; import HelpOutlineOutlinedIcon from '@mui/icons-material/HelpOutlineOutlined'; import { fetchInventories } from "@/app/api/inventory/actions"; import { InventoryResult } from "@/app/api/inventory"; import { useEffect, useState } from "react"; import DoDisturbAltRoundedIcon from '@mui/icons-material/DoDisturbAltRounded'; type JoDetailPickLineWithCalculations = JoDetailPickLine & { stockAvailable: number; isStockSufficient: boolean; }; type Props = { }; const PickTable: React.FC = ({ }) => { const { t } = useTranslation("jo") const { watch } = useFormContext() const [inventoryData, setInventoryData] = useState([]); const pickLines = watch("pickLines"); useEffect(() => { const fetchInventoryData = async () => { try { const inventoryResponse = await fetchInventories({ code: "", name: "", type: "", pageNum: 0, pageSize: 1000 }); setInventoryData(inventoryResponse.records); } catch (error) { console.error("Error fetching inventory data:", error); } }; fetchInventoryData(); }, [pickLines]); const getStockAvailable = (pickLine: JoDetailPickLine) => { const inventory = inventoryData.find(inventory => inventory.itemCode === pickLine.code || inventory.itemName === pickLine.name ); if (inventory) { return inventory.availableQty || (inventory.onHandQty - inventory.onHoldQty - inventory.unavailableQty); } return 0; }; const getUomShortDesc = (pickLine: JoDetailPickLine) => { const inventory = inventoryData.find(inventory => inventory.itemCode === pickLine.code || inventory.itemName === pickLine.name ); return inventory?.uomShortDesc; // || pickLine.uom; }; const isStockSufficient = (pickLine: JoDetailPickLine) => { const stockAvailable = getStockAvailable(pickLine); return stockAvailable >= pickLine.reqQty; }; const sufficientStockIcon = useMemo(() => { return }, []); const insufficientStockIcon = useMemo(() => { return }, []); const rowsWithCalculatedFields = useMemo(() => { return pickLines.map((pickLine, index) => ({ ...pickLine, id: pickLine.id || index, sequence: index + 1, stockAvailable: getStockAvailable(pickLine), isStockSufficient: isStockSufficient(pickLine), })); }, [pickLines, inventoryData]); const notPickedStatusColumn = useMemo(() => { return () }, []) const scanStatusColumn = useCallback((status: boolean) => { return status ? : }, []) const columns = useMemo(() => [ { field: "sequence", headerName: t("Sequence"), flex: 0.2, align: "left", headerAlign: "left", type: "number", renderCell: (params: GridRenderCellParams) => { return params.value; }, }, { field: "code", headerName: t("Item Code"), flex: 0.6, }, { field: "name", headerName: t("Item Name"), flex: 1, renderCell: (params: GridRenderCellParams) => { return `${params.value} (${params.row.uom})`; }, }, /*{ field: "scanStatus", headerName: t("Scan Status"), flex: 0.4, align: "right", headerAlign: "right", renderCell: (params: GridRenderCellParams) => { if (params.row.pickedLotNo === null || params.row.pickedLotNo === undefined) { return notPickedStatusColumn } const scanStatus = params.row.pickedLotNo.map((pln) => Boolean(pln.isScanned)) return isEmpty(scanStatus) ? notPickedStatusColumn : {scanStatus.map((status) => scanStatusColumn(status))} }, }, { field: "lotNo", headerName: t("Lot No."), flex: 1, renderCell: (params: GridRenderCellParams) => { if (params.row.pickedLotNo === null || params.row.pickedLotNo === undefined) { return t("Pending for pick") } const lotNos = params.row.pickedLotNo.map((pln) => pln.lotNo) return isEmpty(lotNos) ? t("Pending for pick") : lotNos.map((lotNo) => (<>{lotNo}
)) }, }, { field: "pickedQty", headerName: t("Picked Qty"), flex: 0.7, align: "right", headerAlign: "right", renderCell: (params: GridRenderCellParams) => { if (params.row.pickedLotNo === null || params.row.pickedLotNo === undefined) { return t("Pending for pick") } const qtys = params.row.pickedLotNo.map((pln) => pln.qty) return isEmpty(qtys) ? t("Pending for pick") : qtys.map((qty) => <>{qty}
) }, },*/ { field: "reqQty", headerName: t("Req. Qty"), flex: 0.7, align: "right", headerAlign: "right", renderCell: (params: GridRenderCellParams) => { const uomShortDesc = getUomShortDesc(params.row); return `${decimalFormatter.format(params.value)} ${uomShortDesc}`; }, }, { field: "stockAvailable", headerName: t("Stock Available"), flex: 0.7, align: "right", headerAlign: "right", type: "number", renderCell: (params: GridRenderCellParams) => { const uomShortDesc = getUomShortDesc(params.row); return `${decimalFormatter.format(params.value)} ${uomShortDesc}`; }, }, { field: "stockStatus", headerName: t("Stock Status"), flex: 0.5, align: "right", headerAlign: "right", type: "boolean", renderCell: (params: GridRenderCellParams) => { return params.row.isStockSufficient ? sufficientStockIcon : insufficientStockIcon; }, } /*{ field: "status", headerName: t("Status"), flex: 1, align: "right", headerAlign: "right", renderCell: (params: GridRenderCellParams) => { return ( <> {params.row.pickedLotNo?.every((lotNo) => Boolean(lotNo.isScanned)) ? t("Scanned") : t(upperFirst(params.value))} {scanStatusColumn(Boolean(params.row.pickedLotNo?.every((lotNo) => Boolean(lotNo.isScanned))))} ) }, },*/ ], [t, inventoryData]) return ( <> 'auto'} /> ) } export default PickTable;