FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

274 line
8.2 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. }
  46. type EntryError =
  47. | {
  48. [field in keyof StockInInput]?: string;
  49. }
  50. | undefined;
  51. // type PoQcRow = TableRow<Partial<PurchaseQcResult>, EntryError>;
  52. const StockInForm: React.FC<Props> = ({
  53. // qc,
  54. itemDetail,
  55. }) => {
  56. const {
  57. t,
  58. i18n: { language },
  59. } = useTranslation();
  60. const apiRef = useGridApiRef();
  61. const {
  62. register,
  63. formState: { errors, defaultValues, touchedFields },
  64. watch,
  65. control,
  66. setValue,
  67. getValues,
  68. reset,
  69. resetField,
  70. setError,
  71. clearErrors,
  72. } = useFormContext<StockInInput>();
  73. console.log(itemDetail);
  74. useEffect(() => {
  75. console.log("triggered");
  76. // receiptDate default tdy
  77. setValue("receiptDate", dayjs().add(0, "month").format(INPUT_DATE_FORMAT));
  78. setValue("status", "received");
  79. }, []);
  80. useEffect(() => {
  81. console.log(errors);
  82. }, [errors]);
  83. const productionDate = watch("productionDate");
  84. const expiryDate = watch("expiryDate");
  85. useEffect(() => {
  86. console.log(productionDate)
  87. console.log(expiryDate)
  88. if (expiryDate) clearErrors()
  89. if (productionDate) clearErrors()
  90. }, [productionDate, expiryDate])
  91. return (
  92. <Grid container justifyContent="flex-start" alignItems="flex-start">
  93. <Grid item xs={12}>
  94. <Typography variant="h6" display="block" marginBlockEnd={1}>
  95. {t("Stock In Detail")}
  96. </Typography>
  97. </Grid>
  98. <Grid
  99. container
  100. justifyContent="flex-start"
  101. alignItems="flex-start"
  102. spacing={2}
  103. sx={{ mt: 0.5 }}
  104. >
  105. <Grid item xs={4}>
  106. <TextField
  107. label={t("productLotNo")}
  108. fullWidth
  109. {...register("productLotNo", {
  110. // required: "productLotNo required!",
  111. })}
  112. // error={Boolean(errors.productLotNo)}
  113. // helperText={errors.productLotNo?.message}
  114. />
  115. </Grid>
  116. <Grid item xs={4}>
  117. <Controller
  118. control={control}
  119. name="receiptDate"
  120. rules={{ required: true }}
  121. render={({ field }) => {
  122. return (
  123. <LocalizationProvider
  124. dateAdapter={AdapterDayjs}
  125. adapterLocale={`${language}-hk`}
  126. >
  127. <DatePicker
  128. {...field}
  129. sx={{ width: "100%" }}
  130. label={t("receiptDate")}
  131. value={dayjs(watch("receiptDate"))}
  132. onChange={(date) => {
  133. if (!date) return
  134. // setValue("receiptDate", date.format(INPUT_DATE_FORMAT));
  135. field.onChange(date);
  136. }}
  137. inputRef={field.ref}
  138. slotProps={{
  139. textField: {
  140. // required: true,
  141. error: Boolean(errors.receiptDate?.message),
  142. helperText: errors.receiptDate?.message,
  143. },
  144. }}
  145. />
  146. </LocalizationProvider>
  147. );
  148. }}
  149. />
  150. </Grid>
  151. <Grid item xs={4}>
  152. <TextField
  153. label={t("acceptedQty")}
  154. fullWidth
  155. {...register("acceptedQty", {
  156. required: "acceptedQty required!",
  157. })}
  158. error={Boolean(errors.acceptedQty)}
  159. helperText={errors.acceptedQty?.message}
  160. />
  161. </Grid>
  162. <Grid item xs={4}>
  163. <TextField
  164. label={t("acceptedWeight")}
  165. fullWidth
  166. // {...register("acceptedWeight", {
  167. // required: "acceptedWeight required!",
  168. // })}
  169. error={Boolean(errors.acceptedWeight)}
  170. helperText={errors.acceptedWeight?.message}
  171. />
  172. </Grid>
  173. <Grid item xs={4}>
  174. <Controller
  175. control={control}
  176. name="productionDate"
  177. // rules={{ required: !Boolean(expiryDate) }}
  178. render={({ field }) => {
  179. return (
  180. <LocalizationProvider
  181. dateAdapter={AdapterDayjs}
  182. adapterLocale={`${language}-hk`}
  183. >
  184. <DatePicker
  185. {...field}
  186. sx={{ width: "100%" }}
  187. label={t("productionDate")}
  188. value={productionDate ? dayjs(productionDate) : undefined}
  189. onChange={(date) => {
  190. if (!date) return
  191. setValue("productionDate", date.format(INPUT_DATE_FORMAT));
  192. // field.onChange(date);
  193. }}
  194. inputRef={field.ref}
  195. slotProps={{
  196. textField: {
  197. // required: true,
  198. error: Boolean(errors.productionDate?.message),
  199. helperText: errors.productionDate?.message,
  200. },
  201. }}
  202. />
  203. </LocalizationProvider>
  204. );
  205. }}
  206. />
  207. </Grid>
  208. <Grid item xs={4}>
  209. <Controller
  210. control={control}
  211. name="expiryDate"
  212. // rules={{ required: !Boolean(productionDate) }}
  213. render={({ field }) => {
  214. return (
  215. <LocalizationProvider
  216. dateAdapter={AdapterDayjs}
  217. adapterLocale={`${language}-hk`}
  218. >
  219. <DatePicker
  220. {...field}
  221. sx={{ width: "100%" }}
  222. label={t("expiryDate")}
  223. value={expiryDate ? dayjs(expiryDate) : undefined}
  224. onChange={(date) => {
  225. console.log(date)
  226. if (!date) return
  227. console.log(date.format(INPUT_DATE_FORMAT))
  228. setValue("expiryDate", date.format(INPUT_DATE_FORMAT));
  229. // field.onChange(date);
  230. }}
  231. inputRef={field.ref}
  232. slotProps={{
  233. textField: {
  234. // required: true,
  235. error: Boolean(errors.expiryDate?.message),
  236. helperText: errors.expiryDate?.message,
  237. },
  238. }}
  239. />
  240. </LocalizationProvider>
  241. );
  242. }}
  243. />
  244. </Grid>
  245. </Grid>
  246. <Grid
  247. container
  248. justifyContent="flex-start"
  249. alignItems="flex-start"
  250. spacing={2}
  251. sx={{ mt: 0.5 }}
  252. >
  253. {/* <Grid item xs={12}>
  254. <InputDataGrid<PurchaseQCInput, PurchaseQcResult, EntryError>
  255. apiRef={apiRef}
  256. checkboxSelection={false}
  257. _formKey={"qcCheck"}
  258. columns={columns}
  259. validateRow={validationTest}
  260. />
  261. </Grid> */}
  262. </Grid>
  263. </Grid>
  264. );
  265. };
  266. export default StockInForm;