diff --git a/app/service/systemMethods/storage/entry.js b/app/service/systemMethods/storage/entry.js index 2dc7da3..7e68924 100644 --- a/app/service/systemMethods/storage/entry.js +++ b/app/service/systemMethods/storage/entry.js @@ -349,8 +349,9 @@ class entryService extends MainService { } } /** - * 入库单商品与收货单商品逐行对照视图(system method 包装层)。 - * 支持传入多个收货单ID进行批量对照预览。 + * 入库单商品与收货单商品对照视图(system method 包装层)。 + * 支持传入多个收货单ID:多个收货单明细先合并为一个整体,再与入库单做一次对照, + * 每个入库明细仅输出一行(含合并收货数量与各收货单来源明细)。 * @param {Object} data 请求体 * @param {number} data.entry_order_id 入库单ID * @param {number[]} [data.receiving_ids] 收货单ID列表(优先使用) diff --git a/app/service/systems/storage/entry.js b/app/service/systems/storage/entry.js index 26b619d..956e6ad 100644 --- a/app/service/systems/storage/entry.js +++ b/app/service/systems/storage/entry.js @@ -1717,9 +1717,10 @@ class EntryService extends Service { } /** - * 入库单商品与收货单商品逐行对照视图(支持多个收货单)。 - * 以入库单明细为主,按 stock_id ↔ system_product_id 匹配收货单明细, - * 返回每个收货单的匹配/未匹配/收货单多余对照结果及汇总。 + * 入库单商品与收货单商品对照视图(支持多个收货单)。 + * 以入库单明细为对比主体:多个收货单的明细先合并为一个整体, + * 再与入库单明细做一次对照,每个入库明细只输出一行, + * 避免多收货单时入库计划数量被重复展开。 * * @param {Object} data 请求参数 * @param {number} data.entry_order_id 入库单ID @@ -1728,7 +1729,8 @@ class EntryService extends Service { * @return {Promise} code/data/message 结构 */ async receivingCompare(data = {}) { - const { ctx } = this; + const { ctx, app } = this; + const { Op } = app.Sequelize; const entryOrderId = Number(data.entry_order_id || 0); const warehouseId = ctx.user.store_id; @@ -1754,39 +1756,53 @@ class EntryService extends Service { return { code: 404, message: '入库单不存在或无权访问', data: null }; } - // 2. 查询入库单明细(所有收货单共用) + // 2. 查询入库单明细 const entryItems = await ctx.model.EntryItems.findAll({ where: { order_id: entryOrderId }, attributes: [ 'id', 'stock_id', 'count', 'received_quantity' ], raw: true, }); - // 3. 收集入库单所有 stock_id - const entryStockIds = [ ...new Set(entryItems.map(item => Number(item.stock_id)).filter(Boolean)) ]; - - // 4. 对每个收货单执行对比 - const comparisons = []; + // 3. 批量校验收货单(同仓库、未删除),无效的进入 errors + const receivingOrders = await ctx.model.Receiving.findAll({ + where: { + id: { [Op.in]: receivingIds }, + warehouse_id: warehouseId, + is_deleted: false, + }, + attributes: [ 'id', 'receiving_number' ], + raw: true, + }); + const receivingOrderMap = new Map(receivingOrders.map(row => [ Number(row.id), row ])); const errors = []; - for (const receivingId of receivingIds) { - const singleResult = await this._compareSingleReceiving({ - receivingId, - warehouseId, - entryOrderId, - entryOrder, - entryItems, - entryStockIds, - }); - if (singleResult._error) { - errors.push(singleResult._error); - } else { - comparisons.push(singleResult); + if (!receivingOrderMap.has(receivingId)) { + errors.push({ + receiving_id: receivingId, + status: 'error', + error_code: 'RECEIVING_NOT_FOUND', + error_message: '收货单不存在或无权访问', + }); } } + const validReceivingIds = [ ...receivingOrderMap.keys() ]; - const totalReceivings = receivingIds.length; - const matchedCount = comparisons.length; - const errorCount = errors.length; + // 4. 合并所有有效收货单的明细,再与入库单做一次对照 + let receivingDetails = []; + if (validReceivingIds.length > 0) { + receivingDetails = await ctx.model.ReceivingDetail.findAll({ + where: { receiving_id: { [Op.in]: validReceivingIds } }, + attributes: [ 'id', 'receiving_id', 'system_product_id', 'actual_quantity', 'product_name' ], + raw: true, + }); + } + + const merged = await this._buildMergedComparison({ + warehouseId, + entryItems, + receivingDetails, + receivingOrderMap, + }); return { code: 200, @@ -1794,93 +1810,64 @@ class EntryService extends Service { data: { entry_order_id: entryOrderId, entry_order_code: entryOrder.entry_order_code, - comparisons, + comparison: merged.comparison, + receiving_only: merged.receivingOnly, + summary: merged.summary, errors, total_summary: { - total_receivings: totalReceivings, - matched_count: matchedCount, - error_count: errorCount, + total_receivings: receivingIds.length, + matched_count: validReceivingIds.length, + error_count: errors.length, }, }, }; } /** - * 对单个收货单执行商品对照逻辑。 + * 将多个收货单的明细合并后,与入库单明细做一次对照。 + * 每个入库明细只输出一行(matched/entry_only),匹配时合并各收货单数量, + * 并通过 receiving_sources 列出各收货单来源明细。 + * * @param {Object} params 参数 - * @param {number} params.receivingId 收货单ID * @param {number} params.warehouseId 仓库ID - * @param {number} params.entryOrderId 入库单ID - * @param {Object} params.entryOrder 入库单记录 * @param {Object[]} params.entryItems 入库单明细列表 - * @param {number[]} params.entryStockIds 入库单 stock_id 集合 - * @return {Promise} 单个收货单的对照结果,或 { _error: ... } + * @param {Object[]} params.receivingDetails 已合并的收货单明细列表 + * @param {Map} params.receivingOrderMap 有效收货单 Map(id → 收货单记录) + * @return {Promise} { comparison, receivingOnly, summary } */ - async _compareSingleReceiving(params = {}) { + async _buildMergedComparison(params = {}) { const { ctx, app } = this; - const { receivingId, warehouseId, entryItems, entryStockIds } = params; - - // 校验收货单存在性与仓库匹配 - const receivingOrder = await ctx.model.Receiving.findOne({ - where: { - id: receivingId, - warehouse_id: warehouseId, - is_deleted: false, - }, - attributes: [ 'id', 'receiving_number', 'status', 'storage_status' ], - raw: true, - }); - if (!receivingOrder) { - return { - _error: { - receiving_id: receivingId, - status: 'error', - error_code: 'RECEIVING_NOT_FOUND', - error_message: '收货单不存在或无权访问', - }, - }; - } - - // 查询收货单明细 - const receivingDetails = await ctx.model.ReceivingDetail.findAll({ - where: { receiving_id: receivingId }, - attributes: [ 'id', 'system_product_id', 'actual_quantity', 'product_name' ], - raw: true, - }); + const { warehouseId, entryItems, receivingDetails, receivingOrderMap } = params; // 收集所有 stock_id,批量查询商品信息 + const entryStockIds = [ ...new Set(entryItems.map(item => Number(item.stock_id)).filter(Boolean)) ]; const receivingSystemProductIds = [ ...new Set(receivingDetails.map(d => Number(d.system_product_id)).filter(Boolean)) ]; const allStockIds = [ ...new Set([ ...entryStockIds, ...receivingSystemProductIds ]) ]; const stockDetailMap = new Map(); if (allStockIds.length > 0) { const stockDetails = await ctx.service.systems.stockWarehouse.getStockDetails(allStockIds, { warehouseId }); for (const detail of stockDetails) { - stockDetailMap.set(detail.id, detail); + stockDetailMap.set(Number(detail.id), detail); } } - // stock_id ↔ system_product_id 映射 - const stockIdToSystemProductId = new Map(); - const systemProductIdToEntryStockIds = new Map(); + // 仓库范围内有效的入库商品 stock_id(与原逻辑一致:仅匹配本仓库库存记录) + const validEntryStockIds = new Set(entryStockIds); if (entryStockIds.length > 0) { const swRows = await ctx.model.StockWarehouse.findAll({ where: { id: { [app.Sequelize.Op.in]: entryStockIds }, warehouse_id: warehouseId }, attributes: [ 'id' ], raw: true, }); + validEntryStockIds.clear(); for (const row of swRows) { - const stockId = Number(row.id); - stockIdToSystemProductId.set(stockId, stockId); - if (!systemProductIdToEntryStockIds.has(stockId)) { - systemProductIdToEntryStockIds.set(stockId, []); - } - systemProductIdToEntryStockIds.get(stockId).push(stockId); + validEntryStockIds.add(Number(row.id)); } } // 查询已分配量(限定本入库单的 entry_items,避免全局绑定干扰) - const receivingDetailIds = receivingDetails.map(d => d.id); - const entryItemIds = entryItems.map(ei => ei.id); + const receivingDetailIds = receivingDetails.map(d => Number(d.id)); + const entryItemIds = entryItems.map(ei => Number(ei.id)); const allocatedMap = new Map(); if (receivingDetailIds.length > 0 && entryItemIds.length > 0) { const bindingRows = await ctx.model.InboundDirectLog.findAll({ @@ -1902,42 +1889,67 @@ class EntryService extends Service { } } - // 构建对照视图:以入库单明细为主 + // 按 stock_id 索引收货明细,供合并匹配 + const detailsByStockId = new Map(); + for (const rd of receivingDetails) { + const stockId = Number(rd.system_product_id) || 0; + if (stockId <= 0) continue; + if (!detailsByStockId.has(stockId)) { + detailsByStockId.set(stockId, []); + } + detailsByStockId.get(stockId).push(rd); + } + + // 组装单个收货明细的来源信息 + const buildSource = rd => { + const rdStockDetail = (rd.system_product_id != null && stockDetailMap.get(Number(rd.system_product_id))) || null; + const allocated = allocatedMap.get(Number(rd.id)) || 0; + const quantity = Number(rd.actual_quantity) || 0; + return { + receiving_id: Number(rd.receiving_id), + receiving_number: (receivingOrderMap.get(Number(rd.receiving_id)) || {}).receiving_number || '', + receiving_detail_id: Number(rd.id), + receiving_product_code: (rdStockDetail && rdStockDetail.item_code) || '', + receiving_product_name: rd.product_name || (rdStockDetail && rdStockDetail.item_name) || '', + receiving_actual_quantity: quantity, + receiving_allocated: allocated, + receiving_available: Math.max(0, quantity - allocated), + }; + }; + + // 构建对照视图:以入库单明细为主,每个入库明细只输出一行 const comparison = []; const matchedReceivingDetailIds = new Set(); for (const entryItem of entryItems) { const stockId = Number(entryItem.stock_id || 0); - const systemProductId = stockIdToSystemProductId.get(stockId) || 0; const stockDetail = stockDetailMap.get(stockId) || null; - - const matchedReceiving = systemProductId > 0 - ? receivingDetails.filter(rd => Number(rd.system_product_id) === systemProductId) + const matchedDetails = stockId > 0 && validEntryStockIds.has(stockId) + ? (detailsByStockId.get(stockId) || []) : []; - if (matchedReceiving.length > 0) { - for (const rd of matchedReceiving) { - matchedReceivingDetailIds.add(rd.id); - const rdStockDetail = (rd.system_product_id != null && stockDetailMap.get(Number(rd.system_product_id))) || null; - const allocated = allocatedMap.get(Number(rd.id)) || 0; - const available = Math.max(0, (Number(rd.actual_quantity) || 0) - allocated); - comparison.push({ - entry_item_id: Number(entryItem.id), - stock_id: stockId, - warehouse_item_code: (stockDetail && stockDetail.item_code) || '', - warehouse_item_name: (stockDetail && stockDetail.item_name) || '', - warehouse_item_spec: (stockDetail && stockDetail.sku_property) || '', - entry_count: Number(entryItem.count) || 0, - entry_received: Number(entryItem.received_quantity) || 0, - receiving_detail_id: Number(rd.id), - receiving_product_code: (rdStockDetail && rdStockDetail.item_code) || '', - receiving_product_name: rd.product_name || (rdStockDetail && rdStockDetail.item_name) || '', - receiving_actual_quantity: Number(rd.actual_quantity) || 0, - receiving_allocated: allocated, - receiving_available: available, - match_status: 'matched', - }); + if (matchedDetails.length > 0) { + const sources = []; + for (const rd of matchedDetails) { + matchedReceivingDetailIds.add(Number(rd.id)); + sources.push(buildSource(rd)); } + const mergedQuantity = sources.reduce((sum, source) => sum + source.receiving_actual_quantity, 0); + const mergedAllocated = sources.reduce((sum, source) => sum + source.receiving_allocated, 0); + comparison.push({ + entry_item_id: Number(entryItem.id), + stock_id: stockId, + warehouse_item_code: (stockDetail && stockDetail.item_code) || '', + warehouse_item_name: (stockDetail && stockDetail.item_name) || '', + warehouse_item_spec: (stockDetail && stockDetail.sku_property) || '', + entry_count: Number(entryItem.count) || 0, + entry_received: Number(entryItem.received_quantity) || 0, + match_status: 'matched', + receiving_quantity: mergedQuantity, + receiving_allocated: mergedAllocated, + receiving_available: Math.max(0, mergedQuantity - mergedAllocated), + receiving_sources: sources, + }); } else { comparison.push({ entry_item_id: Number(entryItem.id), @@ -1947,69 +1959,52 @@ class EntryService extends Service { warehouse_item_spec: (stockDetail && stockDetail.sku_property) || '', entry_count: Number(entryItem.count) || 0, entry_received: Number(entryItem.received_quantity) || 0, - receiving_detail_id: null, - receiving_product_code: null, - receiving_product_name: null, - receiving_actual_quantity: null, - receiving_allocated: null, - receiving_available: null, match_status: 'entry_only', + receiving_quantity: 0, + receiving_allocated: 0, + receiving_available: 0, + receiving_sources: [], }); } } - // 收货单中多余的商品 - const receivingOnly = []; - const receivingStockMap = new Map(); + // 收货单中多余的商品(合并后仍未匹配到任何入库明细的收货明细,按商品合并) + const receivingOnlyMap = new Map(); for (const rd of receivingDetails) { - if (!matchedReceivingDetailIds.has(Number(rd.id))) { - const rdStockDetail = (rd.system_product_id != null && stockDetailMap.get(Number(rd.system_product_id))) || null; - const stockId = rd.system_product_id != null ? Number(rd.system_product_id) : null; - const qty = Number(rd.actual_quantity) || 0; - const name = rd.product_name || (rdStockDetail && rdStockDetail.item_name) || ''; - const allocated = allocatedMap.get(Number(rd.id)) || 0; - const available = Math.max(0, qty - allocated); - - if (stockId === null) { - receivingOnly.push({ - receiving_product_name: name, - receiving_actual_quantity: qty, - receiving_allocated: allocated, - receiving_available: available, - }); - } else if (receivingStockMap.has(stockId)) { - const existing = receivingStockMap.get(stockId); - existing.receiving_actual_quantity += qty; - existing.receiving_allocated += allocated; - existing.receiving_available += available; - if (!existing.receiving_product_name && name) { - existing.receiving_product_name = name; - } - } else { - receivingStockMap.set(stockId, { - receiving_product_name: name, - receiving_actual_quantity: qty, - receiving_allocated: allocated, - receiving_available: available, - }); + if (matchedReceivingDetailIds.has(Number(rd.id))) continue; + const stockId = rd.system_product_id != null ? Number(rd.system_product_id) : null; + const source = buildSource(rd); + const key = stockId === null || Number.isNaN(stockId) ? '__none__' : String(stockId); + if (receivingOnlyMap.has(key)) { + const existing = receivingOnlyMap.get(key); + existing.receiving_actual_quantity += source.receiving_actual_quantity; + existing.receiving_allocated += source.receiving_allocated; + existing.receiving_available = Math.max(0, existing.receiving_available + source.receiving_available); + if (!existing.receiving_product_name && source.receiving_product_name) { + existing.receiving_product_name = source.receiving_product_name; } + existing.receiving_sources.push(source); + } else { + receivingOnlyMap.set(key, { + stock_id: stockId, + receiving_product_code: source.receiving_product_code, + receiving_product_name: source.receiving_product_name, + receiving_actual_quantity: source.receiving_actual_quantity, + receiving_allocated: source.receiving_allocated, + receiving_available: source.receiving_available, + receiving_sources: [ source ], + }); } } - for (const item of receivingStockMap.values()) { - item.receiving_available = Math.max(0, item.receiving_available); - receivingOnly.push(item); - } + const receivingOnly = [ ...receivingOnlyMap.values() ]; // 汇总 const matchedItems = comparison.filter(c => c.match_status === 'matched').length; const entryOnlyItems = comparison.filter(c => c.match_status === 'entry_only').length; return { - receiving_id: receivingId, - receiving_number: receivingOrder.receiving_number, - status: 'matched', comparison, - receiving_only: receivingOnly, + receivingOnly, summary: { total_entry_items: entryItems.length, matched_items: matchedItems, diff --git a/methods/changes/system.json b/methods/changes/system.json index 65c557e..6464fae 100644 --- a/methods/changes/system.json +++ b/methods/changes/system.json @@ -1,4 +1,20 @@ [ + { + "version": "2026.08.24-storage.entry-receivingCompare-merge", + "date": "2026-08-24", + "type": "fixed", + "summary": "storage.entry.receivingCompare 对比主体修正:多个收货单先合并明细,再与入库单做一次对照。原实现每个收货单各对照一遍,入库计划数量被重复展开多份,前端对比表格出现重复行。", + "methods": [ + "storage.entry.receivingCompare" + ], + "notes": [ + "app/service/systems/storage/entry.js receivingCompare 重写:批量校验收货单后合并全部有效收货单明细,新增 _buildMergedComparison(替代 _compareSingleReceiving)输出单一对照视图", + "出参结构调整:comparisons[](按收货单拆分)改为 comparison[](合并后逐行对照,每个入库明细仅一行)+ receiving_only[] + summary;matched 行新增 receiving_quantity/receiving_allocated/receiving_available(合并值)与 receiving_sources[](各收货单来源明细),entry_only 行对应数量为 0 且 receiving_sources 为空", + "total_summary.matched_count 语义调整为有效参与对比的收货单数(原为处理成功的对照数);无效收货单仍进入 errors[]", + "同步更新 methods/system.json 中 receivingCompare 的 response 结构说明" + ], + "module": "storage.entry" + }, { "version": "2026.08.23-returnUnpack.video-url-cdn-domain", "date": "2026-08-23", diff --git a/methods/system.json b/methods/system.json index 072429b..14fdc57 100644 --- a/methods/system.json +++ b/methods/system.json @@ -22646,7 +22646,7 @@ }, { "method": "storage.entry.receivingCompare", - "description": "入库单与收货单商品对照预览:给定入库单ID和收货单ID(支持多个),返回入库商品与收货商品的逐行对照视图,含匹配状态(matched/entry_only)和汇总统计。纯只读接口。", + "description": "入库单与收货单商品对照预览:给定入库单ID和收货单ID(支持多个),先将多个收货单的明细合并为一个整体,再与入库单商品做一次逐行对照,返回合并后的对照视图(每个入库明细仅一行,含匹配状态 matched/entry_only、合并收货数量与各收货单来源明细)及汇总统计。纯只读接口。", "isNeedAuth": false, "isNeedLogin": true, "headers": [], @@ -22687,66 +22687,102 @@ "description": "入库单号" }, { - "name": "comparisons", + "name": "comparison", "type": "array", "required": true, - "description": "各收货单对照结果列表", + "description": "合并后的逐行对照列表(多个收货单明细先合并,再与入库单对照;以入库单明细为主,每个入库明细仅一行)", "children": [ { - "name": "receiving_id", + "name": "entry_item_id", "type": "number", "required": true, - "description": "收货单ID" + "description": "入库单明细ID" }, { - "name": "receiving_number", + "name": "stock_id", + "type": "number", + "required": true, + "description": "库存商品ID(stock_warehouse.id)" + }, + { + "name": "warehouse_item_code", "type": "string", "required": true, - "description": "收货单号" + "description": "入库商品编码" }, { - "name": "status", + "name": "warehouse_item_name", "type": "string", "required": true, - "description": "对比状态:matched-匹配成功, error-校验失败" + "description": "入库商品名称" }, { - "name": "error_code", + "name": "warehouse_item_spec", "type": "string", - "required": false, - "description": "错误码(仅 status=error 时),如 RECEIVING_NOT_FOUND" + "required": true, + "description": "入库商品规格" }, { - "name": "error_message", + "name": "entry_count", + "type": "number", + "required": true, + "description": "计划数量" + }, + { + "name": "entry_received", + "type": "number", + "required": true, + "description": "入库单已收数量" + }, + { + "name": "match_status", "type": "string", - "required": false, - "description": "错误信息(仅 status=error 时)" + "required": true, + "description": "匹配状态:matched-已匹配, entry_only-仅入库(无收货商品)" }, { - "name": "comparison", + "name": "receiving_quantity", + "type": "number", + "required": true, + "description": "合并后收货实际数量(所有收货单该商品明细数量之和,entry_only 时为 0)" + }, + { + "name": "receiving_allocated", + "type": "number", + "required": true, + "description": "合并后已分配数量" + }, + { + "name": "receiving_available", + "type": "number", + "required": true, + "description": "合并后可再用量(收货实际-已分配)" + }, + { + "name": "receiving_sources", "type": "array", - "required": false, - "description": "逐行对照列表(仅 status=matched 时),每项含 entry_item_id/stock_id/warehouse_item_code/warehouse_item_name/warehouse_item_spec/entry_count/entry_received/receiving_detail_id/receiving_product_code/receiving_product_name/receiving_actual_quantity/receiving_allocated/receiving_available/match_status" - }, - { - "name": "receiving_only", - "type": "array", - "required": false, - "description": "仅收货单有的商品列表(仅 status=matched 时),每项含 receiving_detail_id/receiving_product_code/receiving_product_name/receiving_actual_quantity/receiving_allocated/receiving_available" - }, - { - "name": "summary", - "type": "object", - "required": false, - "description": "该收货单的汇总统计(仅 status=matched 时),含 total_entry_items/matched_items/entry_only_items/receiving_only_items" + "required": true, + "description": "收货来源明细列表(matched 时非空),每项含 receiving_id/receiving_number/receiving_detail_id/receiving_product_code/receiving_product_name/receiving_actual_quantity/receiving_allocated/receiving_available" } ] }, + { + "name": "receiving_only", + "type": "array", + "required": true, + "description": "仅收货单有的商品列表(合并后仍未匹配到任何入库明细,按商品合并),每项含 stock_id/receiving_product_code/receiving_product_name/receiving_actual_quantity/receiving_allocated/receiving_available/receiving_sources" + }, + { + "name": "summary", + "type": "object", + "required": true, + "description": "合并对照汇总统计,含 total_entry_items/matched_items/entry_only_items/receiving_only_items" + }, { "name": "errors", "type": "array", "required": false, - "description": "校验失败的收货单列表(status=error 的记录也会在此汇总)" + "description": "校验失败的收货单列表(status=error 的记录)" }, { "name": "total_summary", @@ -22762,7 +22798,7 @@ { "name": "matched_count", "type": "number", - "description": "匹配成功的收货单数" + "description": "有效参与对比的收货单数" }, { "name": "error_count",