FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

128 line
4.3 KiB

  1. import { DoDetail, DoDetailLine } from "@/app/api/do/actions";
  2. import { decimalFormatter } from "@/app/utils/formatUtil";
  3. import { GridColDef, GridRenderCellParams } from "@mui/x-data-grid";
  4. import { isEmpty } from "lodash";
  5. import { useMemo } from "react";
  6. import { useFormContext } from "react-hook-form";
  7. import { useTranslation } from "react-i18next";
  8. import StyledDataGrid from "../StyledDataGrid/StyledDataGrid";
  9. import CheckCircleOutlineOutlinedIcon from '@mui/icons-material/CheckCircleOutlineOutlined';
  10. import DoDisturbAltRoundedIcon from '@mui/icons-material/DoDisturbAltRounded';
  11. type DoLineRow = DoDetailLine & {
  12. stockAvailable: number;
  13. isStockSufficient: boolean;
  14. };
  15. type Props = Record<string, never>;
  16. const getStockAvailable = (line: DoDetailLine): number => {
  17. if (line.stockQty != null && !Number.isNaN(Number(line.stockQty))) {
  18. return Number(line.stockQty);
  19. }
  20. return 0;
  21. };
  22. const isStockSufficient = (line: DoDetailLine): boolean => {
  23. if (line.availableStatus === "available") return true;
  24. if (line.availableStatus === "insufficient") return false;
  25. return getStockAvailable(line) >= (line.qty ?? 0);
  26. };
  27. const DoLineTable: React.FC<Props> = () => {
  28. const { t } = useTranslation("do");
  29. const { watch } = useFormContext<DoDetail>();
  30. const deliveryOrderLines = watch("deliveryOrderLines");
  31. const sufficientStockIcon = useMemo(
  32. () => <CheckCircleOutlineOutlinedIcon fontSize={"large"} color="success" />,
  33. [],
  34. );
  35. const insufficientStockIcon = useMemo(
  36. () => <DoDisturbAltRoundedIcon fontSize={"large"} color="error" />,
  37. [],
  38. );
  39. const rowsWithCalculatedFields = useMemo((): DoLineRow[] => {
  40. return deliveryOrderLines.map((line, index) => ({
  41. ...line,
  42. id: line.id || index,
  43. stockAvailable: getStockAvailable(line),
  44. isStockSufficient: isStockSufficient(line),
  45. }));
  46. }, [deliveryOrderLines]);
  47. const columns = useMemo<GridColDef[]>(() => [
  48. {
  49. field: "itemNo",
  50. headerName: t("Item No."),
  51. flex: 0.6,
  52. },
  53. {
  54. field: "itemName",
  55. headerName: t("Item Name"),
  56. flex: 1,
  57. renderCell: (params: GridRenderCellParams<DoLineRow>) => {
  58. const name = isEmpty(params.value) ? "N/A" : params.value;
  59. const uom = params.row.uom || "";
  60. return `${name} (${uom})`;
  61. },
  62. },
  63. {
  64. field: "qty",
  65. headerName: t("Quantity"),
  66. flex: 0.7,
  67. align: "right",
  68. headerAlign: "right",
  69. renderCell: (params: GridRenderCellParams<DoLineRow>) => {
  70. return `${decimalFormatter.format(params.value)} (${params.row.shortUom})`;
  71. },
  72. },
  73. {
  74. field: "stockAvailable",
  75. headerName: t("Stock Available"),
  76. flex: 0.7,
  77. align: "right",
  78. headerAlign: "right",
  79. type: "number",
  80. renderCell: (params: GridRenderCellParams<DoLineRow>) => {
  81. return `${decimalFormatter.format(params.value)} (${params.row.shortUom})`;
  82. },
  83. },
  84. {
  85. field: "stockStatus",
  86. headerName: t("Stock Status"),
  87. flex: 0.5,
  88. align: "right",
  89. headerAlign: "right",
  90. type: "boolean",
  91. renderCell: (params: GridRenderCellParams<DoLineRow>) => {
  92. return params.row.isStockSufficient ? sufficientStockIcon : insufficientStockIcon;
  93. },
  94. },
  95. ], [t, sufficientStockIcon, insufficientStockIcon]);
  96. return (
  97. <StyledDataGrid
  98. sx={{
  99. "--DataGrid-overlayHeight": "100px",
  100. ".MuiDataGrid-row .MuiDataGrid-cell.hasError": {
  101. border: "1px solid",
  102. borderColor: "error.main",
  103. },
  104. ".MuiDataGrid-row .MuiDataGrid-cell.hasWarning": {
  105. border: "1px solid",
  106. borderColor: "warning.main",
  107. },
  108. }}
  109. disableColumnMenu
  110. rows={rowsWithCalculatedFields}
  111. columns={columns}
  112. getRowHeight={() => 'auto'}
  113. />
  114. );
  115. };
  116. export default DoLineTable;