oms/backend/database/migrations/20260915001100-create-stock-records.js
liufumingren 39c19a4de7 feat(backend): 出入库记录与库存锁定链路、出库单快递/收件人与推送队列
- 出入库记录(库存流水):新增 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 为本地生成、不入库)
2026-09-14 00:59:23 +08:00

94 lines
5.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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