'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; };