diff --git a/src/components/StockTakeManagement/PickerStockTake.tsx b/src/components/StockTakeManagement/PickerStockTake.tsx index f2f4b2d..7ece7a7 100644 --- a/src/components/StockTakeManagement/PickerStockTake.tsx +++ b/src/components/StockTakeManagement/PickerStockTake.tsx @@ -137,24 +137,30 @@ const PickerStockTake: React.FC = ({ setLoadingDetails(false); } }, [selectedSession, total]); + useEffect(() => { - const inputs: Record = {}; - inventoryLotDetails.forEach((detail) => { - const firstTotal = detail.firstStockTakeQty != null - ? (detail.firstStockTakeQty + (detail.firstBadQty ?? 0)).toString() - : ""; - const secondTotal = detail.secondStockTakeQty != null - ? (detail.secondStockTakeQty + (detail.secondBadQty ?? 0)).toString() - : ""; - inputs[detail.id] = { - firstQty: firstTotal, - secondQty: secondTotal, - firstBadQty: detail.firstBadQty?.toString() || "", - secondBadQty: detail.secondBadQty?.toString() || "", - remark: detail.remarks || "", - }; + setRecordInputs((prev) => { + const next: Record = {}; + inventoryLotDetails.forEach((detail) => { + const hasServerFirst = detail.firstStockTakeQty != null; + const hasServerSecond = detail.secondStockTakeQty != null; + const firstTotal = hasServerFirst + ? (detail.firstStockTakeQty! + (detail.firstBadQty ?? 0)).toString() + : ""; + const secondTotal = hasServerSecond + ? (detail.secondStockTakeQty! + (detail.secondBadQty ?? 0)).toString() + : ""; + const existing = prev[detail.id]; + next[detail.id] = { + firstQty: hasServerFirst ? firstTotal : (existing?.firstQty ?? firstTotal), + secondQty: hasServerSecond ? secondTotal : (existing?.secondQty ?? secondTotal), + firstBadQty: hasServerFirst ? (detail.firstBadQty?.toString() || "") : (existing?.firstBadQty ?? ""), + secondBadQty: hasServerSecond ? (detail.secondBadQty?.toString() || "") : (existing?.secondBadQty ?? ""), + remark: hasServerSecond ? (detail.remarks || "") : (existing?.remark ?? detail.remarks ?? ""), + }; + }); + return next; }); - setRecordInputs(inputs); }, [inventoryLotDetails]); useEffect(() => { loadDetails(page, pageSize); @@ -399,7 +405,40 @@ const PickerStockTake: React.FC = ({ } return false; }, [selectedSession?.status]); - + const handleSubmitAllInputted = useCallback(async () => { + if (!selectedSession || !currentUserId) return; + + const toSave = inventoryLotDetails.filter((detail) => { + const submitDisabled = isSubmitDisabled(detail); + if (submitDisabled) return false; + const isFirstSubmit = !detail.stockTakeRecordId || !detail.firstStockTakeQty; + const isSecondSubmit = detail.stockTakeRecordId && detail.firstStockTakeQty && !detail.secondStockTakeQty; + const totalQtyStr = isFirstSubmit ? recordInputs[detail.id]?.firstQty : recordInputs[detail.id]?.secondQty; + return !!totalQtyStr && totalQtyStr.trim() !== ""; + }); + + if (toSave.length === 0) { + onSnackbar(t("No valid input to submit"), "warning"); + return; + } + + setBatchSaving(true); + let successCount = 0; + let errorCount = 0; + for (const detail of toSave) { + try { + await handleSaveStockTake(detail); + successCount++; + } catch { + errorCount++; + } + } + setBatchSaving(false); + onSnackbar( + t("Submit completed: {{success}} success, {{errors}} errors", { success: successCount, errors: errorCount }), + errorCount > 0 ? "warning" : "success" + ); + }, [inventoryLotDetails, recordInputs, isSubmitDisabled, handleSaveStockTake, selectedSession, currentUserId, onSnackbar, t]); const uniqueWarehouses = Array.from( new Set( inventoryLotDetails @@ -422,6 +461,15 @@ const PickerStockTake: React.FC = ({ <> {t("Warehouse")}: {uniqueWarehouses} )} +{/* + + */} {/* 如果需要显示快捷键输入,可以把这块注释打开 */} {/* {shortcutInput && ( diff --git a/src/i18n/zh/inventory.json b/src/i18n/zh/inventory.json index a2a2fd8..b0d5a32 100644 --- a/src/i18n/zh/inventory.json +++ b/src/i18n/zh/inventory.json @@ -16,6 +16,8 @@ "available": "可用", "Issue Qty": "問題數量", "tke": "盤點", + "Submit completed: {{success}} success, {{errors}} errors": "提交完成:{{success}} 成功,{{errors}} 錯誤", + "Submit All Inputted": "提交所有輸入", "Submit Bad Item": "提交不良品", "Remain available Quantity": "剩餘可用數量", "Submitting...": "提交中...",