WMS_MAIN/database/migrations/20260907160000-add-allocated-quantity-to-wms-entry-order-receiving.js
liufumingren 86222a24eb feat: COMPLETED 入库单绑定收货单与防超扣台账链路
- 入库单 COMPLETED 状态放开绑定收货单,事务内按 SKU 同步收货单明细入库进度(stored_quantity/storage_status)

- wms_entry_order_receiving 新增 allocated_quantity 台账列(迁移已在生产执行),多收货单绑定按剩余可分配量封顶防超扣

- 统一锁序(入库单主行→绑定行→收货明细行)修复并发绑定死锁;解绑/作废按台账回滚,不再整体清零共享收货单

- 同步 entry_items.received_quantity 口径,与绑定收货单 stored_quantity 可对账

- 同步 methods 文档与变更记录、AI_METHOD_ROUTING 方法清单
2026-09-07 22:16:54 +08:00

73 lines
3.0 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';
/**
* wms_entry_order_receiving 绑定表增加 allocated_quantity(数量台账)字段。
*
* 背景:入库单-收货单绑定后按入库单计划数量向收货单明细累加 stored_quantity,
* 同一入库单绑定多个收货单时会重复全额分配(合计超出计划数量)。
* 修复方案 A:在绑定行上维护数量台账(收货明细ID → 已分配数量),
* 分配时按 SKU 封顶 min(入库单计划数 − 其他绑定已分配量, 收货明细剩余可入量)。
*
* 取值约定:JSON,可空。
* - NULL:迁移前的历史绑定行(未初始化台账,业务代码按“收货单侧已累计量”兜底重建);
* - {}:新链路创建/解绑清零后的空台账;
* - {"<receiving_detail_id>": <qty>}:明细级分配数量台账。
*
* 幂等:列已存在则跳过。仅基表(无分表)。
*/
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<boolean>} 是否存在。
*/
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<void>} 无返回值。
*/
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<void>} 无返回值。
*/
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}`);
},
};