512 lines
12 KiB
JavaScript
512 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = app => {
|
|
const { STRING, INTEGER, BIGINT, ENUM, TEXT, TINYINT, DATE } = app.Sequelize;
|
|
|
|
const EntryOrder = app.model.define('wms_entry_order', {
|
|
id: {
|
|
type: BIGINT(20).UNSIGNED,
|
|
primaryKey: true,
|
|
autoIncrement: true,
|
|
comment: '入库单ID',
|
|
},
|
|
entry_order_code: {
|
|
type: STRING(50),
|
|
allowNull: false,
|
|
comment: '入库单号',
|
|
validate: {
|
|
notEmpty: true,
|
|
},
|
|
},
|
|
order_type: {
|
|
type: ENUM('SCRK', 'LYRK', 'CCRK', 'CGRK', 'DBRK', 'QTRK', 'B2BRK', 'XNRK'),
|
|
allowNull: false,
|
|
comment: '业务类型',
|
|
},
|
|
purchase_order_code: {
|
|
type: STRING(50),
|
|
allowNull: true,
|
|
comment: '采购单号',
|
|
},
|
|
total_order_lines: {
|
|
type: INTEGER(11),
|
|
allowNull: false,
|
|
defaultValue: 0,
|
|
comment: '单据总行数',
|
|
},
|
|
order_create_time: {
|
|
type: DATE,
|
|
allowNull: true,
|
|
comment: '订单创建时间',
|
|
},
|
|
expect_start_time: {
|
|
type: DATE,
|
|
allowNull: true,
|
|
comment: '预期到货时间',
|
|
},
|
|
expect_end_time: {
|
|
type: DATE,
|
|
allowNull: true,
|
|
comment: '最迟预期到货时间',
|
|
},
|
|
logistics_code: {
|
|
type: STRING(50),
|
|
allowNull: true,
|
|
comment: '物流公司编码',
|
|
},
|
|
logistics_name: {
|
|
type: STRING(200),
|
|
allowNull: true,
|
|
comment: '物流公司名称',
|
|
},
|
|
express_code: {
|
|
type: STRING(50),
|
|
allowNull: true,
|
|
comment: '运单号',
|
|
},
|
|
supplier_code: {
|
|
type: STRING(50),
|
|
allowNull: true,
|
|
comment: '供应商编码',
|
|
},
|
|
supplier_name: {
|
|
type: STRING(200),
|
|
allowNull: true,
|
|
comment: '供应商名称',
|
|
},
|
|
source_type: {
|
|
type: ENUM('WMS', 'API', 'QM'),
|
|
allowNull: false,
|
|
defaultValue: 'API',
|
|
comment: '来源类型',
|
|
},
|
|
order_status: {
|
|
type: ENUM('PENDING', 'CONFIRMED', 'PARTIAL_RECEIVED', 'COMPLETED', 'CANCELLED', 'CREATED', 'CREATING'),
|
|
allowNull: false,
|
|
defaultValue: 'PENDING',
|
|
comment: '订单状态',
|
|
},
|
|
sync_status: {
|
|
type: ENUM('PENDING', 'SUCCESS', 'FAILED'),
|
|
allowNull: false,
|
|
defaultValue: 'PENDING',
|
|
comment: '同步状态',
|
|
},
|
|
sync_message: {
|
|
type: TEXT,
|
|
allowNull: true,
|
|
comment: '同步返回信息',
|
|
},
|
|
is_enabled: {
|
|
type: TINYINT(1),
|
|
allowNull: false,
|
|
defaultValue: 1,
|
|
comment: '是否启用',
|
|
},
|
|
created_at: {
|
|
type: DATE,
|
|
allowNull: true,
|
|
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
|
|
comment: '创建时间',
|
|
},
|
|
updated_at: {
|
|
type: DATE,
|
|
allowNull: true,
|
|
comment: '更新时间',
|
|
},
|
|
warehouse_id: {
|
|
type: BIGINT(20).UNSIGNED,
|
|
allowNull: false,
|
|
comment: '仓库ID',
|
|
references: {
|
|
model: 'wms_store',
|
|
key: 'id',
|
|
},
|
|
},
|
|
store_id: {
|
|
type: BIGINT(20).UNSIGNED,
|
|
allowNull: false,
|
|
comment: '货主ID',
|
|
references: {
|
|
model: 'wms_store',
|
|
key: 'id',
|
|
},
|
|
},
|
|
remark: {
|
|
type: STRING(500),
|
|
allowNull: true,
|
|
comment: 'ERP备注',
|
|
},
|
|
store_name: {
|
|
type: STRING(200),
|
|
allowNull: true,
|
|
comment: '货主名称快照',
|
|
},
|
|
warehouse_name: {
|
|
type: STRING(200),
|
|
allowNull: true,
|
|
comment: '仓库名称快照',
|
|
},
|
|
deleted_at: {
|
|
type: DATE,
|
|
allowNull: true,
|
|
comment: '删除时间',
|
|
},
|
|
qimen_owner_code: {
|
|
type: STRING(100),
|
|
allowNull: true,
|
|
comment: '奇门货主code',
|
|
},
|
|
qimen_warehouse_code: {
|
|
type: STRING(100),
|
|
allowNull: true,
|
|
comment: '奇门仓库code',
|
|
},
|
|
}, {
|
|
tableName: 'wms_entry_order',
|
|
comment: '入库单主表',
|
|
timestamps: false,
|
|
indexes: [
|
|
{
|
|
name: 'uk_entry_order_code',
|
|
unique: true,
|
|
fields: [ 'entry_order_code', 'warehouse_id', 'store_id' ],
|
|
},
|
|
{
|
|
name: 'idx_order_type',
|
|
fields: [ 'order_type' ],
|
|
},
|
|
{
|
|
name: 'idx_order_status',
|
|
fields: [ 'order_status' ],
|
|
},
|
|
{
|
|
name: 'idx_sync_status',
|
|
fields: [ 'sync_status' ],
|
|
},
|
|
{
|
|
name: 'idx_source',
|
|
fields: [ 'source_type' ],
|
|
},
|
|
{
|
|
name: 'idx_create_time',
|
|
fields: [ 'order_create_time' ],
|
|
},
|
|
{
|
|
name: 'idx_warehouse_store',
|
|
fields: [ 'warehouse_id', 'store_id' ],
|
|
},
|
|
{
|
|
name: 'idx_is_enabled',
|
|
fields: [ 'is_enabled' ],
|
|
},
|
|
{
|
|
name: 'idx_deleted_at',
|
|
fields: [ 'deleted_at' ],
|
|
},
|
|
],
|
|
});
|
|
|
|
// 关联关系
|
|
EntryOrder.associate = function() {
|
|
const { EntryOrder, Store, EntryItems } = app.model;
|
|
|
|
// 关联仓库
|
|
EntryOrder.belongsTo(Store, {
|
|
foreignKey: 'warehouse_id',
|
|
as: 'warehouse',
|
|
});
|
|
|
|
// 关联货主
|
|
EntryOrder.belongsTo(Store, {
|
|
foreignKey: 'store_id',
|
|
as: 'store',
|
|
});
|
|
|
|
// 关联入库明细
|
|
EntryOrder.hasMany(EntryItems, {
|
|
foreignKey: 'order_id',
|
|
as: 'items',
|
|
});
|
|
};
|
|
|
|
// 实例方法
|
|
EntryOrder.prototype.isPending = function() {
|
|
return this.order_status === 'PENDING';
|
|
};
|
|
|
|
EntryOrder.prototype.isConfirmed = function() {
|
|
return this.order_status === 'CONFIRMED';
|
|
};
|
|
|
|
EntryOrder.prototype.isCompleted = function() {
|
|
return this.order_status === 'COMPLETED';
|
|
};
|
|
|
|
EntryOrder.prototype.isCancelled = function() {
|
|
return this.order_status === 'CANCELLED';
|
|
};
|
|
|
|
EntryOrder.prototype.isPartialReceived = function() {
|
|
return this.order_status === 'PARTIAL_RECEIVED';
|
|
};
|
|
|
|
EntryOrder.prototype.canConfirm = function() {
|
|
return [ 'PENDING', 'CREATED' ].includes(this.order_status);
|
|
};
|
|
|
|
EntryOrder.prototype.canCancel = function() {
|
|
return [ 'PENDING', 'CONFIRMED', 'CREATED' ].includes(this.order_status);
|
|
};
|
|
|
|
EntryOrder.prototype.canComplete = function() {
|
|
return [ 'CONFIRMED', 'PARTIAL_RECEIVED' ].includes(this.order_status);
|
|
};
|
|
|
|
EntryOrder.prototype.isSyncSuccess = function() {
|
|
return this.sync_status === 'SUCCESS';
|
|
};
|
|
|
|
EntryOrder.prototype.isSyncFailed = function() {
|
|
return this.sync_status === 'FAILED';
|
|
};
|
|
|
|
EntryOrder.prototype.isEnabled = function() {
|
|
return this.is_enabled === 1 && !this.deleted_at;
|
|
};
|
|
|
|
// 静态方法
|
|
EntryOrder.findByOrderCode = async function(app, entryOrderCode, warehouseId, storeId) {
|
|
return await app.model.EntryOrder.findOne({
|
|
where: {
|
|
entry_order_code: entryOrderCode,
|
|
warehouse_id: warehouseId,
|
|
store_id: storeId,
|
|
is_enabled: 1,
|
|
deleted_at: null,
|
|
},
|
|
include: [
|
|
{
|
|
model: app.model.Store,
|
|
as: 'warehouse',
|
|
attributes: [ 'store_name', 'store_code' ],
|
|
},
|
|
{
|
|
model: app.model.Store,
|
|
as: 'store',
|
|
attributes: [ 'store_name' ],
|
|
},
|
|
{
|
|
model: app.model.EntryItems,
|
|
as: 'items',
|
|
include: [{
|
|
model: app.model.StockWarehouse,
|
|
as: 'stock',
|
|
attributes: [ 'product_code', 'product_name' ],
|
|
}],
|
|
},
|
|
],
|
|
});
|
|
};
|
|
|
|
EntryOrder.findByStatus = async function(app, status, params = {}) {
|
|
const { warehouse_id, store_id, start_date, end_date, page = 1, pageSize = 20 } = params;
|
|
|
|
const where = {
|
|
order_status: status,
|
|
is_enabled: 1,
|
|
deleted_at: null,
|
|
};
|
|
|
|
if (warehouse_id) {
|
|
where.warehouse_id = warehouse_id;
|
|
}
|
|
|
|
if (store_id) {
|
|
where.store_id = store_id;
|
|
}
|
|
|
|
if (start_date || end_date) {
|
|
where.created_at = {};
|
|
if (start_date) {
|
|
where.created_at[app.Sequelize.Op.gte] = start_date;
|
|
}
|
|
if (end_date) {
|
|
where.created_at[app.Sequelize.Op.lte] = end_date;
|
|
}
|
|
}
|
|
|
|
const { count, rows } = await app.model.EntryOrder.findAndCountAll({
|
|
where,
|
|
include: [
|
|
{
|
|
model: app.model.Store,
|
|
as: 'warehouse',
|
|
attributes: [ 'store_name' ],
|
|
},
|
|
{
|
|
model: app.model.Store,
|
|
as: 'store',
|
|
attributes: [ 'store_name' ],
|
|
},
|
|
],
|
|
order: [[ 'created_at', 'DESC' ]],
|
|
offset: (page - 1) * pageSize,
|
|
limit: parseInt(pageSize),
|
|
distinct: true,
|
|
});
|
|
|
|
return {
|
|
list: rows,
|
|
pagination: {
|
|
page: parseInt(page),
|
|
pageSize: parseInt(pageSize),
|
|
total: count,
|
|
totalPages: Math.ceil(count / pageSize),
|
|
},
|
|
};
|
|
};
|
|
|
|
EntryOrder.getStatsByWarehouse = async function(app, warehouseId, startDate, endDate) {
|
|
const where = {
|
|
warehouse_id: warehouseId,
|
|
is_enabled: 1,
|
|
deleted_at: null,
|
|
};
|
|
|
|
if (startDate || endDate) {
|
|
where.created_at = {};
|
|
if (startDate) {
|
|
where.created_at[app.Sequelize.Op.gte] = startDate;
|
|
}
|
|
if (endDate) {
|
|
where.created_at[app.Sequelize.Op.lte] = endDate;
|
|
}
|
|
}
|
|
|
|
const result = await app.model.EntryOrder.findAll({
|
|
attributes: [
|
|
'order_status',
|
|
[ app.Sequelize.fn('COUNT', app.Sequelize.col('id')), 'count' ],
|
|
[ app.Sequelize.fn('SUM', app.Sequelize.col('total_order_lines')), 'total_lines' ],
|
|
],
|
|
where,
|
|
group: [ 'order_status' ],
|
|
raw: true,
|
|
});
|
|
|
|
const stats = {
|
|
pending: 0,
|
|
confirmed: 0,
|
|
partial_received: 0,
|
|
completed: 0,
|
|
cancelled: 0,
|
|
total: 0,
|
|
total_lines: 0,
|
|
};
|
|
|
|
result.forEach(item => {
|
|
const status = item.order_status.toLowerCase();
|
|
if (stats[status] !== undefined) {
|
|
stats[status] = parseInt(item.count) || 0;
|
|
}
|
|
stats.total += parseInt(item.count) || 0;
|
|
stats.total_lines += parseInt(item.total_lines) || 0;
|
|
});
|
|
|
|
return stats;
|
|
};
|
|
|
|
EntryOrder.updateSyncStatus = async function(app, orderId, status, message = null) {
|
|
const order = await app.model.EntryOrder.findByPk(orderId);
|
|
|
|
if (!order) {
|
|
throw new Error('入库单不存在');
|
|
}
|
|
|
|
const updateData = {
|
|
sync_status: status,
|
|
};
|
|
|
|
if (message !== null) {
|
|
updateData.sync_message = message;
|
|
}
|
|
|
|
return await order.update(updateData);
|
|
};
|
|
|
|
EntryOrder.updateOrderStatus = async function(app, orderId, status) {
|
|
const order = await app.model.EntryOrder.findByPk(orderId);
|
|
|
|
if (!order) {
|
|
throw new Error('入库单不存在');
|
|
}
|
|
|
|
const validTransitions = {
|
|
PENDING: [ 'CONFIRMED', 'CANCELLED' ],
|
|
CONFIRMED: [ 'PARTIAL_RECEIVED', 'COMPLETED', 'CANCELLED' ],
|
|
PARTIAL_RECEIVED: [ 'COMPLETED', 'CANCELLED' ],
|
|
CREATED: [ 'PENDING', 'CANCELLED' ],
|
|
CREATING: [ 'PENDING', 'CANCELLED' ],
|
|
};
|
|
|
|
const allowedTransitions = validTransitions[order.order_status];
|
|
if (!allowedTransitions || !allowedTransitions.includes(status)) {
|
|
throw new Error(`状态从${order.order_status}转换到${status}不被允许`);
|
|
}
|
|
|
|
return await order.update({ order_status: status });
|
|
};
|
|
|
|
EntryOrder.calculateReceivedQuantities = async function(app, orderId) {
|
|
// 这里需要根据实际业务逻辑计算已收货数量
|
|
// 通常需要查询入库明细和实际收货记录
|
|
|
|
// 示例:查询入库明细
|
|
const items = await app.model.EntryItems.findAll({
|
|
where: { order_id: orderId },
|
|
attributes: [
|
|
'id',
|
|
'count',
|
|
[ app.Sequelize.literal('(SELECT SUM(quantity) FROM wms_storage WHERE inbound_order_no = wms_entry_order.entry_order_code AND item_id = wms_entry_items.item_id)'), 'received' ],
|
|
],
|
|
include: [{
|
|
model: app.model.EntryOrder,
|
|
as: 'order',
|
|
attributes: [ 'entry_order_code' ],
|
|
}],
|
|
});
|
|
|
|
let totalExpected = 0;
|
|
let totalReceived = 0;
|
|
let allReceived = true;
|
|
let anyReceived = false;
|
|
|
|
items.forEach(item => {
|
|
const expected = item.count || 0;
|
|
const received = item.received || 0;
|
|
|
|
totalExpected += expected;
|
|
totalReceived += received;
|
|
|
|
if (expected > 0 && received < expected) {
|
|
allReceived = false;
|
|
}
|
|
|
|
if (received > 0) {
|
|
anyReceived = true;
|
|
}
|
|
});
|
|
|
|
return {
|
|
total_expected: totalExpected,
|
|
total_received: totalReceived,
|
|
all_received: allReceived,
|
|
any_received: anyReceived,
|
|
percentage: totalExpected > 0 ? Math.round((totalReceived / totalExpected) * 100) : 0,
|
|
};
|
|
};
|
|
|
|
return EntryOrder;
|
|
};
|