FPSMS-frontend
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

123 рядки
4.1 KiB

  1. import { Button, Card, CardContent, Stack, Typography, Box } from "@mui/material";
  2. import { useTranslation } from "react-i18next";
  3. import { JoDetailPickLine } from "@/app/api/jo";
  4. import { fetchInventories } from "@/app/api/inventory/actions";
  5. import { InventoryResult } from "@/app/api/inventory";
  6. import { getStockAvailableFromInventories } from "@/app/api/inventory/inventoryBucket";
  7. import { useEffect, useState, useMemo } from "react";
  8. import { useFormContext } from "react-hook-form";
  9. import { JoDetail } from "@/app/api/jo";
  10. type Props = {
  11. onActionClick?: () => void;
  12. pickLines: JoDetailPickLine[];
  13. handleRelease: () => void;
  14. }
  15. const JoRelease: React.FC<Props> = ({
  16. onActionClick,
  17. pickLines,
  18. handleRelease
  19. }) => {
  20. const { t } = useTranslation("jo");
  21. const [inventoryData, setInventoryData] = useState<InventoryResult[]>([]);
  22. const { watch } = useFormContext<JoDetail>();
  23. const status = useMemo(() => {
  24. const currentStatus = watch("status").toLowerCase();
  25. console.log("JoRelease status:", currentStatus, "id:", pickLines[0]?.id);
  26. return currentStatus;
  27. }, [watch("status")])
  28. useEffect(() => {
  29. const fetchInventoryData = async () => {
  30. try {
  31. const inventoryResponse = await fetchInventories({
  32. code: "",
  33. name: "",
  34. type: "",
  35. pageNum: 0,
  36. pageSize: 1000
  37. });
  38. setInventoryData(inventoryResponse.records);
  39. } catch (error) {
  40. console.error("Error fetching inventory data:", error);
  41. }
  42. };
  43. fetchInventoryData();
  44. }, [pickLines]);
  45. const getStockAvailable = (pickLine: JoDetailPickLine) => {
  46. return getStockAvailableFromInventories(inventoryData, {
  47. itemId: pickLine.itemId,
  48. itemCode: pickLine.code,
  49. itemName: pickLine.name,
  50. uomId: pickLine.uomId,
  51. uom: pickLine.uom,
  52. shortUom: pickLine.shortUom,
  53. });
  54. };
  55. const isStockSufficient = (pickLine: JoDetailPickLine) => {
  56. const stockAvailable = getStockAvailable(pickLine);
  57. return stockAvailable >= pickLine.reqQty;
  58. };
  59. const stockCounts = useMemo(() => {
  60. const totalLines = pickLines.length;
  61. const nonMatAndNonItemLines = pickLines.filter(pickLine =>
  62. pickLine.type !== 'mat' && pickLine.type !== 'item'
  63. );
  64. const sufficientLines = nonMatAndNonItemLines.filter(pickLine => isStockSufficient(pickLine)).length;
  65. const insufficientLines = nonMatAndNonItemLines.length - sufficientLines;
  66. return {
  67. total: totalLines,
  68. sufficient: sufficientLines,
  69. insufficient: insufficientLines
  70. };
  71. }, [pickLines, inventoryData]);
  72. return (
  73. <Card>
  74. <CardContent>
  75. <Stack
  76. direction="row"
  77. alignItems="center"
  78. justifyContent="space-between"
  79. spacing={2}
  80. >
  81. <Typography variant="body2" color="text.secondary" sx={{ mt: 1 }}>
  82. {t("Total lines: ")}<strong>{stockCounts.total}</strong>
  83. </Typography>
  84. <Typography variant="body2" color="text.secondary" sx={{ mt: 1 }}>
  85. {t("Lines with sufficient stock: ")}<strong style={{ color: 'green' }}>{stockCounts.sufficient}</strong>
  86. </Typography>
  87. <Typography variant="body2" color="text.secondary" sx={{ mt: 1 }}>
  88. {t("Lines with insufficient stock: ")}<strong style={{ color: 'red' }}>{stockCounts.insufficient}</strong>
  89. </Typography>
  90. <Button
  91. variant="contained"
  92. color="primary"
  93. onClick={handleRelease}
  94. disabled={stockCounts.insufficient > 0 || status !== "planning"}
  95. >
  96. {t("Release")}
  97. </Button>
  98. </Stack>
  99. </CardContent>
  100. </Card>
  101. );
  102. };
  103. export default JoRelease;