- Updated the `update` function in the outbound controller to include detailed inventory adjustments when modifying outbound records, ensuring accurate stock management. - Implemented rollback mechanisms for both outbound and receiving updates to maintain data integrity in case of errors. - Enhanced the `deleteOutbound` function to include inventory recovery and historical logging for deleted outbound records. - Introduced a new utility function `adjustInventory` to handle inventory changes consistently across different controllers. - Improved error handling and logging for better traceability during outbound and receiving operations.
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
"use client";
|
|
|
|
import { usePathname } from "next/navigation";
|
|
import { useMemo } from "react";
|
|
import { useMenu } from "@/contexts/MenuContext";
|
|
|
|
function stripCompanyPrefix(pathname: string): string {
|
|
return pathname.replace(/^\/COMPANY_\d+/, "") || "/";
|
|
}
|
|
|
|
/**
|
|
* 현재 경로가 속한 "대분류" 메뉴의 objid 반환.
|
|
* - 메뉴 트리에서 parent_obj_id를 따라 올라가며, 트리에 부모가 더 이상 존재하지 않는 루트 바로 직전의 노드를 반환
|
|
* - lev 필드에 의존하지 않음 (백엔드 재귀 쿼리 결과와 무관)
|
|
*/
|
|
export function useCurrent2ndLevelMenuObjid(): number | null {
|
|
const pathname = usePathname();
|
|
const { userMenus, adminMenus } = useMenu();
|
|
|
|
return useMemo(() => {
|
|
if (!pathname) return null;
|
|
|
|
const all: any[] = [...(userMenus as any[]), ...(adminMenus as any[])];
|
|
if (all.length === 0) return null;
|
|
|
|
const targetUrl = stripCompanyPrefix(pathname);
|
|
|
|
const byObjid = new Map<string, any>();
|
|
for (const m of all) {
|
|
byObjid.set(String(m.objid), m);
|
|
}
|
|
|
|
const current = all.find((m) => m.menu_url === targetUrl);
|
|
if (!current) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn("[useCurrent2ndLevelMenuObjid] 메뉴 매칭 실패", { targetUrl, sample: all.slice(0, 3) });
|
|
return null;
|
|
}
|
|
|
|
let node: any = current;
|
|
let prev: any = null;
|
|
let safety = 20;
|
|
while (node && safety-- > 0) {
|
|
const parentId = node.parent_obj_id;
|
|
if (parentId === null || parentId === undefined || parentId === 0 || parentId === "0") {
|
|
break;
|
|
}
|
|
const parent = byObjid.get(String(parentId));
|
|
if (!parent) break;
|
|
prev = node;
|
|
node = parent;
|
|
}
|
|
|
|
const resultObjid = prev ? Number(prev.objid) : Number(node.objid);
|
|
// eslint-disable-next-line no-console
|
|
console.log("[useCurrent2ndLevelMenuObjid]", {
|
|
targetUrl,
|
|
currentObjid: current.objid,
|
|
currentName: current.menu_name_kor,
|
|
resultObjid,
|
|
resultName: prev ? prev.menu_name_kor : node.menu_name_kor,
|
|
});
|
|
return resultObjid;
|
|
}, [pathname, userMenus, adminMenus]);
|
|
}
|