diff --git a/src/app/api/pickOrder/actions.ts b/src/app/api/pickOrder/actions.ts index 962ae03..a97835b 100644 --- a/src/app/api/pickOrder/actions.ts +++ b/src/app/api/pickOrder/actions.ts @@ -550,24 +550,25 @@ const fetchSuggestionsWithStatus = async (pickOrderLineId: number) => { } }; +// Update the existing function to use the non-auto-assign endpoint export const fetchALLPickOrderLineLotDetails = cache(async (userId: number): Promise => { try { console.log("🔍 Fetching all pick order line lot details for userId:", userId); - // ✅ 使用 serverFetchJson 而不是直接的 fetch + // ✅ Use the non-auto-assign endpoint const data = await serverFetchJson( - `${BASE_API_URL}/pickOrder/all-lots-with-details/${userId}`, + `${BASE_API_URL}/pickOrder/all-lots-with-details-no-auto-assign/${userId}`, { method: 'GET', next: { tags: ["pickorder"] }, } ); - console.log("✅ API Response:", data); + console.log("✅ Fetched lot details:", data); return data; } catch (error) { - console.error("❌ Error fetching all pick order line lot details:", error); - throw error; + console.error("❌ Error fetching lot details:", error); + return []; } }); export const fetchAllPickOrderDetails = cache(async (userId?: number) => { diff --git a/src/components/FinishedGoodSearch/AssignAndRelease.tsx b/src/components/FinishedGoodSearch/AssignAndRelease.tsx index 5c9582d..ab19e48 100644 --- a/src/components/FinishedGoodSearch/AssignAndRelease.tsx +++ b/src/components/FinishedGoodSearch/AssignAndRelease.tsx @@ -156,7 +156,10 @@ const AssignAndRelease: React.FC = ({ filterArgs }) => { if (res && res.records) { console.log("Records received:", res.records.length); console.log("First record:", res.records[0]); - + console.log("First record targetDate:", res.records[0]?.targetDate); +console.log("First record targetDate type:", typeof res.records[0]?.targetDate); +console.log("First record targetDate parsed:", new Date(res.records[0]?.targetDate)); +console.log("First record targetDate formatted:", new Date(res.records[0]?.targetDate).toLocaleDateString()); // 新增:在前端也过滤掉 "assigned" 状态的项目 const filteredRecords = res.records.filter((item: any) => item.status !== "assigned"); @@ -496,12 +499,14 @@ const AssignAndRelease: React.FC = ({ filterArgs }) => { {/* Target Date - 只在第一个项目显示 */} - {index === 0 ? ( - arrayToDayjs(item.targetDate) - .add(-1, "month") - .format(OUTPUT_DATE_FORMAT) - ) : null} - + {index === 0 ? ( + (() => { + console.log("targetDate:", item.targetDate); + console.log("formatted:", arrayToDayjs(item.targetDate).format(OUTPUT_DATE_FORMAT)); + return arrayToDayjs(item.targetDate).format(OUTPUT_DATE_FORMAT); + })() + ) : null} + {/* Pick Order Status - 只在第一个项目显示 */} diff --git a/src/components/FinishedGoodSearch/FinishedGoodSearch.tsx b/src/components/FinishedGoodSearch/FinishedGoodSearch.tsx index 43c171c..aef2ca4 100644 --- a/src/components/FinishedGoodSearch/FinishedGoodSearch.tsx +++ b/src/components/FinishedGoodSearch/FinishedGoodSearch.tsx @@ -24,8 +24,10 @@ import NewCreateItem from "./newcreatitem"; import AssignAndRelease from "./AssignAndRelease"; import AssignTo from "./assignTo"; import { fetchAllItemsInClient, ItemCombo } from "@/app/api/settings/item/actions"; -import { fetchPickOrderClient } from "@/app/api/pickOrder/actions"; +import { fetchPickOrderClient, autoAssignAndReleasePickOrder } from "@/app/api/pickOrder/actions"; import Jobcreatitem from "./Jobcreatitem"; +import { useSession } from "next-auth/react"; +import { SessionWithTokens } from "@/config/authConfig"; interface Props { pickOrders: PickOrderResult[]; @@ -39,6 +41,8 @@ type SearchParamNames = keyof SearchQuery; const PickOrderSearch: React.FC = ({ pickOrders }) => { const { t } = useTranslation("pickOrder"); + const { data: session } = useSession() as { data: SessionWithTokens | null }; + const currentUserId = session?.id ? parseInt(session.id) : undefined; const [isOpenCreateModal, setIsOpenCreateModal] = useState(false) const [items, setItems] = useState([]) @@ -47,6 +51,37 @@ const PickOrderSearch: React.FC = ({ pickOrders }) => { const [searchQuery, setSearchQuery] = useState>({}); const [tabIndex, setTabIndex] = useState(0); const [totalCount, setTotalCount] = useState(); + const [isAssigning, setIsAssigning] = useState(false); + + // ✅ Manual assignment handler - uses the action function + const handleManualAssign = useCallback(async () => { + if (!currentUserId || isAssigning) return; + + setIsAssigning(true); + try { + console.log("🎯 Manual assignment triggered for user:", currentUserId); + + // ✅ Use the action function instead of direct fetch + const result = await autoAssignAndReleasePickOrder(currentUserId); + console.log("✅ Manual assignment result:", result); + + if (result.code === "SUCCESS") { + console.log("✅ Successfully assigned pick order manually"); + // Trigger refresh of the PickExecution component + window.dispatchEvent(new CustomEvent('pickOrderAssigned')); + } else if (result.code === "EXISTS") { + console.log("ℹ️ User already has active pick orders"); + // Still trigger refresh to show existing orders + window.dispatchEvent(new CustomEvent('pickOrderAssigned')); + } else { + console.log("ℹ️ No available pick orders or other status:", result.message); + } + } catch (error) { + console.error("❌ Error in manual assignment:", error); + } finally { + setIsAssigning(false); + } + }, [currentUserId, isAssigning]); const handleTabChange = useCallback>( (_e, newValue) => { @@ -266,13 +301,31 @@ const PickOrderSearch: React.FC = ({ pickOrders }) => { - {/* Tabs section */} + {/* Tabs section - ✅ Move the click handler here */} - + + {isAssigning && ( + + + {t("Assigning pick order...")} + + + )} {/* Content section - NO overflow: 'auto' here */} @@ -285,4 +338,4 @@ const PickOrderSearch: React.FC = ({ pickOrders }) => { ); }; -export default PickOrderSearch; +export default PickOrderSearch; \ No newline at end of file diff --git a/src/components/FinishedGoodSearch/GoodPickExecution.tsx b/src/components/FinishedGoodSearch/GoodPickExecution.tsx index be24cd1..0377ee6 100644 --- a/src/components/FinishedGoodSearch/GoodPickExecution.tsx +++ b/src/components/FinishedGoodSearch/GoodPickExecution.tsx @@ -415,7 +415,7 @@ const PickExecution: React.FC = ({ filterArgs }) => { return; } - // ✅ 使用新的API路径,后端会自动处理分配逻辑 + // ✅ Use the non-auto-assign endpoint - this only fetches existing data const allLotDetails = await fetchALLPickOrderLineLotDetails(userIdToUse); console.log("✅ All combined lot details:", allLotDetails); setCombinedLotData(allLotDetails); @@ -429,19 +429,30 @@ const PickExecution: React.FC = ({ filterArgs }) => { } }, [currentUserId]); - // ✅ 简化初始化逻辑,移除前端的自动分配检查 + // ✅ Only fetch existing data when session is ready, no auto-assignment useEffect(() => { if (session && currentUserId && !initializationRef.current) { console.log("✅ Session loaded, initializing pick order..."); initializationRef.current = true; - // ✅ 直接获取数据,后端会自动处理分配逻辑 + // ✅ Only fetch existing data, no auto-assignment fetchAllCombinedLotData(); } }, [session, currentUserId, fetchAllCombinedLotData]); - // ✅ 移除前端的自动分配逻辑,因为后端已经处理了 - // const handleAutoAssignAndRelease = useCallback(async () => { ... }); // 删除这个函数 + // ✅ Add event listener for manual assignment + useEffect(() => { + const handlePickOrderAssigned = () => { + console.log("🔄 Pick order assigned event received, refreshing data..."); + fetchAllCombinedLotData(); + }; + + window.addEventListener('pickOrderAssigned', handlePickOrderAssigned); + + return () => { + window.removeEventListener('pickOrderAssigned', handlePickOrderAssigned); + }; + }, [fetchAllCombinedLotData]); // ✅ Handle QR code submission for matched lot (external scanning) // ✅ Handle QR code submission for matched lot (external scanning) @@ -925,13 +936,15 @@ const PickExecution: React.FC = ({ filterArgs }) => { {/* Search Box */} + + {/* {t("FG Pick Orders")} - + */} {fgPickOrdersLoading ? ( @@ -980,11 +993,11 @@ const PickExecution: React.FC = ({ filterArgs }) => { {t("Item Name")} {t("Lot#")} {t("Target Date")} - {t("Lot Location")} + {/* {t("Lot Location")} */} {t("Lot Required Pick Qty")} {t("Original Available Qty")} {t("Lot Actual Pick Qty")} - {t("Remaining Available Qty")} + {/* {t("Remaining Available Qty")} */} {t("Action")} @@ -1033,7 +1046,7 @@ const PickExecution: React.FC = ({ filterArgs }) => { {lot.pickOrderTargetDate} - {lot.location} + {/* {lot.location} */} {calculateRemainingRequiredQty(lot).toLocaleString()} {(() => { @@ -1123,14 +1136,14 @@ const PickExecution: React.FC = ({ filterArgs }) => { )} - + {/* {(() => { const inQty = lot.inQty || 0; const outQty = lot.outQty || 0; const result = inQty - outQty; return result.toLocaleString(); })()} - + */}