oms/backend/app/model/orderDelivery.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

155 lines
9.5 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 表(独立于出库单 outbound_orders)
*
* 字段口径对齐 WMS `wms_order_delivery` 与奇门 `deliveryorder.create` 的
* request.deliveryOrder + receiverInfo(receiverInfo 在 WMS 侧扁平落主表,此处同样扁平):
* - delivery_order_code ↔ deliveryOrderCode(必填,去重键的一部分)
* - order_type ↔ orderType、operator_name ↔ operatorName
* - need_oms_waybill ↔ needOmsWaybill:OMS 走 WMS openapi,WMS 侧按通道自行写入创建渠道(API),OMS 不落该字段
* - createTime 不落库(WMS 显式忽略),本地用 created_at
* - sourceOrderCode 不单独落列,仅作 pay_no 缺省回退;多来源用 source_order_codes
* @param {Egg.Application} app egg application
* @return {object} orderDelivery model
*/
module.exports = app => {
const { BIGINT, STRING, TINYINT, DECIMAL, INTEGER, TEXT, JSON, ENUM, DATE } = app.Sequelize;
const OrderDelivery = app.model.define('order_delivery', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
// ---------- OMS 归属与键 ----------
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID(货主,对应 WMS store_id)' },
// 历史数据来源:orders.id(仅搬迁数据有值,正常新建为 NULL)
legacy_order_id: { type: BIGINT.UNSIGNED, comment: '历史数据来源:orders.id(非搬迁数据为 NULL)' },
warehouse_id: { type: BIGINT.UNSIGNED, comment: '仓库ID(关联 warehouses,对应 WMS warehouse_id)' },
shop_id: { type: BIGINT.UNSIGNED, comment: '店铺ID(关联 shops)' },
warehouse_code: { type: STRING(50), comment: '仓库编码(报文 warehouseCode 快照)' },
// ---------- 主键 / 类型 / 标记 ----------
delivery_order_code: { type: STRING(50), allowNull: false, comment: '发货单号(必填,唯一去重键)' },
order_type: {
type: STRING(50),
defaultValue: 'JYCK',
comment: '订单类型(ORDER 组 WMS 编码):JYCK 一般交易出库单 / HHCK 换货出库 / BFCK 补发出库,默认 JYCK',
},
order_flag: { type: TEXT, comment: '订单标记' },
pre_delivery_order_code: { type: STRING(50), comment: '预发货单号' },
pre_delivery_order_id: { type: STRING(50), comment: '预发货单ID' },
is_urgency: { type: ENUM('Y', 'N'), defaultValue: 'N', comment: '是否紧急:Y是 N否(默认 N)' },
invoice_flag: { type: ENUM('Y', 'N'), defaultValue: 'N', comment: '是否需要发票:Y是 N否(默认 N)' },
// ---------- 平台 / 店铺 ----------
source_platform_code: { type: STRING(50), comment: '来源平台编码' },
source_platform_name: { type: STRING(200), comment: '来源平台名称' },
shop_nick: { type: STRING(200), comment: '店铺名称' },
seller_nick: { type: STRING(200), comment: '卖家名称' },
buyer_nick: { type: STRING(200), comment: '买家名称' },
// ---------- 留言 / 备注 ----------
buyer_message: { type: STRING(500), comment: '买家留言(手工单不填写)' },
seller_message: { type: STRING(500), comment: '卖家留言(手工单表单「留言」写入此字段)' },
// ---------- 时间 ----------
place_order_time: { type: DATE, comment: '店铺创建订单时间' },
pay_time: { type: DATE, comment: '支付时间' },
pay_no: { type: STRING(50), comment: '支付单号(缺省回退取 sourceOrderCode)' },
operate_time: { type: DATE, comment: 'ERP 审核时间' },
// ---------- 金额 ----------
total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单总金额' },
item_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '商品总金额(Σ 明细 plan_qty × actual_price,明细增删改后自动重算)' },
discount_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单折扣金额' },
freight: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '快递费用' },
ar_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '应收金额' },
got_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '已收金额' },
// ---------- 快递 ----------
logistics_code: { type: STRING(50), comment: '物流公司编码(WMS 标准编码,如 SF/YTO)' },
logistics_name: { type: STRING(200), comment: '物流公司名称' },
express_code: { type: STRING(50), comment: '运单号(面单号,取首个包裹)' },
express_codes: { type: JSON, comment: '运单号集合(WMS 发货确认 packages.package[].expressCode,多包裹时为数组)' },
logistics_area_code: { type: STRING(50), comment: '快递区域编码(大头笔信息)' },
// ---------- 操作人 / 其他 ----------
operator_name: { type: STRING(50), comment: 'ERP 审核人(奇门 operatorName)' },
oaid_order_source_code: { type: STRING(50), comment: 'OAID 订单来源编码' },
source_order_codes: { type: TEXT, comment: '来源订单号集合(对齐 WMS 列名 source_order_codes)' },
extend_props: { type: JSON, comment: '请求级扩展属性(如 ewaybill_order_code)' },
// ---------- 渠道 / 取号 ----------
need_oms_waybill: {
type: TINYINT,
allowNull: false,
defaultValue: 0,
comment: '是否需要回 OMS 取号:0不需要 1需要(OMS 走 WMS openapi,渠道由 WMS 侧写入 API,该开关即生效)',
},
// ---------- 收件人(奇门 receiverInfo 扁平落库,列名与 WMS 一致) ----------
name: { type: STRING(100), comment: '收件人姓名(receiverInfo.name)' },
tel: { type: STRING(50), comment: '收件人电话(receiverInfo.tel)' },
mobile: { type: STRING(50), comment: '收件人移动电话(receiverInfo.mobile)' },
email: { type: STRING(100), comment: '收件人邮箱(receiverInfo.email)' },
country_code: { type: STRING(50), comment: '国家编码(receiverInfo.countryCode)' },
province: { type: STRING(100), comment: '省份(receiverInfo.province)' },
city: { type: STRING(100), comment: '城市(receiverInfo.city)' },
area: { type: STRING(100), comment: '区县(receiverInfo.area)' },
town: { type: STRING(100), comment: '街道/镇(receiverInfo.town)' },
detail_address: { type: STRING(500), comment: '详细地址(receiverInfo.detailAddress)' },
zip_code: { type: STRING(20), comment: '邮编(receiverInfo.zipCode)' },
oaid: { type: STRING(100), comment: 'OAID(receiverInfo.oaid)' },
// ---------- OMS 站内字段:统计 / 状态流转 / 推送记录 ----------
item_total: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '商品总数(明细计划数量合计)' },
item_kind: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '商品种类数(不重复商品数)' },
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1待处理 2已审核 3已推送 4已完成 5已作废 9已取消' },
stock_locked: {
type: TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '库存锁定标记:1=审核时已按明细锁定库存(推送失败/驳回/取消/作废时释放,发货确认时扣减),0=未锁定',
},
audit_by: { type: BIGINT.UNSIGNED, comment: '审核人ID(关联 users,待处理→已审核时写入)' },
audit_time: { type: DATE, comment: '审核时间' },
void_at: { type: DATE, comment: '作废时间(状态 5=已作废)' },
void_by: { type: BIGINT.UNSIGNED, comment: '作废人ID(关联 users)' },
void_reason: { type: STRING(255), comment: '作废原因' },
created_by: { type: BIGINT.UNSIGNED, comment: '创建人ID(关联 users)' },
pushed_at: { type: DATE, comment: 'WMS 推送时间' },
wms_push_result: { type: JSON, comment: 'WMS 推送结果(外部单号/返回码/消息/原始报文)' },
// ---------- 回传平台店铺(shops 表:淘宝/抖音等;与推送 WMS 无关) ----------
shop_sync_status: {
type: TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '回传平台店铺状态:0未同步 1同步中 2同步成功 3同步失败(口径见 app/lib/shopSyncStatus.js,链路待实现)',
},
shop_sync_retry_count: {
type: INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '回传平台店铺的重试次数(每次失败后 +1)',
},
created_at: { type: DATE, comment: '创建时间(对应 WMS create_at,取服务器时间)' },
updated_at: { type: DATE, comment: '更新时间' },
}, {
tableName: 'order_deliveries',
comment: '发货单(订单)表',
});
OrderDelivery.associate = () => {
app.model.OrderDelivery.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
app.model.OrderDelivery.belongsTo(app.model.Shop, { foreignKey: 'shop_id', as: 'shop' });
app.model.OrderDelivery.belongsTo(app.model.Warehouse, { foreignKey: 'warehouse_id', as: 'warehouse' });
app.model.OrderDelivery.belongsTo(app.model.User, { foreignKey: 'created_by', as: 'creator' });
app.model.OrderDelivery.belongsTo(app.model.User, { foreignKey: 'audit_by', as: 'auditor' });
app.model.OrderDelivery.belongsTo(app.model.User, { foreignKey: 'void_by', as: 'voider' });
app.model.OrderDelivery.hasMany(app.model.OrderDeliveryItem, { foreignKey: 'order_delivery_id', as: 'items' });
};
return OrderDelivery;
};