FPSMS-frontend
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

310 行
9.3 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, clearErrors]);
  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={6}>
  108. <TextField
  109. label={t("dnNo")}
  110. fullWidth
  111. {...register("dnNo", {
  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={6}>
  120. <TextField
  121. label={t("invoiceNo")}
  122. fullWidth
  123. {...register("invoiceNo", {
  124. // required: "productLotNo required!",
  125. })}
  126. disabled={disabled}
  127. // error={Boolean(errors.productLotNo)}
  128. // helperText={errors.productLotNo?.message}
  129. />
  130. </Grid>
  131. <Grid item xs={4}>
  132. <TextField
  133. label={t("productLotNo")}
  134. fullWidth
  135. {...register("productLotNo", {
  136. // required: "productLotNo required!",
  137. })}
  138. disabled={disabled}
  139. error={Boolean(errors.productLotNo)}
  140. helperText={errors.productLotNo?.message}
  141. />
  142. </Grid>
  143. <Grid item xs={4}>
  144. <Controller
  145. control={control}
  146. name="receiptDate"
  147. rules={{ required: true }}
  148. render={({ field }) => {
  149. return (
  150. <LocalizationProvider
  151. dateAdapter={AdapterDayjs}
  152. adapterLocale={`${language}-hk`}
  153. >
  154. <DatePicker
  155. {...field}
  156. sx={{ width: "100%" }}
  157. label={t("receiptDate")}
  158. value={dayjs(watch("receiptDate"))}
  159. disabled={disabled}
  160. onChange={(date) => {
  161. if (!date) return;
  162. // setValue("receiptDate", date.format(INPUT_DATE_FORMAT));
  163. field.onChange(date);
  164. }}
  165. inputRef={field.ref}
  166. slotProps={{
  167. textField: {
  168. // required: true,
  169. error: Boolean(errors.receiptDate?.message),
  170. helperText: errors.receiptDate?.message,
  171. },
  172. }}
  173. />
  174. </LocalizationProvider>
  175. );
  176. }}
  177. />
  178. </Grid>
  179. <Grid item xs={4}>
  180. <TextField
  181. label={t("acceptedQty")}
  182. fullWidth
  183. {...register("acceptedQty", {
  184. required: "acceptedQty required!",
  185. })}
  186. disabled={disabled}
  187. error={Boolean(errors.acceptedQty)}
  188. helperText={errors.acceptedQty?.message}
  189. />
  190. </Grid>
  191. {/* <Grid item xs={4}>
  192. <TextField
  193. label={t("acceptedWeight")}
  194. fullWidth
  195. // {...register("acceptedWeight", {
  196. // required: "acceptedWeight required!",
  197. // })}
  198. disabled={disabled}
  199. error={Boolean(errors.acceptedWeight)}
  200. helperText={errors.acceptedWeight?.message}
  201. />
  202. </Grid> */}
  203. <Grid item xs={4}>
  204. <Controller
  205. control={control}
  206. name="productionDate"
  207. // rules={{ required: !Boolean(expiryDate) }}
  208. render={({ field }) => {
  209. return (
  210. <LocalizationProvider
  211. dateAdapter={AdapterDayjs}
  212. adapterLocale={`${language}-hk`}
  213. >
  214. <DatePicker
  215. {...field}
  216. sx={{ width: "100%" }}
  217. label={t("productionDate")}
  218. value={productionDate ? dayjs(productionDate) : undefined}
  219. disabled={disabled}
  220. onChange={(date) => {
  221. if (!date) return;
  222. setValue(
  223. "productionDate",
  224. date.format(INPUT_DATE_FORMAT),
  225. );
  226. // field.onChange(date);
  227. }}
  228. inputRef={field.ref}
  229. slotProps={{
  230. textField: {
  231. // required: true,
  232. error: Boolean(errors.productionDate?.message),
  233. helperText: errors.productionDate?.message,
  234. },
  235. }}
  236. />
  237. </LocalizationProvider>
  238. );
  239. }}
  240. />
  241. </Grid>
  242. <Grid item xs={4}>
  243. <Controller
  244. control={control}
  245. name="expiryDate"
  246. // rules={{ required: !Boolean(productionDate) }}
  247. render={({ field }) => {
  248. return (
  249. <LocalizationProvider
  250. dateAdapter={AdapterDayjs}
  251. adapterLocale={`${language}-hk`}
  252. >
  253. <DatePicker
  254. {...field}
  255. sx={{ width: "100%" }}
  256. label={t("expiryDate")}
  257. value={expiryDate ? dayjs(expiryDate) : undefined}
  258. disabled={disabled}
  259. onChange={(date) => {
  260. console.log(date);
  261. if (!date) return;
  262. console.log(date.format(INPUT_DATE_FORMAT));
  263. setValue("expiryDate", date.format(INPUT_DATE_FORMAT));
  264. // field.onChange(date);
  265. }}
  266. inputRef={field.ref}
  267. slotProps={{
  268. textField: {
  269. // required: true,
  270. error: Boolean(errors.expiryDate?.message),
  271. helperText: errors.expiryDate?.message,
  272. },
  273. }}
  274. />
  275. </LocalizationProvider>
  276. );
  277. }}
  278. />
  279. </Grid>
  280. </Grid>
  281. <Grid
  282. container
  283. justifyContent="flex-start"
  284. alignItems="flex-start"
  285. spacing={2}
  286. sx={{ mt: 0.5 }}
  287. >
  288. {/* <Grid item xs={12}>
  289. <InputDataGrid<PurchaseQCInput, PurchaseQcResult, EntryError>
  290. apiRef={apiRef}
  291. checkboxSelection={false}
  292. _formKey={"qcCheck"}
  293. columns={columns}
  294. validateRow={validationTest}
  295. />
  296. </Grid> */}
  297. </Grid>
  298. </Grid>
  299. );
  300. };
  301. export default StockInForm;