- 新增 app/lib/billOwnerScope.js:仓库可访问性(主体 + 所在公司)、明细商品/组合 与仓库同货主校验、按单据仓库取货主 - 入库单/出库单 create:仓库可访问且明细商品必须与仓库同货主 (原明细校验未带 principal_id,一并修正) - 入库单/出库单 update:改仓库时校验新仓库可访问且已有明细同货主 - 明细接口(stock_items create/update):按所属单据仓库货主校验商品,堵住 单据建好后再塞入其它货主商品的路径 - 前端新增 useWarehouseGoodsScope:仓库与商品双向约束(先选仓库限制商品、 先选商品限制仓库、同单明细不得跨货主) - 入库/出库创建页与单据明细编辑区接入该约束并给出提示 - ProductSelect 新增 beforeSelect 前置校验与受控显示值,校验不通过时回滚显示
113 lines
5.2 KiB
JavaScript
113 lines
5.2 KiB
JavaScript
'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<number>}>} 权限范围
|
||
*/
|
||
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 };
|