FPSMS-frontend
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

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