299 lines
7.8 KiB
JavaScript
299 lines
7.8 KiB
JavaScript
'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.batchUpdateReceived = async function(app, updates, options = {}) {
|
||
const transaction = options.transaction || await app.model.transaction();
|
||
|
||
try {
|
||
// 先检查所有更新是否合法
|
||
for (const update of updates) {
|
||
const { id, quantity } = update;
|
||
const item = await app.model.EntryItems.findByPk(id, { transaction });
|
||
|
||
if (!item) {
|
||
throw new Error(`入库明细 ${id} 不存在`);
|
||
}
|
||
|
||
// 验证数量
|
||
item.validateReceiveQuantity(quantity);
|
||
}
|
||
|
||
// 执行更新
|
||
for (const update of updates) {
|
||
const { id, quantity } = update;
|
||
const item = await app.model.EntryItems.findByPk(id, { transaction });
|
||
|
||
const newReceived = (item.received_quantity || 0) + quantity;
|
||
await item.update({
|
||
received_quantity: newReceived,
|
||
}, { transaction });
|
||
}
|
||
|
||
if (!options.transaction) {
|
||
await transaction.commit();
|
||
}
|
||
|
||
return { success: true };
|
||
} catch (error) {
|
||
if (!options.transaction) {
|
||
await transaction.rollback();
|
||
}
|
||
throw error;
|
||
}
|
||
};
|
||
|
||
// 静态方法 - 获取入库单总进度
|
||
EntryItems.getOrderProgress = async function(app, orderId) {
|
||
const items = await app.model.EntryItems.findAll({
|
||
where: { order_id: orderId },
|
||
attributes: [
|
||
[ app.Sequelize.fn('SUM', app.Sequelize.col('count')), 'total_plan' ],
|
||
[ app.Sequelize.fn('SUM', app.Sequelize.col('received_quantity')), 'total_received' ],
|
||
[ app.Sequelize.fn('COUNT', app.Sequelize.col('id')), 'item_count' ],
|
||
],
|
||
raw: true,
|
||
});
|
||
|
||
const result = items[0] || {};
|
||
const totalPlan = parseInt(result.total_plan) || 0;
|
||
const totalReceived = parseInt(result.total_received) || 0;
|
||
|
||
return {
|
||
total_plan: totalPlan,
|
||
total_received: totalReceived,
|
||
total_remaining: Math.max(0, totalPlan - totalReceived),
|
||
item_count: parseInt(result.item_count) || 0,
|
||
progress_percent: totalPlan > 0 ? Math.min(100, Math.round((totalReceived / totalPlan) * 100)) : 0,
|
||
is_completed: totalReceived >= totalPlan,
|
||
};
|
||
};
|
||
|
||
// 钩子函数 - 保存前验证
|
||
EntryItems.beforeSave(async (instance, options) => {
|
||
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, options) => {
|
||
// 如果更新了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;
|
||
};
|