FPSMS-frontend
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

349 lignes
8.4 KiB

  1. "use client";
  2. import { CreateItemInputs } from "@/app/api/settings/item/actions";
  3. import {
  4. GridColDef,
  5. GridRowModel,
  6. GridRenderEditCellParams,
  7. GridEditInputCell,
  8. GridRowSelectionModel,
  9. useGridApiRef,
  10. } from "@mui/x-data-grid";
  11. import { MutableRefObject, useCallback, useMemo, useState } from "react";
  12. import { useFieldArray, useFormContext } from "react-hook-form";
  13. import { useTranslation } from "react-i18next";
  14. import InputDataGrid, { TableRow } from "../InputDataGrid/InputDataGrid";
  15. import { Box, Grid, Tooltip, Typography } from "@mui/material";
  16. import { ItemQc } from "@/app/api/settings/item";
  17. import { QcChecksInputs } from "@/app/api/settings/qcCheck/actions";
  18. import { GridApiCommunity } from "@mui/x-data-grid/internals";
  19. import { RiceBowl } from "@mui/icons-material";
  20. import ScheduleTable from "@/components/ScheduleTable";
  21. import { Column } from "@/components/ScheduleTable/ScheduleTable";
  22. import {
  23. arrayToDayjs,
  24. dayjsToDateString,
  25. decimalFormatter,
  26. integerFormatter,
  27. } from "@/app/utils/formatUtil";
  28. import {
  29. RoughProdScheduleLineResultByFg,
  30. RoughProdScheduleResult,
  31. ScheduleType,
  32. } from "@/app/api/scheduling";
  33. type Props = {
  34. apiRef: MutableRefObject<GridApiCommunity>;
  35. isEdit: boolean;
  36. type: ScheduleType;
  37. dayPeriod: string[];
  38. };
  39. // export type FGRecord = {
  40. // id: string | number
  41. // code: string;
  42. // name: string;
  43. // inStockQty: number;
  44. // productionQty?: number;
  45. // purchaseQty?: number
  46. // }
  47. const ViewByFGDetails: React.FC<Props> = ({
  48. apiRef,
  49. isEdit,
  50. type,
  51. dayPeriod,
  52. }) => {
  53. const {
  54. t,
  55. i18n: { language },
  56. } = useTranslation("schedule");
  57. const {
  58. control,
  59. getValues,
  60. formState: { errors, defaultValues, touchedFields },
  61. } = useFormContext<RoughProdScheduleResult>();
  62. const { fields } = useFieldArray({
  63. control,
  64. name: "prodScheduleLinesByFg",
  65. });
  66. // const apiRef = useGridApiRef();
  67. console.log("%c DayPeriod: ", "background: #222; color: #bada55", dayPeriod);
  68. const [pagingController, setPagingController] = useState([
  69. {
  70. pageNum: 1,
  71. pageSize: 10,
  72. totalCount: 0,
  73. },
  74. {
  75. pageNum: 1,
  76. pageSize: 10,
  77. totalCount: 0,
  78. },
  79. {
  80. pageNum: 1,
  81. pageSize: 10,
  82. totalCount: 0,
  83. },
  84. {
  85. pageNum: 1,
  86. pageSize: 10,
  87. totalCount: 0,
  88. },
  89. {
  90. pageNum: 1,
  91. pageSize: 10,
  92. totalCount: 0,
  93. },
  94. {
  95. pageNum: 1,
  96. pageSize: 10,
  97. totalCount: 0,
  98. },
  99. {
  100. pageNum: 1,
  101. pageSize: 10,
  102. totalCount: 0,
  103. },
  104. {
  105. pageNum: 1,
  106. pageSize: 10,
  107. totalCount: 0,
  108. },
  109. ]);
  110. const updatePagingController = (updatedObj: {
  111. pageNum: number;
  112. pageSize: number;
  113. totalCount: number;
  114. index?: number | undefined;
  115. }) => {
  116. setPagingController((prevState) => {
  117. return prevState.map((item, index) => {
  118. if (index === updatedObj?.index) {
  119. return {
  120. ...item,
  121. pageNum: item.pageNum,
  122. pageSize: item.pageSize,
  123. totalCount: item.totalCount,
  124. };
  125. } else return item;
  126. });
  127. });
  128. };
  129. const columns = useMemo<Column<RoughProdScheduleLineResultByFg>[]>(
  130. () => [
  131. {
  132. field: "code",
  133. label: t("code"),
  134. type: "read-only",
  135. // editable: true,
  136. },
  137. {
  138. field: "name",
  139. label: t("name"),
  140. type: "read-only",
  141. },
  142. {
  143. field: "type",
  144. label: t("type"),
  145. type: "read-only",
  146. // editable: true,
  147. renderCell: (row) => {
  148. return t(row.type)
  149. }
  150. },
  151. {
  152. field: "availableQty",
  153. label: t("Available Qty"),
  154. type: "read-only",
  155. style: {
  156. textAlign: "right",
  157. },
  158. // editable: true,
  159. renderCell: (row) => {
  160. if (typeof row.availableQty == "number") {
  161. return decimalFormatter.format(row.availableQty);
  162. }
  163. return row.availableQty;
  164. },
  165. },
  166. {
  167. field: "prodQty",
  168. label: t("Demand Qty"),
  169. type: "input",
  170. style: {
  171. textAlign: "right",
  172. },
  173. renderCell: (row) => {
  174. if (typeof row.prodQty == "number") {
  175. return decimalFormatter.format(row.prodQty ?? 0);
  176. }
  177. return row.prodQty;
  178. },
  179. },
  180. {
  181. field: "uomName",
  182. label: t("UoM"),
  183. type: "read-only",
  184. style: {
  185. textAlign: "left",
  186. },
  187. // editable: true,
  188. renderCell: (row) => {
  189. return row.uomName;
  190. },
  191. },
  192. ],
  193. [],
  194. );
  195. const overallColumns = useMemo<Column<RoughProdScheduleLineResultByFg>[]>(
  196. () => [
  197. {
  198. field: "code",
  199. label: t("code"),
  200. type: "read-only",
  201. // editable: true,
  202. },
  203. {
  204. field: "name",
  205. label: t("name"),
  206. type: "read-only",
  207. },
  208. {
  209. field: "type",
  210. label: t("type"),
  211. type: "read-only",
  212. renderCell: (params) => {
  213. return t(params.type);
  214. },
  215. // editable: true,
  216. },
  217. {
  218. field: "lastMonthAvgSales",
  219. label: t("Last Month Average Stock"),
  220. type: "read-only",
  221. style: {
  222. textAlign: "right",
  223. },
  224. renderCell: (row) => {
  225. if (typeof row.lastMonthAvgSales == "number") {
  226. return decimalFormatter.format(row.lastMonthAvgSales);
  227. }
  228. return row.lastMonthAvgSales;
  229. },
  230. // editable: true,
  231. },
  232. {
  233. field: "estCloseBal",
  234. label: t("Safety Stock"),
  235. type: "read-only",
  236. style: {
  237. textAlign: "right",
  238. },
  239. renderCell: (row) => {
  240. if (typeof row.estCloseBal == "number") {
  241. return decimalFormatter.format(row.estCloseBal);
  242. }
  243. return row.estCloseBal;
  244. },
  245. // editable: true,
  246. },
  247. {
  248. field: "availableQty",
  249. label: t("Available Qty"),
  250. type: "read-only",
  251. style: {
  252. textAlign: "right",
  253. },
  254. renderCell: (row) => {
  255. if (typeof row.availableQty == "number") {
  256. return decimalFormatter.format(row.availableQty);
  257. }
  258. return row.availableQty;
  259. },
  260. // editable: true,
  261. },
  262. {
  263. field: "prodQty",
  264. label: t("Demand Qty (7 Days)"),
  265. type: "read-only",
  266. style: {
  267. textAlign: "right",
  268. },
  269. renderCell: (row) => {
  270. if (typeof row.prodQty == "number") {
  271. return decimalFormatter.format(row.prodQty);
  272. }
  273. return row.prodQty;
  274. },
  275. },
  276. {
  277. field: "uomName",
  278. label: t("UoM"),
  279. type: "read-only",
  280. style: {
  281. textAlign: "left",
  282. },
  283. renderCell: (row) => {
  284. return row.uomName;
  285. },
  286. },
  287. ],
  288. [],
  289. );
  290. return (
  291. <Grid container spacing={2}>
  292. <Grid item xs={12} key={"all"}>
  293. <Typography variant="overline" display="block" marginBlockEnd={1}>
  294. {t("FG Demand List (7 Days)")}
  295. </Typography>
  296. <ScheduleTable<RoughProdScheduleLineResultByFg>
  297. // index={7}
  298. type={type}
  299. items={getValues("prodScheduleLinesByFg")}
  300. columns={overallColumns}
  301. setPagingController={updatePagingController}
  302. pagingController={pagingController[7]}
  303. isAutoPaging={false}
  304. isEditable={false}
  305. isEdit={isEdit}
  306. hasCollapse={true}
  307. />
  308. </Grid>
  309. {dayPeriod.map((date, index) => (
  310. <Grid item xs={12} key={index}>
  311. <Typography variant="overline" display="block" marginBlockEnd={1}>
  312. {`${t("FG Demand Date")}: ${date}`}
  313. </Typography>
  314. <ScheduleTable<RoughProdScheduleLineResultByFg>
  315. type={type}
  316. items={
  317. getValues("prodScheduleLinesByFgByDate") &&
  318. Object.entries(getValues("prodScheduleLinesByFgByDate")).length >
  319. 0
  320. ? getValues("prodScheduleLinesByFgByDate")[index + 1]
  321. : []
  322. } // Use the corresponding records for the day
  323. columns={columns}
  324. setPagingController={updatePagingController}
  325. pagingController={pagingController[index]}
  326. isAutoPaging={false}
  327. isEditable={true}
  328. isEdit={isEdit}
  329. hasCollapse={true}
  330. />
  331. </Grid>
  332. ))}
  333. </Grid>
  334. );
  335. };
  336. export default ViewByFGDetails;