FPSMS-frontend
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DoLineTable.tsx 4.3 KiB

1ヶ月前
10ヶ月前
9ヶ月前
1ヶ月前
10ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
10ヶ月前
1ヶ月前
10ヶ月前
1ヶ月前
10ヶ月前
1ヶ月前
10ヶ月前
1ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
10ヶ月前
9ヶ月前
10ヶ月前
1ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
10ヶ月前
9ヶ月前
10ヶ月前
1ヶ月前
9ヶ月前
10ヶ月前
9ヶ月前
10ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
10ヶ月前
9ヶ月前
1ヶ月前
9ヶ月前
10ヶ月前
1ヶ月前
10ヶ月前
1ヶ月前
10ヶ月前
1ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;