FPSMS-frontend
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

248 wiersze
7.1 KiB

  1. "use client";
  2. import { PurchaseQcResult, PurchaseQCInput } from "@/app/api/po/actions";
  3. import {
  4. Box,
  5. Card,
  6. CardContent,
  7. Grid,
  8. Stack,
  9. TextField,
  10. Tooltip,
  11. Typography,
  12. } from "@mui/material";
  13. import { useFormContext } from "react-hook-form";
  14. import { useTranslation } from "react-i18next";
  15. import StyledDataGrid from "../StyledDataGrid";
  16. import { useCallback, useEffect, useMemo, useState } from "react";
  17. import {
  18. GridColDef,
  19. GridRowIdGetter,
  20. GridRowModel,
  21. useGridApiContext,
  22. GridRenderCellParams,
  23. GridRenderEditCellParams,
  24. useGridApiRef,
  25. } from "@mui/x-data-grid";
  26. import InputDataGrid from "../InputDataGrid";
  27. import { TableRow } from "../InputDataGrid/InputDataGrid";
  28. import { GridEditInputCell } from "@mui/x-data-grid";
  29. import { StockInLine } from "@/app/api/po";
  30. import { stockInLineStatusMap } from "@/app/utils/formatUtil";
  31. import { fetchQcItemCheck, fetchQcResult } from "@/app/api/qc/actions";
  32. import { QcItemWithChecks } from "@/app/api/qc";
  33. import axios from "@/app/(main)/axios/axiosInstance";
  34. import { NEXT_PUBLIC_API_URL } from "@/config/api";
  35. import axiosInstance from "@/app/(main)/axios/axiosInstance";
  36. import TwoLineCell from "../PoDetail/TwoLineCell";
  37. import QcSelect from "../PoDetail/QcSelect";
  38. import { PickOrderQcInput } from "@/app/api/pickOrder/actions";
  39. interface Props {
  40. qcDefaultValues: PickOrderQcInput;
  41. qc: QcItemWithChecks[];
  42. disabled: boolean;
  43. }
  44. type EntryError =
  45. | {
  46. [field in keyof PurchaseQcResult]?: string;
  47. }
  48. | undefined;
  49. type PoQcRow = TableRow<Partial<PurchaseQcResult>, EntryError>;
  50. // fetchQcItemCheck
  51. const QcContent: React.FC<Props> = ({ qc, qcDefaultValues, disabled }) => {
  52. const { t } = useTranslation("purchaseOrder");
  53. const apiRef = useGridApiRef();
  54. const {
  55. register,
  56. formState: { errors, defaultValues, touchedFields },
  57. watch,
  58. control,
  59. setValue,
  60. getValues,
  61. reset,
  62. resetField,
  63. setError,
  64. clearErrors,
  65. } = useFormContext<PickOrderQcInput>();
  66. console.log(qcDefaultValues);
  67. console.log(defaultValues);
  68. //// validate form
  69. const accQty = watch("qty");
  70. const validateForm = useCallback(() => {
  71. console.log(accQty);
  72. if (accQty > qcDefaultValues.qty) {
  73. setError("qty", {
  74. message: `${t("qty must not greater than")} ${qcDefaultValues.qty}`,
  75. type: "required",
  76. });
  77. }
  78. if (accQty < 1) {
  79. setError("qty", {
  80. message: t("minimal value is 1"),
  81. type: "required",
  82. });
  83. }
  84. if (isNaN(accQty)) {
  85. setError("qty", {
  86. message: t("value must be a number"),
  87. type: "required",
  88. });
  89. }
  90. }, [accQty]);
  91. useEffect(() => {
  92. clearErrors();
  93. validateForm();
  94. }, [validateForm]);
  95. // const [recordQty, setRecordQty] = useState(0);
  96. const columns = useMemo<GridColDef[]>(
  97. () => [
  98. {
  99. field: "qcItemId",
  100. headerName: t("qc Check"),
  101. flex: 1,
  102. editable: !disabled,
  103. valueFormatter(params) {
  104. const row = params.id ? params.api.getRow<PoQcRow>(params.id) : null;
  105. if (!row) {
  106. return null;
  107. }
  108. const Qc = qc.find((q) => q.id === row.qcItemId);
  109. return Qc ? `${Qc.code} - ${Qc.name}` : t("Please select QC");
  110. },
  111. renderCell(params: GridRenderCellParams<PoQcRow, number>) {
  112. console.log(params.value);
  113. return <TwoLineCell>{params.formattedValue}</TwoLineCell>;
  114. },
  115. renderEditCell(params: GridRenderEditCellParams<PoQcRow, number>) {
  116. const errorMessage =
  117. params.row._error?.[params.field as keyof PurchaseQcResult];
  118. console.log(errorMessage);
  119. const content = (
  120. <QcSelect
  121. allQcs={qc}
  122. value={params.row.qcItemId}
  123. onQcSelect={async (qcItemId) => {
  124. await params.api.setEditCellValue({
  125. id: params.id,
  126. field: "qcItemId",
  127. value: qcItemId,
  128. });
  129. // await params.api.setEditCellValue({
  130. // id: params.id,
  131. // field: "type",
  132. // value: "determine1",
  133. // });
  134. }}
  135. />
  136. );
  137. return errorMessage ? (
  138. <Tooltip title={errorMessage}>
  139. <Box width="100%">{content}</Box>
  140. </Tooltip>
  141. ) : (
  142. content
  143. );
  144. },
  145. },
  146. {
  147. field: "failQty",
  148. headerName: t("failQty"),
  149. flex: 1,
  150. editable: !disabled,
  151. type: "number",
  152. renderEditCell(params: GridRenderEditCellParams<PoQcRow>) {
  153. // const recordQty = params.row.qty
  154. // if (recordQty !== undefined) {
  155. // setUnrecordQty((prev) => prev - recordQty)
  156. // }
  157. const errorMessage =
  158. params.row._error?.[params.field as keyof PurchaseQcResult];
  159. const content = <GridEditInputCell {...params} />;
  160. return errorMessage ? (
  161. <Tooltip title={t(errorMessage)}>
  162. <Box width="100%">{content}</Box>
  163. </Tooltip>
  164. ) : (
  165. content
  166. );
  167. },
  168. },
  169. ],
  170. [qc],
  171. );
  172. /// validate datagrid
  173. const validation = useCallback(
  174. (newRow: GridRowModel<PoQcRow>): EntryError => {
  175. const error: EntryError = {};
  176. const { qcItemId, failQty } = newRow;
  177. if (!qcItemId || qcItemId <= 0) {
  178. error["qcItemId"] = t("select qc");
  179. }
  180. if (!failQty || failQty <= 0) {
  181. error["failQty"] = t("enter a failQty");
  182. }
  183. if (failQty && failQty > qcDefaultValues.qty) {
  184. error["failQty"] = t("qty too big");
  185. }
  186. return Object.keys(error).length > 0 ? error : undefined;
  187. },
  188. [],
  189. );
  190. return (
  191. <Grid container justifyContent="flex-start" alignItems="flex-start">
  192. <Grid item xs={12}>
  193. <Typography variant="h6" display="block" marginBlockEnd={1}>
  194. {t("Qc Detail")}
  195. </Typography>
  196. </Grid>
  197. <Grid
  198. container
  199. justifyContent="flex-start"
  200. alignItems="flex-start"
  201. spacing={2}
  202. sx={{ mt: 0.5 }}
  203. >
  204. <Grid item xs={12} lg={12}>
  205. <TextField
  206. label={t("accepted Qty")}
  207. fullWidth
  208. // value={qcDefaultValues.qty}
  209. {...register("qty", {
  210. required: "qty required!",
  211. valueAsNumber: true,
  212. max: qcDefaultValues.qty,
  213. })}
  214. disabled={disabled}
  215. error={Boolean(errors.qty)}
  216. helperText={errors.qty?.message}
  217. />
  218. </Grid>
  219. </Grid>
  220. <Grid
  221. container
  222. justifyContent="flex-start"
  223. alignItems="flex-start"
  224. spacing={2}
  225. sx={{ mt: 0.5 }}
  226. >
  227. <Grid item xs={12}>
  228. <InputDataGrid<PickOrderQcInput, PurchaseQcResult, EntryError>
  229. apiRef={apiRef}
  230. checkboxSelection={false}
  231. _formKey={"qcResult"}
  232. columns={columns}
  233. validateRow={validation}
  234. needAdd={!disabled}
  235. />
  236. </Grid>
  237. </Grid>
  238. </Grid>
  239. );
  240. };
  241. export default QcContent;