2026-09-11 16:02:10 +08:00
|
|
|
import { createPicker, disablePicker, resolvePickerAccess } from './phase4-overseas-pda.mjs'
|
2026-09-15 17:05:07 +08:00
|
|
|
import { runReturnBoundaries, verifyNoMutation } from './return-boundary-scenarios.mjs'
|
2026-09-11 16:02:10 +08:00
|
|
|
|
|
|
|
|
const MODULE = 'phase4-overseas-return-receipt'
|
|
|
|
|
const SCENARIO = 'phase4-overseas-return-receipt-and-billing'
|
|
|
|
|
const OPTIONS = { scenario: SCENARIO, deploymentBlocker: true }
|
|
|
|
|
const RETURN_ORDERS = '/api/merchant/v1/last-leg/return-orders'
|
|
|
|
|
const PDA_RECEIPTS = '/api/pda/v1/last-leg/return-receipts'
|
|
|
|
|
const RETURN_INVENTORY = '/api/merchant/v1/last-leg/return-inventory'
|
|
|
|
|
|
|
|
|
|
function id(value) { return value == null ? '' : String(value) }
|
|
|
|
|
function nonBlank(value) { return typeof value === 'string' && value.trim() !== '' }
|
|
|
|
|
function code(response) { return String(response?.body?.code ?? '') }
|
|
|
|
|
function success(response) { return response?.http === 200 && code(response) === '200' }
|
|
|
|
|
function conflict(response) {
|
|
|
|
|
return (response?.http === 409 || (response?.http === 200 && ['409', 'A0409'].includes(code(response))))
|
|
|
|
|
&& nonBlank(response?.requestId) && nonBlank(response?.traceId)
|
|
|
|
|
}
|
|
|
|
|
function notFound(response) {
|
|
|
|
|
return (response?.http === 404 || (response?.http === 200 && ['404', 'A0404'].includes(code(response))))
|
|
|
|
|
&& nonBlank(response?.requestId) && nonBlank(response?.traceId)
|
|
|
|
|
}
|
2026-09-15 16:10:55 +08:00
|
|
|
export function validationFailure(response) {
|
|
|
|
|
return ((response?.http === 400 || response?.http === 422)
|
|
|
|
|
|| (response?.http === 200 && ['400', '422', 'A0400', 'A0422'].includes(code(response))))
|
|
|
|
|
&& nonBlank(response?.requestId) && nonBlank(response?.traceId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function inventorySnapshot(cartons) {
|
|
|
|
|
return JSON.stringify(records(cartons).map(row => ({
|
|
|
|
|
cartonMark: row.cartonMark, status: row.status, quantity: Number(row.totalQuantity),
|
|
|
|
|
sourceBatchNo: row.sourceBatchNo, sourceReturnNo: row.sourceReturnNo,
|
|
|
|
|
})).sort((a, b) => a.cartonMark.localeCompare(b.cartonMark)))
|
|
|
|
|
}
|
2026-09-11 16:02:10 +08:00
|
|
|
function records(value) { return Array.isArray(value?.records) ? value.records : Array.isArray(value) ? value : [] }
|
|
|
|
|
function trace(response) {
|
|
|
|
|
return `HTTP=${response?.http ?? 'missing'}, code=${code(response) || 'missing'}, requestId=${response?.requestId || 'missing'}, traceId=${response?.traceId || 'missing'}`
|
|
|
|
|
}
|
|
|
|
|
function query(path, values) {
|
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
for (const [name, value] of Object.entries(values || {})) {
|
|
|
|
|
if (value !== undefined && value !== null && String(value) !== '') params.set(name, String(value))
|
|
|
|
|
}
|
|
|
|
|
return params.size ? `${path}?${params}` : path
|
|
|
|
|
}
|
|
|
|
|
function key(context, action) {
|
|
|
|
|
return `${context.marker}-RETURN-${action}`.replace(/[^A-Za-z0-9:_-]/g, '').slice(0, 255)
|
|
|
|
|
}
|
|
|
|
|
function markerSuffix(context) {
|
|
|
|
|
return String(context.marker).replace(/[^A-Za-z0-9]/g, '').slice(-18).toUpperCase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function add(context, name, response, passed, evidence = '') {
|
|
|
|
|
context.addStep(MODULE, name, response, passed, `${evidence}${evidence ? '; ' : ''}${trace(response)}`, OPTIONS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function overseas(context, token, method, name, path, body = undefined) {
|
2026-09-24 00:04:03 +08:00
|
|
|
let response = await context.requestOverseas(method, path, body, {}, token)
|
|
|
|
|
for (let attempt = 1; attempt < 3 && response?.http === 0; attempt += 1) {
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 250 * attempt))
|
|
|
|
|
response = await context.requestOverseas(method, path, body, {}, token)
|
|
|
|
|
}
|
2026-09-11 16:02:10 +08:00
|
|
|
add(context, name, response, success(response))
|
|
|
|
|
return { response, data: success(response) ? response.body?.data : null }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function resolveWarehouse(context) {
|
|
|
|
|
const existing = id(context.createdResources?.phase4OverseasPda?.overseasWarehouseId)
|
|
|
|
|
const response = await context.request('GET',
|
|
|
|
|
'/api/platform/v1/warehouse-admin?tenantId=0&page=1&size=200&capabilityCode=RECEIVING&warehouseType=OVERSEAS&status=ACTIVE')
|
|
|
|
|
const rows = records(response?.body?.data)
|
|
|
|
|
const fixedIds = new Set(['193000000000200003', '193000000000200004'])
|
|
|
|
|
const warehouse = rows.find((item) => id(item?.id) === existing)
|
|
|
|
|
|| rows.find((item) => fixedIds.has(id(item?.id)))
|
|
|
|
|
|| rows[0]
|
|
|
|
|
add(context, '读取可执行退货签收的海外仓', response,
|
|
|
|
|
success(response) && nonBlank(id(warehouse?.id)), `warehouseId=${id(warehouse?.id) || 'missing'}`)
|
|
|
|
|
return success(response) && warehouse ? { id: id(warehouse.id), name: warehouse.warehouseName || warehouse.name } : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loginMerchant(context) {
|
|
|
|
|
const fixture = context.context?.merchantQualificationFixture
|
|
|
|
|
if (fixture?.marker !== context.marker || !fixture?.username || !fixture?.password) {
|
|
|
|
|
add(context, '取得本轮客户账号', null, false, `marker=${fixture?.marker || 'missing'}`)
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
const response = await context.request('POST', '/api/auth/login', {
|
|
|
|
|
loginId: fixture.username, password: fixture.password,
|
|
|
|
|
}, '')
|
|
|
|
|
const token = success(response) ? response.body?.data?.overseasAccessToken : null
|
|
|
|
|
add(context, '客户登录并取得海外访问令牌', response, nonBlank(token), `tokenPresent=${nonBlank(token)}`)
|
|
|
|
|
return nonBlank(token) ? token : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function itemMap(items) {
|
|
|
|
|
return new Map((Array.isArray(items) ? items : []).map((item) => [item.skuCode, item]))
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-20 18:25:12 +08:00
|
|
|
export async function approveReturnFixture(context, merchantToken, created) {
|
|
|
|
|
const expectedCustomer = id(context.context?.merchantQualificationFixture?.tenantId)
|
|
|
|
|
const order = created?.order
|
|
|
|
|
if (!expectedCustomer || !nonBlank(id(order?.returnNo))
|
|
|
|
|
|| order.status !== 'PENDING_REVIEW') throw new Error('退货审核前置的客户、单号或状态不符')
|
|
|
|
|
// Summary deliberately omits customerId. Verify ownership via the merchant-scoped detail API.
|
|
|
|
|
const owned = await overseas(context, merchantToken, 'GET', '客户核对本轮待审核退货单',
|
|
|
|
|
`${RETURN_ORDERS}/${encodeURIComponent(order.returnNo)}`)
|
|
|
|
|
if (!success(owned.response) || owned.data?.order?.returnNo !== order.returnNo
|
|
|
|
|
|| owned.data?.order?.status !== 'PENDING_REVIEW') throw new Error('客户不可读取当前待审核退货单')
|
|
|
|
|
const path = `/api/platform/v1/last-leg/return-orders/${encodeURIComponent(order.returnNo)}/review`
|
|
|
|
|
const response = await context.requestOverseas('POST', path,
|
|
|
|
|
{ decision: 'APPROVE', remark: `${context.marker} 退货验收审核` }, { 'X-Customer-Id': expectedCustomer })
|
|
|
|
|
add(context, '平台审核本轮退货预报', response, success(response))
|
|
|
|
|
if (!success(response)) throw new Error(`退货审核失败:${trace(response)}`)
|
|
|
|
|
const detail = await overseas(context, merchantToken, 'GET', '客户回查退货审核状态',
|
|
|
|
|
`${RETURN_ORDERS}/${encodeURIComponent(order.returnNo)}`)
|
|
|
|
|
const verified = success(detail.response) && detail.data?.order?.returnNo === order.returnNo
|
|
|
|
|
&& detail.data?.order?.status === 'PENDING_RECEIPT'
|
|
|
|
|
&& Number(detail.data?.order?.receivedQuantity) === 0
|
|
|
|
|
context.assert(MODULE, '审核后同一客户同一退货单进入待签收且尚未入库', detail.response, verified,
|
|
|
|
|
trace(detail.response), OPTIONS)
|
|
|
|
|
if (!verified) throw new Error('退货审核回查未收敛,停止签收链路')
|
|
|
|
|
return detail.data
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 16:02:10 +08:00
|
|
|
export async function run(context) {
|
|
|
|
|
context.createdResources.phase4OverseasReturnReceipt = {
|
|
|
|
|
marker: context.marker, returnNo: null, receiptNos: [], receiptBatchNos: [], cartonMarks: [],
|
|
|
|
|
billingExpectation: 'One PENDING_PRICING storage billing batch per successful non-replayed receipt',
|
|
|
|
|
}
|
|
|
|
|
const merchantToken = await loginMerchant(context)
|
|
|
|
|
const warehouse = await resolveWarehouse(context)
|
|
|
|
|
const access = await resolvePickerAccess(context)
|
|
|
|
|
if (!merchantToken || !warehouse || !access) return
|
|
|
|
|
const picker = await createPicker(context, 91, warehouse.id, access)
|
|
|
|
|
if (!picker?.token) {
|
|
|
|
|
add(context, '创建退货签收PDA操作员', null, false, '未取得绑定仓PDA令牌')
|
|
|
|
|
if (picker?.membershipId) await disablePicker(context, picker, 91)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const suffix = markerSuffix(context)
|
|
|
|
|
const skuA = `RET-A-${suffix}`.slice(0, 128)
|
|
|
|
|
const skuB = `RET-B-${suffix}`.slice(0, 128)
|
|
|
|
|
const createBody = {
|
|
|
|
|
overseasWarehouseId: warehouse.id,
|
|
|
|
|
warehouseContactName: '全量验收海外仓', warehouseContactPhone: '0000000000',
|
|
|
|
|
customerReferenceNo: `REF-${suffix}`, needRelabel: false, remark: context.marker,
|
|
|
|
|
idempotencyKey: key(context, 'CREATE'),
|
|
|
|
|
items: [
|
|
|
|
|
{ skuCode: skuA, productNameZh: '退货验收商品A', expectedQuantity: 3 },
|
|
|
|
|
{ skuCode: skuB, productNameZh: '退货验收商品B', expectedQuantity: 2 },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const created = await overseas(context, merchantToken, 'POST', '客户创建双SKU退货预报', RETURN_ORDERS, createBody)
|
|
|
|
|
const returnNo = id(created.data?.order?.returnNo)
|
|
|
|
|
context.assert(MODULE, '退货预报初始数量和状态正确', created.response,
|
2026-09-20 18:25:12 +08:00
|
|
|
nonBlank(returnNo) && created.data?.order?.status === 'PENDING_REVIEW'
|
2026-09-11 16:02:10 +08:00
|
|
|
&& Number(created.data?.order?.expectedQuantity) === 5
|
|
|
|
|
&& Number(created.data?.order?.receivedQuantity) === 0
|
|
|
|
|
&& itemMap(created.data?.items).size === 2,
|
|
|
|
|
`returnNo=${returnNo || 'missing'}`, OPTIONS)
|
2026-09-20 18:25:12 +08:00
|
|
|
if (!success(created.response) || !returnNo || created.data?.order?.status !== 'PENDING_REVIEW') {
|
|
|
|
|
throw new Error('退货预报创建前置失败,停止后续签收')
|
|
|
|
|
}
|
2026-09-11 16:02:10 +08:00
|
|
|
context.createdResources.phase4OverseasReturnReceipt.returnNo = returnNo
|
|
|
|
|
|
2026-09-15 17:05:07 +08:00
|
|
|
async function snapshot() {
|
|
|
|
|
const reads = [
|
|
|
|
|
['订单列表', query(RETURN_ORDERS, { page: 1, size: 200 })],
|
|
|
|
|
['订单及签收批次', `${RETURN_ORDERS}/${encodeURIComponent(returnNo)}`],
|
|
|
|
|
['全部状态箱', query(`${RETURN_INVENTORY}/cartons`, { page: 1, size: 200, returnNo })],
|
|
|
|
|
['SKU汇总', query(`${RETURN_INVENTORY}/summary`, { page: 1, size: 200, overseasWarehouseId: warehouse.id })],
|
|
|
|
|
]
|
|
|
|
|
const state = {}
|
|
|
|
|
for (const [name, path] of reads) {
|
|
|
|
|
const result = await overseas(context, merchantToken, 'GET', `失败操作逐项回查${name}`, path)
|
|
|
|
|
if (!success(result.response)) throw new Error(`无法读取${name},不能证明失败未落账`)
|
|
|
|
|
state[name] = result.data
|
|
|
|
|
}
|
|
|
|
|
state.items = []
|
|
|
|
|
for (const row of records(state['全部状态箱'])) {
|
|
|
|
|
const result = await overseas(context, merchantToken, 'GET', '失败操作逐箱回查明细', `${RETURN_INVENTORY}/cartons/${encodeURIComponent(row.cartonMark)}`)
|
|
|
|
|
if (!success(result.response)) throw new Error('无法读取箱明细,不能证明失败未落账')
|
|
|
|
|
state.items.push(result.data)
|
|
|
|
|
}
|
|
|
|
|
return state
|
|
|
|
|
}
|
|
|
|
|
const reject = (name, expected, action) => verifyNoMutation({ snapshot, action, name, expected,
|
|
|
|
|
check: (label, response, passed) => add(context, label, response, passed) })
|
|
|
|
|
|
2026-09-11 16:02:10 +08:00
|
|
|
const createReplay = await overseas(context, merchantToken, 'POST', '退货预报同键同载荷重放', RETURN_ORDERS, createBody)
|
|
|
|
|
context.assert(MODULE, '预报幂等重放复用同一退货单', createReplay.response,
|
|
|
|
|
id(createReplay.data?.order?.returnNo) === returnNo, trace(createReplay.response), OPTIONS)
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('退货预报同键异载荷返回冲突', conflict, () => context.requestOverseas('POST', RETURN_ORDERS,
|
|
|
|
|
{ ...createBody, remark: `${context.marker}-CHANGED` }, {}, merchantToken))
|
2026-09-11 16:02:10 +08:00
|
|
|
|
2026-09-20 18:25:12 +08:00
|
|
|
await approveReturnFixture(context, merchantToken, created.data)
|
|
|
|
|
|
2026-09-15 16:10:55 +08:00
|
|
|
for (const [name, items] of [
|
|
|
|
|
['空SKU明细', []],
|
|
|
|
|
['零预报数量', [{ skuCode: skuA, expectedQuantity: 0 }]],
|
|
|
|
|
['负预报数量', [{ skuCode: skuA, expectedQuantity: -1 }]],
|
2026-09-15 17:05:07 +08:00
|
|
|
['小数预报数量', [{ skuCode: skuA, expectedQuantity: 1.5 }]],
|
2026-09-15 16:10:55 +08:00
|
|
|
['重复预报SKU', [createBody.items[0], createBody.items[0]]],
|
|
|
|
|
]) {
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject(`拒绝${name}`, validationFailure, () => context.requestOverseas('POST', RETURN_ORDERS,
|
|
|
|
|
{ ...createBody, items, idempotencyKey: key(context, `INVALID-CREATE-${encodeURIComponent(name)}`) }, {}, merchantToken))
|
2026-09-15 16:10:55 +08:00
|
|
|
}
|
|
|
|
|
const onlyOrder = await overseas(context, merchantToken, 'GET', '非法预报后回查本轮订单数量',
|
|
|
|
|
query(RETURN_ORDERS, { page: 1, size: 20, skuCode: skuA }))
|
|
|
|
|
context.assert(MODULE, '非法预报不新增订单且原单仍为待签收', onlyOrder.response,
|
|
|
|
|
Number(onlyOrder.data?.total) === 1 && records(onlyOrder.data)[0]?.returnNo === returnNo
|
|
|
|
|
&& records(onlyOrder.data)[0]?.status === 'PENDING_RECEIPT', trace(onlyOrder.response), OPTIONS)
|
|
|
|
|
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('不存在退货单返回可诊断未找到', notFound, () => context.requestOverseas('POST', `${PDA_RECEIPTS}/start`,
|
|
|
|
|
{ returnNo: `MISSING-${suffix}` }, {}, picker.token))
|
2026-09-15 16:10:55 +08:00
|
|
|
|
2026-09-11 16:02:10 +08:00
|
|
|
const emptySku = await overseas(context, picker.token, 'POST', 'PDA查询不存在SKU返回正常空候选',
|
|
|
|
|
`${PDA_RECEIPTS}/sku-candidates`, { skuCode: `MISSING-${suffix}` })
|
|
|
|
|
context.assert(MODULE, '不存在SKU不是业务错误', emptySku.response,
|
|
|
|
|
success(emptySku.response) && Array.isArray(emptySku.data?.candidates) && emptySku.data.candidates.length === 0,
|
|
|
|
|
trace(emptySku.response), OPTIONS)
|
|
|
|
|
|
|
|
|
|
const started = await overseas(context, picker.token, 'POST', 'PDA按退货单号开始签收',
|
|
|
|
|
`${PDA_RECEIPTS}/start`, { returnNo })
|
|
|
|
|
context.assert(MODULE, '首次开始返回全部未收SKU', started.response,
|
|
|
|
|
started.data?.status === 'PENDING_RECEIPT' && itemMap(started.data?.expectedItems).size === 2,
|
|
|
|
|
trace(started.response), OPTIONS)
|
|
|
|
|
const skuCandidates = await overseas(context, picker.token, 'POST', 'PDA按SKU查询当前仓候选退货单',
|
|
|
|
|
`${PDA_RECEIPTS}/sku-candidates`, { skuCode: skuB })
|
|
|
|
|
context.assert(MODULE, 'SKU候选包含本轮退货单及准确数量', skuCandidates.response,
|
|
|
|
|
skuCandidates.data?.candidates?.some?.((candidate) => candidate.returnNo === returnNo
|
|
|
|
|
&& Number(candidate.skuExpectedQuantity) === 2 && Number(candidate.skuReceivedQuantity) === 0),
|
|
|
|
|
trace(skuCandidates.response), OPTIONS)
|
|
|
|
|
|
2026-09-20 18:25:12 +08:00
|
|
|
const palletA = await overseas(context, picker.token, 'POST', 'PDA生成本轮SKU甲签收托盘', `${PDA_RECEIPTS}/pallets`, { returnNo, skuCode: skuA })
|
|
|
|
|
const palletB = await overseas(context, picker.token, 'POST', 'PDA生成本轮SKU乙签收托盘', `${PDA_RECEIPTS}/pallets`, { returnNo, skuCode: skuB })
|
|
|
|
|
if (!success(palletA.response) || !nonBlank(palletA.data?.palletCode)
|
|
|
|
|
|| !success(palletB.response) || !nonBlank(palletB.data?.palletCode)) throw new Error('签收托盘准备失败')
|
|
|
|
|
const cartonA = palletA.data.palletCode
|
2026-09-11 16:02:10 +08:00
|
|
|
const firstBody = {
|
2026-09-20 18:25:12 +08:00
|
|
|
returnNo, trackingNo: `TRACK-${suffix}`, startedBy: 'RETURN_NO', palletCode: cartonA,
|
|
|
|
|
items: [{ skuCode: skuA, receivedQuantity: 3 }], idempotencyKey: key(context, 'RECEIPT-1'),
|
2026-09-11 16:02:10 +08:00
|
|
|
}
|
2026-09-20 18:25:12 +08:00
|
|
|
const first = await overseas(context, picker.token, 'POST', 'PDA首次单SKU签收甲3件', `${PDA_RECEIPTS}/confirm`, firstBody)
|
|
|
|
|
if (!success(first.response)) throw new Error('首次签收失败,停止后续库存场景')
|
2026-09-11 16:02:10 +08:00
|
|
|
context.assert(MODULE, '首次签收形成唯一箱和仓储计费批次', first.response,
|
|
|
|
|
first.data?.orderStatus === 'PARTIALLY_RECEIVED' && first.data?.fullyMatched === false
|
2026-09-20 18:25:12 +08:00
|
|
|
&& Number(first.data?.receivedQuantity) === 3 && first.data?.palletCode === cartonA
|
2026-09-11 16:02:10 +08:00
|
|
|
&& nonBlank(first.data?.receiptNo) && nonBlank(first.data?.receiptBatchNo),
|
|
|
|
|
`receiptNo=${id(first.data?.receiptNo) || 'missing'}, billingBatch=${id(first.data?.receiptBatchNo) || 'missing'}`, OPTIONS)
|
|
|
|
|
const firstReceiptNo = id(first.data?.receiptNo)
|
|
|
|
|
const firstBatchNo = id(first.data?.receiptBatchNo)
|
|
|
|
|
|
|
|
|
|
const firstReplay = await overseas(context, picker.token, 'POST', '首次签收同键同载荷重放', `${PDA_RECEIPTS}/confirm`, firstBody)
|
|
|
|
|
context.assert(MODULE, '签收重放不重复创建签收单、箱或计费批次', firstReplay.response,
|
|
|
|
|
id(firstReplay.data?.receiptNo) === firstReceiptNo
|
|
|
|
|
&& id(firstReplay.data?.receiptBatchNo) === firstBatchNo
|
2026-09-20 18:25:12 +08:00
|
|
|
&& id(firstReplay.data?.palletCode) === cartonA,
|
2026-09-11 16:02:10 +08:00
|
|
|
trace(firstReplay.response), OPTIONS)
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('签收同键异载荷返回冲突', conflict, () => context.requestOverseas('POST', `${PDA_RECEIPTS}/confirm`, {
|
2026-09-11 16:02:10 +08:00
|
|
|
...firstBody, trackingNo: `TRACK-${suffix}-CHANGED`,
|
2026-09-15 17:05:07 +08:00
|
|
|
}, {}, picker.token))
|
2026-09-11 16:02:10 +08:00
|
|
|
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('超收被拒绝且不得生成费用批次', conflict, () => context.requestOverseas('POST', `${PDA_RECEIPTS}/confirm`, {
|
2026-09-20 18:25:12 +08:00
|
|
|
returnNo, startedBy: 'SKU', palletCode: cartonA, items: [{ skuCode: skuA, receivedQuantity: 2 }],
|
2026-09-11 16:02:10 +08:00
|
|
|
idempotencyKey: key(context, 'OVER-RECEIPT'),
|
2026-09-15 17:05:07 +08:00
|
|
|
}, {}, picker.token))
|
2026-09-11 16:02:10 +08:00
|
|
|
|
2026-09-15 16:10:55 +08:00
|
|
|
for (const [name, changes, expected] of [
|
|
|
|
|
['零实收数量', { items: [{ skuCode: skuA, receivedQuantity: 0 }] }, validationFailure],
|
|
|
|
|
['负实收数量', { items: [{ skuCode: skuA, receivedQuantity: -1 }] }, validationFailure],
|
|
|
|
|
['空实收明细', { items: [] }, validationFailure],
|
|
|
|
|
['非法签收方式', { startedBy: 'INVALID' }, validationFailure],
|
|
|
|
|
['未预报SKU', { items: [{ skuCode: `UNKNOWN-${suffix}`, receivedQuantity: 1 }] }, conflict],
|
|
|
|
|
]) {
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject(`拒绝${name}`, expected, () => context.requestOverseas('POST', `${PDA_RECEIPTS}/confirm`,
|
|
|
|
|
{ ...firstBody, ...changes, idempotencyKey: key(context, `INVALID-RECEIPT-${encodeURIComponent(name)}`) }, {}, picker.token))
|
2026-09-15 16:10:55 +08:00
|
|
|
}
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('部分签收退货单禁止合箱', conflict, () => context.requestOverseas('POST', '/api/pda/v1/last-leg/return-inventory/cartons/merge', {
|
2026-09-15 16:10:55 +08:00
|
|
|
returnNos: [returnNo], items: [{ skuCode: skuA, quantity: 1 }], length: 30, width: 20, height: 10,
|
|
|
|
|
weight: 1, idempotencyKey: key(context, 'EARLY-MERGE'),
|
2026-09-15 17:05:07 +08:00
|
|
|
}, {}, picker.token))
|
2026-09-15 16:10:55 +08:00
|
|
|
|
2026-09-11 16:02:10 +08:00
|
|
|
const afterPartial = await overseas(context, merchantToken, 'GET', '客户回查部分签收状态',
|
|
|
|
|
`${RETURN_ORDERS}/${encodeURIComponent(returnNo)}`)
|
|
|
|
|
context.assert(MODULE, '超收失败后订单、签收和费用批次数量均未变化', afterPartial.response,
|
|
|
|
|
afterPartial.data?.order?.status === 'PARTIALLY_RECEIVED'
|
2026-09-20 18:25:12 +08:00
|
|
|
&& Number(afterPartial.data?.order?.receivedQuantity) === 3
|
2026-09-11 16:02:10 +08:00
|
|
|
&& afterPartial.data?.receipts?.length === 1
|
|
|
|
|
&& id(afterPartial.data.receipts[0]?.receiptBatchNo) === firstBatchNo,
|
|
|
|
|
`receipts=${afterPartial.data?.receipts?.length ?? 'missing'}`, OPTIONS)
|
|
|
|
|
|
|
|
|
|
const secondBody = {
|
2026-09-20 18:25:12 +08:00
|
|
|
returnNo, startedBy: 'SKU', palletCode: palletB.data.palletCode,
|
|
|
|
|
items: [{ skuCode: skuB, receivedQuantity: 2 }],
|
2026-09-11 16:02:10 +08:00
|
|
|
idempotencyKey: key(context, 'RECEIPT-2'),
|
|
|
|
|
}
|
2026-09-20 18:25:12 +08:00
|
|
|
await reject('最后一批缺少打托照片拒绝且库存不变', conflict, () => context.requestOverseas('POST', `${PDA_RECEIPTS}/confirm`, secondBody, {}, picker.token))
|
|
|
|
|
const photoForm = new FormData()
|
|
|
|
|
// Synthetic HTTP fixture, not a photograph of real warehouse activity.
|
|
|
|
|
photoForm.append('file', new Blob([Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aWQAAAABJRU5ErkJggg==', 'base64')], { type: 'image/png' }), 'return-receipt-test.png')
|
|
|
|
|
const photo = await context.requestOverseasMultipart('POST', `${PDA_RECEIPTS}/${encodeURIComponent(returnNo)}/photos/files`, photoForm, picker.token)
|
|
|
|
|
add(context, '上传本轮合成打托测试照片', photo, success(photo))
|
|
|
|
|
if (!success(photo) || !photo.body?.data?.fileId) throw new Error('打托照片上传失败')
|
|
|
|
|
const second = await overseas(context, picker.token, 'POST', 'PDA按单SKU完成乙2件签收', `${PDA_RECEIPTS}/confirm`, secondBody)
|
|
|
|
|
if (!success(second.response)) throw new Error('最终签收失败,停止合箱和换标后续')
|
2026-09-11 16:02:10 +08:00
|
|
|
context.assert(MODULE, '最终签收自动核平并生成第二个费用批次', second.response,
|
|
|
|
|
second.data?.orderStatus === 'COMPLETED' && second.data?.fullyMatched === true
|
2026-09-20 18:25:12 +08:00
|
|
|
&& Number(second.data?.receivedQuantity) === 5 && second.data?.palletCode === palletB.data.palletCode
|
2026-09-11 16:02:10 +08:00
|
|
|
&& nonBlank(second.data?.receiptNo) && nonBlank(second.data?.receiptBatchNo)
|
|
|
|
|
&& id(second.data?.receiptBatchNo) !== firstBatchNo,
|
|
|
|
|
`billingBatch=${id(second.data?.receiptBatchNo) || 'missing'}`, OPTIONS)
|
|
|
|
|
const secondReceiptNo = id(second.data?.receiptNo)
|
|
|
|
|
const secondBatchNo = id(second.data?.receiptBatchNo)
|
2026-09-20 18:25:12 +08:00
|
|
|
const cartonB = id(second.data?.palletCode)
|
2026-09-11 16:02:10 +08:00
|
|
|
context.createdResources.phase4OverseasReturnReceipt.receiptNos.push(firstReceiptNo, secondReceiptNo)
|
|
|
|
|
context.createdResources.phase4OverseasReturnReceipt.receiptBatchNos.push(firstBatchNo, secondBatchNo)
|
|
|
|
|
context.createdResources.phase4OverseasReturnReceipt.cartonMarks.push(cartonA, cartonB)
|
|
|
|
|
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('已完成退货单不再进入待签收流程', notFound,
|
|
|
|
|
() => context.requestOverseas('POST', `${PDA_RECEIPTS}/start`, { returnNo }, {}, picker.token))
|
2026-09-11 16:02:10 +08:00
|
|
|
const completedCandidates = await overseas(context, picker.token, 'POST', '完成后SKU候选自动清空',
|
|
|
|
|
`${PDA_RECEIPTS}/sku-candidates`, { skuCode: skuB })
|
|
|
|
|
context.assert(MODULE, '完成单不再出现在SKU候选', completedCandidates.response,
|
|
|
|
|
!completedCandidates.data?.candidates?.some?.((candidate) => candidate.returnNo === returnNo),
|
|
|
|
|
trace(completedCandidates.response), OPTIONS)
|
|
|
|
|
|
|
|
|
|
const detail = await overseas(context, merchantToken, 'GET', '客户回查完成退货单与两次签收',
|
|
|
|
|
`${RETURN_ORDERS}/${encodeURIComponent(returnNo)}`)
|
|
|
|
|
const detailItems = itemMap(detail.data?.items)
|
|
|
|
|
const detailReceipts = Array.isArray(detail.data?.receipts) ? detail.data.receipts : []
|
|
|
|
|
const detailBatches = new Set(detailReceipts.map((receipt) => id(receipt.receiptBatchNo)))
|
|
|
|
|
context.assert(MODULE, '订单5件核平且恰有两个唯一仓储计费批次', detail.response,
|
|
|
|
|
detail.data?.order?.status === 'COMPLETED' && Number(detail.data?.order?.receivedQuantity) === 5
|
|
|
|
|
&& Number(detailItems.get(skuA)?.receivedQuantity) === 3
|
|
|
|
|
&& Number(detailItems.get(skuB)?.receivedQuantity) === 2
|
|
|
|
|
&& detailReceipts.length === 2 && detailBatches.size === 2
|
|
|
|
|
&& detailBatches.has(firstBatchNo) && detailBatches.has(secondBatchNo),
|
|
|
|
|
`receipts=${detailReceipts.length}, billingBatches=${detailBatches.size}`, OPTIONS)
|
|
|
|
|
|
|
|
|
|
const merchantInventory = await context.requestOverseas('GET',
|
|
|
|
|
query(RETURN_INVENTORY, { page: 1, size: 20, overseasWarehouseId: warehouse.id, returnNo }),
|
|
|
|
|
undefined, {}, merchantToken)
|
|
|
|
|
add(context, '客户权限可读取自身退货库存', merchantInventory, success(merchantInventory))
|
|
|
|
|
|
|
|
|
|
const customerId = id(context.context?.merchantQualificationFixture?.tenantId)
|
|
|
|
|
const inventory = await overseas(context, context.overseasToken, 'GET', '平台按客户和退货单回查全部退货箱库存',
|
|
|
|
|
query('/api/platform/v1/last-leg/return-inventory/cartons', {
|
|
|
|
|
page: 1, size: 20, customerId, overseasWarehouseId: warehouse.id, returnNo,
|
|
|
|
|
}))
|
|
|
|
|
const inventoryRows = records(inventory.data)
|
|
|
|
|
const sourceBatches = new Set(inventoryRows.map((row) => id(row.sourceBatchNo)))
|
|
|
|
|
context.assert(MODULE, '两个实收箱逐一引用对应计费批次且数量守恒', inventory.response,
|
|
|
|
|
inventoryRows.length === 2
|
|
|
|
|
&& inventoryRows.reduce((sum, row) => sum + Number(row.totalQuantity || 0), 0) === 5
|
|
|
|
|
&& sourceBatches.size === 2 && sourceBatches.has(firstBatchNo) && sourceBatches.has(secondBatchNo),
|
|
|
|
|
`cartons=${inventoryRows.length}, quantity=${inventoryRows.reduce((sum, row) => sum + Number(row.totalQuantity || 0), 0)}`, OPTIONS)
|
|
|
|
|
|
|
|
|
|
const summary = await overseas(context, context.overseasToken, 'GET', '平台回查退货SKU汇总库存',
|
|
|
|
|
query('/api/platform/v1/last-leg/return-inventory', {
|
|
|
|
|
page: 1, size: 20, customerId, overseasWarehouseId: warehouse.id,
|
|
|
|
|
}))
|
|
|
|
|
const summaries = new Map(records(summary.data).filter((row) => [skuA, skuB].includes(row.skuCode))
|
|
|
|
|
.map((row) => [row.skuCode, row]))
|
|
|
|
|
context.assert(MODULE, '退货SKU汇总数量与两个实收箱一致', summary.response,
|
|
|
|
|
Number(summaries.get(skuA)?.totalQuantity) === 3
|
|
|
|
|
&& Number(summaries.get(skuB)?.totalQuantity) === 2,
|
|
|
|
|
`skuA=${summaries.get(skuA)?.totalQuantity ?? 'missing'}, skuB=${summaries.get(skuB)?.totalQuantity ?? 'missing'}`, OPTIONS)
|
|
|
|
|
|
2026-09-20 18:25:12 +08:00
|
|
|
for (const [index, expected] of [{ cartonMark: cartonA, batchNo: firstBatchNo, quantity: 3 },
|
|
|
|
|
{ cartonMark: cartonB, batchNo: secondBatchNo, quantity: 2 }].entries()) {
|
2026-09-11 16:02:10 +08:00
|
|
|
const carton = await overseas(context, context.overseasToken, 'GET', `平台回查第${index + 1}个退货箱明细`,
|
|
|
|
|
`/api/platform/v1/last-leg/return-inventory/cartons/${encodeURIComponent(expected.cartonMark)}`)
|
|
|
|
|
context.assert(MODULE, `第${index + 1}个退货箱来源费用批次和数量正确`, carton.response,
|
|
|
|
|
carton.data?.sourceBatchNo === expected.batchNo
|
|
|
|
|
&& Number(carton.data?.totalQuantity) === expected.quantity
|
|
|
|
|
&& Array.isArray(carton.data?.items) && carton.data.items.length > 0,
|
|
|
|
|
`batch=${carton.data?.sourceBatchNo || 'missing'}, quantity=${carton.data?.totalQuantity ?? 'missing'}`, OPTIONS)
|
|
|
|
|
const label = await overseas(context, context.overseasToken, 'GET', `平台生成第${index + 1}个退货箱标签`,
|
|
|
|
|
`/api/platform/v1/last-leg/return-inventory/cartons/${encodeURIComponent(expected.cartonMark)}/label`)
|
|
|
|
|
context.assert(MODULE, `第${index + 1}个箱标签保留来源批次`, label.response,
|
|
|
|
|
label.data?.cartonMark === expected.cartonMark && label.data?.sourceBatchNo === expected.batchNo
|
|
|
|
|
&& Number(label.data?.totalQuantity) === expected.quantity && nonBlank(label.data?.printableHtml),
|
|
|
|
|
trace(label.response), OPTIONS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const packing = await overseas(context, picker.token, 'GET', 'PDA查询完成退货单的可合箱库存',
|
|
|
|
|
query('/api/pda/v1/last-leg/return-inventory/packing/orders', { returnNo }))
|
|
|
|
|
const packingSkus = new Map((packing.data?.skus || []).map((row) => [row.skuCode, row]))
|
|
|
|
|
context.assert(MODULE, '退货合箱候选只含本轮完成单且库存准确', packing.response,
|
|
|
|
|
packing.data?.orders?.some?.((order) => order.returnNo === returnNo && order.status === 'COMPLETED')
|
|
|
|
|
&& Number(packingSkus.get(skuA)?.availableQuantity) === 3
|
|
|
|
|
&& Number(packingSkus.get(skuB)?.availableQuantity) === 2,
|
|
|
|
|
trace(packing.response), OPTIONS)
|
|
|
|
|
|
|
|
|
|
const mergeBody = {
|
|
|
|
|
returnNos: [returnNo], items: [{ skuCode: skuA, quantity: 1 }, { skuCode: skuB, quantity: 1 }],
|
|
|
|
|
length: 30, width: 20, height: 10, weight: 2, idempotencyKey: key(context, 'MERGE'),
|
|
|
|
|
}
|
|
|
|
|
const merged = await overseas(context, picker.token, 'POST', 'PDA将同客户退货库存合为新箱',
|
|
|
|
|
'/api/pda/v1/last-leg/return-inventory/cartons/merge', mergeBody)
|
|
|
|
|
if (success(merged.response)) {
|
|
|
|
|
context.assert(MODULE, '退货合箱生成含两SKU和箱规的可打印新箱', merged.response,
|
|
|
|
|
nonBlank(merged.data?.cartonMark) && Number(merged.data?.totalQuantity) === 2
|
2026-09-14 12:00:48 +08:00
|
|
|
&& nonBlank(merged.data?.label?.sourceBatchNo) && merged.data?.label?.sourceBatchNo !== 'MERGED'
|
|
|
|
|
&& merged.data?.label?.sourceReturnNo === returnNo
|
2026-09-11 16:02:10 +08:00
|
|
|
&& Number(merged.data?.label?.items?.length) === 2
|
|
|
|
|
&& nonBlank(merged.data?.label?.printableHtml),
|
|
|
|
|
`cartonMark=${merged.data?.cartonMark || 'missing'}`, OPTIONS)
|
2026-09-14 12:00:48 +08:00
|
|
|
const consumedBatches = []
|
2026-09-20 18:25:12 +08:00
|
|
|
for (const [mark, batch, before] of [[cartonA, firstBatchNo, 3], [cartonB, secondBatchNo, 2]]) {
|
2026-09-14 12:00:48 +08:00
|
|
|
const source = await overseas(context, merchantToken, 'GET', '合箱后回查来源箱实际扣减',
|
|
|
|
|
`${RETURN_INVENTORY}/cartons/${encodeURIComponent(mark)}`)
|
|
|
|
|
if (success(source.response) && Number(source.data?.totalQuantity) < before) consumedBatches.push(batch)
|
|
|
|
|
}
|
|
|
|
|
const actualBatches = String(merged.data?.label?.sourceBatchNo || '').split(',')
|
|
|
|
|
context.assert(MODULE, '合箱来源批次精确等于实际消耗来源且去重', merged.response,
|
|
|
|
|
consumedBatches.length > 0 && new Set(actualBatches).size === actualBatches.length
|
|
|
|
|
&& JSON.stringify([...consumedBatches].sort()) === JSON.stringify([...actualBatches].sort()),
|
|
|
|
|
`expected=${consumedBatches.join(',')}, actual=${actualBatches.join(',')}`, OPTIONS)
|
|
|
|
|
const cartonPage = await overseas(context, merchantToken, 'GET', '客户箱号管理按实物箱分页',
|
|
|
|
|
query(`${RETURN_INVENTORY}/cartons`, { page: 1, size: 20, returnNo }))
|
|
|
|
|
context.assert(MODULE, '箱号分页包含本轮合箱且来源退货单保持真实业务号', cartonPage.response,
|
|
|
|
|
records(cartonPage.data).some(row => row.cartonMark === merged.data.cartonMark
|
|
|
|
|
&& row.sourceReturnNo === returnNo && Number(row.totalQuantity) === 2), trace(cartonPage.response), OPTIONS)
|
|
|
|
|
const label = await overseas(context, merchantToken, 'GET', '客户读取本轮合箱条码标签',
|
|
|
|
|
`${RETURN_INVENTORY}/cartons/${encodeURIComponent(merged.data.cartonMark)}/label`)
|
|
|
|
|
context.assert(MODULE, '标签包含SVG条码和当前箱号', label.response,
|
|
|
|
|
label.data?.cartonMark === merged.data.cartonMark
|
|
|
|
|
&& /<svg\b/.test(label.data?.printableHtml || '')
|
|
|
|
|
&& label.data?.printableHtml?.includes(merged.data.cartonMark), trace(label.response), OPTIONS)
|
2026-09-11 16:02:10 +08:00
|
|
|
const mergeReplay = await overseas(context, picker.token, 'POST', '退货合箱同键同载荷重放',
|
|
|
|
|
'/api/pda/v1/last-leg/return-inventory/cartons/merge', mergeBody)
|
|
|
|
|
context.assert(MODULE, '合箱重放复用同一新箱且不重复扣减', mergeReplay.response,
|
|
|
|
|
success(mergeReplay.response) && nonBlank(merged.data?.cartonMark)
|
|
|
|
|
&& id(mergeReplay.data?.cartonMark) === id(merged.data?.cartonMark), trace(mergeReplay.response), OPTIONS)
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject('合箱同键异载荷返回冲突', conflict, () => context.requestOverseas('POST',
|
2026-09-11 16:02:10 +08:00
|
|
|
'/api/pda/v1/last-leg/return-inventory/cartons/merge',
|
2026-09-15 17:05:07 +08:00
|
|
|
{ ...mergeBody, weight: 3 }, {}, picker.token))
|
2026-09-15 16:10:55 +08:00
|
|
|
|
|
|
|
|
const beforeInvalid = await overseas(context, merchantToken, 'GET', '冻结合箱异常检查前库存快照',
|
|
|
|
|
query(`${RETURN_INVENTORY}/cartons`, { page: 1, size: 200, returnNo }))
|
|
|
|
|
for (const [name, changes, expected] of [
|
|
|
|
|
['零箱长', { length: 0 }, validationFailure],
|
|
|
|
|
['负重量', { weight: -1 }, validationFailure],
|
|
|
|
|
['空合箱明细', { items: [] }, validationFailure],
|
|
|
|
|
['零合箱数量', { items: [{ skuCode: skuA, quantity: 0 }] }, validationFailure],
|
|
|
|
|
['超可用库存', { items: [{ skuCode: skuA, quantity: 999 }] }, conflict],
|
|
|
|
|
['不存在来源单', { returnNos: [`MISSING-${suffix}`] }, notFound],
|
|
|
|
|
]) {
|
2026-09-15 17:05:07 +08:00
|
|
|
await reject(`合箱拒绝${name}`, expected, () => context.requestOverseas('POST', '/api/pda/v1/last-leg/return-inventory/cartons/merge',
|
|
|
|
|
{ ...mergeBody, ...changes, idempotencyKey: key(context, `INVALID-MERGE-${encodeURIComponent(name)}`) }, {}, picker.token))
|
2026-09-15 16:10:55 +08:00
|
|
|
}
|
|
|
|
|
await overseas(context, picker.token, 'POST', '异常检查后再重放原合箱请求',
|
|
|
|
|
'/api/pda/v1/last-leg/return-inventory/cartons/merge', mergeBody)
|
|
|
|
|
const afterInvalid = await overseas(context, merchantToken, 'GET', '回查异常及重放后的全部箱库存',
|
|
|
|
|
query(`${RETURN_INVENTORY}/cartons`, { page: 1, size: 200, returnNo }))
|
|
|
|
|
context.assert(MODULE, '失败与幂等重放不改变箱数数量状态及来源', afterInvalid.response,
|
|
|
|
|
success(beforeInvalid.response) && success(afterInvalid.response)
|
|
|
|
|
&& inventorySnapshot(beforeInvalid.data) === inventorySnapshot(afterInvalid.data)
|
|
|
|
|
&& Number(afterInvalid.data?.total) === 3
|
|
|
|
|
&& records(afterInvalid.data).reduce((sum, row) => sum + Number(row.totalQuantity), 0) === 5,
|
|
|
|
|
trace(afterInvalid.response), OPTIONS)
|
|
|
|
|
const pagedMarks = []
|
|
|
|
|
for (let page = 1; page <= 3; page += 1) {
|
|
|
|
|
const result = await overseas(context, merchantToken, 'GET', `实物箱逐页回查第${page}页`,
|
|
|
|
|
query(`${RETURN_INVENTORY}/cartons`, { page, size: 1, returnNo }))
|
|
|
|
|
context.assert(MODULE, `第${page}页保持总箱数且只有一个箱`, result.response,
|
|
|
|
|
Number(result.data?.total) === 3 && records(result.data).length === 1, trace(result.response), OPTIONS)
|
|
|
|
|
pagedMarks.push(...records(result.data).map(row => row.cartonMark))
|
|
|
|
|
}
|
|
|
|
|
context.assert(MODULE, '跨页箱号无重复无遗漏', afterInvalid.response,
|
|
|
|
|
new Set(pagedMarks).size === 3 && JSON.stringify([...pagedMarks].sort())
|
|
|
|
|
=== JSON.stringify(records(afterInvalid.data).map(row => row.cartonMark).sort()), trace(afterInvalid.response), OPTIONS)
|
|
|
|
|
const merchantSummary = await overseas(context, merchantToken, 'GET', '客户合箱后回查SKU汇总',
|
|
|
|
|
query(`${RETURN_INVENTORY}/summary`, { page: 1, size: 20, skuCode: `RET-`, overseasWarehouseId: warehouse.id }))
|
|
|
|
|
const quantities = itemMap(records(merchantSummary.data))
|
|
|
|
|
context.assert(MODULE, '合箱及异常后客户SKU仍为甲3乙2', merchantSummary.response,
|
|
|
|
|
Number(quantities.get(skuA)?.availableQuantity) === 3 && Number(quantities.get(skuB)?.availableQuantity) === 2,
|
|
|
|
|
trace(merchantSummary.response), OPTIONS)
|
2026-09-11 16:02:10 +08:00
|
|
|
}
|
2026-09-15 15:15:46 +08:00
|
|
|
|
2026-09-22 08:49:21 +08:00
|
|
|
if (context.options.returnMainChainOnly) {
|
|
|
|
|
context.createdResources.phase4OverseasReturnReceipt.excludedScope = 'Additional customer, cross-warehouse and concurrent return boundary scenarios';
|
|
|
|
|
} else {
|
|
|
|
|
await runReturnBoundaries(context, { merchantToken, warehouse, picker, access })
|
|
|
|
|
}
|
2026-09-11 16:02:10 +08:00
|
|
|
} finally {
|
|
|
|
|
await disablePicker(context, picker, 91)
|
|
|
|
|
}
|
|
|
|
|
}
|