FPSMS-frontend
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

282 行
8.5 KiB

  1. "use client";
  2. import {
  3. PurchaseQcResult,
  4. PurchaseQCInput,
  5. StockInInput,
  6. } from "@/app/api/po/actions";
  7. import {
  8. Box,
  9. Card,
  10. CardContent,
  11. Grid,
  12. Stack,
  13. TextField,
  14. Tooltip,
  15. Typography,
  16. } from "@mui/material";
  17. import { Controller, useFormContext } from "react-hook-form";
  18. import { useTranslation } from "react-i18next";
  19. import StyledDataGrid from "../StyledDataGrid";
  20. import { useCallback, useEffect, useMemo } from "react";
  21. import {
  22. GridColDef,
  23. GridRowIdGetter,
  24. GridRowModel,
  25. useGridApiContext,
  26. GridRenderCellParams,
  27. GridRenderEditCellParams,
  28. useGridApiRef,
  29. } from "@mui/x-data-grid";
  30. import InputDataGrid from "../InputDataGrid";
  31. import { TableRow } from "../InputDataGrid/InputDataGrid";
  32. import TwoLineCell from "./TwoLineCell";
  33. import QcSelect from "./QcSelect";
  34. import { QcItemWithChecks } from "@/app/api/qc";
  35. import { GridEditInputCell } from "@mui/x-data-grid";
  36. import { StockInLine } from "@/app/api/po";
  37. import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers";
  38. import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
  39. import { INPUT_DATE_FORMAT } from "@/app/utils/formatUtil";
  40. import dayjs from "dayjs";
  41. // change PurchaseQcResult to stock in entry props
  42. interface Props {
  43. itemDetail: StockInLine;
  44. // qc: QcItemWithChecks[];
  45. disabled: boolean
  46. }
  47. type EntryError =
  48. | {
  49. [field in keyof StockInInput]?: string;
  50. }
  51. | undefined;
  52. // type PoQcRow = TableRow<Partial<PurchaseQcResult>, EntryError>;
  53. const StockInForm: React.FC<Props> = ({
  54. // qc,
  55. itemDetail,
  56. disabled
  57. }) => {
  58. const {
  59. t,
  60. i18n: { language },
  61. } = useTranslation("purchaseOrder");
  62. const apiRef = useGridApiRef();
  63. const {
  64. register,
  65. formState: { errors, defaultValues, touchedFields },
  66. watch,
  67. control,
  68. setValue,
  69. getValues,
  70. reset,
  71. resetField,
  72. setError,
  73. clearErrors,
  74. } = useFormContext<StockInInput>();
  75. console.log(itemDetail);
  76. useEffect(() => {
  77. console.log("triggered");
  78. // receiptDate default tdy
  79. setValue("receiptDate", dayjs().add(0, "month").format(INPUT_DATE_FORMAT));
  80. setValue("status", "received");
  81. }, []);
  82. useEffect(() => {
  83. console.log(errors);
  84. }, [errors]);
  85. const productionDate = watch("productionDate");
  86. const expiryDate = watch("expiryDate");
  87. useEffect(() => {
  88. console.log(productionDate)
  89. console.log(expiryDate)
  90. if (expiryDate) clearErrors()
  91. if (productionDate) clearErrors()
  92. }, [productionDate, expiryDate])
  93. return (
  94. <Grid container justifyContent="flex-start" alignItems="flex-start">
  95. <Grid item xs={12}>
  96. <Typography variant="h6" display="block" marginBlockEnd={1}>
  97. {t("Stock In Detail")}
  98. </Typography>
  99. </Grid>
  100. <Grid
  101. container
  102. justifyContent="flex-start"
  103. alignItems="flex-start"
  104. spacing={2}
  105. sx={{ mt: 0.5 }}
  106. >
  107. <Grid item xs={4}>
  108. <TextField
  109. label={t("productLotNo")}
  110. fullWidth
  111. {...register("productLotNo", {
  112. // required: "productLotNo required!",
  113. })}
  114. disabled={disabled}
  115. // error={Boolean(errors.productLotNo)}
  116. // helperText={errors.productLotNo?.message}
  117. />
  118. </Grid>
  119. <Grid item xs={4}>
  120. <Controller
  121. control={control}
  122. name="receiptDate"
  123. rules={{ required: true }}
  124. render={({ field }) => {
  125. return (
  126. <LocalizationProvider
  127. dateAdapter={AdapterDayjs}
  128. adapterLocale={`${language}-hk`}
  129. >
  130. <DatePicker
  131. {...field}
  132. sx={{ width: "100%" }}
  133. label={t("receiptDate")}
  134. value={dayjs(watch("receiptDate"))}
  135. disabled={disabled}
  136. onChange={(date) => {
  137. if (!date) return
  138. // setValue("receiptDate", date.format(INPUT_DATE_FORMAT));
  139. field.onChange(date);
  140. }}
  141. inputRef={field.ref}
  142. slotProps={{
  143. textField: {
  144. // required: true,
  145. error: Boolean(errors.receiptDate?.message),
  146. helperText: errors.receiptDate?.message,
  147. },
  148. }}
  149. />
  150. </LocalizationProvider>
  151. );
  152. }}
  153. />
  154. </Grid>
  155. <Grid item xs={4}>
  156. <TextField
  157. label={t("acceptedQty")}
  158. fullWidth
  159. {...register("acceptedQty", {
  160. required: "acceptedQty required!",
  161. })}
  162. disabled={disabled}
  163. error={Boolean(errors.acceptedQty)}
  164. helperText={errors.acceptedQty?.message}
  165. />
  166. </Grid>
  167. <Grid item xs={4}>
  168. <TextField
  169. label={t("acceptedWeight")}
  170. fullWidth
  171. // {...register("acceptedWeight", {
  172. // required: "acceptedWeight required!",
  173. // })}
  174. disabled={disabled}
  175. error={Boolean(errors.acceptedWeight)}
  176. helperText={errors.acceptedWeight?.message}
  177. />
  178. </Grid>
  179. <Grid item xs={4}>
  180. <Controller
  181. control={control}
  182. name="productionDate"
  183. // rules={{ required: !Boolean(expiryDate) }}
  184. render={({ field }) => {
  185. return (
  186. <LocalizationProvider
  187. dateAdapter={AdapterDayjs}
  188. adapterLocale={`${language}-hk`}
  189. >
  190. <DatePicker
  191. {...field}
  192. sx={{ width: "100%" }}
  193. label={t("productionDate")}
  194. value={productionDate ? dayjs(productionDate) : undefined}
  195. disabled={disabled}
  196. onChange={(date) => {
  197. if (!date) return
  198. setValue("productionDate", date.format(INPUT_DATE_FORMAT));
  199. // field.onChange(date);
  200. }}
  201. inputRef={field.ref}
  202. slotProps={{
  203. textField: {
  204. // required: true,
  205. error: Boolean(errors.productionDate?.message),
  206. helperText: errors.productionDate?.message,
  207. },
  208. }}
  209. />
  210. </LocalizationProvider>
  211. );
  212. }}
  213. />
  214. </Grid>
  215. <Grid item xs={4}>
  216. <Controller
  217. control={control}
  218. name="expiryDate"
  219. // rules={{ required: !Boolean(productionDate) }}
  220. render={({ field }) => {
  221. return (
  222. <LocalizationProvider
  223. dateAdapter={AdapterDayjs}
  224. adapterLocale={`${language}-hk`}
  225. >
  226. <DatePicker
  227. {...field}
  228. sx={{ width: "100%" }}
  229. label={t("expiryDate")}
  230. value={expiryDate ? dayjs(expiryDate) : undefined}
  231. disabled={disabled}
  232. onChange={(date) => {
  233. console.log(date)
  234. if (!date) return
  235. console.log(date.format(INPUT_DATE_FORMAT))
  236. setValue("expiryDate", date.format(INPUT_DATE_FORMAT));
  237. // field.onChange(date);
  238. }}
  239. inputRef={field.ref}
  240. slotProps={{
  241. textField: {
  242. // required: true,
  243. error: Boolean(errors.expiryDate?.message),
  244. helperText: errors.expiryDate?.message,
  245. },
  246. }}
  247. />
  248. </LocalizationProvider>
  249. );
  250. }}
  251. />
  252. </Grid>
  253. </Grid>
  254. <Grid
  255. container
  256. justifyContent="flex-start"
  257. alignItems="flex-start"
  258. spacing={2}
  259. sx={{ mt: 0.5 }}
  260. >
  261. {/* <Grid item xs={12}>
  262. <InputDataGrid<PurchaseQCInput, PurchaseQcResult, EntryError>
  263. apiRef={apiRef}
  264. checkboxSelection={false}
  265. _formKey={"qcCheck"}
  266. columns={columns}
  267. validateRow={validationTest}
  268. />
  269. </Grid> */}
  270. </Grid>
  271. </Grid>
  272. );
  273. };
  274. export default StockInForm;