- 出入库记录(库存流水):新增 stock_records 表/模型/只读接口(列表 + 业务动作选项)与「出入库记录」菜单; 库存变动统一收敛到 app/utils/stockRecord.js#applyStockChange,与流水写入同事务(含变动前后总/锁定/可用数量) - 发货单:审核(1→2) 按明细锁定库存、发货确认(3→4) 扣减、推送失败退回/驳回/取消/作废释放(新增 stock_locked 列); 恢复(9→1) 时曾推送过的单据换号(WMS 取消后原单号仍被占用,沿用旧号重推会被判「订单已存在」) - 出库单:新增快递(express_code/express_name)、收件人(receiver_*) 与多运单号(express_codes) 字段; 推送报文补齐 logisticsCode/receiverInfo;新增出库单推送服务与定时任务 - 快递:多运单号回填、仓库快递映射(warehouse_express_mappings) 与快递字典补录等迁移 - 契约:补充出入库记录、出库单快递/收件人等定义(swagger.json 为本地生成、不入库)
57 lines
3.7 KiB
JavaScript
57 lines
3.7 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 出入库记录(库存流水)模型,对应 stock_records 表
|
||
*
|
||
* 每次库存变动(出库推送锁定 / 出库解锁 / 出库确认扣减 / 入库确认增加)写一行,
|
||
* 记录变动数量与变动前后的总数量、锁定数量、可用数量,用于对账与排查。
|
||
* 业务动作常量与写入入口统一在 app/utils/stockRecord.js(BIZ_TYPES / applyStockChange)。
|
||
*
|
||
* - item_type:ZC=正品 / CC=残次品(product_stocks 维度)
|
||
* - direction:1入库 2出库;bill_type:1入库单 2出库单
|
||
* @param {Egg.Application} app egg application
|
||
* @return {object} stockRecord model
|
||
*/
|
||
module.exports = app => {
|
||
const { BIGINT, STRING, TINYINT, DECIMAL, DATE } = app.Sequelize;
|
||
|
||
const StockRecord = app.model.define('stock_record', {
|
||
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
|
||
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||
warehouse_id: { type: BIGINT.UNSIGNED, comment: '仓库ID(关联 warehouses)' },
|
||
product_id: { type: BIGINT.UNSIGNED, comment: '系统商品ID(关联 products)' },
|
||
item_code: { type: STRING(100), comment: '商品编码(快照)' },
|
||
item_name: { type: STRING(255), comment: '商品名称(快照)' },
|
||
item_type: { type: STRING(4), allowNull: false, defaultValue: 'ZC', comment: '库存类型:ZC=正品 CC=残次品' },
|
||
biz_type: { type: STRING(30), allowNull: false, comment: '业务动作:outbound_lock / outbound_unlock / outbound_deduct / inbound_increase' },
|
||
direction: { type: TINYINT, allowNull: false, comment: '出入方向:1入库 2出库' },
|
||
bill_type: { type: TINYINT, allowNull: false, comment: '单据类型:1入库单 2出库单' },
|
||
bill_id: { type: BIGINT.UNSIGNED, comment: '单据头ID(inbound_orders.id / outbound_orders.id)' },
|
||
bill_no: { type: STRING(50), comment: '单据号(RK_{id} / CK_{id})' },
|
||
quantity: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '本次变动数量(恒为正数)' },
|
||
quantity_before: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动前总数量' },
|
||
quantity_after: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动后总数量' },
|
||
lock_quantity: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '本次锁定变动量(正=加锁 负=解锁)' },
|
||
lock_before: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动前锁定数量' },
|
||
lock_after: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动后锁定数量' },
|
||
available_before: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动前可用数量' },
|
||
available_after: { type: DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动后可用数量' },
|
||
remark: { type: STRING(255), comment: '备注' },
|
||
created_by: { type: BIGINT.UNSIGNED, comment: '操作人ID(系统自动写入为空)' },
|
||
created_at: { type: DATE, comment: '记录时间(业务发生时间)' },
|
||
updated_at: { type: DATE, comment: '更新时间' },
|
||
}, {
|
||
tableName: 'stock_records',
|
||
comment: '出入库记录(库存流水)表',
|
||
});
|
||
|
||
StockRecord.associate = () => {
|
||
app.model.StockRecord.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
|
||
app.model.StockRecord.belongsTo(app.model.Product, { foreignKey: 'product_id', as: 'product' });
|
||
app.model.StockRecord.belongsTo(app.model.Warehouse, { foreignKey: 'warehouse_id', as: 'warehouse' });
|
||
app.model.StockRecord.belongsTo(app.model.User, { foreignKey: 'created_by', as: 'creator' });
|
||
};
|
||
|
||
return StockRecord;
|
||
};
|