- 迁移:inbound_orders / outbound_orders 增加 subsidiary_id 与索引,按仓库回填 - 单据货主由仓库决定:创建写入、改仓库同步,接口不接受直写 subsidiary_id - 入库单/出库单列表返回 subsidiary_name 并新增货主筛选;详情/编辑/删除/审核/ 推送/完成/取消/恢复/作废/驳回与明细接口统一加货主越界 403 - 出入库记录按仓库归属货主:列表与导出收窄、返回 subsidiary_id/subsidiary_name、 新增货主列与筛选 - 导出:入库单/出库单/出入库记录新增货主列并按所在公司权限收窄 - 新增 resolveAccountNameMap:列表/详情返回 creator_name/auditor_name/voider_name, 创建人展示员工名称(principal_users.name),不再回退成邮箱账号 - 前端:三个列表新增货主列与筛选,创建人/作废人展示改为员工名称 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
308 lines
12 KiB
JavaScript
308 lines
12 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const recalcStock = require('../utils/recalcStock');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
const { assertItemsOwner, resolveBillSubsidiary, resolveAccess, isVisible } = 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, status?: number, 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, { attributes: [ 'id', 'status', 'subsidiary_id' ] });
|
||
if (!order) return { ok: false, status: 404, message: '所属单据不存在' };
|
||
const access = await resolveAccess(ctx);
|
||
if (!isVisible(access, order.subsidiary_id)) {
|
||
return { ok: false, status: 403, message: '该单据不属于自己所属公司(货主),无权操作' };
|
||
}
|
||
if (order.status !== 1) {
|
||
return { ok: false, message: '单据已审核或已结束,明细已锁定,不允许增删改' };
|
||
}
|
||
return { ok: true };
|
||
}
|
||
|
||
/**
|
||
* 校验单据所属货主是否在当前账号可见范围内(只读场景:明细列表 / 详情)
|
||
* @param {Object} ctx egg context
|
||
* @param {Number} billType 1入库 2出库
|
||
* @param {Number} billId 单据头ID
|
||
* @return {Promise<{ok: boolean, status?: number, message?: string}>} 校验结果
|
||
*/
|
||
async function assertBillVisible(ctx, billType, billId) {
|
||
const OrderModel = Number(billType) === 1 ? ctx.model.InboundOrder : ctx.model.OutboundOrder;
|
||
const order = await OrderModel.findByPk(billId, { attributes: [ 'id', 'subsidiary_id' ] });
|
||
if (!order) return { ok: false, status: 404, message: '所属单据不存在' };
|
||
const access = await resolveAccess(ctx);
|
||
if (!isVisible(access, order.subsidiary_id)) {
|
||
return { ok: false, status: 403, 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 visible = await assertBillVisible(ctx, billType, billId);
|
||
if (!visible.ok) {
|
||
ctx.status = visible.status;
|
||
ctx.body = { code: visible.status, message: visible.message };
|
||
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;
|
||
}
|
||
const visible = await assertBillVisible(ctx, item.bill_type, item.bill_id);
|
||
if (!visible.ok) {
|
||
ctx.status = visible.status;
|
||
ctx.body = { code: visible.status, message: visible.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) {
|
||
const status = editable.status || 400;
|
||
ctx.status = status;
|
||
ctx.body = { code: status, 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) {
|
||
const status = editable.status || 400;
|
||
ctx.status = status;
|
||
ctx.body = { code: status, 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) {
|
||
const status = editable.status || 400;
|
||
ctx.status = status;
|
||
ctx.body = { code: status, 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;
|