From e1b655696e86d5a5465b278f376a683b8bd1cd92 Mon Sep 17 00:00:00 2001 From: "CANCERYS\\kw093" Date: Tue, 1 Sep 2026 11:45:26 +0800 Subject: [PATCH 1/3] update --- src/app/api/stockIn/actions.ts | 5 + src/app/api/stockIn/index.ts | 1 + src/components/PoDetail/PoDetail.tsx | 398 +++--------------- src/components/PoDetail/PoDetailRow.tsx | 360 ++++++++++++++++ .../PoDetail/askStockQtyRoundDialog.ts | 64 +++ src/components/PoDetail/stockQtyRound.ts | 41 ++ src/components/PoSearch/PoSearch.tsx | 159 +++---- src/components/PoSearch/PoSearchWrapper.tsx | 37 +- src/components/Qc/QcComponent.tsx | 10 +- src/components/Qc/QcStockInModal.tsx | 65 ++- src/components/StockIn/StockInForm.tsx | 65 +++ src/i18n/en/purchaseOrder.json | 7 + src/i18n/zh/purchaseOrder.json | 7 + 13 files changed, 761 insertions(+), 458 deletions(-) create mode 100644 src/components/PoDetail/PoDetailRow.tsx create mode 100644 src/components/PoDetail/askStockQtyRoundDialog.ts create mode 100644 src/components/PoDetail/stockQtyRound.ts diff --git a/src/app/api/stockIn/actions.ts b/src/app/api/stockIn/actions.ts index 9f74ae87..159c564a 100644 --- a/src/app/api/stockIn/actions.ts +++ b/src/app/api/stockIn/actions.ts @@ -38,6 +38,10 @@ export interface StockInLineEntry { receiptDate?: string; dnDate?: string; dnNo?: string; + stockQtyRoundMode?: "CEILING" | "FLOOR" | "HALF_UP" | "CUSTOM"; + stockQtyCustomQty?: number; + stockQtyRoundSource?: "CREATE" | "QC"; + stockQtyCustomReason?: string; } export interface QcResult{ @@ -66,6 +70,7 @@ export interface StockInInput { productionDate?: string; expiryDate: string; uom: Uom; + stockQtyRoundMode?: "CEILING" | "FLOOR"; } export interface QCInput { status: string; diff --git a/src/app/api/stockIn/index.ts b/src/app/api/stockIn/index.ts index b4951bcc..94d1aeec 100644 --- a/src/app/api/stockIn/index.ts +++ b/src/app/api/stockIn/index.ts @@ -50,6 +50,7 @@ export interface StockInInput { productionDate?: string; expiryDate: string; uom?: Uom; + stockQtyRoundMode?: "CEILING" | "FLOOR"; } export interface PoResult { diff --git a/src/components/PoDetail/PoDetail.tsx b/src/components/PoDetail/PoDetail.tsx index 1b1fb889..97c32260 100644 --- a/src/components/PoDetail/PoDetail.tsx +++ b/src/components/PoDetail/PoDetail.tsx @@ -9,9 +9,7 @@ import { Box, Button, ButtonProps, - Collapse, Grid, - IconButton, Paper, Stack, Tab, @@ -29,16 +27,14 @@ import { FormControlLabel, Card, CardContent, - Radio, - alpha, Dialog, DialogActions, DialogContent, DialogTitle, } from "@mui/material"; import { useTranslation } from "react-i18next"; -import { submitDialogWithWarning } from "../Swal/CustomAlerts"; import PrinterSelect from "@/components/common/PrinterSelect"; +import { PoDetailRow } from "./PoDetailRow"; // import InputDataGrid, { TableRow } from "../InputDataGrid/InputDataGrid"; import { GridColDef, @@ -52,9 +48,6 @@ import { fetchPoSummariesClient, startPo, } from "@/app/api/po/actions"; -import { - createStockInLine -} from "@/app/api/stockIn/actions"; import { useCallback, useContext, @@ -63,20 +56,16 @@ import { useRef, useState, } from "react"; -import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; -import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp"; import PoInputGrid from "./PoInputGrid"; // import { QcItemWithChecks } from "@/app/api/qc"; import { useRouter, useSearchParams, usePathname } from "next/navigation"; import { WarehouseResult } from "@/app/api/warehouse"; -import { calculateWeight, dateStringToDayjs, dayjsToDateString, OUTPUT_DATE_FORMAT, outputDateStringToInputDateString, returnWeightUnit } from "@/app/utils/formatUtil"; +import { dateStringToDayjs, dayjsToDateString, OUTPUT_DATE_FORMAT, outputDateStringToInputDateString, decimalFormatter, arrayToDateString } from "@/app/utils/formatUtil"; import { CameraContext } from "../Cameras/CameraProvider"; import QrModal from "./QrModal"; import { PlayArrow } from "@mui/icons-material"; import DoneIcon from "@mui/icons-material/Done"; import { downloadFile, getCustomWidth } from "@/app/utils/commonUtil"; -import { decimalFormatter, integerFormatter } from "@/app/utils/formatUtil"; -import { arrayToDateString } from "@/app/utils/formatUtil"; import { List, ListItem, ListItemButton, ListItemText, Divider } from "@mui/material"; import { Controller, FormProvider, useForm } from "react-hook-form"; import dayjs, { Dayjs } from "dayjs"; @@ -100,41 +89,6 @@ type Props = { printerCombo: PrinterCombo[]; }; -/** PO stock-in lines still in pre-complete workflow (align with nav alert: pending / receiving). */ -const PURCHASE_STOCK_IN_ALERT_STATUSES = new Set(["pending", "receiving"]); - -/** Sum of put-away in stock units (matches StockInForm「已上架數量」stockQty). */ -function totalPutAwayStockQtyForPol(row: PurchaseOrderLine): number { - return row.stockInLine - .filter((sil) => sil.purchaseOrderLineId === row.id) - .reduce((acc, sil) => { - const lineSum = - sil.putAwayLines?.reduce( - (s, p) => s + Number(p.stockQty ?? p.qty ?? 0), - 0, - ) ?? 0; - return acc + lineSum; - }, 0); -} - -/** POL order demand in stock units (same basis as PoDetail processed / backend PO detail). */ -function polOrderStockQty(row: PurchaseOrderLine): number { - return Number(row.stockUom?.stockQty ?? row.qty ?? 0); -} - -function purchaseOrderLineHasIncompleteStockIn(row: PurchaseOrderLine): boolean { - const orderStock = polOrderStockQty(row); - const putAway = totalPutAwayStockQtyForPol(row); - if (orderStock > 0 && putAway >= orderStock) { - return false; - } - return row.stockInLine - .filter((sil) => sil.purchaseOrderLineId === row.id) - .some((sil) => - PURCHASE_STOCK_IN_ALERT_STATUSES.has((sil.status ?? "").toLowerCase().trim()), - ); -} - type EntryError = | { [field in keyof StockInLine]?: string; @@ -567,6 +521,48 @@ const PoDetail: React.FC = ({ po, warehouse, printerCombo }) => { setPurchaseOrder(newPo); }, [purchaseOrder.id]); + const getDnValues = useCallback(() => dnFormProps.getValues(), [dnFormProps]); + + const formatReceiptDate = useCallback((receiptDate?: string) => { + return outputDateStringToInputDateString(receiptDate ?? ""); + }, []); + + const handleSelectPol = useCallback((row: PurchaseOrderLine) => { + selectedPolIdRef.current = row.id; + setSelectedRow(row); + setStockInLine(row.stockInLine ?? []); + setProcessedQty(row.processed); + patchPoEditQuery((params) => { + params.set("polId", String(row.id)); + params.delete("stockInLineId"); + }); + }, [patchPoEditQuery]); + + const handleRowInputBlur = useCallback((rowId: number, lotNo: string, dnQty: string) => { + setPolInputList((prev) => { + const current = prev[rowId] ?? { lotNo: "", dnQty: "" }; + if (current.lotNo === lotNo && current.dnQty === dnQty) return prev; + return { + ...prev, + [rowId]: { lotNo, dnQty }, + }; + }); + }, []); + + const handleRowSubmitted = useCallback((row: PurchaseOrderLine) => { + setPolInputList((prev) => ({ + ...prev, + [row.id]: { lotNo: "", dnQty: "" }, + })); + selectedPolIdRef.current = row.id; + setSelectedRow(row); + patchPoEditQuery((params) => { + params.set("polId", String(row.id)); + params.delete("stockInLineId"); + }); + fetchPoDetail(selectedPoId.toString(), true, row.id); + }, [fetchPoDetail, patchPoEditQuery, selectedPoId]); + const handleMailTemplateForStockInLine = useCallback(async (stockInLineId: number) => { const response = await getMailTemplatePdfForStockInLine(stockInLineId) if (response) { @@ -582,298 +578,7 @@ const PoDetail: React.FC = ({ po, warehouse, printerCombo }) => { // setStockInLine([]) // }, []); - function Row(props: { row: PurchaseOrderLine }) { - const { row } = props; - // const [firstReceiveQty, setFirstReceiveQty] = useState() - // const [secondReceiveQty, setSecondReceiveQty] = useState() - // const [open, setOpen] = useState(false); - const [processedQty, setProcessedQty] = useState(row.processed); - const [currStatus, setCurrStatus] = useState(row.status); - const [lotNoInput, setLotNoInput] = useState(polInputList[row.id]?.lotNo ?? ""); - const [dnQtyInput, setDnQtyInput] = useState(polInputList[row.id]?.dnQty ?? ""); - // const [stockInLine, setStockInLine] = useState(row.stockInLine); - const totalWeight = useMemo( - () => calculateWeight(row.qty, row.uom), - [row.qty, row.uom], - ); - const weightUnit = useMemo( - () => returnWeightUnit(row.uom), - [row.uom], - ); - useEffect(() => { - // `processedQty` comes from putAwayLines (stock unit). - // After the fix, `row.qty` is qtyM18 (M18 unit), so compare using stockUom demand. - const targetStockQty = Number(row.stockUom?.stockQty ?? row.qty ?? 0); - if (targetStockQty > 0 && processedQty >= targetStockQty) { - setCurrStatus("completed".toUpperCase()); - } else if (processedQty > 0) { - setCurrStatus("receiving".toUpperCase()); - } else { - setCurrStatus("pending".toUpperCase()); - } - }, [processedQty, row.qty, row.stockUom?.stockQty]); - - useEffect(() => { - setLotNoInput(polInputList[row.id]?.lotNo ?? ""); - setDnQtyInput(polInputList[row.id]?.dnQty ?? ""); - }, [polInputList, row.id]); - const changeStockInLines = useCallback( - (id: number) => { - const target = rows.find((r) => r.id === id); - if (!target) return; - selectedPolIdRef.current = id; - setSelectedRow(target); - setStockInLine(target.stockInLine ?? []); - setProcessedQty(target.processed); - - // history.replaceState: keep URL in sync without scrolling to top - patchPoEditQuery((params) => { - params.set("polId", String(id)); - params.delete("stockInLineId"); - }); - }, - [rows, patchPoEditQuery], - ); - - const handleStart = useCallback( - () => { - const orderQty = Number(row?.qty) ?? 0; - const acceptedQty = Number(dnQtyInput.trim()); - - if (isNaN(acceptedQty) || acceptedQty <= 0) { - alert("來貨數量必須大於0!"); - return; - } - const doSubmit = () => { - setTimeout(async () => { - const currentDnNo = dnFormProps.watch("dnNo"); - const postData = { - dnNo: dnFormProps.watch("dnNo"), - receiptDate: outputDateStringToInputDateString(dnFormProps.watch("receiptDate")), - itemId: row.itemId, - itemNo: row.itemNo, - itemName: row.itemName, - purchaseOrderLineId: row.id, - acceptedQty: acceptedQty, - productLotNo: lotNoInput || "", - }; - const res = await createStockInLine(postData); - if (res) { - setLotNoInput(""); - setDnQtyInput(""); - setPolInputList((prev) => ({ - ...prev, - [row.id]: { lotNo: "", dnQty: "" }, - })); - selectedPolIdRef.current = row.id; - setSelectedRow(row); - patchPoEditQuery((params) => { - params.set("polId", String(row.id)); - params.delete("stockInLineId"); - }); - fetchPoDetail(selectedPoId.toString(), true, row.id); - } - console.log(res); - }, 200); - }; - - const exceedOrderBy10Percent = orderQty > 0 && acceptedQty > orderQty * 1.1; - if (exceedOrderBy10Percent) { - submitDialogWithWarning(doSubmit, t, { - title: t("Confirm submit"), - html: t("This batch quantity exceeds order quantity. Do you still want to submit?"), - confirmButtonText: t("Submit"), - }); - } else { - doSubmit(); - } - }, - [dnQtyInput, row, dnFormProps, selectedPoId, fetchPoDetail, t, lotNoInput, patchPoEditQuery], - ); - - const syncRowInputToParent = useCallback((lotNo: string, dnQty: string) => { - setPolInputList((prev) => { - const current = prev[row.id] ?? { lotNo: "", dnQty: "" }; - if (current.lotNo === lotNo && current.dnQty === dnQty) return prev; - return { - ...prev, - [row.id]: { lotNo, dnQty }, - }; - }); - }, [row.id]); - - // const [focusField, setFocusField] = useState(); - - // 本批收貨數量(訂單單位): 使用者在該行輸入的 dnQty - const batchPurchaseQty = Number(dnQtyInput.trim()) || 0; - - // 已來貨總數(庫存單位): 同一 POL 底下所有 stock_in_line.acceptedQty 的合計 - const totalStockReceived = row.stockInLine - .filter((sil) => sil.purchaseOrderLineId === row.id) - .reduce((acc, cur) => acc + (cur.acceptedQty ?? 0), 0); - const receivedTotalText = decimalFormatter.format(totalStockReceived); - const highlightColor = - Number(receivedTotalText.replace(/,/g, "")) <= 0 ? "red" : "inherit"; - const needsStockInAttention = - canSeeStockInReminders && purchaseOrderLineHasIncompleteStockIn(row); - return ( - <> - - - *": { borderBottom: "unset" }, - color: "black", - ...(needsStockInAttention - ? (theme) => ({ - boxShadow: `inset 4px 0 0 ${theme.palette.error.main}`, - backgroundColor: alpha(theme.palette.error.main, 0.07), - }) - : {}), - }} - onClick={() => changeStockInLines(row.id)} - > - - {/* - setOpen(!open)} - > - {open ? : } - - */} - - {needsStockInAttention && ( - `0 0 0 1px ${alpha(theme.palette.error.main, 0.45)}`, - zIndex: 1, - }} - /> - )} - - - - {row.itemNo} - - - {row.itemName} - - {integerFormatter.format(row.qty)} - {integerFormatter.format(row.processed)} - {row.uom?.udfudesc} - {/* {decimalFormatter.format(row.stockUom.stockQty)} */} - {/* {receivedTotal} */} - - {decimalFormatter.format(totalStockReceived)} - - {row.stockUom.stockUomDesc} - {/* - {decimalFormatter.format(totalWeight)} {weightUnit} - */} - {/* {weightUnit} */} - {/* {decimalFormatter.format(row.price)} */} - {/* {row.expiryDate} */} - {t(`${row.status.toLowerCase()}`)} - {/* {t(`${currStatus.toLowerCase()}`)} */} - {/* {integerFormatter.format(row.receivedQty)} */} - - setLotNoInput(e.target.value)} - onBlur={() => syncRowInputToParent(lotNoInput, dnQtyInput)} - onClick={(e) => e.stopPropagation()} - // onFocus={(e) => {setFocusField(e.target as HTMLInputElement);}} - /> - - - setDnQtyInput(e.target.value)} - onBlur={() => syncRowInputToParent(lotNoInput, dnQtyInput)} - onClick={(e) => e.stopPropagation()} - InputProps={{ - inputProps: { - min: 0, // Optional: set a minimum value - step: "any", - inputMode: "decimal", - } - }} - /> - - - - - - {/* */} - {/* */} - {/* */} - {/* */} - {/* */} - {/* - - - - - - - - - -
*/} - {/*
*/} - {/*
*/} - {/*
*/} - - ); - } -// ROW END const [tabIndex, setTabIndex] = useState(0); const handleTabChange = useCallback>( @@ -1178,7 +883,20 @@ const PoDetail: React.FC = ({ po, warehouse, printerCombo }) => { {rows.map((row) => ( - + ))} diff --git a/src/components/PoDetail/PoDetailRow.tsx b/src/components/PoDetail/PoDetailRow.tsx new file mode 100644 index 00000000..0cca3b58 --- /dev/null +++ b/src/components/PoDetail/PoDetailRow.tsx @@ -0,0 +1,360 @@ +"use client"; + +import { PurchaseOrderLine } from "@/app/api/po"; +import { + Box, + Button, + Radio, + Stack, + TableCell, + TableRow, + TextField, + Typography, + alpha, +} from "@mui/material"; +import { memo, useCallback, useEffect, useRef, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { decimalFormatter, integerFormatter } from "@/app/utils/formatUtil"; +import { submitDialogWithWarning } from "../Swal/CustomAlerts"; +import { createStockInLine } from "@/app/api/stockIn/actions"; +import { + isNotIntegerQty, + previewPoBatchStockQty, + roundStockQty, + StockQtyRoundChoice, + StockQtyRoundMode, +} from "./stockQtyRound"; + +const PURCHASE_STOCK_IN_ALERT_STATUSES = new Set(["pending", "receiving"]); + +function totalPutAwayStockQtyForPol(row: PurchaseOrderLine): number { + return row.stockInLine + .filter((sil) => sil.purchaseOrderLineId === row.id) + .reduce((acc, sil) => { + const lineSum = + sil.putAwayLines?.reduce( + (s, p) => s + Number(p.stockQty ?? p.qty ?? 0), + 0, + ) ?? 0; + return acc + lineSum; + }, 0); +} + +function polOrderStockQty(row: PurchaseOrderLine): number { + return Number(row.stockUom?.stockQty ?? row.qty ?? 0); +} + +function purchaseOrderLineHasIncompleteStockIn(row: PurchaseOrderLine): boolean { + const orderStock = polOrderStockQty(row); + const putAway = totalPutAwayStockQtyForPol(row); + if (orderStock > 0 && putAway >= orderStock) { + return false; + } + return row.stockInLine + .filter((sil) => sil.purchaseOrderLineId === row.id) + .some((sil) => + PURCHASE_STOCK_IN_ALERT_STATUSES.has((sil.status ?? "").toLowerCase().trim()), + ); +} + +export type PoDetailRowDnValues = { + dnNo?: string; + receiptDate?: string; +}; + +type Props = { + row: PurchaseOrderLine; + selected: boolean; + canSeeStockInReminders: boolean; + showDnQty: boolean; + savedLotNo: string; + savedDnQty: string; + onSelect: (row: PurchaseOrderLine) => void; + onInputBlur: (rowId: number, lotNo: string, dnQty: string) => void; + getDnValues: () => PoDetailRowDnValues; + formatReceiptDate: (receiptDate?: string) => string | undefined; + onSubmitted: (row: PurchaseOrderLine) => void; +}; + +export const PoDetailRow = memo(function PoDetailRow({ + row, + selected, + canSeeStockInReminders, + showDnQty, + savedLotNo, + savedDnQty, + onSelect, + onInputBlur, + getDnValues, + formatReceiptDate, + onSubmitted, +}: Props) { + const { t } = useTranslation("purchaseOrder"); + const [lotNoInput, setLotNoInput] = useState(savedLotNo); + const [dnQtyInput, setDnQtyInput] = useState(savedDnQty); + const submitInFlightRef = useRef(false); + const [isStarting, setIsStarting] = useState(false); + + useEffect(() => { + setLotNoInput(savedLotNo); + setDnQtyInput(savedDnQty); + }, [savedLotNo, savedDnQty]); + + const handleStart = useCallback( + (roundMode?: StockQtyRoundMode) => { + if (submitInFlightRef.current || isStarting) return; + const orderQty = Number(row?.qty) ?? 0; + const acceptedQty = Number(dnQtyInput.trim()); + + if (isNaN(acceptedQty) || acceptedQty <= 0) { + alert("來貨數量必須大於0!"); + return; + } + + const previewStockQty = previewPoBatchStockQty( + orderQty, + Number(row.stockUom?.stockQty ?? 0), + acceptedQty, + ); + const needsRound = isNotIntegerQty(previewStockQty); + if (needsRound && roundMode !== "CEILING" && roundMode !== "FLOOR") { + return; + } + const round: StockQtyRoundChoice | undefined = + needsRound && roundMode + ? { + mode: roundMode, + before: previewStockQty, + after: roundStockQty(previewStockQty, roundMode), + } + : undefined; + + const doSubmit = () => { + if (submitInFlightRef.current) return; + submitInFlightRef.current = true; + setIsStarting(true); + void (async () => { + try { + const dn = getDnValues(); + const postData = { + dnNo: dn.dnNo, + receiptDate: formatReceiptDate(dn.receiptDate), + itemId: row.itemId, + itemNo: row.itemNo, + itemName: row.itemName, + purchaseOrderLineId: row.id, + acceptedQty: acceptedQty, + productLotNo: lotNoInput || "", + ...(round + ? { + stockQtyRoundMode: round.mode, + stockQtyRoundSource: "CREATE" as const, + } + : {}), + }; + const res = await createStockInLine(postData); + if (res) { + setLotNoInput(""); + setDnQtyInput(""); + onSubmitted(row); + } + console.log(res); + } finally { + setIsStarting(false); + submitInFlightRef.current = false; + } + })(); + }; + + const exceedOrderBy10Percent = orderQty > 0 && acceptedQty > orderQty * 1.1; + if (exceedOrderBy10Percent) { + submitDialogWithWarning(doSubmit, t, { + title: t("Confirm submit"), + html: t("This batch quantity exceeds order quantity. Do you still want to submit?"), + confirmButtonText: t("Submit"), + }); + } else { + doSubmit(); + } + }, + [ + dnQtyInput, + formatReceiptDate, + getDnValues, + isStarting, + lotNoInput, + onSubmitted, + row, + t, + ], + ); + + const batchPurchaseQty = Number(dnQtyInput.trim()) || 0; + const previewStockQty = previewPoBatchStockQty( + Number(row?.qty) ?? 0, + Number(row.stockUom?.stockQty ?? 0), + batchPurchaseQty, + ); + const needsStockQtyRound = batchPurchaseQty > 0 && isNotIntegerQty(previewStockQty); + const roundUpQty = roundStockQty(previewStockQty, "CEILING"); + const roundDownQty = roundStockQty(previewStockQty, "FLOOR"); + const stockUomLabel = row.stockUom?.stockUomDesc?.trim() + ? ` ${row.stockUom.stockUomDesc.trim()}` + : ""; + + const totalStockReceived = row.stockInLine + .filter((sil) => sil.purchaseOrderLineId === row.id) + .reduce((acc, cur) => acc + (cur.acceptedQty ?? 0), 0); + const receivedTotalText = decimalFormatter.format(totalStockReceived); + const highlightColor = + Number(receivedTotalText.replace(/,/g, "")) <= 0 ? "red" : "inherit"; + const needsStockInAttention = + canSeeStockInReminders && purchaseOrderLineHasIncompleteStockIn(row); + + return ( + *": { borderBottom: "unset" }, + color: "black", + ...(needsStockInAttention + ? (theme) => ({ + boxShadow: `inset 4px 0 0 ${theme.palette.error.main}`, + backgroundColor: alpha(theme.palette.error.main, 0.07), + }) + : {}), + }} + onClick={() => onSelect(row)} + > + + {needsStockInAttention && ( + `0 0 0 1px ${alpha(theme.palette.error.main, 0.45)}`, + zIndex: 1, + }} + /> + )} + + + + {row.itemNo} + + + {row.itemName} + + {integerFormatter.format(row.qty)} + {integerFormatter.format(row.processed)} + {row.uom?.udfudesc} + + {decimalFormatter.format(totalStockReceived)} + + + {row.stockUom.stockUomDesc} + + + {t(`${row.status.toLowerCase()}`)} + + + setLotNoInput(e.target.value)} + onBlur={() => onInputBlur(row.id, lotNoInput, dnQtyInput)} + onClick={(e) => e.stopPropagation()} + /> + + {showDnQty ? ( + + setDnQtyInput(e.target.value)} + onBlur={() => onInputBlur(row.id, lotNoInput, dnQtyInput)} + onClick={(e) => e.stopPropagation()} + InputProps={{ + inputProps: { + min: 0, + step: "any", + inputMode: "decimal", + }, + }} + /> + + ) : null} + + {needsStockQtyRound ? ( + e.stopPropagation()}> + + {t("Converted stock qty is")} {decimalFormatter.format(previewStockQty)} + {row.stockUom?.stockUomDesc ? ` (${row.stockUom.stockUomDesc})` : ""} + + + + + ) : ( + + )} + + + ); +}); diff --git a/src/components/PoDetail/askStockQtyRoundDialog.ts b/src/components/PoDetail/askStockQtyRoundDialog.ts new file mode 100644 index 00000000..cdcef384 --- /dev/null +++ b/src/components/PoDetail/askStockQtyRoundDialog.ts @@ -0,0 +1,64 @@ +import Swal from "sweetalert2"; +import { TFunction } from "i18next"; +import { + isNotIntegerQty, + roundStockQty, + StockQtyRoundChoice, + StockQtyRoundMode, +} from "./stockQtyRound"; + +type Translate = TFunction<["translation", ...string[]], undefined>; + +export async function askStockQtyRoundDialog( + before: number, + t: Translate, + stockUomDesc?: string, +): Promise { + if (!isNotIntegerQty(before)) return null; + + const ceiling = roundStockQty(before, "CEILING"); + const floor = roundStockQty(before, "FLOOR"); + const uom = stockUomDesc?.trim() ? ` (${stockUomDesc.trim()})` : ""; + const beforeText = before.toFixed(2); + + const result = await Swal.fire({ + icon: "warning", + title: t("Stock qty is not an integer"), + html: ` +
+

${t("Converted stock qty is")} ${beforeText}${uom}${t("Choose rounding method")}

+ + +
+ `, + showCancelButton: true, + confirmButtonText: t("Confirm"), + cancelButtonText: t("Cancel"), + customClass: { + container: "swal-container-class", + popup: "swal-popup-class", + }, + preConfirm: () => { + const popup = Swal.getPopup(); + const selected = popup?.querySelector('input[name="stockQtyRoundMode"]:checked')?.value as StockQtyRoundMode | undefined; + if (selected !== "CEILING" && selected !== "FLOOR") { + Swal.showValidationMessage(t("Please choose a rounding method")); + return false; + } + return { + mode: selected, + before, + after: roundStockQty(before, selected), + } satisfies StockQtyRoundChoice; + }, + }); + + if (!result.isConfirmed) return null; + return (result.value as StockQtyRoundChoice) ?? null; +} diff --git a/src/components/PoDetail/stockQtyRound.ts b/src/components/PoDetail/stockQtyRound.ts new file mode 100644 index 00000000..ae2c6bf0 --- /dev/null +++ b/src/components/PoDetail/stockQtyRound.ts @@ -0,0 +1,41 @@ +export type StockQtyRoundMode = "CEILING" | "FLOOR"; +export type StockQtyRoundSource = "CREATE" | "QC"; + +export type StockQtyRoundChoice = { + mode: StockQtyRoundMode; + before: number; + after: number; +}; + +export function previewPoBatchStockQty( + orderM18Qty: number, + orderStockQty: number, + batchM18Qty: number, +): number { + if (!Number.isFinite(orderM18Qty) || orderM18Qty === 0) { + return Number(batchM18Qty.toFixed(2)); + } + return Number(((batchM18Qty * orderStockQty) / orderM18Qty).toFixed(2)); +} + +export function isNotIntegerQty(qty: number): boolean { + if (!Number.isFinite(qty)) return false; + return Math.abs(qty - Math.round(qty)) > 1e-9; +} + +/** PO 收貨/品檢:pending 或 escalated 且庫存數量非整數時需選擇 CEILING / FLOOR */ +export function needsPoQcStockQtyRound( + purchaseOrderLineId: number | null | undefined, + status: string | null | undefined, + acceptedQty: number | null | undefined, +): boolean { + if (!purchaseOrderLineId) return false; + const silStatus = (status ?? "").toLowerCase().trim(); + if (silStatus !== "pending" && silStatus !== "escalated") return false; + return isNotIntegerQty(Number(acceptedQty ?? 0)); +} + +export function roundStockQty(before: number, mode: StockQtyRoundMode): number { + if (mode === "CEILING") return Math.ceil(before); + return Math.floor(before); +} diff --git a/src/components/PoSearch/PoSearch.tsx b/src/components/PoSearch/PoSearch.tsx index 9e26723f..a4465dc0 100644 --- a/src/components/PoSearch/PoSearch.tsx +++ b/src/components/PoSearch/PoSearch.tsx @@ -260,8 +260,15 @@ const PoSearch: React.FC = ({ ); const onReset = useCallback(() => { - setFilteredPo(po); - }, [po]); + const today = dayjsToDateString(dayjs(), "input"); + setSelectedPoIds([]); + setSelectAll(false); + setPagingController(defaultPagingController); + setFilterArgs({ + estimatedArrivalDate: today, + estimatedArrivalDateTo: today, + }); + }, []); const [autoSyncStatus, setAutoSyncStatus] = useState(null); const [isM18LookupLoading, setIsM18LookupLoading] = useState(false); @@ -286,93 +293,97 @@ const PoSearch: React.FC = ({ if (typeof v === "string" && (v as string).trim() === "") return; cleanedQuery[k] = String(v); }); - const baseListResp = await clientAuthFetch( - `${NEXT_PUBLIC_API_URL}/po/list?${new URLSearchParams(cleanedQuery).toString()}`, - { method: "GET" }, - ); - if (!baseListResp.ok) { - throw new Error(`PO list fetch failed: ${baseListResp.status}`); - } - const res = await baseListResp.json(); - if (!res) return; - - if (res.records && res.records.length > 0) { - setFilteredPo(res.records); - setTotalCount(res.total); - return; - } - - const searchedCodeRaw = (filterArgs as any)?.code; - const searchedCode = - typeof searchedCodeRaw === "string" ? searchedCodeRaw.trim() : ""; - - const shouldAutoSyncFromM18 = - searchedCode.length > 14 && - (searchedCode.startsWith("PP") || searchedCode.startsWith("PF")); - - if (!shouldAutoSyncFromM18 || autoSyncInProgressRef.current) { - setFilteredPo(res.records); - setTotalCount(res.total); - return; - } - try { - autoSyncInProgressRef.current = true; - setIsM18LookupLoading(true); - setAutoSyncStatus("正在從M18找尋PO..."); - const syncResp = await clientAuthFetch( - `${NEXT_PUBLIC_API_URL}/m18/test/po-by-code?code=${encodeURIComponent( - searchedCode, - )}`, + const baseListResp = await clientAuthFetch( + `${NEXT_PUBLIC_API_URL}/po/list?${new URLSearchParams(cleanedQuery).toString()}`, { method: "GET" }, ); - - if (!syncResp.ok) { - throw new Error(`M18 sync failed: ${syncResp.status}`); + if (!baseListResp.ok) { + throw new Error(`PO list fetch failed: ${baseListResp.status}`); } + const res = await baseListResp.json(); + if (!res) return; - let syncJson: any = null; - try { - syncJson = await syncResp.json(); - } catch { - // Some endpoints may respond with plain text - const txt = await syncResp.text(); - syncJson = { raw: txt }; + if (res.records && res.records.length > 0) { + setFilteredPo(res.records); + setTotalCount(res.total); + return; } - const syncOk = Boolean(syncJson?.totalSuccess && syncJson.totalSuccess > 0); - if (syncOk) { - setAutoSyncStatus("成功找到PO"); + const searchedCodeRaw = (filterArgs as any)?.code; + const searchedCode = + typeof searchedCodeRaw === "string" ? searchedCodeRaw.trim() : ""; + + const shouldAutoSyncFromM18 = + searchedCode.length > 14 && + (searchedCode.startsWith("PP") || searchedCode.startsWith("PF")); - const listResp = await clientAuthFetch( - `${NEXT_PUBLIC_API_URL}/po/list?${new URLSearchParams( - cleanedQuery, - ).toString()}`, + if (!shouldAutoSyncFromM18 || autoSyncInProgressRef.current) { + setFilteredPo(res.records); + setTotalCount(res.total); + return; + } + + try { + autoSyncInProgressRef.current = true; + setIsM18LookupLoading(true); + setAutoSyncStatus("正在從M18找尋PO..."); + const syncResp = await clientAuthFetch( + `${NEXT_PUBLIC_API_URL}/m18/test/po-by-code?code=${encodeURIComponent( + searchedCode, + )}`, { method: "GET" }, ); - if (listResp.ok) { - const listJson = await listResp.json(); - setFilteredPo(listJson.records ?? []); - setTotalCount(listJson.total ?? 0); + + if (!syncResp.ok) { + throw new Error(`M18 sync failed: ${syncResp.status}`); + } + + let syncJson: any = null; + try { + syncJson = await syncResp.json(); + } catch { + // Some endpoints may respond with plain text + const txt = await syncResp.text(); + syncJson = { raw: txt }; + } + + const syncOk = Boolean(syncJson?.totalSuccess && syncJson.totalSuccess > 0); + if (syncOk) { setAutoSyncStatus("成功找到PO"); - return; + + const listResp = await clientAuthFetch( + `${NEXT_PUBLIC_API_URL}/po/list?${new URLSearchParams( + cleanedQuery, + ).toString()}`, + { method: "GET" }, + ); + if (listResp.ok) { + const listJson = await listResp.json(); + setFilteredPo(listJson.records ?? []); + setTotalCount(listJson.total ?? 0); + setAutoSyncStatus("成功找到PO"); + return; + } + setAutoSyncStatus("找不到PO"); + } else { + setAutoSyncStatus("找不到PO"); } + + // Ensure UI updates even if sync didn't change results + setFilteredPo(res.records); + setTotalCount(res.total ?? 0); + } catch (e) { + console.error("Auto sync error:", e); setAutoSyncStatus("找不到PO"); - } else { - setAutoSyncStatus("找不到PO"); + setFilteredPo(res.records); + setTotalCount(res.total ?? 0); + } finally { + setIsM18LookupLoading(false); + autoSyncInProgressRef.current = false; } - - // Ensure UI updates even if sync didn't change results - setFilteredPo(res.records); - setTotalCount(res.total ?? 0); } catch (e) { - console.error("Auto sync error:", e); - setAutoSyncStatus("找不到PO"); - setFilteredPo(res.records); - setTotalCount(res.total ?? 0); - } finally { - setIsM18LookupLoading(false); - autoSyncInProgressRef.current = false; + console.error("PO list fetch error:", e); } }, [], diff --git a/src/components/PoSearch/PoSearchWrapper.tsx b/src/components/PoSearch/PoSearchWrapper.tsx index de505552..06f238ea 100644 --- a/src/components/PoSearch/PoSearchWrapper.tsx +++ b/src/components/PoSearch/PoSearchWrapper.tsx @@ -1,45 +1,12 @@ -import { fetchAllItems } from "@/app/api/settings/item"; -// import ItemsSearch from "./ItemsSearch"; -// import ItemsSearchLoading from "./ItemsSearchLoading"; -import { SearchParams } from "@/app/utils/fetchUtil"; -import { TypeEnum } from "@/app/utils/typeEnum"; -import { notFound } from "next/navigation"; import PoSearchLoading from "./PoSearchLoading"; import PoSearch from "./PoSearch"; -import { fetchPoList, PoResult } from "@/app/api/po"; -import dayjs from "dayjs"; -import arraySupport from "dayjs/plugin/arraySupport"; -import { OUTPUT_DATE_FORMAT } from "@/app/utils/formatUtil"; -import { defaultPagingController } from "../SearchResults/SearchResults"; -dayjs.extend(arraySupport); interface SubComponents { Loading: typeof PoSearchLoading; } -type Props = { - // type: TypeEnum; -}; - -const PoSearchWrapper: React.FC & SubComponents = async ( - { - // type, - }, -) => { - // console.log(defaultPagingController) - const po = await fetchPoList({ - pageNum: 1, - pageSize: 10, - }); - const fixPoDate = po.records.map((p) => { - return { - ...p, - orderDate: dayjs(p.orderDate).add(-1, "month").format(OUTPUT_DATE_FORMAT), - }; - }); - return ( - - ); +const PoSearchWrapper: React.FC & SubComponents = () => { + return ; }; PoSearchWrapper.Loading = PoSearchLoading; diff --git a/src/components/Qc/QcComponent.tsx b/src/components/Qc/QcComponent.tsx index 3c48e137..3c111f7d 100644 --- a/src/components/Qc/QcComponent.tsx +++ b/src/components/Qc/QcComponent.tsx @@ -150,9 +150,9 @@ const QcComponent: React.FC = ({ itemDetail, disabled = false, compactLay if (isNaN(accQty) || accQty === undefined || accQty === null || typeof(accQty) != "number") { setError("acceptQty", { message: t("value must be a number") }); } else - if (!isJobOrder && accQty > itemDetail.acceptedQty) { + if (!isJobOrder && accQty > Math.ceil(itemDetail.acceptedQty)) { setError("acceptQty", { message: `${t("acceptQty must not greater than")} ${ - itemDetail.acceptedQty}` }); + Math.ceil(itemDetail.acceptedQty)}` }); } else if (accQty <= 0) { setError("acceptQty", { message: t("minimal value is 1") }); @@ -163,8 +163,8 @@ const QcComponent: React.FC = ({ itemDetail, disabled = false, compactLay },[setError, qcDecision, accQty, itemDetail, isJobOrder]) useEffect(() => { // W I P // ----- if (qcDecision == 1) { - if (!isJobOrder && validateFieldFail("acceptQty", accQty > itemDetail.acceptedQty, `${t("acceptQty must not greater than")} ${ - itemDetail.acceptedQty}`)) return; + if (!isJobOrder && validateFieldFail("acceptQty", accQty > Math.ceil(itemDetail.acceptedQty), `${t("acceptQty must not greater than")} ${ + Math.ceil(itemDetail.acceptedQty)}`)) return; if (validateFieldFail("acceptQty", accQty <= 0, t("minimal value is 1"))) return; if (validateFieldFail("acceptQty", isNaN(accQty), t("value must be a number"))) return; @@ -616,7 +616,7 @@ useEffect(() => { } e.target.value = r; }} - inputProps={isJobOrder ? { min: 0.01, step: 0.01 } : { min: 0.01, max: itemDetail.acceptedQty, step: 0.01 }} + inputProps={isJobOrder ? { min: 0.01, step: 0.01 } : { min: 0.01, max: Math.ceil(itemDetail.acceptedQty), step: 0.01 }} // onChange={(e) => { // const inputValue = e.target.value; // if (inputValue === '' || /^[0-9]*$/.test(inputValue)) { diff --git a/src/components/Qc/QcStockInModal.tsx b/src/components/Qc/QcStockInModal.tsx index 8607d15c..0161f4f9 100644 --- a/src/components/Qc/QcStockInModal.tsx +++ b/src/components/Qc/QcStockInModal.tsx @@ -14,7 +14,7 @@ import { TextField, Typography, } from "@mui/material"; -import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useState } from "react"; +import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useRef, useState } from "react"; import { FormProvider, SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form"; import { StockInLineRow } from "../PoDetail/PoInputGrid"; import { useTranslation } from "react-i18next"; @@ -23,6 +23,7 @@ import QcComponent from "./QcComponent"; import PutAwayForm from "../PoDetail/PutAwayForm"; import { GridRowModes, GridRowSelectionModel, useGridApiRef } from "@mui/x-data-grid"; import {msg, submitDialogWithWarning} from "../Swal/CustomAlerts"; +import { needsPoQcStockQtyRound, roundStockQty } from "../PoDetail/stockQtyRound"; import { INPUT_DATE_FORMAT, arrayToDateString, dayjsToDateTimeString } from "@/app/utils/formatUtil"; import dayjs from "dayjs"; import { fetchPoQrcode } from "@/app/api/pdf/actions"; @@ -99,6 +100,7 @@ const QcStockInModal: React.FC = ({ const [stockInLineInfo, setStockInLineInfo] = useState(); const [isLoading, setIsLoading] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); + const qcSubmitInFlightRef = useRef(false); // const [skipQc, setSkipQc] = useState(false); // const [viewOnly, setViewOnly] = useState(false); @@ -241,6 +243,29 @@ const QcStockInModal: React.FC = ({ ...defaultNewValue, }, }); + const stockQtyRoundMode = formProps.watch("stockQtyRoundMode"); + const qcDecision = formProps.watch("qcDecision"); + const roundChoiceRequired = useMemo(() => { + const needsRound = needsPoQcStockQtyRound( + stockInLineInfo?.purchaseOrderLineId, + stockInLineInfo?.status, + stockInLineInfo?.acceptedQty, + ); + const willAcceptStock = Boolean(skipQc) || qcDecision == 1; + return ( + needsRound && + willAcceptStock && + stockQtyRoundMode !== "CEILING" && + stockQtyRoundMode !== "FLOOR" + ); + }, [ + stockInLineInfo?.purchaseOrderLineId, + stockInLineInfo?.status, + stockInLineInfo?.acceptedQty, + skipQc, + qcDecision, + stockQtyRoundMode, + ]); const closeWithResult = useCallback( (updatedStockInLine?: StockInLine) => { @@ -410,6 +435,34 @@ const QcStockInModal: React.FC = ({ return; } + if (qcSubmitInFlightRef.current || isSubmitting) return; + + const storedStockQty = Number(stockInLineInfo?.acceptedQty ?? 0); + const needsStockQtyRound = needsPoQcStockQtyRound( + stockInLineInfo?.purchaseOrderLineId, + stockInLineInfo?.status, + storedStockQty, + ); + const willAcceptStock = Boolean(skipQc) || qcAcceptLocal; + let roundPayload: + | { + stockQtyRoundMode: "CEILING" | "FLOOR"; + stockQtyRoundSource: "QC"; + } + | undefined; + if (willAcceptStock && needsStockQtyRound) { + const selectedMode = data.stockQtyRoundMode; + if (selectedMode !== "CEILING" && selectedMode !== "FLOOR") { + alert(t("Please choose a rounding method")); + return; + } + acceptQtyLocal = roundStockQty(storedStockQty, selectedMode); + roundPayload = { + stockQtyRoundMode: selectedMode, + stockQtyRoundSource: "QC", + }; + } + const isJobOrderSource = Boolean(stockInLineInfo?.jobOrderId) || printSource === "productionProcess"; const qcData = { dnNo : data.dnNo? data.dnNo : "DN00000", @@ -423,6 +476,7 @@ const QcStockInModal: React.FC = ({ // For Job Order QC, allow updating received qty beyond demand/accepted. // Backend uses request.acceptedQty in QC flow, so we must send it explicitly. acceptedQty: (qcAcceptLocal && isJobOrderSource) ? (acceptQtyLocal ? acceptQtyLocal : 0) : stockInLineInfo?.acceptedQty, + ...roundPayload, // qcResult: itemDetail.status != "escalated" ? qcResults.map(item => ({ qcResult: qcResultsLocal.map(item => ({ // id: item.id, @@ -431,7 +485,7 @@ const QcStockInModal: React.FC = ({ // qcDescription: item.qcDescription, qcPassed: item.qcPassed? item.qcPassed : false, failQty: (item.failQty && !item.qcPassed) ? item.failQty : 0, - // failedQty: (typeof item.failedQty === "number" && !item.isPassed) ? item.failedQty : 0, + // failedQty: (typeof item.failedQty === "number" && !item.isPassed) ? item.failQty : 0, remarks: item.remarks || '', ...(QC_MEASUREMENT_ENABLED && isMeasurableQcItem(item) ? { measurement: buildQcMeasurementPayload(item) } @@ -457,11 +511,13 @@ const QcStockInModal: React.FC = ({ } console.log("Escalation Data for submission", escalationLog); + qcSubmitInFlightRef.current = true; setIsSubmitting(true); const resEscalate = await postStockInLine({...qcData, escalationLog}); qcRes = Array.isArray(resEscalate.entity) ? resEscalate.entity[0] : (resEscalate.entity as StockInLine); } else { + qcSubmitInFlightRef.current = true; setIsSubmitting(true); const resNormal = await postStockInLine(qcData); qcRes = Array.isArray(resNormal.entity) ? resNormal.entity[0] : (resNormal.entity as StockInLine); @@ -553,7 +609,8 @@ const QcStockInModal: React.FC = ({ } else { closeWithResult(qcRes); } - setIsSubmitting(false); + setIsSubmitting(false); + qcSubmitInFlightRef.current = false; msg("已更新來貨狀態", { position: typeof window !== "undefined" && @@ -815,7 +872,7 @@ const printQrcode = useCallback( color="primary" sx={{ mt: 1 }} onClick={formProps.handleSubmit(onSubmitQc, onSubmitErrorQc)} - disabled={isSubmitting || isLoading} + disabled={isSubmitting || isLoading || roundChoiceRequired} > {isSubmitting ? (t("submitting")) : (skipQc ? t("confirm") : t("confirm qc result"))} )} diff --git a/src/components/StockIn/StockInForm.tsx b/src/components/StockIn/StockInForm.tsx index 7894f678..632b0ae7 100644 --- a/src/components/StockIn/StockInForm.tsx +++ b/src/components/StockIn/StockInForm.tsx @@ -23,6 +23,11 @@ import { useGridApiRef, } from "@mui/x-data-grid"; import { StockInLine } from "@/app/api/stockIn"; +import { + needsPoQcStockQtyRound, + roundStockQty, + StockQtyRoundMode, +} from "@/components/PoDetail/stockQtyRound"; import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { dayjsToDateString, INPUT_DATE_FORMAT, OUTPUT_DATE_FORMAT } from "@/app/utils/formatUtil"; @@ -114,6 +119,36 @@ const StockInForm: React.FC = ({ const productionDate = watch("productionDate"); const expiryDate = watch("expiryDate"); const uom = watch("uom"); + const stockQtyRoundMode = watch("stockQtyRoundMode"); + const displayedAcceptedQty = watch("acceptedQty"); + const originalAcceptedQty = Number(itemDetail.acceptedQty ?? 0); + const showStockQtyRoundButtons = + !putawayMode && + !disabled && + needsPoQcStockQtyRound( + itemDetail.purchaseOrderLineId, + itemDetail.status, + originalAcceptedQty, + ); + const stockUomLabel = itemDetail.stockUomDesc ? ` ${itemDetail.stockUomDesc}` : ""; + const roundUpQty = roundStockQty(originalAcceptedQty, "CEILING"); + const roundDownQty = roundStockQty(originalAcceptedQty, "FLOOR"); + + const selectStockQtyRound = useCallback( + (mode: StockQtyRoundMode) => { + const after = roundStockQty(Number(itemDetail.acceptedQty ?? 0), mode); + setValue("stockQtyRoundMode", mode, { shouldDirty: true, shouldTouch: true }); + setValue("acceptedQty", after, { shouldDirty: true, shouldTouch: true }); + (setValue as (name: string, value: unknown, options?: object) => void)( + "acceptQty", + after, + { shouldDirty: true, shouldTouch: true }, + ); + const accInput = document.getElementById("accQty") as HTMLInputElement | null; + if (accInput) accInput.value = String(after); + }, + [itemDetail.acceptedQty, setValue], + ); const [openModal, setOpenModal] = useState(false); const [openExpDatePicker, setOpenExpDatePicker] = useState(false); @@ -401,17 +436,47 @@ const StockInForm: React.FC = ({ ) : ( + <> + {showStockQtyRoundButtons && ( + + + + {t("Please choose a rounding method")} + + + + + + + + )} + )} {/* Date: Thu, 10 Sep 2026 17:17:11 +0800 Subject: [PATCH 2/3] =?UTF-8?q?QC=20=E5=BE=8C=E6=93=8D=E4=BD=9C=E6=AC=84?= =?UTF-8?q?=E5=8A=A0=E5=AF=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PoDetail/PoDetail.tsx | 2 +- src/components/PoDetail/PoDetailRow.tsx | 2 +- src/components/PoDetail/PoInputGrid.tsx | 5 +++-- src/components/PoDetail/QcStockInModal.tsx | 2 +- src/components/PoDetail/StockInLineRowActions.tsx | 2 +- src/components/PoDetail/stockQtyRound.ts | 6 +++--- src/components/Qc/QcStockInModal.tsx | 2 +- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/components/PoDetail/PoDetail.tsx b/src/components/PoDetail/PoDetail.tsx index 0853d6d5..4b16fc53 100644 --- a/src/components/PoDetail/PoDetail.tsx +++ b/src/components/PoDetail/PoDetail.tsx @@ -207,7 +207,7 @@ interface PolInputResult { dnQty: string, } -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ const PoDetail: React.FC = ({ po, warehouse, printerCombo }) => { const cameras = useContext(CameraContext); const { data: session } = useSession(); diff --git a/src/components/PoDetail/PoDetailRow.tsx b/src/components/PoDetail/PoDetailRow.tsx index 904b4b26..682aa4dd 100644 --- a/src/components/PoDetail/PoDetailRow.tsx +++ b/src/components/PoDetail/PoDetailRow.tsx @@ -68,7 +68,7 @@ type Props = { onSubmitted: (row: PurchaseOrderLine) => void; }; -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ export const PoDetailRow = memo(function PoDetailRow({ row, selected, diff --git a/src/components/PoDetail/PoInputGrid.tsx b/src/components/PoDetail/PoInputGrid.tsx index 8e848e2d..b4572b58 100644 --- a/src/components/PoDetail/PoInputGrid.tsx +++ b/src/components/PoDetail/PoInputGrid.tsx @@ -71,7 +71,8 @@ import { deleteDialog } from "../Swal/CustomAlerts"; import StockInLineRowActions from "./StockInLineRowActions"; import { StockQtyRoundMode, needsPoQcStockQtyRound } from "./stockQtyRound"; -const ACTIONS_COLUMN_WIDTH = 380; +// 3 buttons after QC (view + print QR + delete): 176*3 + gap + cell padding +const ACTIONS_COLUMN_WIDTH = 580; const PURCHASE_QTY_COLUMN_WIDTH = 72; const UOM_COLUMN_WIDTH = 124; const STOCK_QTY_COLUMN_WIDTH = 110; @@ -153,7 +154,7 @@ class ProcessRowUpdateError extends Error { } } -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ function PoInputGrid({ // qc, setRows, diff --git a/src/components/PoDetail/QcStockInModal.tsx b/src/components/PoDetail/QcStockInModal.tsx index 013f4315..56d5a87a 100644 --- a/src/components/PoDetail/QcStockInModal.tsx +++ b/src/components/PoDetail/QcStockInModal.tsx @@ -71,7 +71,7 @@ interface CommonProps extends Omit { interface Props extends CommonProps { // itemDetail: StockInLine & { qcResult?: PurchaseQcResult[] } & { escResult?: EscalationResult[] }; } -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ const PoQcStockInModalVer2: React.FC = ({ open, onClose, diff --git a/src/components/PoDetail/StockInLineRowActions.tsx b/src/components/PoDetail/StockInLineRowActions.tsx index 5d9d32e8..c1431741 100644 --- a/src/components/PoDetail/StockInLineRowActions.tsx +++ b/src/components/PoDetail/StockInLineRowActions.tsx @@ -26,7 +26,7 @@ type Props = { onRound?: (mode: StockQtyRoundMode) => void; }; -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ export default function StockInLineRowActions({ btnSx, onPrimaryClick, diff --git a/src/components/PoDetail/stockQtyRound.ts b/src/components/PoDetail/stockQtyRound.ts index 148193c0..f3dcab3a 100644 --- a/src/components/PoDetail/stockQtyRound.ts +++ b/src/components/PoDetail/stockQtyRound.ts @@ -7,7 +7,7 @@ export type StockQtyRoundChoice = { after: number; }; -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ export function previewPoBatchStockQty( orderM18Qty: number, orderStockQty: number, @@ -25,7 +25,7 @@ export function isNotIntegerQty(qty: number): boolean { } /** - * FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 + * FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 * PO 收貨/品檢:pending 或 escalated 且庫存數量非整數時需選擇 CEILING / FLOOR */ export function needsPoQcStockQtyRound( @@ -39,7 +39,7 @@ export function needsPoQcStockQtyRound( return isNotIntegerQty(Number(acceptedQty ?? 0)); } -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ export function roundStockQty(before: number, mode: StockQtyRoundMode): number { if (mode === "CEILING") return Math.ceil(before); return Math.floor(before); diff --git a/src/components/Qc/QcStockInModal.tsx b/src/components/Qc/QcStockInModal.tsx index e74e1cb2..c6b2b53d 100644 --- a/src/components/Qc/QcStockInModal.tsx +++ b/src/components/Qc/QcStockInModal.tsx @@ -73,7 +73,7 @@ interface Props extends CommonProps { // itemDetail: StockInLine & { qcResult?: PurchaseQcResult[] } & { escResult?: EscalationResult[] }; } -/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.3 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 56 | v1.0.4 | 2026-09-10 */ const QcStockInModal: React.FC = ({ open, onClose, From 218b00c78cbb6331c94adc2eefec59945194f0b8 Mon Sep 17 00:00:00 2001 From: Harry Groves Date: Thu, 10 Sep 2026 18:37:42 +0800 Subject: [PATCH 3/3] Add inventory Location Search tab; async item-code typeahead on reports; require stock-balance date with inline errors --- .../report/AsyncItemCodeAutocomplete.tsx | 223 +++++++++++++++++ .../report/ReportSelectionDashboard.tsx | 2 +- src/app/(main)/report/itemCodeSearchApi.ts | 86 +++++++ src/app/(main)/report/page.tsx | 155 ++++++++---- src/app/(main)/report/reportCategories.ts | 2 +- src/app/(main)/report/reportI18n.ts | 2 +- src/app/api/inventory/actions.ts | 12 +- src/app/api/inventory/index.ts | 1 + .../InventorySearch/InventoryLotLineTable.tsx | 14 +- .../InventorySearch/InventorySearch.tsx | 153 +++++++++--- .../InventorySearch/InventorySearchPage.tsx | 54 +++++ .../InventorySearchWrapper.tsx | 15 +- .../InventorySearch/LocationFilterBar.tsx | 229 ++++++++++++++++++ src/components/SearchBox/SearchBox.tsx | 5 + src/config/reportConfig.ts | 46 ++-- src/i18n/en/common.json | 8 +- src/i18n/en/inventory.json | 9 + src/i18n/en/report.json | 4 + src/i18n/zh/inventory.json | 9 + src/i18n/zh/report.json | 4 + 20 files changed, 929 insertions(+), 104 deletions(-) create mode 100644 src/app/(main)/report/AsyncItemCodeAutocomplete.tsx create mode 100644 src/app/(main)/report/itemCodeSearchApi.ts create mode 100644 src/components/InventorySearch/InventorySearchPage.tsx create mode 100644 src/components/InventorySearch/LocationFilterBar.tsx diff --git a/src/app/(main)/report/AsyncItemCodeAutocomplete.tsx b/src/app/(main)/report/AsyncItemCodeAutocomplete.tsx new file mode 100644 index 00000000..3e1681a1 --- /dev/null +++ b/src/app/(main)/report/AsyncItemCodeAutocomplete.tsx @@ -0,0 +1,223 @@ +"use client"; + +import { useEffect, useMemo, useState } from "react"; +import { Autocomplete, Chip, CircularProgress, TextField } from "@mui/material"; +import { useTranslation } from "react-i18next"; +import { searchItemCodes, type ItemCodeSearchHit } from "./itemCodeSearchApi"; + +type Props = { + label: string; + value: string[]; + onChange: (codes: string[]) => void; + placeholder?: string; + disabled?: boolean; + minChars?: number; +}; + +const hitLabel = (hit: ItemCodeSearchHit) => + hit.name ? `${hit.code} ${hit.name}` : hit.code; + +/** FP-MTMS Version Checklist | Functions Ref. No. 81 | v1.0.0 | 2026-09-10 */ +const AsyncItemCodeAutocomplete: React.FC = ({ + label, + value, + onChange, + placeholder, + disabled = false, + minChars = 2, +}) => { + const { t } = useTranslation("report"); + const [inputValue, setInputValue] = useState(""); + const [suggestions, setSuggestions] = useState([]); + const [labelByCode, setLabelByCode] = useState>({}); + const [isSearching, setIsSearching] = useState(false); + + const trimmedInput = inputValue.trim(); + const needsMoreChars = trimmedInput.length > 0 && trimmedInput.length < minChars; + + useEffect(() => { + if (trimmedInput.length < minChars) { + setSuggestions([]); + setIsSearching(false); + return; + } + + const controller = new AbortController(); + let cancelled = false; + const timer = window.setTimeout(async () => { + setIsSearching(true); + try { + const hits = await searchItemCodes(trimmedInput, controller.signal); + if (cancelled) return; + setSuggestions(hits); + setLabelByCode((prev) => { + const next = { ...prev }; + hits.forEach((hit) => { + next[hit.code] = hitLabel(hit); + }); + return next; + }); + } catch (error) { + if (cancelled) return; + if (error instanceof DOMException && error.name === "AbortError") return; + setSuggestions([]); + } finally { + if (!cancelled) setIsSearching(false); + } + }, 300); + + return () => { + cancelled = true; + window.clearTimeout(timer); + controller.abort(); + }; + }, [trimmedInput, minChars]); + + const options = useMemo(() => { + const seen = new Set(); + const codes: string[] = []; + suggestions.forEach((hit) => { + if (seen.has(hit.code)) return; + seen.add(hit.code); + codes.push(hit.code); + }); + value.forEach((code) => { + if (seen.has(code)) return; + seen.add(code); + codes.push(code); + }); + return codes; + }, [suggestions, value]); + + const noOptionsText = needsMoreChars + ? t("typeToSearchItemCode", { min: minChars }) + : isSearching + ? t("searchingItemCodes") + : trimmedInput.length < minChars + ? t("typeToSearchItemCode", { min: minChars }) + : t("noItemCodeMatches"); + + const hasSelection = value.length > 0; + + return ( + + trimmedInput.length < minChars + ? [] + : opts.filter((code) => !value.includes(code)) + } + isOptionEqualToValue={(option, selected) => option === selected} + autoHighlight + noOptionsText={noOptionsText} + sx={{ + '& .MuiOutlinedInput-root.MuiAutocomplete-inputRoot': hasSelection + ? { + alignItems: 'flex-start', + alignContent: 'flex-start', + flexWrap: 'wrap', + minHeight: 56, + paddingTop: '32px !important', + paddingBottom: '8px !important', + paddingLeft: '14px !important', + } + : { + alignItems: 'center', + height: 56, + minHeight: 56, + maxHeight: 56, + boxSizing: 'border-box', + paddingTop: '16.5px !important', + paddingBottom: '16.5px !important', + paddingLeft: '14px !important', + }, + '& .MuiOutlinedInput-root.MuiAutocomplete-inputRoot .MuiAutocomplete-input': { + fontSize: '1rem', + padding: '0 !important', + }, + '& .MuiOutlinedInput-root.MuiAutocomplete-inputRoot .MuiAutocomplete-input::placeholder': { + color: 'text.disabled', + opacity: 1, + }, + '& .MuiAutocomplete-tag': { + flex: '1 0 100%', + maxWidth: '100%', + width: '100%', + margin: '6px 0 4px', + }, + }} + componentsProps={{ + popper: { + placement: "top-start", + modifiers: [{ name: "flip", enabled: false }], + }, + }} + onInputChange={(_, next, reason) => { + if (reason === "reset") { + setInputValue(""); + return; + } + setInputValue(next); + }} + onChange={(_, newValue) => { + const codes = (Array.isArray(newValue) ? newValue : []) + .map((item) => (typeof item === "string" ? item.trim() : String(item).trim())) + .filter(Boolean); + onChange(Array.from(new Set(codes))); + setInputValue(""); + }} + getOptionLabel={(option) => labelByCode[option] || option} + renderTags={(selected, getTagProps) => + selected.map((option, index) => ( + + )) + } + renderInput={(params) => ( + + {isSearching ? : null} + {params.InputProps.endAdornment} + + ), + }} + /> + )} + /> + ); +}; + +export default AsyncItemCodeAutocomplete; diff --git a/src/app/(main)/report/ReportSelectionDashboard.tsx b/src/app/(main)/report/ReportSelectionDashboard.tsx index 50f5331a..b5eb3802 100644 --- a/src/app/(main)/report/ReportSelectionDashboard.tsx +++ b/src/app/(main)/report/ReportSelectionDashboard.tsx @@ -171,7 +171,7 @@ function CategoryColumn({ ); } -/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.1 | 2026-08-31 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.2 | 2026-09-10 */ export default function ReportSelectionDashboard({ selectedReportId, onSelectReport, diff --git a/src/app/(main)/report/itemCodeSearchApi.ts b/src/app/(main)/report/itemCodeSearchApi.ts new file mode 100644 index 00000000..34e7f0ca --- /dev/null +++ b/src/app/(main)/report/itemCodeSearchApi.ts @@ -0,0 +1,86 @@ +"use client"; + +import { NEXT_PUBLIC_API_URL } from "@/config/api"; +import { clientAuthFetch } from "@/app/utils/clientAuthFetch"; + +export type ItemCodeSearchHit = { + code: string; + name: string; +}; + +const PAGE_SIZE = 50; + +const extractRecords = (data: unknown): Array<{ code?: string; name?: string }> => { + if (!data) return []; + if (Array.isArray(data)) return data; + if (typeof data === "object" && Array.isArray((data as { records?: unknown }).records)) { + return (data as { records: Array<{ code?: string; name?: string }> }).records; + } + return []; +}; + +const toHits = (data: unknown): ItemCodeSearchHit[] => { + const seen = new Set(); + const hits: ItemCodeSearchHit[] = []; + for (const item of extractRecords(data)) { + const code = (item.code || "").trim(); + if (!code || seen.has(code)) continue; + seen.add(code); + hits.push({ code, name: (item.name || "").trim() }); + } + return hits; +}; + +const fetchItemPage = async ( + field: "code" | "name", + query: string, + signal?: AbortSignal, +): Promise => { + const params = new URLSearchParams({ + [field]: query, + pageSize: String(PAGE_SIZE), + pageNum: "1", + }); + + const response = await clientAuthFetch( + `${NEXT_PUBLIC_API_URL}/items/getRecordByPage?${params.toString()}`, + { + method: "GET", + headers: { "Content-Type": "application/json" }, + signal, + }, + ); + + if (response.status === 401 || response.status === 403) return []; + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + + return toHits(await response.json()); +}; + +/** + * FP-MTMS Version Checklist | Functions Ref. No. 81 | v1.0.0 | 2026-09-10 + * Typeahead lookup for report item-code multi-select. + * Uses the existing paged item API so we never load the full catalog. + */ +export const searchItemCodes = async ( + query: string, + signal?: AbortSignal, +): Promise => { + const q = query.trim(); + if (!q) return []; + + const [byCode, byName] = await Promise.all([ + fetchItemPage("code", q, signal), + fetchItemPage("name", q, signal), + ]); + + const seen = new Set(); + const merged: ItemCodeSearchHit[] = []; + for (const hit of [...byCode, ...byName]) { + if (seen.has(hit.code)) continue; + seen.add(hit.code); + merged.push(hit); + if (merged.length >= PAGE_SIZE) break; + } + return merged; +}; diff --git a/src/app/(main)/report/page.tsx b/src/app/(main)/report/page.tsx index 639f62d0..6dec1c10 100644 --- a/src/app/(main)/report/page.tsx +++ b/src/app/(main)/report/page.tsx @@ -29,6 +29,7 @@ import { REPORTS } from '@/config/reportConfig'; import { NEXT_PUBLIC_API_URL } from '@/config/api'; import { clientAuthFetch } from '@/app/utils/clientAuthFetch'; import SemiFGProductionAnalysisReport from './SemiFGProductionAnalysisReport'; +import AsyncItemCodeAutocomplete from './AsyncItemCodeAutocomplete'; import ReportSelectionDashboard from './ReportSelectionDashboard'; import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers'; import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; @@ -48,15 +49,35 @@ import { FEATURE_USAGE_ACTION, logFeatureUsage, } from '@/lib/featureUsageLog'; +import { error as errorColor } from '@/theme/devias-material-kit/colors'; interface ItemCodeWithName { code: string; name: string; } -/** FP-MTMS Version Checklist | Functions Ref. No. 39 | v1.0.0 | 2026-08-03 */ +const FIELD_ERROR_SX = { + '& .MuiOutlinedInput-root.Mui-error': { + '& .MuiOutlinedInput-notchedOutline': { + borderColor: 'error.dark', + boxShadow: `0 0 0 2px ${errorColor.dark}40`, + }, + }, + '& .MuiInputLabel-root.Mui-error': { + color: 'error.dark', + }, + '& .MuiFormHelperText-root.Mui-error': { + color: 'error.dark', + }, + '& .MuiInputLabel-asterisk': { + color: 'error.dark', + }, +}; + +/** FP-MTMS Version Checklist | Functions Ref. No. 39 | v1.0.3 | 2026-09-10 */ /** FP-MTMS Version Checklist | Functions Ref. No. 60 | v1.0.1 | 2026-08-11 */ -/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.1 | 2026-08-31 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.2 | 2026-09-10 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 82 | v1.0.0 | 2026-09-10 */ export default function ReportPage() { const { data: session } = useSession() as { data: SessionWithTokens | null }; const { t, i18n, reportTitle, fieldLabel, optionLabel } = useReportLabels(); @@ -72,6 +93,7 @@ export default function ReportPage() { const [dynamicOptions, setDynamicOptions] = useState>({}); const [showConfirmDialog, setShowConfirmDialog] = useState(false); const [showNoDataDialog, setShowNoDataDialog] = useState(false); + const [fieldErrors, setFieldErrors] = useState>({}); // Find the configuration for the currently selected report const rep012RoundIds = useMemo(() => { @@ -91,6 +113,7 @@ export default function ReportPage() { const handleSelectReport = (reportId: string) => { if (reportId === selectedReportId) return; setSelectedReportId(reportId); + setFieldErrors({}); if (reportId === 'rep-010') { setCriteria({ qcType: 'all', qcItemScope: 'all' }); } else if (reportId === 'rep-004') { @@ -104,6 +127,12 @@ export default function ReportPage() { const handleFieldChange = (name: string, value: string | string[]) => { const stringValue = Array.isArray(value) ? value.join(',') : value; + setFieldErrors((prev) => { + if (!prev[name]) return prev; + const next = { ...prev }; + delete next[name]; + return next; + }); setCriteria((prev) => { const next = { ...prev, [name]: stringValue }; if (currentReport?.id === 'rep-021' && name === 'warehouse') { @@ -246,27 +275,29 @@ export default function ReportPage() { if (currentReport.id === 'rep-012') { if (rep012RoundIds.length === 0) { - alert(t('missingRequired', { - fields: fieldLabel('rep-012', { name: 'stockTakeRoundId', label: '盤點輪次' }), - })); + setFieldErrors({ stockTakeRoundId: t('requiredField') }); return false; } + setFieldErrors({}); return true; } - // Mandatory Field Validation - const missingFields = currentReport.fields - .filter((field) => { - if (!field.required) return false; - return !criteria[field.name]; - }) - .map((field) => fieldLabel(currentReport.id, field)); + const missingFields = currentReport.fields.filter((field) => { + if (!field.required) return false; + return !criteria[field.name]; + }); if (missingFields.length > 0) { - alert(t('missingRequired', { fields: missingFields.join('\n- ') })); + const nextErrors: Record = {}; + missingFields.forEach((field) => { + nextErrors[field.name] = t('requiredField'); + }); + setFieldErrors(nextErrors); return false; } + setFieldErrors({}); + // Date fields with minDate: 'today' must not be before local today const today = new Date(); const todayStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`; @@ -528,6 +559,7 @@ export default function ReportPage() { > {currentReport.fields.map((field) => { + const fieldKey = `${currentReport.id}-${field.name}`; const translatedLabel = fieldLabel(currentReport.id, field); const rawOptions = field.dynamicOptions ? (dynamicOptions[field.name] || field.options || []) @@ -555,8 +587,9 @@ export default function ReportPage() { if (field.type === 'date') { const parsed = currentValue ? dayjs(currentValue) : null; + const dateError = fieldErrors[field.name]; return ( - + @@ -590,7 +629,7 @@ export default function ReportPage() { if (field.type === 'checkbox') { return ( - + + handleFieldChange(field.name, codes)} + minChars={field.asyncSearchMinChars ?? 2} + disabled={disabledByCheckedCheckbox || disabledRep012Status || !!field.disabled} + /> + + ); + } + // Use Autocomplete for fields that allow input if (field.type === 'select' && field.allowInput) { const autocompleteValue = field.multiple @@ -613,7 +668,7 @@ export default function ReportPage() { : (valueForSelect || null); return ( - + )} renderTags={(value, getTagProps) => @@ -696,22 +757,28 @@ export default function ReportPage() { // Regular TextField for other fields return ( - + { if (field.multiple) { const value = typeof e.target.value === 'string' diff --git a/src/app/(main)/report/reportCategories.ts b/src/app/(main)/report/reportCategories.ts index 543e6186..2b2f7aa7 100644 --- a/src/app/(main)/report/reportCategories.ts +++ b/src/app/(main)/report/reportCategories.ts @@ -9,7 +9,7 @@ export interface ReportCategoryConfig { reportIds: string[]; } -/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.1 | 2026-08-31 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.2 | 2026-09-10 */ export const REPORT_CATEGORIES: ReportCategoryConfig[] = [ { id: "inventory", diff --git a/src/app/(main)/report/reportI18n.ts b/src/app/(main)/report/reportI18n.ts index 86daf4e0..7e8be629 100644 --- a/src/app/(main)/report/reportI18n.ts +++ b/src/app/(main)/report/reportI18n.ts @@ -4,7 +4,7 @@ import { useTranslation } from "react-i18next"; import type { TFunction } from "i18next"; import type { ReportDefinition, ReportField } from "@/config/reportConfig"; -/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.1 | 2026-08-31 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.2 | 2026-09-10 */ export function useReportLabels() { const { t, i18n } = useTranslation("report"); diff --git a/src/app/api/inventory/actions.ts b/src/app/api/inventory/actions.ts index fb7d8643..3330a8f0 100644 --- a/src/app/api/inventory/actions.ts +++ b/src/app/api/inventory/actions.ts @@ -28,8 +28,12 @@ export interface LotLineInfo { export interface SearchInventoryLotLine extends Pageable { itemId: number; + uomId?: number; /** Non-expired lots with in > out; includes available and unavailable. */ stockIssueBadItem?: boolean; + storeId?: string; + warehouse?: string; + area?: string; } export interface SearchStockIssueBadItemLotLine extends Pageable { @@ -44,6 +48,9 @@ export interface SearchInventory extends Pageable { name: string; type: string; lotNo?: string; + storeId?: string; + warehouse?: string; + area?: string; } export interface InventoryResultByPage { @@ -172,8 +179,8 @@ async function fetchInventoriesLatestImpl(data: SearchInventory) { export const fetchInventories = cache(fetchInventoriesImpl); /** - * FP-MTMS Version Checklist | Functions Ref. No. 18 | v1.0.3 | 2026-09-07 - * Inventory search page: latest inventory row per item (no baseUnit/uomId filter). + * FP-MTMS Version Checklist | Functions Ref. No. 18 | v1.0.4 | 2026-09-10 + * Inventory search page: latest inventory row per item + stock UoM, with optional location filters. */ export const fetchInventoriesLatest = cache(fetchInventoriesLatestImpl); @@ -195,6 +202,7 @@ async function fetchInventoryLotLinesImpl(data: SearchInventoryLotLine) { ); } +/** FP-MTMS Version Checklist | Functions Ref. No. 18 | v1.0.4 | 2026-09-10 */ export const fetchInventoryLotLines = cache(fetchInventoryLotLinesImpl); /** Bypass React cache() after mutations so lists show fresh qty. */ diff --git a/src/app/api/inventory/index.ts b/src/app/api/inventory/index.ts index 869bed2e..711de65e 100644 --- a/src/app/api/inventory/index.ts +++ b/src/app/api/inventory/index.ts @@ -14,6 +14,7 @@ export interface InventoryResult { onHoldQty: number; unavailableQty: number; availableQty: number; + uomId?: number; uomCode: string; uomUdfudesc: string; uomShortDesc: string; diff --git a/src/components/InventorySearch/InventoryLotLineTable.tsx b/src/components/InventorySearch/InventoryLotLineTable.tsx index 9e3a041f..237112dc 100644 --- a/src/components/InventorySearch/InventoryLotLineTable.tsx +++ b/src/components/InventorySearch/InventoryLotLineTable.tsx @@ -54,15 +54,18 @@ interface Props { totalCount: number; inventory: InventoryResult | null; filterLotNo?: string; + /** Location search: show only the slot (e.g. 00), not the full warehouse code. */ + warehouseDisplay?: "full" | "slot"; onStockTransferSuccess?: () => void | Promise; printerCombo?: PrinterCombo[]; onStockAdjustmentSuccess?: () => void | Promise; } -/** FP-MTMS Version Checklist | Functions Ref. No. 3 | v1.0.7 | 2026-09-08 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 3 | v1.0.8 | 2026-09-10 */ const InventoryLotLineTable: React.FC = ({ inventoryLotLines, pagingController, setPagingController, totalCount, inventory, filterLotNo, + warehouseDisplay = "full", onStockTransferSuccess, printerCombo = [], onStockAdjustmentSuccess, }) => { @@ -479,9 +482,12 @@ const prevAdjustmentModalOpenRef = useRef(false); }, { name: "warehouse", - label: t("Warehouse"), + label: warehouseDisplay === "slot" ? t("Slot") : t("Warehouse"), renderCell: (params) => { - return `${params.warehouse.code}` + const code = params.warehouse?.code ?? ""; + if (warehouseDisplay !== "slot") return code; + const parts = code.split("-").filter(Boolean); + return parts[parts.length - 1] || code; }, }, { @@ -522,7 +528,7 @@ const prevAdjustmentModalOpenRef = useRef(false); // } // }, ], - [t, onDetailClick, downloadQrCode, handleStockTransfer, handlePrintClick], + [t, onDetailClick, downloadQrCode, handleStockTransfer, handlePrintClick, warehouseDisplay], ); diff --git a/src/components/InventorySearch/InventorySearch.tsx b/src/components/InventorySearch/InventorySearch.tsx index a59ff224..2f85f678 100644 --- a/src/components/InventorySearch/InventorySearch.tsx +++ b/src/components/InventorySearch/InventorySearch.tsx @@ -20,10 +20,19 @@ import { fetchItemsByPage } from '@/app/api/settings/item/actions'; import { useSession } from 'next-auth/react'; import { AUTH, hasAbility } from '@/authorities'; import { Button, Box } from '@mui/material'; +import { WarehouseResult } from '@/app/api/warehouse'; +import { fetchWarehouseListClient } from '@/app/api/warehouse/client'; +import LocationFilterBar, { + emptyLocationFilter, + isLocationAll, + LocationFilterValue, +} from './LocationFilterBar'; interface Props { inventories: InventoryResult[]; printerCombo?: PrinterCombo[]; + warehouses?: WarehouseResult[]; + enableLocationFilter?: boolean; } type SearchQuery = Partial< @@ -62,8 +71,13 @@ const extractItemRecords = (res: unknown): ItemLookupRow[] => { return []; }; -/** FP-MTMS Version Checklist | Functions Ref. No. 18 | v1.0.3 | 2026-09-07 */ -const InventorySearch: React.FC = ({ inventories, printerCombo }) => { +/** FP-MTMS Version Checklist | Functions Ref. No. 18 | v1.0.4 | 2026-09-10 */ +const InventorySearch: React.FC = ({ + inventories, + printerCombo, + warehouses = [], + enableLocationFilter = false, +}) => { const { t } = useTranslation(['inventory', 'common', 'item']); const { data: session } = useSession(); const abilities = session?.abilities ?? session?.user?.abilities ?? []; @@ -139,6 +153,19 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { // Resolved lot no for filtering const [lotNoFilter, setLotNoFilter] = useState(''); const [scannedItemId, setScannedItemId] = useState(null); + const [location, setLocation] = useState(emptyLocationFilter); + const [locationWarehouses, setLocationWarehouses] = useState(warehouses); + + useEffect(() => { + if (!enableLocationFilter) return; + if (warehouses.length) { + setLocationWarehouses(warehouses); + return; + } + fetchWarehouseListClient() + .then(setLocationWarehouses) + .catch(console.error); + }, [enableLocationFilter, warehouses]); const defaultInputs = useMemo( () => ({ @@ -185,28 +212,43 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { ); // Inventory + const withLocationParams = useCallback( + (params: T, loc: LocationFilterValue): T & Partial => { + if (!enableLocationFilter) return params; + return { + ...params, + ...(!isLocationAll(loc.storeId) ? { storeId: loc.storeId } : {}), + ...(!isLocationAll(loc.warehouse) ? { warehouse: loc.warehouse } : {}), + ...(!isLocationAll(loc.area) ? { area: loc.area } : {}), + }; + }, + [enableLocationFilter], + ); + const refetchInventoryData = useCallback( async ( query: Record, actionType: 'reset' | 'search' | 'paging' | 'init', pagingController: typeof defaultPagingController, lotNo: string, + loc: LocationFilterValue = location, ) => { - //console.log('%c Action Type 1.', 'color:red', actionType); // Avoid loading data again if (actionType === 'paging' && pagingController === defaultPagingController) { return; } - // console.log('%c Action Type 2.', 'color:blue', actionType); - - const params: SearchInventory = { - code: query?.itemCode ?? '', - name: query?.itemName ?? '', - type: query?.itemType.toLowerCase() === 'all' ? '' : query?.itemType ?? '', - lotNo: lotNo?.trim() ? lotNo.trim() : undefined, - pageNum: pagingController.pageNum - 1, - pageSize: pagingController.pageSize, - }; + + const params: SearchInventory = withLocationParams( + { + code: query?.itemCode ?? '', + name: query?.itemName ?? '', + type: query?.itemType.toLowerCase() === 'all' ? '' : query?.itemType ?? '', + lotNo: lotNo?.trim() ? lotNo.trim() : undefined, + pageNum: pagingController.pageNum - 1, + pageSize: pagingController.pageSize, + }, + loc, + ); const response = await fetchInventoriesLatest(params); @@ -220,19 +262,21 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { break; case 'paging': setFilteredInventories((fi) => - uniqBy([...fi, ...response.records], 'itemId'), + uniqBy([...fi, ...response.records], (row) => `${row.itemId}-${row.uomId ?? row.uomUdfudesc ?? ''}`), ); } } return response; }, - [], + [enableLocationFilter, location, withLocationParams], ); useEffect(() => { refetchInventoryData(defaultInputs, 'init', defaultPagingController, ''); - }, [defaultInputs, refetchInventoryData]); + // Mount / tab open only. Location changes search via handleLocationChange. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [defaultInputs]); useEffect(() => { // if (!isEqual(inventoriesPagingController, defaultPagingController)) { @@ -246,6 +290,8 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { itemId: number | null, actionType: 'reset' | 'search' | 'paging', pagingController: typeof defaultPagingController, + loc: LocationFilterValue = location, + uomId?: number, ) => { if (!itemId) { setSelectedInventory(null); @@ -259,11 +305,15 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { return; } - const params: SearchInventoryLotLine = { - itemId, - pageNum: pagingController.pageNum - 1, - pageSize: pagingController.pageSize, - }; + const params: SearchInventoryLotLine = withLocationParams( + { + itemId, + uomId: uomId || undefined, + pageNum: pagingController.pageNum - 1, + pageSize: pagingController.pageSize, + }, + loc, + ); const response = await fetchInventoryLotLines(params); if (response) { @@ -278,20 +328,27 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { } } }, - [], + [location, withLocationParams], ); useEffect(() => { // if (!isEqual(inventoryLotLinesPagingController, defaultPagingController)) { - refetchInventoryLotLineData(selectedInventory?.itemId ?? null, 'paging', inventoryLotLinesPagingController) + refetchInventoryLotLineData( + selectedInventory?.itemId ?? null, + 'paging', + inventoryLotLinesPagingController, + location, + selectedInventory?.uomId, + ) // } }, [inventoryLotLinesPagingController]) // Reset const onReset = useCallback(() => { - refetchInventoryData(defaultInputs, 'reset', defaultPagingController, ''); - refetchInventoryLotLineData(null, 'reset', defaultPagingController); - // setFilteredInventories(inventories); + const clearedLocation = emptyLocationFilter(); + setLocation(clearedLocation); + refetchInventoryData(defaultInputs, 'reset', defaultPagingController, '', clearedLocation); + refetchInventoryLotLineData(null, 'reset', defaultPagingController, clearedLocation); setLotNoFilter(''); setScannedItemId(null); @@ -304,14 +361,32 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { setInventoryLotLinesPagingController(() => defaultPagingController) }, [defaultInputs, qrScanner, refetchInventoryData, refetchInventoryLotLineData]); + const handleLocationChange = useCallback( + (next: LocationFilterValue) => { + setLocation(next); + setSelectedInventory(null); + setFilteredInventoryLotLines([]); + setInventoryLotLinesTotalCount(0); + setInventoryLotLinesPagingController(() => defaultPagingController); + setInventoriesPagingController(() => defaultPagingController); + if (searchInFlightRef.current) return; + searchInFlightRef.current = true; + refetchInventoryData(inputs, 'search', defaultPagingController, lotNoFilter, next) + .finally(() => { + searchInFlightRef.current = false; + }); + }, + [inputs, lotNoFilter, refetchInventoryData], + ); + // Click Row const onInventoryRowClick = useCallback( (item: InventoryResult) => { - refetchInventoryLotLineData(item.itemId, 'search', defaultPagingController); + refetchInventoryLotLineData(item.itemId, 'search', defaultPagingController, location, item.uomId); setSelectedInventory(item); setInventoryLotLinesPagingController(() => defaultPagingController); }, - [refetchInventoryLotLineData], + [location, refetchInventoryLotLineData], ); // On Search @@ -334,7 +409,7 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { setInventoryLotLinesPagingController(() => defaultPagingController); // No inventory rows: look up item master so 0-qty rows can be selected for stock adjustment. - if (canStockAdjust && invRes?.records?.length === 0) { + if (canStockAdjust && !enableLocationFilter && invRes?.records?.length === 0) { try { const items = await lookupItemsByCodeOrName(query.itemCode, query.itemName); const typeFilter = query.itemType?.trim(); @@ -366,6 +441,7 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { lookupItemsByCodeOrName, applyItemsAsSyntheticInventories, canStockAdjust, + enableLocationFilter, ], ); @@ -451,6 +527,15 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { onSearch(query); }} onReset={onReset} + extraCriteria={ + enableLocationFilter ? ( + + ) : undefined + } extraActions={ {scanUiMode === 'idle' ? ( @@ -484,16 +569,20 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { totalCount={inventoryLotLinesTotalCount} inventory={selectedInventory} filterLotNo={lotNoFilter} + warehouseDisplay={enableLocationFilter ? "slot" : "full"} printerCombo={printerCombo ?? []} onStockTransferSuccess={() => refetchInventoryLotLineData( selectedInventory?.itemId ?? null, 'search', inventoryLotLinesPagingController, + location, + selectedInventory?.uomId, ) } onStockAdjustmentSuccess={async () => { const itemId = selectedInventory?.itemId ?? null; + const uomId = selectedInventory?.uomId; // Refresh both blocks: // - middle: InventoryTable (inventories list) @@ -509,11 +598,15 @@ const InventorySearch: React.FC = ({ inventories, printerCombo }) => { itemId, 'search', inventoryLotLinesPagingController, + location, + uomId, ); // If inventory becomes available again after OPEN/ADJ, sync selected row. if (itemId != null && invRes?.records?.length) { - const target = invRes.records.find((r) => r.itemId === itemId); + const target = invRes.records.find( + (r) => r.itemId === itemId && (uomId == null || r.uomId === uomId), + ); if (target) setSelectedInventory(target); } }} diff --git a/src/components/InventorySearch/InventorySearchPage.tsx b/src/components/InventorySearch/InventorySearchPage.tsx new file mode 100644 index 00000000..b494bde6 --- /dev/null +++ b/src/components/InventorySearch/InventorySearchPage.tsx @@ -0,0 +1,54 @@ +"use client"; + +import { InventoryResult } from "@/app/api/inventory"; +import { PrinterCombo } from "@/app/api/settings/printer"; +import { WarehouseResult } from "@/app/api/warehouse"; +import { Box, Tab, Tabs } from "@mui/material"; +import { useCallback, useState } from "react"; +import { useTranslation } from "react-i18next"; +import InventorySearch from "./InventorySearch"; + +type TabValue = "item" | "location"; + +interface Props { + inventories: InventoryResult[]; + printerCombo?: PrinterCombo[]; + warehouses?: WarehouseResult[]; +} + +/** FP-MTMS Version Checklist | Functions Ref. No. 79 | v1.0.0 | 2026-09-10 */ +const InventorySearchPage: React.FC = ({ + inventories, + printerCombo, + warehouses = [], +}) => { + const { t } = useTranslation("inventory"); + const [tab, setTab] = useState("item"); + + const handleTabChange = useCallback((_: React.SyntheticEvent, value: string) => { + setTab(value as TabValue); + }, []); + + return ( + + + + + + + {tab === "item" && ( + + )} + {tab === "location" && ( + + )} + + ); +}; + +export default InventorySearchPage; diff --git a/src/components/InventorySearch/InventorySearchWrapper.tsx b/src/components/InventorySearch/InventorySearchWrapper.tsx index cffbfc52..6b9227bd 100644 --- a/src/components/InventorySearch/InventorySearchWrapper.tsx +++ b/src/components/InventorySearch/InventorySearchWrapper.tsx @@ -1,20 +1,29 @@ import React from "react"; import GeneralLoading from "../General/GeneralLoading"; import { fetchInventories } from "@/app/api/inventory"; -import InventorySearch from "./InventorySearch"; +import InventorySearchPage from "./InventorySearchPage"; import { fetchPrinterCombo } from "@/app/api/settings/printer"; +import { fetchWarehouseList } from "@/app/api/warehouse"; interface SubComponents { Loading: typeof GeneralLoading; } +/** FP-MTMS Version Checklist | Functions Ref. No. 79 | v1.0.0 | 2026-09-10 */ const InventorySearchWrapper: React.FC & SubComponents = async () => { - const [inventories, printerCombo] = await Promise.all([ + const [inventories, printerCombo, warehouses] = await Promise.all([ fetchInventories(), fetchPrinterCombo(), + fetchWarehouseList().catch(() => []), ]); - return ; + return ( + + ); }; InventorySearchWrapper.Loading = GeneralLoading; diff --git a/src/components/InventorySearch/LocationFilterBar.tsx b/src/components/InventorySearch/LocationFilterBar.tsx new file mode 100644 index 00000000..469b6b17 --- /dev/null +++ b/src/components/InventorySearch/LocationFilterBar.tsx @@ -0,0 +1,229 @@ +"use client"; + +import { WarehouseResult } from "@/app/api/warehouse"; +import { Box, ToggleButton, ToggleButtonGroup, Typography } from "@mui/material"; +import { useMemo } from "react"; +import { useTranslation } from "react-i18next"; + +export const LOCATION_ALL = "ALL"; +const BUTTONS_PER_ROW = 15; +const BUTTON_WIDTH = 80; + +export type LocationFilterValue = { + storeId: string; + warehouse: string; + area: string; +}; + +const EMPTY_LOCATION: LocationFilterValue = { + storeId: "", + warehouse: "", + area: "", +}; + +export const emptyLocationFilter = (): LocationFilterValue => ({ ...EMPTY_LOCATION }); + +export const isLocationAll = (value?: string) => !value || value === LOCATION_ALL; + +const warehouseSegments = (w: WarehouseResult) => { + const parts = (w.code || "").split("-"); + return { + storeId: w.store_id?.trim() || parts[0] || "", + warehouse: w.warehouse?.trim() || parts[1] || "", + area: w.area?.trim() || parts[2] || "", + }; +}; + +const compareAlphanumeric = (a: string, b: string) => + a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }); + +const chunk = (items: T[], size: number): T[][] => { + const rows: T[][] = []; + for (let i = 0; i < items.length; i += size) { + rows.push(items.slice(i, i + size)); + } + return rows; +}; + +const withAllOption = (options: string[]) => + options.length > 1 ? [LOCATION_ALL, ...options] : options; + +interface Props { + warehouses: WarehouseResult[]; + value: LocationFilterValue; + onChange: (next: LocationFilterValue) => void; +} + +/** FP-MTMS Version Checklist | Functions Ref. No. 79 | v1.0.0 | 2026-09-10 */ +const LocationFilterBar: React.FC = ({ warehouses, value, onChange }) => { + const { t } = useTranslation("inventory"); + + const floors = useMemo(() => { + const set = new Set(); + warehouses.forEach((w) => { + const storeId = warehouseSegments(w).storeId; + if (storeId) set.add(storeId); + }); + return Array.from(set).sort(compareAlphanumeric); + }, [warehouses]); + + const warehouseEnabled = Boolean(value.storeId); + const areaEnabled = Boolean(value.storeId && value.warehouse); + + const warehouseZones = useMemo(() => { + if (!value.storeId) return []; + const set = new Set(); + warehouses.forEach((w) => { + const seg = warehouseSegments(w); + if (!isLocationAll(value.storeId) && seg.storeId !== value.storeId) return; + if (seg.warehouse) set.add(seg.warehouse); + }); + return Array.from(set).sort(compareAlphanumeric); + }, [warehouses, value.storeId]); + + const areas = useMemo(() => { + if (!value.storeId || !value.warehouse) return []; + const set = new Set(); + warehouses.forEach((w) => { + const seg = warehouseSegments(w); + if (!isLocationAll(value.storeId) && seg.storeId !== value.storeId) return; + if (!isLocationAll(value.warehouse) && seg.warehouse !== value.warehouse) return; + if (seg.area) set.add(seg.area); + }); + return Array.from(set).sort(compareAlphanumeric); + }, [warehouses, value.storeId, value.warehouse]); + + const floorOptions = useMemo(() => withAllOption(floors), [floors]); + + const warehouseRows = useMemo( + () => chunk(withAllOption(warehouseZones), BUTTONS_PER_ROW), + [warehouseZones], + ); + + const areaRows = useMemo( + () => chunk(withAllOption(areas), BUTTONS_PER_ROW), + [areas], + ); + + return ( + + + + {t("Floor")} + + { + onChange({ storeId: next ?? "", warehouse: "", area: "" }); + }} + > + {floorOptions.map((floor) => ( + + {floor === LOCATION_ALL ? t("All") : floor} + + ))} + + + + + + {t("Warehouse")} + + + {warehouseRows.map((row, rowIndex) => ( + { + if (next == null) return; + onChange({ ...value, warehouse: next, area: "" }); + }} + sx={{ + "& .MuiToggleButtonGroup-grouped": { + width: BUTTON_WIDTH, + minWidth: BUTTON_WIDTH, + maxWidth: BUTTON_WIDTH, + px: 0, + boxSizing: "border-box", + }, + }} + > + {row.map((zone) => ( + + {zone === LOCATION_ALL ? t("All") : zone} + + ))} + + ))} + + {!warehouseEnabled && ( + + {t("Select floor first")} + + )} + + + + + {t("Area")} + + + {areaRows.map((row, rowIndex) => ( + { + if (next == null) return; + onChange({ ...value, area: next }); + }} + sx={{ + "& .MuiToggleButtonGroup-grouped": { + width: BUTTON_WIDTH, + minWidth: BUTTON_WIDTH, + maxWidth: BUTTON_WIDTH, + px: 0, + boxSizing: "border-box", + }, + }} + > + {row.map((area) => ( + + {area === LOCATION_ALL ? t("All") : area} + + ))} + + ))} + + {!areaEnabled && ( + + {value.storeId ? t("Select warehouse first") : t("Select floor first")} + + )} + + + ); +}; + +export default LocationFilterBar; diff --git a/src/components/SearchBox/SearchBox.tsx b/src/components/SearchBox/SearchBox.tsx index 45331726..5ecec020 100644 --- a/src/components/SearchBox/SearchBox.tsx +++ b/src/components/SearchBox/SearchBox.tsx @@ -124,15 +124,19 @@ interface Props { onReset?: () => void; /** Optional actions rendered in the same row as Reset/Search (e.g. Download, Upload buttons) */ extraActions?: React.ReactNode; + /** Optional filters rendered above the standard criteria fields */ + extraCriteria?: React.ReactNode; /** Disable inputs/actions while external task is running */ disabled?: boolean; } +/** FP-MTMS Version Checklist | Functions Ref. No. 79 | v1.0.0 | 2026-09-10 */ function SearchBox({ criteria, onSearch, onReset, extraActions, + extraCriteria, disabled = false, }: Props) { const { t } = useTranslation("common"); @@ -295,6 +299,7 @@ function SearchBox({ {t("Search Criteria")} + {extraCriteria} {criteria.map((c) => { return ( diff --git a/src/config/reportConfig.ts b/src/config/reportConfig.ts index fcf74b84..c6ed8b5b 100644 --- a/src/config/reportConfig.ts +++ b/src/config/reportConfig.ts @@ -15,6 +15,10 @@ export interface ReportField { dynamicOptionsEndpoint?: string; // API endpoint to fetch dynamic options dynamicOptionsParam?: string; // Parameter name to pass when fetching options allowInput?: boolean; // Allow user to input custom values (for select types) + /** Typeahead options instead of preloading the full list (use with allowInput) */ + asyncSearch?: boolean; + /** Minimum characters before asyncSearch fetches. Default 2. */ + asyncSearchMinChars?: number; /** When checkbox is checked, disable these field names (by `name`) */ disablesFieldsWhenChecked?: string[]; /** For date fields: restrict picker so value cannot be before today */ @@ -34,7 +38,21 @@ export interface ReportDefinition { fields: ReportField[]; } -/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.1 | 2026-08-31 */ +/** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.2 | 2026-09-10 */ +const asyncItemCodeField = ( + label = "貨品編號 Item Code", + name = "itemCode", +): ReportField => ({ + label, + name, + type: "select", + required: false, + multiple: true, + allowInput: true, + asyncSearch: true, + placeholder: "e.g. FA0591", +}); + export const REPORTS: ReportDefinition[] = [ //{ // id: "rep-001", @@ -86,7 +104,7 @@ export const REPORTS: ReportDefinition[] = [ fields: [ { label: "入倉日期:由 Last In Date Start", name: "lastInDateStart", type: "date", required: false }, { label: "入倉日期:至 Last In Date End", name: "lastInDateEnd", type: "date", required: false }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), { label: "樓層 Store ID", name: "storeId", @@ -125,7 +143,7 @@ export const REPORTS: ReportDefinition[] = [ { label: "出貨日期:由 Last Out Date Start", name: "lastOutDateStart", type: "date", required: false }, { label: "出貨日期:至 Last Out Date End", name: "lastOutDateEnd", type: "date", required: false }, { label: "年份 Year", name: "year", type: "text", required: false, placeholder: "e.g. 2026" }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), ] }, /* @@ -154,7 +172,7 @@ export const REPORTS: ReportDefinition[] = [ dynamicOptionsEndpoint: `${NEXT_PUBLIC_API_URL}/report/stock-take-rounds`, options: [] }, - { label: "貨品編號", name: "itemCode", type: "text", required: false}, + asyncItemCodeField("貨品編號"), { label: "倉庫樓層", name: "store_id", @@ -232,7 +250,7 @@ export const REPORTS: ReportDefinition[] = [ apiEndpoint: `${NEXT_PUBLIC_API_URL}/report/print-stock-lot-onhand`, responseType: "excel", fields: [ - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false }, + asyncItemCodeField(), { label: "樓層 Store ID", name: "storeId", @@ -279,7 +297,7 @@ export const REPORTS: ReportDefinition[] = [ fields: [ { label: "庫存日期:由 Last In Date Start", name: "lastInDateStart", type: "date", required: false }, { label: "庫存日期:至 Last In Date End", name: "lastInDateEnd", type: "date", required: false }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), ] }, /* Hidden for now: 庫存流水帳報告 (rep-020) @@ -314,14 +332,14 @@ export const REPORTS: ReportDefinition[] = [ ] }, */ + /** FP-MTMS Version Checklist | Functions Ref. No. 82 | v1.0.0 | 2026-09-10 */ { id: "rep-007", title: "庫存結餘報告", apiEndpoint: `${NEXT_PUBLIC_API_URL}/report/print-stock-balance`, fields: [ { label: "庫存日期: Stock Date", name: "stockDate", type: "date", required: true }, - - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), ] }, @@ -334,7 +352,7 @@ export const REPORTS: ReportDefinition[] = [ fields: [ { label: "收貨日期:由 Receipt Date Start", name: "receiptDateStart", type: "date", required: false }, { label: "收貨日期:至 Receipt Date End", name: "receiptDateEnd", type: "date", required: false }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false }, + asyncItemCodeField(), ], }, @@ -344,7 +362,7 @@ export const REPORTS: ReportDefinition[] = [ fields: [ { label: "出貨日期:由 Last Out Date Start", name: "lastOutDateStart", type: "date", required: false }, { label: "出貨日期:至 Last Out Date End", name: "lastOutDateEnd", type: "date", required: false }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), { label: "提料員 Handler", name: "handler", type: "select", required: false, multiple: true, dynamicOptions: true, @@ -367,7 +385,7 @@ export const REPORTS: ReportDefinition[] = [ { label: "IQC(採購)", value: "IQC" }, { label: "EPQC(工單)", value: "EPQC" }, ] }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), { label: "QC 項目範圍", name: "qcItemScope", @@ -386,7 +404,7 @@ export const REPORTS: ReportDefinition[] = [ fields: [ { label: "出倉日期:由 Last Out Date Start", name: "lastOutDateStart", type: "date", required: false }, { label: "出倉日期:至 Last Out Date End", name: "lastOutDateEnd", type: "date", required: false }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false}, + asyncItemCodeField(), { label: "提料人 Handler", name: "handler", type: "select", required: false, multiple: true, dynamicOptions: true, @@ -455,7 +473,7 @@ export const REPORTS: ReportDefinition[] = [ fields: [ { label: "同步日期:由 Sync Date Start", name: "syncDateStart", type: "date", required: false }, { label: "同步日期:至 Sync Date End", name: "syncDateEnd", type: "date", required: false }, - { label: "成品貨號 Finished Item Code", name: "finishedItemCode", type: "text", required: false }, + asyncItemCodeField("成品貨號 Finished Item Code", "finishedItemCode"), { label: "同步狀態 Sync Status", name: "syncStatus", @@ -487,7 +505,7 @@ export const REPORTS: ReportDefinition[] = [ options: [], }, { label: "提票號碼", name: "ticketNo", type: "text", required: false }, - { label: "貨品編號 Item Code", name: "itemCode", type: "text", required: false }, + asyncItemCodeField(), { label: "樓層", name: "storeId", diff --git a/src/i18n/en/common.json b/src/i18n/en/common.json index 61a464d3..a3b15c4e 100644 --- a/src/i18n/en/common.json +++ b/src/i18n/en/common.json @@ -1,7 +1,7 @@ { "Actions": "操作", "Add Document": "新增文件", - "All": "全部", + "All": "All", "Allergic Substances": "過敏原", "An error has occurred. Please try again later.": "An error has occurred. Please try again later.", "Are you sure you want to delete this item?": "您確定要刪除此項目嗎?", @@ -75,15 +75,15 @@ "Remarks": "備註", "Remove Document": "移除文件", "Report": "報告", - "Reset": "重置", + "Reset": "Reset", "Row per page": "每頁行數", "Rows per page": "每頁行數", "Sales Qty": "銷售數量", "Sales UOM": "銷售單位", "Save": "儲存", "Saving": "儲存中", - "Search": "搜索", - "Search Criteria": "搜索條件", + "Search": "Search", + "Search Criteria": "Search Criteria", "Select Date": "選擇日期", "Session expired or unauthorized.": "工作階段已過期或未經授權。", "Sign out": "Sign out", diff --git a/src/i18n/en/inventory.json b/src/i18n/en/inventory.json index 3dcb7bdf..5d170b12 100644 --- a/src/i18n/en/inventory.json +++ b/src/i18n/en/inventory.json @@ -1,5 +1,6 @@ { "Action": "Action", + "All": "All", "Add": "Add", "Add entry": "Add entry", "Add entry for items without inventory": "Add entry for items without inventory", @@ -15,7 +16,14 @@ "Download QR Code": "Download QR Code", "Edit mode": "Edit mode", "Enter item code or name to search": "Enter item code or name to search", + "Area": "Area", "Expiry Date": "Expiry Date", + "Floor": "Floor", + "Item Search": "Item Search", + "Location Search": "Location Search", + "Select a floor to search by location.": "Select a floor to search by location.", + "Select floor first": "Select a floor first", + "Select warehouse first": "Select a warehouse first", "FG": "Finished good", "Failed to transfer stock": "Failed to transfer stock", "Failed to transfer stock. Please try again.": "Failed to transfer stock. Please try again.", @@ -52,6 +60,7 @@ "Remove": "Remove", "Reset": "Reset", "SFG": "Semi-finished good", + "Slot": "Slot", "Save": "Save", "Save failed": "Save failed", "Saved successfully": "Saved successfully", diff --git a/src/i18n/en/report.json b/src/i18n/en/report.json index c80ad1a9..17af3c26 100644 --- a/src/i18n/en/report.json +++ b/src/i18n/en/report.json @@ -16,8 +16,12 @@ "noDataFoundHint": "No inventory records match your search. Please try adjusting the filters.", "ok": "OK", "missingRequired": "Missing required fields:\n- {{fields}}", + "requiredField": "This is a required field", "dateNotBeforeToday": "Date cannot be before today:\n- {{fields}}", "selectOrEnterItemCode": "Select or enter item code", + "typeToSearchItemCode": "Enter at least {{min}} characters to search", + "noItemCodeMatches": "No matching item codes", + "searchingItemCodes": "Searching...", "cancel": "Cancel", "confirmDownloadPdf": "Confirm download PDF", "confirmDownloadExcel": "Confirm download Excel", diff --git a/src/i18n/zh/inventory.json b/src/i18n/zh/inventory.json index fae7d823..8513855b 100644 --- a/src/i18n/zh/inventory.json +++ b/src/i18n/zh/inventory.json @@ -1,5 +1,6 @@ { "Action": "操作", + "All": "全部", "Add": "新增", "Add entry": "新增倉存", "Add entry for items without inventory": "為無庫存貨品新增倉存", @@ -15,7 +16,14 @@ "Download QR Code": "下載", "Edit mode": "編輯模式", "Enter item code or name to search": "輸入貨品編號或名稱以搜索", + "Area": "區域", "Expiry Date": "到期日", + "Floor": "樓層", + "Item Search": "貨品搜尋", + "Location Search": "倉位搜尋", + "Select a floor to search by location.": "請先選擇樓層以搜尋倉位。", + "Select floor first": "請先選擇樓層", + "Select warehouse first": "請先選擇倉庫", "FG": "成品", "Failed to transfer stock": "轉倉失敗", "Failed to transfer stock. Please try again.": "轉倉失敗,請重試。", @@ -52,6 +60,7 @@ "Remove": "移除", "Reset": "重置", "SFG": "半成品", + "Slot": "儲位", "Save": "儲存", "Save failed": "儲存失敗", "Saved successfully": "儲存成功", diff --git a/src/i18n/zh/report.json b/src/i18n/zh/report.json index 3812c6a3..52a2a638 100644 --- a/src/i18n/zh/report.json +++ b/src/i18n/zh/report.json @@ -16,8 +16,12 @@ "noDataFoundHint": "找不到符合您搜尋條件的庫存資料,請嘗試調整篩選條件。", "ok": "確定", "missingRequired": "缺少必填條件:\n- {{fields}}", + "requiredField": "此為必填欄位", "dateNotBeforeToday": "日期不可早於今天:\n- {{fields}}", "selectOrEnterItemCode": "選擇或輸入物料編號", + "typeToSearchItemCode": "請輸入至少 {{min}} 個字元以搜尋貨品編號", + "noItemCodeMatches": "沒有符合的貨品編號", + "searchingItemCodes": "搜尋中...", "cancel": "取消", "confirmDownloadPdf": "確認下載 PDF", "confirmDownloadExcel": "確認下載 Excel",