166 lines
14 KiB
JavaScript
166 lines
14 KiB
JavaScript
import { run as createOrder } from './orders.mjs'
|
|
import { run as measure } from './inbound-measurement.mjs'
|
|
import { run as createCustomer } from './merchant-qualification-fixture.mjs'
|
|
|
|
const MODULE = 'dropship-mixed-palletization'
|
|
const BASE = '/api/platform/v1/first-leg/pda-dropship'
|
|
const OPTIONS = { scenario: MODULE, deploymentBlocker: true }
|
|
|
|
// 正式下单和测量产生两客户三票,再用 PDA 公网接口逐箱混托并保留全部证据。
|
|
export async function run(context) {
|
|
if (context.options?.fixedCustomerId) throw new Error('混托需要第二个固定客户,停止下单和测量造数')
|
|
const originalMarker = context.marker
|
|
const savedContext = { ...context.context }
|
|
const originalManualCount = context.options.prepareDropshipManualCount
|
|
const records = []
|
|
let secondCustomer = null
|
|
const result = { marker: originalMarker, stage: 'PREPARING', records, fullFlowVerified: false }
|
|
context.createdResources.dropshipMixedPalletization = result
|
|
function check(name, response, passed, details = '') {
|
|
context.assert(MODULE, name, response, passed, details, OPTIONS)
|
|
if (!passed) throw new Error(name)
|
|
}
|
|
async function command(name, method, path, body, headers = {}) {
|
|
const response = await context.request(method, path, body, undefined, headers)
|
|
const value = context.apiSuccess(MODULE, name, response, '', OPTIONS)
|
|
if (String(response?.body?.code) !== '200') throw new Error(`${name}: ${response?.body?.code}: ${response?.body?.message}`)
|
|
return value
|
|
}
|
|
async function rejected(name, method, path, body) {
|
|
const response = await context.request(method, path, body)
|
|
check(name, response, response.http === 200 && ['A0404', 'A0409'].includes(String(response.body?.code)), response.body?.message)
|
|
}
|
|
async function readTask(record) {
|
|
return command(`${record.label}回查任务`, 'GET', `${BASE}/tasks/${record.source.taskId}?warehouseId=${record.source.warehouseId}`)
|
|
}
|
|
try {
|
|
context.context.orderFixtureOnly = true
|
|
context.context.allowAnyExistingPallet = true
|
|
check('存在本轮已审核客户', null, Boolean(context.context.merchantQualificationFixture?.tenantId))
|
|
for (const [index, count] of [2, 3, 1].entries()) {
|
|
const label = ['A-2BOX', 'B-3BOX', 'B-1BOX'][index]
|
|
context.marker = `${originalMarker}-${label}`
|
|
if (index === 1) { await createCustomer(context, { publishEnvironment: false }); secondCustomer = context.context.merchantQualificationFixture }
|
|
for (const key of ['orders', 'inboundMeasurement', 'dropshipManualCount']) delete context.createdResources[key]
|
|
for (const key of ['inboundWeightOcr', 'ocrInbound', 'testInbound', 'createdInbound']) delete context.context[key]
|
|
context.context.orderDestinationType = 'OVERSEAS'
|
|
context.context.orderFulfillmentType = 'ONE_CLICK_DROPSHIP'
|
|
context.context.orderProfile = { taxMode: 'BUY_DECLARATION', pickupMethod: 'SELF_EXPRESS', containerMode: 'LCL' }
|
|
context.context.waitSplitVariant = { skuCount: 2, packageCountPerSku: count, packageCounts: [count, count],
|
|
totalPackageCount: count, palletCount: 1, skuCodes: [`MIX-SAME-${originalMarker}`.slice(-60), `MIX-SECOND-${originalMarker}`.slice(-60)] }
|
|
if (records.length) context.context.waitSplitRoute = { originWarehouseName: records[0].order.receivingWarehouseName,
|
|
channelCode: records[0].order.channelCode, destinationIndex: 0 }
|
|
context.options.prepareDropshipManualCount = true
|
|
const start = context.steps.length
|
|
await createOrder(context)
|
|
await measure(context)
|
|
const source = context.createdResources.dropshipManualCount
|
|
check(`${label}真实下单审核测量全部通过`, null, Boolean(source?.taskId)
|
|
&& !context.steps.slice(start).some((step) => !step.passed))
|
|
records.push({ label, order: { ...context.createdResources.orders }, measurement: { ...context.createdResources.inboundMeasurement }, source: { ...source } })
|
|
check(`${label}实测物理箱数正确`, null, source.totalCartons === count)
|
|
}
|
|
check('两客户三票保持独立归属', null, records.length === 3 && new Set(records.map((r) => r.order.tenantId)).size === 2)
|
|
check('三票同仓同渠道同目的仓', null, records.every((r) => r.source.warehouseId === records[0].source.warehouseId
|
|
&& r.order.channelCode === records[0].order.channelCode && r.order.destinationWarehouseId === records[0].order.destinationWarehouseId))
|
|
context.marker = originalMarker
|
|
const warehouseId = records[0].source.warehouseId
|
|
const query = `warehouseId=${warehouseId}`
|
|
const generated = await command('读取第一票预生成托盘', 'GET', `${BASE}/inbound-orders/${records[0].source.inboundId}/pallets?accountId=${records[0].order.tenantId}`)
|
|
const palletNo = generated?.[0]?.palletNo
|
|
check('取得空的预生成DP托盘', null, Boolean(palletNo) && generated[0].status === 'OPEN' && generated[0].cartonCount === 0)
|
|
result.palletNo = palletNo; result.warehouseId = warehouseId
|
|
const palletPath = `${BASE}/pallets/resolve?${query}&palletNo=${encodeURIComponent(palletNo)}`
|
|
const initial = await command('先扫空托盘只读识别', 'GET', palletPath)
|
|
check('空托盘不会虚构客户组成', null, initial.pallet.cartonCount === 0 && initial.members.length === 0)
|
|
await rejected('无效托盘码被拒绝', 'GET', `${BASE}/pallets/resolve?${query}&palletNo=DP-000000-0000`)
|
|
await rejected('跨仓托盘码被拒绝', 'GET', `${BASE}/pallets/resolve?warehouseId=1&palletNo=${encodeURIComponent(palletNo)}`)
|
|
await rejected('无效箱码被拒绝', 'GET', `${BASE}/cartons/resolve?${query}&boxCode=NO-SUCH-${originalMarker}`)
|
|
result.stage = 'COUNTING'
|
|
const counted = []
|
|
// 交替扫描客户 A/B,证明页面无需先手工切换入仓单。
|
|
const scans = records.flatMap((record) => Array.from({ length: record.source.totalCartons }, (_, index) => ({ record, boxCode: `${record.source.inboundCode}#${record.source.totalCartons}-${index + 1}` })))
|
|
scans.sort((a, b) => Number(a.boxCode.split('-').at(-1)) - Number(b.boxCode.split('-').at(-1)))
|
|
for (const [index, { record, boxCode }] of scans.entries()) {
|
|
const resolved = await command('跨客户只读扫码预览', 'GET', `${BASE}/cartons/resolve?${query}&boxCode=${encodeURIComponent(boxCode)}`)
|
|
check('箱码返回原客户和原入仓任务', null, String(resolved.task.taskId) === record.source.taskId
|
|
&& String(resolved.task.tenantId) === record.order.tenantId && resolved.carton.skuLines.length === 2)
|
|
const before = await readTask(record)
|
|
check('读取箱码未改变清点和归托事实', null, before.countedCartons === counted.filter((x) => x.taskId === record.source.taskId).length)
|
|
if (before.taskStatus === 'PENDING') await command('确认时领取原任务', 'POST', `${BASE}/tasks/${record.source.taskId}/claim`, { warehouseId })
|
|
const body = { warehouseId, boxCode, palletNo: index === 0 ? null : palletNo,
|
|
skuLines: resolved.carton.skuLines.map((line) => ({ inboundDetailId: line.inboundDetailId, skuCode: line.skuCode,
|
|
actualQuantity: line.expectedQuantity, discrepancyAction: 'MATCH' })), operationId: `${originalMarker}-count-${index}` }
|
|
const submitted = await command('按原任务确认逐箱实收', 'POST', `${BASE}/tasks/${record.source.taskId}/cartons/count`, body)
|
|
const replayed = await command('清点同键重放不重复入账', 'POST', `${BASE}/tasks/${record.source.taskId}/cartons/count`, body)
|
|
check('重放保持箱身份和清点数量', null, submitted.carton.cartonId === replayed.carton.cartonId && submitted.countedCartons === replayed.countedCartons)
|
|
counted.push({ taskId: record.source.taskId, boxCode, cartonId: submitted.carton.cartonId, inboundId: record.source.inboundId, skuLines: body.skuLines })
|
|
if (index === 0) await command('先扫箱再扫托盘批量归托', 'POST', `${BASE}/tasks/${record.source.taskId}/pallets/bind-pending`, { warehouseId, palletNo, operationId: `${originalMarker}-bind` })
|
|
}
|
|
const beforeSeal = await command('封托前逐客户汇总核对', 'GET', palletPath)
|
|
check('六个实物箱两客户三票汇总守恒', null, beforeSeal.pallet.cartonCount === 6 && beforeSeal.members.length === 3
|
|
&& new Set(beforeSeal.members.map((m) => String(m.customerId))).size === 2
|
|
&& beforeSeal.members.every((m) => m.cartonCount === records.find((r) => r.source.taskId === String(m.taskId))?.source.totalCartons))
|
|
const moved = counted.find((item) => item.taskId === records[1].source.taskId)
|
|
await command('按客户B原任务移回待绑托', 'POST', `${BASE}/tasks/${moved.taskId}/cartons/${moved.cartonId}/move`, { warehouseId, targetPalletNo: null, operationId: `${originalMarker}-move-out` })
|
|
const movedTask = await readTask(records[1])
|
|
check('只改变原票归托数量', null, movedTask.unboundCartons === 1 && movedTask.countedCartons === 3)
|
|
await command('客户B已点箱归回当前混托', 'POST', `${BASE}/tasks/${moved.taskId}/cartons/${moved.cartonId}/move`, { warehouseId, targetPalletNo: palletNo, operationId: `${originalMarker}-move-back` })
|
|
const open = await command('归回后读取权威托盘', 'GET', palletPath)
|
|
const sealPath = `${BASE}/tasks/${records[0].source.taskId}/pallets/${open.pallet.palletId}/seal`
|
|
await command('混托整托封托', 'POST', sealPath, { warehouseId, expectedVersion: open.pallet.version, operationId: `${originalMarker}-seal` })
|
|
let sealed = await command('回查已封托状态', 'GET', palletPath)
|
|
check('托盘已封托且六箱完整', null, sealed.pallet.status === 'SEALED' && sealed.pallet.cartonCount === 6)
|
|
for (const record of records) {
|
|
record.task = await readTask(record)
|
|
const inbound = await command('封托后回查原票待分柜', 'GET', `/api/platform/inbounds/${record.source.inboundId}?tenantId=${record.order.tenantId}`)
|
|
check('参与票全部完成且进入待分柜', null, record.task.taskStatus === 'COMPLETED' && inbound.summary.processStatus === 'WAIT_SPLIT')
|
|
}
|
|
const actual = sealed.pallet.cartons
|
|
check('每箱客户归属和双SKU实收未合并', null, actual.length === counted.length && actual.every((carton) => {
|
|
const source = counted.find((item) => item.boxCode === carton.boxCode)
|
|
return source && String(carton.inboundId) === source.inboundId && carton.skuLines.length === 2
|
|
&& carton.skuLines.every((line) => source.skuLines.some((expected) => String(line.inboundDetailId) === String(expected.inboundDetailId) && line.actualQuantity === expected.actualQuantity))
|
|
}))
|
|
await rejected('已封托禁止移箱', 'POST', `${BASE}/tasks/${moved.taskId}/cartons/${moved.cartonId}/move`, { warehouseId, targetPalletNo: null, operationId: `${originalMarker}-sealed-move` })
|
|
await command('未入装柜计划允许解除封托', 'POST', `${BASE}/tasks/${records[0].source.taskId}/pallets/${sealed.pallet.palletId}/unseal`, { warehouseId, expectedVersion: sealed.pallet.version, operationId: `${originalMarker}-unseal` })
|
|
for (const record of records) check('解封同步恢复所有参与任务', null, (await readTask(record)).taskStatus === 'CLAIMED')
|
|
const reopened = await command('回查解封后开放状态', 'GET', palletPath)
|
|
await command('核对后重新封托', 'POST', sealPath, { warehouseId, expectedVersion: reopened.pallet.version, operationId: `${originalMarker}-reseal` })
|
|
sealed = await command('最终托盘事实', 'GET', palletPath)
|
|
result.pallet = sealed.pallet; result.members = sealed.members; result.counted = counted
|
|
result.stage = 'MIXED_PALLET_SEALED_VERIFIED'
|
|
for (const record of records) record.task = await readTask(record)
|
|
// 保留同客户两票和跨客户两票独立样本,均停在未领取、未组托状态。
|
|
result.manualRecords = []
|
|
for (const [index, count] of [2, 1, 2, 3].entries()) {
|
|
context.marker = `${originalMarker}-MANUAL-${index + 1}`
|
|
context.context.merchantQualificationFixture = index < 3 ? savedContext.merchantQualificationFixture : secondCustomer
|
|
for (const key of ['orders', 'inboundMeasurement', 'dropshipManualCount']) delete context.createdResources[key]
|
|
for (const key of ['inboundWeightOcr', 'ocrInbound', 'testInbound', 'createdInbound']) delete context.context[key]
|
|
context.context.orderDestinationType = 'OVERSEAS'
|
|
context.context.orderFulfillmentType = 'ONE_CLICK_DROPSHIP'
|
|
context.context.waitSplitVariant = { skuCount: 2, packageCountPerSku: count, packageCounts: [count, count],
|
|
totalPackageCount: count, palletCount: 1, skuCodes: [`MANUAL-COMMON-${originalMarker}`.slice(-60), `MANUAL-SECOND-${originalMarker}`.slice(-60)] }
|
|
context.options.prepareDropshipManualCount = true
|
|
const start = context.steps.length
|
|
await createOrder(context)
|
|
await measure(context)
|
|
const source = context.createdResources.dropshipManualCount
|
|
check('人工打托样本正式下单测量通过', null, Boolean(source?.taskId) && !context.steps.slice(start).some((step) => !step.passed))
|
|
const order = { ...context.createdResources.orders }
|
|
const list = await command('回查一件代发打托列表可见', 'GET', `/api/platform/v1/orders?mainStatus=WAIT_PALLETIZE&inboundCode=${encodeURIComponent(source.inboundCode)}&page=1&size=20`)
|
|
const row = list.records?.find((item) => String(item.orderId) === order.orderId)
|
|
check('人工样本出现在用户的一件代发打托页签', null, Boolean(row) && row.mainStatus === 'WAIT_PALLETIZE')
|
|
const task = await readTask({ label: `MANUAL-${index + 1}`, source })
|
|
check('人工样本未领取未清点未归托', null, task.taskStatus === 'PENDING' && task.countedCartons === 0 && task.boundCartons === 0)
|
|
result.manualRecords.push({ label: `MANUAL-${index + 1}`, group: index < 2 ? 'SAME_CUSTOMER' : 'CROSS_CUSTOMER',
|
|
order, source: { ...source }, measurement: { ...context.createdResources.inboundMeasurement } })
|
|
}
|
|
result.manualStage = 'WAIT_PALLETIZE_VISIBLE'
|
|
} finally {
|
|
context.marker = originalMarker; context.context = savedContext
|
|
context.options.prepareDropshipManualCount = originalManualCount
|
|
}
|
|
}
|