'use strict'; /** * wms_entry_order_receiving 绑定表增加 allocated_quantity(数量台账)字段。 * * 背景:入库单-收货单绑定后按入库单计划数量向收货单明细累加 stored_quantity, * 同一入库单绑定多个收货单时会重复全额分配(合计超出计划数量)。 * 修复方案 A:在绑定行上维护数量台账(收货明细ID → 已分配数量), * 分配时按 SKU 封顶 min(入库单计划数 − 其他绑定已分配量, 收货明细剩余可入量)。 * * 取值约定:JSON,可空。 * - NULL:迁移前的历史绑定行(未初始化台账,业务代码按“收货单侧已累计量”兜底重建); * - {}:新链路创建/解绑清零后的空台账; * - {"": }:明细级分配数量台账。 * * 幂等:列已存在则跳过。仅基表(无分表)。 */ const TABLE_NAME = 'wms_entry_order_receiving'; const COLUMN_NAME = 'allocated_quantity'; /** * 判断列是否已存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @param {string} columnName 列名。 * @return {Promise} 是否存在。 */ async function columnExists(queryInterface, tableName, columnName) { const described = await queryInterface.describeTable(tableName); return Boolean(described && described[columnName]); } module.exports = { /** * 执行迁移:为绑定表增加 allocated_quantity 台账列。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 构造器。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { if (await columnExists(queryInterface, TABLE_NAME, COLUMN_NAME)) { console.log(`[20260907160000-add-allocated-quantity-to-wms-entry-order-receiving] up skip: ${TABLE_NAME}.${COLUMN_NAME} exists`); return; } await queryInterface.addColumn(TABLE_NAME, COLUMN_NAME, { type: Sequelize.JSON, allowNull: true, defaultValue: null, comment: '数量台账:收货明细ID→已分配数量(JSON对象);null 表示旧数据未初始化', }); console.log(`[20260907160000-add-allocated-quantity-to-wms-entry-order-receiving] up added: ${TABLE_NAME}.${COLUMN_NAME}`); }, /** * 回滚迁移:移除台账列。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { if (!await columnExists(queryInterface, TABLE_NAME, COLUMN_NAME)) { console.log(`[20260907160000-add-allocated-quantity-to-wms-entry-order-receiving] down skip: ${TABLE_NAME}.${COLUMN_NAME} missing`); return; } await queryInterface.removeColumn(TABLE_NAME, COLUMN_NAME); console.log(`[20260907160000-add-allocated-quantity-to-wms-entry-order-receiving] down removed: ${TABLE_NAME}.${COLUMN_NAME}`); }, };