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.
 
 

309 lines
9.8 KiB

  1. "use client";
  2. import { StockInLine } from "@/app/api/po";
  3. import { ModalFormInput, PurchaseQcResult } from "@/app/api/po/actions";
  4. import { QcItemWithChecks } from "@/app/api/qc";
  5. import {
  6. Box,
  7. Button,
  8. Grid,
  9. Modal,
  10. ModalProps,
  11. Stack,
  12. Typography,
  13. } from "@mui/material";
  14. import { Dispatch, SetStateAction, useCallback, useState } from "react";
  15. import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
  16. import { StockInLineRow } from "./PoInputGrid";
  17. import { useTranslation } from "react-i18next";
  18. import StockInForm from "./StockInForm";
  19. import StockInFormVer2 from "./StockInFormVer2";
  20. import QcFormVer2 from "./QcFormVer2";
  21. import PutawayForm from "./PutawayForm";
  22. import { dummyPutawayLine, dummyQCData } from "./dummyQcTemplate";
  23. import { useGridApiRef } from "@mui/x-data-grid";
  24. const style = {
  25. position: "absolute",
  26. top: "50%",
  27. left: "50%",
  28. transform: "translate(-50%, -50%)",
  29. bgcolor: "background.paper",
  30. pt: 5,
  31. px: 5,
  32. pb: 10,
  33. display: "block",
  34. width: { xs: "60%", sm: "60%", md: "60%" },
  35. // height: { xs: "60%", sm: "60%", md: "60%" },
  36. };
  37. interface CommonProps extends Omit<ModalProps, "children"> {
  38. // setRows: Dispatch<SetStateAction<PurchaseOrderLine[]>>;
  39. setEntries?: Dispatch<SetStateAction<StockInLineRow[]>>;
  40. setStockInLine?: Dispatch<SetStateAction<StockInLine[]>>;
  41. itemDetail: StockInLine & { qcResult?: PurchaseQcResult[] };
  42. setItemDetail: Dispatch<
  43. SetStateAction<
  44. | (StockInLine & {
  45. warehouseId?: number;
  46. })
  47. | undefined
  48. >
  49. >;
  50. qc?: QcItemWithChecks[];
  51. warehouse?: any[];
  52. // type: "qc" | "stockIn" | "escalation" | "putaway" | "reject";
  53. }
  54. interface Props extends CommonProps {
  55. itemDetail: StockInLine & { qcResult?: PurchaseQcResult[] };
  56. }
  57. const PoQcStockInModalVer2: React.FC<Props> = ({
  58. // type,
  59. // setRows,
  60. setEntries,
  61. setStockInLine,
  62. open,
  63. onClose,
  64. itemDetail,
  65. setItemDetail,
  66. qc,
  67. warehouse,
  68. }) => {
  69. console.log(warehouse);
  70. const {
  71. t,
  72. i18n: { language },
  73. } = useTranslation("purchaseOrder");
  74. const [qcItems, setQcItems] = useState(dummyQCData)
  75. const formProps = useForm<ModalFormInput>({
  76. defaultValues: {
  77. ...itemDetail,
  78. putawayLine: dummyPutawayLine,
  79. // receiptDate: itemDetail.receiptDate || dayjs().add(-1, "month").format(INPUT_DATE_FORMAT),
  80. // warehouseId: itemDetail.defaultWarehouseId || 0
  81. },
  82. });
  83. const closeHandler = useCallback<NonNullable<ModalProps["onClose"]>>(
  84. (...args) => {
  85. onClose?.(...args);
  86. // reset();
  87. },
  88. [onClose],
  89. );
  90. const [openPutaway, setOpenPutaway] = useState(false);
  91. const onOpenPutaway = useCallback(() => {
  92. setOpenPutaway(true);
  93. }, []);
  94. const onClosePutaway = useCallback(() => {
  95. setOpenPutaway(false);
  96. }, []);
  97. // Stock In submission handler
  98. const onSubmitStockIn = useCallback<SubmitHandler<ModalFormInput>>(
  99. async (data, event) => {
  100. console.log("Stock In Submission:", event!.nativeEvent);
  101. // Extract only stock-in related fields
  102. const stockInData = {
  103. // quantity: data.quantity,
  104. // receiptDate: data.receiptDate,
  105. // batchNumber: data.batchNumber,
  106. // expiryDate: data.expiryDate,
  107. // warehouseId: data.warehouseId,
  108. // location: data.location,
  109. // unitCost: data.unitCost,
  110. data: data,
  111. // Add other stock-in specific fields from your form
  112. };
  113. console.log("Stock In Data:", stockInData);
  114. // Handle stock-in submission logic here
  115. // e.g., call API, update state, etc.
  116. },
  117. [],
  118. );
  119. // QC submission handler
  120. const onSubmitQc = useCallback<SubmitHandler<ModalFormInput>>(
  121. async (data, event) => {
  122. console.log("QC Submission:", event!.nativeEvent);
  123. // Extract only QC related fields
  124. const qcData = {
  125. // qcStatus: data.qcStatus,
  126. // qcComments: data.qcComments,
  127. // qcResult: data.qcResult,
  128. // approvedBy: data.approvedBy,
  129. // qualityGrade: data.qualityGrade,
  130. // defectNotes: data.defectNotes,
  131. data: data,
  132. // Add other QC specific fields from your form
  133. };
  134. console.log(qcItems)
  135. console.log("QC Data:", qcData);
  136. // Handle QC submission logic here
  137. // After QC approval, open putaway form
  138. // onOpenPutaway();
  139. },
  140. [onOpenPutaway, qcItems],
  141. );
  142. // Email supplier handler
  143. const onSubmitEmailSupplier = useCallback<SubmitHandler<ModalFormInput>>(
  144. async (data, event) => {
  145. console.log("Email Supplier Submission:", event!.nativeEvent);
  146. // Extract only email supplier related fields
  147. const emailData = {
  148. // supplierEmail: data.supplierEmail,
  149. // issueDescription: data.issueDescription,
  150. // qcComments: data.qcComments,
  151. // defectNotes: data.defectNotes,
  152. // attachments: data.attachments,
  153. // escalationReason: data.escalationReason,
  154. data: data,
  155. // Add other email-specific fields
  156. };
  157. console.log("Email Supplier Data:", emailData);
  158. // Handle email supplier logic here
  159. // e.g., send email to supplier, log escalation, etc.
  160. },
  161. [],
  162. );
  163. // Putaway submission handler
  164. const onSubmitPutaway = useCallback<SubmitHandler<ModalFormInput>>(
  165. async (data, event) => {
  166. console.log("Putaway Submission:", event!.nativeEvent);
  167. // Extract only putaway related fields
  168. const putawayData = {
  169. // putawayLine: data.putawayLine,
  170. // putawayLocation: data.putawayLocation,
  171. // binLocation: data.binLocation,
  172. // putawayQuantity: data.putawayQuantity,
  173. // putawayNotes: data.putawayNotes,
  174. data: data,
  175. // Add other putaway specific fields
  176. };
  177. console.log("Putaway Data:", putawayData);
  178. // Handle putaway submission logic here
  179. // Close modal after successful putaway
  180. closeHandler({}, "backdropClick");
  181. },
  182. [closeHandler],
  183. );
  184. // Print handler
  185. const onPrint = useCallback(() => {
  186. console.log("Print putaway documents");
  187. // Handle print logic here
  188. window.print();
  189. }, []);
  190. return (
  191. <>
  192. <FormProvider {...formProps}>
  193. <Modal open={open} onClose={closeHandler}>
  194. <Box
  195. sx={{
  196. ...style,
  197. padding: 2,
  198. maxHeight: "90vh",
  199. overflowY: "auto",
  200. marginLeft: 3,
  201. marginRight: 3,
  202. }}
  203. >
  204. {openPutaway ? (
  205. <Box
  206. component="form"
  207. onSubmit={formProps.handleSubmit(onSubmitPutaway)}
  208. >
  209. <PutawayForm
  210. itemDetail={itemDetail}
  211. warehouse={warehouse!}
  212. disabled={false}
  213. />
  214. <Stack direction="row" justifyContent="flex-end" gap={1}>
  215. <Button
  216. id="printButton"
  217. type="button"
  218. variant="contained"
  219. color="secondary"
  220. sx={{ mt: 1 }}
  221. onClick={onPrint}
  222. >
  223. {t("print")}
  224. </Button>
  225. <Button
  226. id="putawaySubmit"
  227. type="submit"
  228. variant="contained"
  229. color="secondary"
  230. sx={{ mt: 1 }}
  231. >
  232. {t("confirm putaway")}
  233. </Button>
  234. </Stack>
  235. </Box>
  236. ) : (
  237. <>
  238. <Grid
  239. container
  240. justifyContent="flex-start"
  241. alignItems="flex-start"
  242. >
  243. <Grid item xs={12}>
  244. <Typography variant="h6" display="block" marginBlockEnd={1}>
  245. {t("qc processing")}
  246. </Typography>
  247. </Grid>
  248. <Grid item xs={12}>
  249. <StockInFormVer2 itemDetail={itemDetail} disabled={false} />
  250. </Grid>
  251. </Grid>
  252. <Stack direction="row" justifyContent="flex-end" gap={1}>
  253. <Button
  254. id="stockInSubmit"
  255. type="button"
  256. variant="contained"
  257. color="primary"
  258. onClick={formProps.handleSubmit(onSubmitStockIn)}
  259. >
  260. {t("submitStockIn")}
  261. </Button>
  262. </Stack>
  263. <Grid
  264. container
  265. justifyContent="flex-start"
  266. alignItems="flex-start"
  267. >
  268. <QcFormVer2
  269. qc={qc!}
  270. itemDetail={itemDetail}
  271. disabled={false}
  272. qcItems={qcItems}
  273. setQcItems={setQcItems}
  274. />
  275. </Grid>
  276. <Stack direction="row" justifyContent="flex-end" gap={1}>
  277. <Button
  278. id="emailSupplier"
  279. type="button"
  280. variant="contained"
  281. color="secondary"
  282. sx={{ mt: 1 }}
  283. onClick={formProps.handleSubmit(onSubmitEmailSupplier)}
  284. >
  285. {t("email supplier")}
  286. </Button>
  287. <Button
  288. id="qcSubmit"
  289. type="button"
  290. variant="contained"
  291. color="secondary"
  292. sx={{ mt: 1 }}
  293. onClick={formProps.handleSubmit(onSubmitQc)}
  294. >
  295. {t("confirm putaway")}
  296. </Button>
  297. </Stack>
  298. </>
  299. )}
  300. </Box>
  301. </Modal>
  302. </FormProvider>
  303. </>
  304. );
  305. };
  306. export default PoQcStockInModalVer2;