'use strict'; /** * 出入库记录表(库存流水):stock_records * * 与 product_stocks(库存余量表)互补——product_stocks 只存当前余量,本表存「每一次库存变动」的 * 发生时间、单据来源、变动数量与变动前后的总数量/锁定数量/可用数量,用于对账与排查。 * * 记录的业务动作(biz_type,常量单一来源 app/utils/stockRecord.js): * - outbound_lock 出库单推送锁定(出库单 push:可用 → 锁定) * - outbound_unlock 出库解锁(推送失败释放 / 驳回 / 取消回滚) * - outbound_deduct 出库确认扣减(WMS 回调 stockout.confirm / notify.delivery:总数量与锁定同减) * - inbound_increase 入库确认增加(WMS 回调 entryorder.confirm:总数量增加) * * 数量口径(DECIMAL(15,3),与 product_stocks 一致): * - quantity 本次变动数量(入库为增加量;出库锁定/扣减为占用量,恒为正数) * - quantity_before/after 变动前/后总数量 * - lock_quantity 本次锁定变动量(正=加锁,负=解锁,由 lock_before/after 差值反推) * - lock_before/after 变动前/后锁定数量 * - available_before/after 变动前/后可用数量(= 总数量 - 锁定数量) * * 库存维度 item_type:ZC=正品 / CC=残次品(与 product_stocks.item_type 对齐, * 与 stock_items.inventory_type 的 ZP/CC 不同层:ZP 会在写入库存时映射为 ZC)。 */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable( 'stock_records', { id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true, }, principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID', references: { model: 'principals', key: 'id' }, }, warehouse_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '仓库ID(关联 warehouses)' }, product_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '系统商品ID(关联 products)' }, item_code: { type: Sequelize.STRING(100), allowNull: true, comment: '商品编码(快照)' }, item_name: { type: Sequelize.STRING(255), allowNull: true, comment: '商品名称(快照)' }, item_type: { type: Sequelize.STRING(4), allowNull: false, defaultValue: 'ZC', comment: '库存类型:ZC=正品 CC=残次品(与 product_stocks.item_type 对齐)', }, biz_type: { type: Sequelize.STRING(30), allowNull: false, comment: '业务动作:outbound_lock / outbound_unlock / outbound_deduct / inbound_increase' }, direction: { type: Sequelize.TINYINT, allowNull: false, comment: '出入方向:1入库 2出库(便于按方向筛选)' }, bill_type: { type: Sequelize.TINYINT, allowNull: false, comment: '单据类型:1入库单 2出库单(对齐 stock_items.bill_type)' }, bill_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '单据头ID(inbound_orders.id / outbound_orders.id)' }, bill_no: { type: Sequelize.STRING(50), allowNull: true, comment: '单据号(RK_{id} / CK_{id})' }, quantity: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '本次变动数量(恒为正数,方向见 direction / biz_type)' }, quantity_before: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动前总数量' }, quantity_after: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动后总数量' }, lock_quantity: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '本次锁定变动量(正=加锁 负=解锁)' }, lock_before: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动前锁定数量' }, lock_after: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动后锁定数量' }, available_before: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动前可用数量' }, available_after: { type: Sequelize.DECIMAL(15, 3), allowNull: false, defaultValue: 0, comment: '变动后可用数量' }, remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注(如推送失败释放、驳回回滚、WMS 回推确认等)' }, created_by: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '操作人ID(系统自动写入为空)' }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '记录时间(业务发生时间)', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), }, }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '出入库记录(库存流水)表' } ); await queryInterface.addIndex('stock_records', { fields: [ 'principal_id' ], name: 'idx_sr_principal' }); await queryInterface.addIndex('stock_records', { fields: [ 'product_id' ], name: 'idx_sr_product' }); await queryInterface.addIndex('stock_records', { fields: [ 'warehouse_id' ], name: 'idx_sr_warehouse' }); await queryInterface.addIndex('stock_records', { fields: [ 'bill_no' ], name: 'idx_sr_bill_no' }); await queryInterface.addIndex('stock_records', { fields: [ 'biz_type' ], name: 'idx_sr_biz_type' }); await queryInterface.addIndex('stock_records', { fields: [ 'created_at' ], name: 'idx_sr_created' }); }, async down(queryInterface) { await queryInterface.dropTable('stock_records'); }, };