WMS_MAIN/app/model/entry_items.js
2026-04-09 01:36:49 +08:00

231 lines
5.6 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';
module.exports = app => {
const { STRING, INTEGER, BIGINT } = app.Sequelize;
const EntryItems = app.model.define('wms_entry_items', {
id: {
type: BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
comment: '入库明细ID',
},
order_id: {
type: BIGINT(20).UNSIGNED,
allowNull: false,
comment: '入库单ID',
references: {
model: 'wms_entry_order',
key: 'id',
},
},
out_biz_code: {
type: STRING(255),
allowNull: true,
comment: '外部业务编码',
},
order_line_no: {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '订单行号',
},
owner_code: {
type: STRING(50),
allowNull: true,
comment: '货主编码',
},
item_code: {
type: STRING(50),
allowNull: true,
comment: '商品编码',
},
item_id: {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '商品ID',
},
count: {
type: INTEGER(12),
allowNull: true,
comment: '计划入库数量',
validate: {
min: 0,
},
},
inventory_type: {
type: STRING(255),
allowNull: true,
comment: '库存类型',
},
stock_id: {
type: BIGINT(20),
allowNull: true,
comment: '商品SKU ID',
references: {
model: 'wms_stock_warehouse',
key: 'id',
},
},
received_quantity: {
type: INTEGER(12),
allowNull: false,
defaultValue: 0,
comment: '已收货数量',
validate: {
min: 0,
},
},
confirm_quantity: {
type: INTEGER(12),
allowNull: false,
defaultValue: 0,
comment: '已通知数量',
validate: {
min: 0,
},
},
}, {
tableName: 'wms_entry_items',
comment: '入库明细表',
timestamps: false,
indexes: [
{
name: 'order_id',
unique: true,
fields: [ 'order_id', 'order_line_no' ],
},
{
name: 'idx_stock_id',
fields: [ 'stock_id' ],
},
{
name: 'idx_item_code',
fields: [ 'item_code' ],
},
{
name: 'idx_order_id',
fields: [ 'order_id' ],
},
{
name: 'idx_received_quantity',
fields: [ 'received_quantity' ],
},
],
});
// 关联关系
EntryItems.associate = function() {
const { EntryItems, EntryOrder, StockWarehouse } = app.model;
// 关联入库单
EntryItems.belongsTo(EntryOrder, {
foreignKey: 'order_id',
as: 'order',
});
// 关联商品SKU
EntryItems.belongsTo(StockWarehouse, {
foreignKey: 'stock_id',
as: 'stock',
});
};
// 实例方法 - 验证收货数量
EntryItems.prototype.validateReceiveQuantity = function(quantity) {
if (quantity <= 0) {
throw new Error('收货数量必须大于0');
}
const currentReceived = this.received_quantity || 0;
const planQuantity = this.count || 0;
const newReceived = currentReceived + quantity;
if (newReceived > planQuantity) {
throw new Error(`收货数量超过计划数量:计划${planQuantity},已收${currentReceived},本次${quantity},合计${newReceived}`);
}
return true;
};
// 实例方法 - 更新收货数量(带验证)
EntryItems.prototype.updateReceivedQuantity = async function(quantity, options = {}) {
// 验证数量
this.validateReceiveQuantity(quantity);
// 更新数量
const newReceived = (this.received_quantity || 0) + quantity;
await this.update({
received_quantity: newReceived,
}, options);
return this;
};
// 实例方法 - 获取剩余可收货数量
EntryItems.prototype.getRemainingQuantity = function() {
const planQuantity = this.count || 0;
const receivedQuantity = this.received_quantity || 0;
return Math.max(0, planQuantity - receivedQuantity);
};
// 实例方法 - 是否已完成收货
EntryItems.prototype.isCompleted = function() {
const planQuantity = this.count || 0;
const receivedQuantity = this.received_quantity || 0;
return receivedQuantity >= planQuantity;
};
// 实例方法 - 获取收货进度
EntryItems.prototype.getProgress = function() {
const planQuantity = this.count || 0;
const receivedQuantity = this.received_quantity || 0;
if (planQuantity === 0) return 0;
return {
received: receivedQuantity,
plan: planQuantity,
remaining: Math.max(0, planQuantity - receivedQuantity),
percent: Math.min(100, Math.round((receivedQuantity / planQuantity) * 100)),
};
};
// 钩子函数 - 保存前验证
EntryItems.beforeSave(async instance => {
const received = instance.received_quantity || 0;
const plan = instance.count || 0;
if (received > plan) {
throw new Error(`已收货数量(${received})不能超过计划数量(${plan})`);
}
if (received < 0) {
throw new Error('已收货数量不能为负数');
}
if (plan < 0) {
throw new Error('计划数量不能为负数');
}
});
// 钩子函数 - 更新前验证
EntryItems.beforeUpdate(async instance => {
// 如果更新了received_quantity,进行验证
if (instance.changed('received_quantity')) {
const received = instance.received_quantity || 0;
const plan = instance.count || 0;
if (received > plan) {
throw new Error(`已收货数量(${received})不能超过计划数量(${plan})`);
}
if (received < 0) {
throw new Error('已收货数量不能为负数');
}
}
});
return EntryItems;
};