oms/backend/app/controller/stockItem.js
liufumingren 4e56077770 feat(stock): 出入库单仓库与商品按货主双向限制并校验防注入
- 新增 app/lib/billOwnerScope.js:仓库可访问性(主体 + 所在公司)、明细商品/组合
  与仓库同货主校验、按单据仓库取货主
- 入库单/出库单 create:仓库可访问且明细商品必须与仓库同货主
  (原明细校验未带 principal_id,一并修正)
- 入库单/出库单 update:改仓库时校验新仓库可访问且已有明细同货主
- 明细接口(stock_items create/update):按所属单据仓库货主校验商品,堵住
  单据建好后再塞入其它货主商品的路径
- 前端新增 useWarehouseGoodsScope:仓库与商品双向约束(先选仓库限制商品、
  先选商品限制仓库、同单明细不得跨货主)
- 入库/出库创建页与单据明细编辑区接入该约束并给出提示
- ProductSelect 新增 beforeSelect 前置校验与受控显示值,校验不通过时回滚显示
2026-11-05 15:02:12 +08:00

269 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
const recalcStock = require('../utils/recalcStock');
const { parsePagination, buildPageResult } = require('../lib/pagination');
const { assertItemsOwner, resolveBillSubsidiary } = require('../lib/billOwnerScope');
/**
* 应/已数量由系统维护(应=明细数量,已=WMS 回推),禁止前端直接写入。
*/
const QTY_MANAGED_FIELDS = [ 'plan_in_qty', 'actual_in_qty', 'plan_out_qty', 'actual_out_qty' ];
/**
* 明细仅在所属单据处于「待处理(1)」时可增删改;
* 单据一经审核(2) 及之后明细即锁定,避免审核后篡改导致库存与对账不一致。
* @param {Object} ctx egg context
* @param {Number} billType 1入库 2出库
* @param {Number} billId 单据头ID
* @return {Promise<{ok: boolean, message?: string}>}
*/
async function assertItemsEditable(ctx, billType, billId) {
const OrderModel = Number(billType) === 1 ? ctx.model.InboundOrder : ctx.model.OutboundOrder;
const order = await OrderModel.findByPk(billId);
if (!order) return { ok: false, message: '所属单据不存在' };
if (order.status !== 1) {
return { ok: false, message: '单据已审核或已结束,明细已锁定,不允许增删改' };
}
return { ok: true };
}
/**
* @controller StockItemController 出入库明细管理
* 明细挂在 stock_items 表,由 bill_type(1入库 2出库)+ bill_id 区分归属单据
*/
class StockItemController extends Controller {
/**
* @summary 明细列表
* @description 按 bill_id + bill_type 查询明细(必填),带出系统商品/组合商品
* @router get /api/stock-items
* @apikey
* @request query integer bill_id 所属单据ID(必填)
* @request query integer bill_type 单据类型(必填):1入库 2出库
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 50
* @response 200 stockItemListResponse success
*/
async index() {
const { ctx } = this;
const { page, pageSize, offset, limit } = parsePagination(ctx.query, { defaultPageSize: 50 });
const { bill_id: billId, bill_type: billType } = ctx.query;
if (!billId || !billType) {
ctx.status = 400;
ctx.body = { code: 400, message: 'bill_id 与 bill_type 均为必填项' };
return;
}
const where = { bill_id: Number(billId), bill_type: Number(billType), principal_id: ctx.state.principalId };
const { count, rows } = await ctx.model.StockItem.findAndCountAll({
where,
offset,
limit,
order: [[ 'id', 'ASC' ]],
include: [
{ model: ctx.model.Product, as: 'product', attributes: [ 'id', 'item_code', 'item_name', 'sku_property' ] },
{ model: ctx.model.ComboProduct, as: 'comboProduct', attributes: [ 'id', 'code', 'name' ] },
],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
}
/**
* @summary 明细详情
* @router get /api/stock-items/{id}
* @apikey
* @request path integer *id 明细主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const item = await ctx.model.StockItem.findByPk(ctx.params.id, {
include: [
{ model: ctx.model.Product, as: 'product', attributes: [ 'id', 'item_code', 'item_name' ] },
{ model: ctx.model.ComboProduct, as: 'comboProduct', attributes: [ 'id', 'code', 'name' ] },
],
});
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '明细不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: item };
}
/**
* @summary 新增明细
* @router post /api/stock-items
* @apikey
* @request body createStockItemRequest *body
* @response 200 baseResponse success
*/
async create() {
const { ctx } = this;
const { bill_id: billId, bill_type: billType, product_id: productId } = ctx.request.body;
if (!billId || !billType) {
ctx.status = 400;
ctx.body = { code: 400, message: 'bill_id 与 bill_type 均为必填项' };
return;
}
// 明细锁定校验:单据已审核及之后不允许再新增明细
const editable = await assertItemsEditable(ctx, billType, billId);
if (!editable.ok) {
ctx.status = 400;
ctx.body = { code: 400, message: editable.message };
return;
}
// 关联系统商品时必须启用,禁用商品不可绑定到明细
let product = null;
if (productId) {
product = await ctx.model.Product.findByPk(productId);
if (!product || product.status !== 1) {
ctx.status = 400;
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,
principal_id: ctx.state.principalId,
quantity: ctx.request.body.quantity || 0,
total_amount: Number((Number(ctx.request.body.quantity || 0) * Number(ctx.request.body.price || 0)).toFixed(2)),
};
// 应/已数量由系统维护:应数量取明细数量,已数量默认 0(由 WMS 回推写入)
for (const f of QTY_MANAGED_FIELDS) delete data[f];
if (Number(billType) === 1) data.plan_in_qty = data.quantity;
else data.plan_out_qty = data.quantity;
// 绑定商品时以商品快照覆盖名称/编码/规格/单位(未填才覆盖)
if (product) {
data.product_id = product.id;
data.name = ctx.request.body.name || product.item_name;
data.code = ctx.request.body.code || product.item_code;
data.spec = ctx.request.body.spec || product.sku_property;
data.unit = ctx.request.body.unit || product.stock_unit;
}
const item = await ctx.model.StockItem.create(data);
await recalcStock(ctx, Number(billType), Number(billId));
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 更新明细
* @router put /api/stock-items/{id}
* @apikey
* @request path integer *id 明细主键ID
* @request body updateStockItemRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const item = await ctx.model.StockItem.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '明细不存在' };
return;
}
// 明细锁定校验:单据已审核及之后不允许修改明细
const editable = await assertItemsEditable(ctx, item.bill_type, item.bill_id);
if (!editable.ok) {
ctx.status = 400;
ctx.body = { code: 400, message: editable.message };
return;
}
const body = { ...ctx.request.body };
delete body.principal_id;
for (const f of QTY_MANAGED_FIELDS) delete body[f];
// 变更关联商品时校验启用状态,并以商品快照覆盖名称/编码/规格/单位
if (body.product_id !== undefined) {
const pid = body.product_id;
if (pid) {
const product = await ctx.model.Product.findByPk(pid);
if (!product || product.status !== 1) {
ctx.status = 400;
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;
body.unit = body.unit || product.stock_unit;
} else {
body.name = body.name || null;
body.code = body.code || null;
body.spec = body.spec || null;
body.unit = body.unit || null;
}
}
if (body.quantity !== undefined || body.price !== undefined) {
const qty = body.quantity != null ? body.quantity : item.quantity;
const price = body.price != null ? body.price : item.price;
body.total_amount = Number((Number(qty || 0) * Number(price || 0)).toFixed(2));
}
// 数量变动时同步「应」数量(已数量仍由 WMS 回推维护)
if (body.quantity !== undefined) {
const qty = Number(body.quantity) || 0;
if (Number(item.bill_type) === 1) body.plan_in_qty = qty;
else body.plan_out_qty = qty;
}
await item.update(body);
await recalcStock(ctx, item.bill_type, item.bill_id);
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 删除明细
* @router delete /api/stock-items/{id}
* @apikey
* @request path integer *id 明细主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const item = await ctx.model.StockItem.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '明细不存在' };
return;
}
// 明细锁定校验:单据已审核及之后不允许删除明细
const editable = await assertItemsEditable(ctx, item.bill_type, item.bill_id);
if (!editable.ok) {
ctx.status = 400;
ctx.body = { code: 400, message: editable.message };
return;
}
await item.destroy();
await recalcStock(ctx, item.bill_type, item.bill_id);
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = StockItemController;