From 4e56077770e80ebbd0db1b57fb5b2786dfa10a6e Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 5 Nov 2026 15:02:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(stock):=20=E5=87=BA=E5=85=A5=E5=BA=93?= =?UTF-8?q?=E5=8D=95=E4=BB=93=E5=BA=93=E4=B8=8E=E5=95=86=E5=93=81=E6=8C=89?= =?UTF-8?q?=E8=B4=A7=E4=B8=BB=E5=8F=8C=E5=90=91=E9=99=90=E5=88=B6=E5=B9=B6?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E9=98=B2=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 app/lib/billOwnerScope.js:仓库可访问性(主体 + 所在公司)、明细商品/组合 与仓库同货主校验、按单据仓库取货主 - 入库单/出库单 create:仓库可访问且明细商品必须与仓库同货主 (原明细校验未带 principal_id,一并修正) - 入库单/出库单 update:改仓库时校验新仓库可访问且已有明细同货主 - 明细接口(stock_items create/update):按所属单据仓库货主校验商品,堵住 单据建好后再塞入其它货主商品的路径 - 前端新增 useWarehouseGoodsScope:仓库与商品双向约束(先选仓库限制商品、 先选商品限制仓库、同单明细不得跨货主) - 入库/出库创建页与单据明细编辑区接入该约束并给出提示 - ProductSelect 新增 beforeSelect 前置校验与受控显示值,校验不通过时回滚显示 --- backend/app/controller/inboundOrder.js | 54 ++++++--- backend/app/controller/outboundOrder.js | 54 ++++++--- backend/app/controller/stockItem.js | 27 +++++ backend/app/lib/billOwnerScope.js | 112 ++++++++++++++++++ frontend/src/components/ProductSelect.vue | 46 +++++-- .../src/composables/useWarehouseGoodsScope.ts | 101 ++++++++++++++++ frontend/src/views/InboundOrder/Create.vue | 52 +++++++- frontend/src/views/InboundOrder/Index.vue | 17 +++ frontend/src/views/OutboundOrder/Create.vue | 52 +++++++- frontend/src/views/OutboundOrder/Index.vue | 17 +++ 10 files changed, 491 insertions(+), 41 deletions(-) create mode 100644 backend/app/lib/billOwnerScope.js create mode 100644 frontend/src/composables/useWarehouseGoodsScope.ts diff --git a/backend/app/controller/inboundOrder.js b/backend/app/controller/inboundOrder.js index 9f7f5bc..f805b45 100644 --- a/backend/app/controller/inboundOrder.js +++ b/backend/app/controller/inboundOrder.js @@ -6,6 +6,7 @@ const completeInbound = require('../utils/completeInbound'); const nextRejectedBillNo = require('../utils/rejectBillNo'); const { parsePagination, buildPageResult } = require('../lib/pagination'); const { resolveAccountName } = require('../lib/accountProfile'); +const { resolveWarehouse, assertItemsOwner } = require('../lib/billOwnerScope'); const QimenWmsClient = require('../lib/wms/qimenClient'); const { DEFAULT_INBOUND_ORDER_TYPE, INBOUND_ORDER_TYPE_LIST, normalizeInboundOrderType } = require('../lib/wms/orderTypes'); @@ -149,7 +150,8 @@ class InboundOrderController extends Controller { /** * @summary 新增入库单 - * @description 单号由系统按 RK_{自增id} 自动生成(不接受传入);仓库必填;可同时传入 items(stock_items, bill_type=1)一次性写入明细 + * @description 单号由系统按 RK_{自增id} 自动生成(不接受传入);仓库必填;可同时传入 items(stock_items, bill_type=1)一次性写入明细。 + * 单据货主由仓库决定(warehouses.subsidiary_id),明细商品/组合必须与仓库同货主,否则拒绝创建 * @router post /api/inbound-orders * @apikey * @request body createInboundOrderRequest *body @@ -181,19 +183,20 @@ class InboundOrderController extends Controller { } payload.inbound_type = inboundType || DEFAULT_INBOUND_ORDER_TYPE; - // 校验明细关联商品均存在且已启用 - const productIds = (items || []).map((i) => i.product_id).filter(Boolean); - if (productIds.length) { - const validProducts = await ctx.model.Product.findAll({ - where: { id: productIds, status: 1 }, - attributes: [ 'id' ], - }); - const validIds = new Set(validProducts.map((p) => p.id)); - if (productIds.some((id) => !validIds.has(id))) { - ctx.status = 400; - ctx.body = { code: 400, message: '明细关联的商品不存在或已禁用,无法创建单据' }; - return; - } + // 货主口径:仓库决定单据货主,明细商品必须与仓库同货主(防止跨货主串货) + const warehouseScope = await resolveWarehouse(ctx, warehouseId); + if (!warehouseScope.ok) { + ctx.status = warehouseScope.status; + ctx.body = { code: warehouseScope.status, message: warehouseScope.message }; + return; + } + + // 校验明细关联商品均存在、已启用且与仓库同货主 + const itemsOwner = await assertItemsOwner(ctx, items, warehouseScope.subsidiaryId); + if (!itemsOwner.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: itemsOwner.message }; + return; } const transaction = await ctx.model.transaction(); @@ -283,6 +286,29 @@ class InboundOrderController extends Controller { } else { delete ctx.request.body.inbound_type; } + + // 换仓库时:仓库需可访问,且单据已有明细必须与新仓库同货主(防止改仓库把单据挪到别的货主) + if (ctx.request.body.warehouse_id !== undefined + && Number(ctx.request.body.warehouse_id) !== Number(order.warehouse_id)) { + const warehouseScope = await resolveWarehouse(ctx, ctx.request.body.warehouse_id); + if (!warehouseScope.ok) { + ctx.status = warehouseScope.status; + ctx.body = { code: warehouseScope.status, message: warehouseScope.message }; + return; + } + const existingItems = await ctx.model.StockItem.findAll({ + where: { bill_type: 1, bill_id: order.id, principal_id: ctx.state.principalId }, + attributes: [ 'product_id', 'combo_product_id' ], + raw: true, + }); + const itemsOwner = await assertItemsOwner(ctx, existingItems, warehouseScope.subsidiaryId); + if (!itemsOwner.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: itemsOwner.message }; + return; + } + } + await order.update(ctx.request.body); ctx.body = { code: 0, message: 'success', data: { id: order.id } }; } diff --git a/backend/app/controller/outboundOrder.js b/backend/app/controller/outboundOrder.js index 28d9988..f0ff9be 100644 --- a/backend/app/controller/outboundOrder.js +++ b/backend/app/controller/outboundOrder.js @@ -6,6 +6,7 @@ const { applyStockChange } = require('../utils/stockRecord'); const nextRejectedBillNo = require('../utils/rejectBillNo'); const { parsePagination, buildPageResult } = require('../lib/pagination'); const { resolveAccountName } = require('../lib/accountProfile'); +const { resolveWarehouse, assertItemsOwner } = require('../lib/billOwnerScope'); const QimenWmsClient = require('../lib/wms/qimenClient'); const { DEFAULT_OUTBOUND_ORDER_TYPE, OUTBOUND_ORDER_TYPE_LIST, normalizeOutboundOrderType } = require('../lib/wms/orderTypes'); @@ -109,7 +110,8 @@ class OutboundOrderController extends Controller { /** * @summary 新增出库单 - * @description 单号由系统按 CK_{自增id} 自动生成(不接受传入);可同时传入 items(stock_items, bill_type=2)一次性写入明细 + * @description 单号由系统按 CK_{自增id} 自动生成(不接受传入);可同时传入 items(stock_items, bill_type=2)一次性写入明细。 + * 单据货主由仓库决定(warehouses.subsidiary_id),明细商品/组合必须与仓库同货主,否则拒绝创建 * @router post /api/outbound-orders * @apikey * @request body createOutboundOrderRequest *body @@ -140,19 +142,20 @@ class OutboundOrderController extends Controller { } payload.outbound_type = outboundType || DEFAULT_OUTBOUND_ORDER_TYPE; - // 校验明细关联商品均存在且已启用 - const productIds = (items || []).map((i) => i.product_id).filter(Boolean); - if (productIds.length) { - const validProducts = await ctx.model.Product.findAll({ - where: { id: productIds, status: 1 }, - attributes: [ 'id' ], - }); - const validIds = new Set(validProducts.map((p) => p.id)); - if (productIds.some((id) => !validIds.has(id))) { - ctx.status = 400; - ctx.body = { code: 400, message: '明细关联的商品不存在或已禁用,无法创建单据' }; - return; - } + // 货主口径:仓库决定单据货主,明细商品必须与仓库同货主(防止跨货主串货) + const warehouseScope = await resolveWarehouse(ctx, warehouseId); + if (!warehouseScope.ok) { + ctx.status = warehouseScope.status; + ctx.body = { code: warehouseScope.status, message: warehouseScope.message }; + return; + } + + // 校验明细关联商品均存在、已启用且与仓库同货主 + const itemsOwner = await assertItemsOwner(ctx, items, warehouseScope.subsidiaryId); + if (!itemsOwner.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: itemsOwner.message }; + return; } const transaction = await ctx.model.transaction(); @@ -242,6 +245,29 @@ class OutboundOrderController extends Controller { } else { delete ctx.request.body.outbound_type; } + + // 换仓库时:仓库需可访问,且单据已有明细必须与新仓库同货主(防止改仓库把单据挪到别的货主) + if (ctx.request.body.warehouse_id !== undefined + && Number(ctx.request.body.warehouse_id) !== Number(order.warehouse_id)) { + const warehouseScope = await resolveWarehouse(ctx, ctx.request.body.warehouse_id); + if (!warehouseScope.ok) { + ctx.status = warehouseScope.status; + ctx.body = { code: warehouseScope.status, message: warehouseScope.message }; + return; + } + const existingItems = await ctx.model.StockItem.findAll({ + where: { bill_type: 2, bill_id: order.id, principal_id: ctx.state.principalId }, + attributes: [ 'product_id', 'combo_product_id' ], + raw: true, + }); + const itemsOwner = await assertItemsOwner(ctx, existingItems, warehouseScope.subsidiaryId); + if (!itemsOwner.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: itemsOwner.message }; + return; + } + } + await order.update(ctx.request.body); ctx.body = { code: 0, message: 'success', data: { id: order.id } }; } diff --git a/backend/app/controller/stockItem.js b/backend/app/controller/stockItem.js index c7a7428..18fc127 100644 --- a/backend/app/controller/stockItem.js +++ b/backend/app/controller/stockItem.js @@ -3,6 +3,7 @@ const { Controller } = require('egg'); const recalcStock = require('../utils/recalcStock'); const { parsePagination, buildPageResult } = require('../lib/pagination'); +const { assertItemsOwner, resolveBillSubsidiary } = require('../lib/billOwnerScope'); /** * 应/已数量由系统维护(应=明细数量,已=WMS 回推),禁止前端直接写入。 @@ -123,6 +124,19 @@ class StockItemController extends Controller { ctx.body = { code: 400, message: '商品不存在或已禁用,无法关联' }; return; } + // 商品必须与单据仓库同货主(防止跨货主串货) + const billScope = await resolveBillSubsidiary(ctx, billType, billId); + if (!billScope.ok) { + ctx.status = billScope.status; + ctx.body = { code: billScope.status, message: billScope.message }; + return; + } + const itemsOwner = await assertItemsOwner(ctx, [ ctx.request.body ], billScope.subsidiaryId); + if (!itemsOwner.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: itemsOwner.message }; + return; + } } const data = { ...ctx.request.body, @@ -183,6 +197,19 @@ class StockItemController extends Controller { ctx.body = { code: 400, message: '商品不存在或已禁用,无法关联' }; return; } + // 商品必须与单据仓库同货主(防止跨货主串货) + const billScope = await resolveBillSubsidiary(ctx, item.bill_type, item.bill_id); + if (!billScope.ok) { + ctx.status = billScope.status; + ctx.body = { code: billScope.status, message: billScope.message }; + return; + } + const itemsOwner = await assertItemsOwner(ctx, [ body ], billScope.subsidiaryId); + if (!itemsOwner.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: itemsOwner.message }; + return; + } body.name = body.name || product.item_name; body.code = body.code || product.item_code; body.spec = body.spec || product.sku_property; diff --git a/backend/app/lib/billOwnerScope.js b/backend/app/lib/billOwnerScope.js new file mode 100644 index 0000000..bd8c2af --- /dev/null +++ b/backend/app/lib/billOwnerScope.js @@ -0,0 +1,112 @@ +'use strict'; + +/** + * 出入库单据的货主(下属公司)口径 + * + * 单据头不单独存货主字段:货主由所选仓库决定(warehouses.subsidiary_id)。 + * 为避免「某货主的货物进入另一货主的出入库单」,明细商品/组合必须与仓库同货主; + * 同时,所在公司权限的账号只能使用自己所属公司的仓库。 + */ + +/** 单据类型 → 单据头模型名(stock_items.bill_type) */ +const BILL_MODEL = { 1: 'InboundOrder', 2: 'OutboundOrder' }; + +/** + * 解析当前账号在该主体下的权限范围(无登录态视为系统操作,按集团放行) + * @param {Object} ctx egg context + * @return {Promise<{isGroup: boolean, ownCompanyIds: Array}>} 权限范围 + */ +async function resolveAccess(ctx) { + const principalId = Number(ctx.state.principalId); + if (!ctx.userId || !Number.isInteger(principalId)) return { isGroup: true, ownCompanyIds: [] }; + return ctx.service.access.resolve(ctx.userId, principalId); +} + +/** + * 取仓库并校验主体隔离与货主权限 + * @param {Object} ctx egg context + * @param {number|string} warehouseId 仓库ID + * @return {Promise<{ok: boolean, status?: number, message?: string, warehouse?: Object, subsidiaryId?: number|null}>} 校验结果 + */ +async function resolveWarehouse(ctx, warehouseId) { + const principalId = Number(ctx.state.principalId); + const warehouse = await ctx.model.Warehouse.findOne({ + where: { id: Number(warehouseId), principal_id: principalId }, + attributes: [ 'id', 'code', 'name', 'subsidiary_id' ], + }); + if (!warehouse) return { ok: false, status: 404, message: '仓库不存在或不属于当前主体' }; + + const subsidiaryId = warehouse.subsidiary_id == null ? null : Number(warehouse.subsidiary_id); + const access = await resolveAccess(ctx); + if (!access.isGroup && subsidiaryId !== null && !access.ownCompanyIds.includes(subsidiaryId)) { + return { ok: false, status: 403, message: '仓库不属于自己所属公司(货主),无权使用' }; + } + return { ok: true, warehouse, subsidiaryId }; +} + +/** + * 校验明细商品/组合与仓库同货主(存在性 + 启用状态 + 货主一致) + * @param {Object} ctx egg context + * @param {Array} items 明细 [{ product_id, combo_product_id }] + * @param {number|null} subsidiaryId 仓库所属公司ID(null 表示仓库未关联货主,不做货主校验) + * @return {Promise<{ok: boolean, message?: string}>} 校验结果 + */ +async function assertItemsOwner(ctx, items, subsidiaryId) { + const Op = ctx.app.Sequelize.Op; + const principalId = Number(ctx.state.principalId); + const list = Array.isArray(items) ? items : []; + const pickIds = key => [ ...new Set(list.map(it => Number(it && it[key])).filter(id => Number.isInteger(id) && id > 0)) ]; + const productIds = pickIds('product_id'); + const comboIds = pickIds('combo_product_id'); + if (!productIds.length && !comboIds.length) return { ok: true }; + + const [ products, combos ] = await Promise.all([ + productIds.length + ? ctx.model.Product.findAll({ + where: { id: { [Op.in]: productIds }, principal_id: principalId }, + attributes: [ 'id', 'status', 'subsidiary_id' ], + raw: true, + }) + : [], + comboIds.length + ? ctx.model.ComboProduct.findAll({ + where: { id: { [Op.in]: comboIds }, principal_id: principalId }, + attributes: [ 'id', 'subsidiary_id' ], + raw: true, + }) + : [], + ]); + + const disabled = products.filter(row => Number(row.status) !== 1); + if (disabled.length) return { ok: false, message: '明细关联的商品不存在或已禁用,无法保存' }; + if (products.length !== productIds.length || combos.length !== comboIds.length) { + return { ok: false, message: '明细关联的商品/组合不存在或不属于当前主体' }; + } + if (subsidiaryId === null || subsidiaryId === undefined) return { ok: true }; + + const mismatch = [ ...products, ...combos ].find( + row => row.subsidiary_id != null && Number(row.subsidiary_id) !== Number(subsidiaryId) + ); + if (mismatch) return { ok: false, message: '明细商品不属于该仓库的货主,请选择同货主的商品' }; + return { ok: true }; +} + +/** + * 取单据所在仓库的货主ID(明细增删改时校验商品货主用) + * @param {Object} ctx egg context + * @param {number} billType 单据类型:1入库 2出库 + * @param {number} billId 单据头ID + * @return {Promise<{ok: boolean, status?: number, message?: string, subsidiaryId?: number|null}>} 校验结果 + */ +async function resolveBillSubsidiary(ctx, billType, billId) { + const modelName = BILL_MODEL[Number(billType)]; + if (!modelName) return { ok: false, status: 400, message: 'bill_type 不合法,仅支持 1入库 / 2出库' }; + const order = await ctx.model[modelName].findByPk(billId, { attributes: [ 'id', 'warehouse_id' ] }); + if (!order) return { ok: false, status: 404, message: '所属单据不存在' }; + if (!order.warehouse_id) return { ok: true, subsidiaryId: null }; + const warehouse = await resolveWarehouse(ctx, order.warehouse_id); + if (!warehouse.ok) return { ok: false, status: warehouse.status, message: warehouse.message }; + return { ok: true, subsidiaryId: warehouse.subsidiaryId }; +} + +module.exports = { resolveAccess, resolveWarehouse, assertItemsOwner, resolveBillSubsidiary }; diff --git a/frontend/src/components/ProductSelect.vue b/frontend/src/components/ProductSelect.vue index ca65b4c..2bb83d4 100644 --- a/frontend/src/components/ProductSelect.vue +++ b/frontend/src/components/ProductSelect.vue @@ -1,5 +1,5 @@