- 商品/商品图片、店铺、店铺商品、组合商品及明细 - 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField) - 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js) - 库存单据与对接日志:stock orders、api_outbound_logs - 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
242 lines
9.0 KiB
JavaScript
242 lines
9.0 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const recalcStock = require('../utils/recalcStock');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/**
|
||
* 应/已数量由系统维护(应=明细数量,已=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 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;
|
||
}
|
||
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;
|