diff --git a/src/lib/permissions.ts b/src/lib/permissions.ts index abb432b..15ae4ff 100644 --- a/src/lib/permissions.ts +++ b/src/lib/permissions.ts @@ -258,10 +258,12 @@ export async function getUserCompanies() { }, }); + type CompanyRelation = NonNullable['companies'][0]; + return ( questUser?.companies - .filter((c) => c.company.is_active) - .map((c) => c.company) || [] + .filter((c: CompanyRelation) => c.company.is_active) + .map((c: CompanyRelation) => c.company) || [] ); } diff --git a/src/services/inventory.ts b/src/services/inventory.ts index db71bb8..ea40f22 100644 --- a/src/services/inventory.ts +++ b/src/services/inventory.ts @@ -185,41 +185,71 @@ export async function getInventoryDetails( warehouse?: string; } ): Promise { - // Map category to stored procedure name - const procMap: Record = { - wip: 'portal_WorkInProgressInventoryDetailV6', - 'finished-goods': 'portal_FinishedGoodsInventoryDetailV6', - 'processed-other': 'portal_ProcessedOtherInventoryDetailV6', - unprocessed: 'portal_UnprocessedInventoryDetail', - 'unprocessed-rr': 'portal_UnprocessedRRInventoryDetail', - 'processed-rr': 'portal_ProcessedRRInventoryDetail', + const hasFilters = filters?.partNum || filters?.plant || filters?.warehouse; + + // Map category to stored procedure names (filtered and ALL variants) + const procMap: Record< + InventoryCategory, + { filtered: string; all: string; isV6: boolean } + > = { + wip: { + filtered: 'PortalWorkInProgressInventoryDetailsV6', + all: 'PortalWorkInProgressInventoryDetailsALLV6', + isV6: true, + }, + 'finished-goods': { + filtered: 'PortalFinishedGoodsInventoryDetailsV6', + all: 'PortalFinishedGoodsInventoryDetailsALLV6', + isV6: true, + }, + 'processed-other': { + filtered: 'PortalProcessedOtherInventoryDetailsV6', + all: 'PortalProcessedOtherInventoryDetailsALLV6', + isV6: true, + }, + unprocessed: { + filtered: 'PortalUnprocessedInventoryDetails', + all: 'PortalUnprocessedInventoryDetailsALL', + isV6: false, + }, + 'unprocessed-rr': { + filtered: 'PortalUnprocessedInventoryRejectsAndReturnsDetails', + all: 'PortalUnprocessedInventoryRejectsAndReturnsDetailsALL', + isV6: false, + }, + 'processed-rr': { + filtered: 'PortalProcessedInventoryRejectsAndReturnsDetails', + all: 'PortalProcessedInventoryRejectsAndReturnsDetailsALL', + isV6: false, + }, }; - const procName = procMap[category]; + const procConfig = procMap[category]; + const procName = hasFilters ? procConfig.filtered : procConfig.all; const params: Record = { - CustID: custId, DBNAME: dbName, }; - // V6 procedures use 'sub' parameter - if ( - category === 'wip' || - category === 'finished-goods' || - category === 'processed-other' - ) { + // V6 procedures use CUSTID (uppercase), non-V6 use custID + if (procConfig.isV6) { + params.CUSTID = custId; params.sub = sub; + } else { + params.custID = custId; } - // Add filters if provided - if (filters?.partNum) { - params.PartNum = filters.partNum; - } - if (filters?.plant) { - params.Plant = filters.plant; - } - if (filters?.warehouse) { - params.Warehouse = filters.warehouse; + // Add filters if provided (only for filtered variant) + if (hasFilters) { + if (procConfig.isV6) { + params.PART = filters?.partNum || ''; + params.PLANT = filters?.plant || ''; + params.WAREHOUSE = filters?.warehouse || ''; + } else { + params.part = filters?.partNum || ''; + params.plant = filters?.plant || ''; + params.warehouse = filters?.warehouse || ''; + } } const result = await execStoredProc(procName, params);