oms/backend/database/migrations/20260915001300-order-deliveries-stock-locked.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

40 lines
1.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';
/**
* 发货单(订单)库存锁定标记:order_deliveries.stock_locked
*
* 背景:发货单推送到 WMS 后,WMS 侧 deliveryorder.create 会对库存做预占;OMS 侧原先完全不碰库存,
* 导致「订单创建/审核 → 推送 → 发货确认」这条链路没有库存流水。现按以下口径补齐(与出库单同源,
* 库存动作统一走 app/utils/stockRecord.js#applyStockChange,与 stock_records 同事务):
* - 审核(1→2):按明细锁定库存(product_stocks.lock_quantity 增加)并写流水 biz_type=order_lock
* - 推送失败退回待审核 / 驳回(2→1) / 取消(1|2|3→9) / 作废(2→5):释放锁定并写流水 biz_type=order_unlock
* - WMS 发货确认(deliveryorder.confirm, 3→4):扣减库存(总数量与锁定同减)并写流水 biz_type=order_deduct
*
* stock_locked 作为幂等与兼容依据:
* - 1 = 该单当前占用着锁定库存(审核成功后置 1)
* - 0 = 未锁定 / 已释放 / 已扣减
* 历史单据(本迁移之前已审核/已推送)落 0 → 发货确认时跳过扣减,避免「没有锁定却扣总数量」。
*
* 幂等:迁移可重复执行(先 describeTable 判断列是否存在)。
*/
module.exports = {
async up(queryInterface, Sequelize) {
const columns = await queryInterface.describeTable('order_deliveries');
if (columns.stock_locked) return;
await queryInterface.addColumn('order_deliveries', 'stock_locked', {
type: Sequelize.TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '库存锁定标记:1=审核时已锁定库存(推送失败/驳回/取消/作废释放,发货确认扣减),0=未锁定',
after: 'status',
});
},
async down(queryInterface) {
const columns = await queryInterface.describeTable('order_deliveries');
if (!columns.stock_locked) return;
await queryInterface.removeColumn('order_deliveries', 'stock_locked');
},
};