ming #1
@ -45,13 +45,23 @@ class stockoutServer extends MainService {
|
|||||||
const items = [];
|
const items = [];
|
||||||
for (let i = 0; i < orderLine.length; i++) {
|
for (let i = 0; i < orderLine.length; i++) {
|
||||||
const line = orderLine[i];
|
const line = orderLine[i];
|
||||||
const wmsStock = await app.model.StockStore.findByPk(line.itemId);
|
let wmsStock = await app.model.StockStore.findByPk(line.itemId);
|
||||||
// 校验 StockStore 记录的 store_id 与当前上下文一致,防止跨货主越权
|
// 校验 StockStore 记录的 store_id 与当前上下文一致,防止跨货主越权
|
||||||
if (wmsStock && Number(wmsStock.store_id) !== Number(this.storeId)) {
|
if (wmsStock && Number(wmsStock.store_id) !== Number(this.storeId)) {
|
||||||
errors.push(
|
wmsStock = null;
|
||||||
`SKU${line.itemId}商品《${line.itemName}》编码《${line.itemCode}》\n未同步推送到仓库`
|
}
|
||||||
);
|
// 外部渠道(奇门)itemId 可能是平台商品标识或条码而非 WMS 主键,
|
||||||
continue;
|
// 主键查不到或跨货主时降级按 item_code + 仓库货主维度查找
|
||||||
|
if (!wmsStock && line.itemCode) {
|
||||||
|
const stockWhere = {
|
||||||
|
item_code: line.itemCode,
|
||||||
|
warehouse_id: warehouseId,
|
||||||
|
store_id: storeId,
|
||||||
|
};
|
||||||
|
if (this.appKey) {
|
||||||
|
stockWhere.app_key = this.appKey;
|
||||||
|
}
|
||||||
|
wmsStock = await app.model.StockStore.findOne({ where: stockWhere });
|
||||||
}
|
}
|
||||||
if (!wmsStock) {
|
if (!wmsStock) {
|
||||||
errors.push(
|
errors.push(
|
||||||
|
|||||||
@ -1497,22 +1497,34 @@ class DeliveryOrderService extends Service {
|
|||||||
errors.push(`商品《${line.itemName}》编码《${line.itemCode}》\n库存归属货主(store_id=${stockWarehouseRecord.store_id})与当前货主(store_id=${storeId})不一致,无法创建订单`);
|
errors.push(`商品《${line.itemName}》编码《${line.itemCode}》\n库存归属货主(store_id=${stockWarehouseRecord.store_id})与当前货主(store_id=${storeId})不一致,无法创建订单`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if (!line.itemId) {
|
|
||||||
// 查找唯一键(appKey 为空时不作为条件,兼容内部手动建单场景)
|
|
||||||
const stockWhere = {
|
|
||||||
item_code: line.itemCode,
|
|
||||||
warehouse_id: warehouseId,
|
|
||||||
store_id: storeId,
|
|
||||||
};
|
|
||||||
if (appKey) {
|
|
||||||
stockWhere.app_key = appKey;
|
|
||||||
}
|
|
||||||
wms_stock = await app.model.StockStore.findOne({ where: stockWhere, transaction });
|
|
||||||
} else {
|
} else {
|
||||||
wms_stock = await app.model.StockStore.findByPk(line.itemId, { transaction });
|
// 查找唯一键(appKey 为空时不作为条件,兼容内部手动建单场景)
|
||||||
// 校验 StockStore 记录的 store_id 与当前上下文一致,防止跨货主越权
|
const findByItemCode = async () => {
|
||||||
if (wms_stock && Number(wms_stock.store_id) !== Number(storeId)) {
|
if (!line.itemCode) return null;
|
||||||
wms_stock = null;
|
const stockWhere = {
|
||||||
|
item_code: line.itemCode,
|
||||||
|
warehouse_id: warehouseId,
|
||||||
|
store_id: storeId,
|
||||||
|
};
|
||||||
|
if (appKey) {
|
||||||
|
stockWhere.app_key = appKey;
|
||||||
|
}
|
||||||
|
return await app.model.StockStore.findOne({ where: stockWhere, transaction });
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!line.itemId) {
|
||||||
|
wms_stock = await findByItemCode();
|
||||||
|
} else {
|
||||||
|
wms_stock = await app.model.StockStore.findByPk(line.itemId, { transaction });
|
||||||
|
// 校验 StockStore 记录的 store_id 与当前上下文一致,防止跨货主越权
|
||||||
|
if (wms_stock && Number(wms_stock.store_id) !== Number(storeId)) {
|
||||||
|
wms_stock = null;
|
||||||
|
}
|
||||||
|
// 外部渠道(奇门/开放平台)itemId 可能是平台商品标识或条码而非 WMS 主键,
|
||||||
|
// 主键查不到或跨货主时降级按 item_code 查找,避免误报“未同步推送到仓库”
|
||||||
|
if (!wms_stock) {
|
||||||
|
wms_stock = await findByItemCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -32,20 +32,29 @@ class entryOrderService extends Service {
|
|||||||
for (let i = 0; i < orderLine.length; i++) {
|
for (let i = 0; i < orderLine.length; i++) {
|
||||||
let wms_stock = null;
|
let wms_stock = null;
|
||||||
// ERP不处理WMS的外部商品编码
|
// ERP不处理WMS的外部商品编码
|
||||||
if (!orderLine[i].itemId) {
|
// 查找唯一键
|
||||||
// 查找唯一键
|
const findByItemCode = async () => {
|
||||||
wms_stock = await app.model.StockStore.findOne({ where: {
|
if (!orderLine[i].itemCode) return null;
|
||||||
|
return await app.model.StockStore.findOne({ where: {
|
||||||
item_code: orderLine[i].itemCode,
|
item_code: orderLine[i].itemCode,
|
||||||
app_key: appKey,
|
app_key: appKey,
|
||||||
warehouse_id: warehouseId,
|
warehouse_id: warehouseId,
|
||||||
store_id: storeId,
|
store_id: storeId,
|
||||||
}, transaction });
|
}, transaction });
|
||||||
|
};
|
||||||
|
if (!orderLine[i].itemId) {
|
||||||
|
wms_stock = await findByItemCode();
|
||||||
} else {
|
} else {
|
||||||
wms_stock = await app.model.StockStore.findByPk(orderLine[i].itemId, { transaction });
|
wms_stock = await app.model.StockStore.findByPk(orderLine[i].itemId, { transaction });
|
||||||
// 校验 StockStore 记录的 store_id 与当前上下文一致,防止跨货主越权
|
// 校验 StockStore 记录的 store_id 与当前上下文一致,防止跨货主越权
|
||||||
if (wms_stock && Number(wms_stock.store_id) !== Number(storeId)) {
|
if (wms_stock && Number(wms_stock.store_id) !== Number(storeId)) {
|
||||||
wms_stock = null;
|
wms_stock = null;
|
||||||
}
|
}
|
||||||
|
// 外部渠道 itemId 可能是平台商品标识或条码而非 WMS 主键,
|
||||||
|
// 主键查不到或跨货主时降级按 item_code 查找,避免误报“未同步推送到仓库”
|
||||||
|
if (!wms_stock) {
|
||||||
|
wms_stock = await findByItemCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!wms_stock) {
|
if (!wms_stock) {
|
||||||
errors.push(`SKU${orderLine[i].itemId}商品《${orderLine[i].itemName}》编码《${orderLine[i].itemCode}》\n未同步推送到仓库`);
|
errors.push(`SKU${orderLine[i].itemId}商品《${orderLine[i].itemName}》编码《${orderLine[i].itemCode}》\n未同步推送到仓库`);
|
||||||
|
|||||||
@ -0,0 +1,168 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const DeliveryOrderService = require('../../../../app/service/systems/delivery_order');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 addOrderLines 测试桩。
|
||||||
|
*
|
||||||
|
* @param {Object} options 可配置项。
|
||||||
|
* @param {Object} options.stockStoreRecords StockStore 桩返回值:{ byPk, byCode }。
|
||||||
|
* @return {Object} 服务实例与调用记录。
|
||||||
|
*/
|
||||||
|
function createHarness({ stockStoreRecords = {} } = {}) {
|
||||||
|
const findByPkCalls = [];
|
||||||
|
const findOneCalls = [];
|
||||||
|
const bulkCreateCalls = [];
|
||||||
|
|
||||||
|
const appLogger = {
|
||||||
|
debug() {},
|
||||||
|
info() {},
|
||||||
|
warn() {},
|
||||||
|
error() {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const app = {
|
||||||
|
logger: appLogger,
|
||||||
|
getCacheAsync: async () => null,
|
||||||
|
setCacheAsync: async () => {},
|
||||||
|
delCacheAsync: async () => {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const ctx = {
|
||||||
|
app,
|
||||||
|
logger: appLogger,
|
||||||
|
model: {
|
||||||
|
OrderItemsFactory: {
|
||||||
|
async getCurrentModel() {
|
||||||
|
return {
|
||||||
|
async bulkCreate(lineData) {
|
||||||
|
bulkCreateCalls.push(lineData);
|
||||||
|
return lineData;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
StockStore: {
|
||||||
|
async findByPk(itemId) {
|
||||||
|
findByPkCalls.push(String(itemId));
|
||||||
|
return stockStoreRecords.byPk || null;
|
||||||
|
},
|
||||||
|
async findOne({ where } = {}) {
|
||||||
|
findOneCalls.push(where);
|
||||||
|
return stockStoreRecords.byCode || null;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
StockWarehouse: {
|
||||||
|
async findAll() {
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: {
|
||||||
|
systems: {
|
||||||
|
stockStore: {
|
||||||
|
async getStoreWarehousePolicyByStoreAndWarehouse() {
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
async lockItemStorages() {
|
||||||
|
return { success: true };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
app.model = ctx.model;
|
||||||
|
const service = new DeliveryOrderService(ctx);
|
||||||
|
|
||||||
|
return { service, findByPkCalls, findOneCalls, bulkCreateCalls };
|
||||||
|
}
|
||||||
|
|
||||||
|
const WMS_STOCK = {
|
||||||
|
id: 1001076,
|
||||||
|
stock_id: 154103,
|
||||||
|
store_id: 1000186,
|
||||||
|
warehouse_id: 1000028,
|
||||||
|
item_code: '6934864204356',
|
||||||
|
item_name: '黑妹炭丝护龈牙刷',
|
||||||
|
goods_code: 'A.16.006.000001',
|
||||||
|
barcode: '6934864204356',
|
||||||
|
brand_name: null,
|
||||||
|
sku_property: null,
|
||||||
|
stock_unit: null,
|
||||||
|
category_id: 0,
|
||||||
|
category_name: null,
|
||||||
|
item_type: 'ZC',
|
||||||
|
retail_price: 0,
|
||||||
|
gross_weight: 0,
|
||||||
|
net_weight: 0,
|
||||||
|
length: 0,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
volume: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const INFO = { warehouseId: 1000028, appKey: '21226717', storeId: 1000186 };
|
||||||
|
const LINE = {
|
||||||
|
itemId: '6934864204356',
|
||||||
|
itemCode: '6934864204356',
|
||||||
|
itemName: '',
|
||||||
|
planQty: 12,
|
||||||
|
orderLineNo: '0',
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('test/app/service/systems/delivery_order.addOrderLines.fallback.test.js', () => {
|
||||||
|
it('itemId 非空但非 WMS 主键时,降级按 item_code 查到商品并成功创建明细', async () => {
|
||||||
|
const { service, findByPkCalls, findOneCalls, bulkCreateCalls } = createHarness({
|
||||||
|
stockStoreRecords: { byCode: WMS_STOCK },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await service.addOrderLines([ LINE ], 7788, INFO, {});
|
||||||
|
|
||||||
|
assert.equal(result.stock_count, 1);
|
||||||
|
assert.ok(result.items_str.includes('154103+12'), `items_str=${result.items_str}`);
|
||||||
|
assert.deepEqual(findByPkCalls, [ '6934864204356' ]);
|
||||||
|
assert.equal(findOneCalls.length, 1);
|
||||||
|
assert.equal(findOneCalls[0].item_code, '6934864204356');
|
||||||
|
assert.equal(findOneCalls[0].warehouse_id, 1000028);
|
||||||
|
assert.equal(findOneCalls[0].store_id, 1000186);
|
||||||
|
assert.equal(findOneCalls[0].app_key, '21226717');
|
||||||
|
assert.equal(bulkCreateCalls.length, 1);
|
||||||
|
assert.equal(bulkCreateCalls[0][0].item_id, 1001076);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('itemId 对应主键存在且同货主时,直接按主键命中,不做 item_code 降级查询', async () => {
|
||||||
|
const { service, findOneCalls } = createHarness({
|
||||||
|
stockStoreRecords: { byPk: WMS_STOCK },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await service.addOrderLines([ LINE ], 7788, INFO, {});
|
||||||
|
|
||||||
|
assert.equal(result.stock_count, 1);
|
||||||
|
assert.equal(findOneCalls.length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('主键记录跨货主且 item_code 也查不到时,报未同步推送到仓库', async () => {
|
||||||
|
const { service } = createHarness({
|
||||||
|
stockStoreRecords: { byPk: { ...WMS_STOCK, store_id: 999999 } },
|
||||||
|
});
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
() => service.addOrderLines([ LINE ], 7788, INFO, {}),
|
||||||
|
/未同步推送到仓库/
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('itemId 为空时,仅按 item_code 查询(既有行为回归)', async () => {
|
||||||
|
const { service, findByPkCalls, findOneCalls } = createHarness({
|
||||||
|
stockStoreRecords: { byCode: WMS_STOCK },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await service.addOrderLines([{ ...LINE, itemId: '' }], 7788, INFO, {});
|
||||||
|
|
||||||
|
assert.equal(result.stock_count, 1);
|
||||||
|
assert.equal(findByPkCalls.length, 0);
|
||||||
|
assert.equal(findOneCalls.length, 1);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user