2026-09-10 22:43:11 +08:00
|
|
|
import { overseasRequest } from './overseas.js'
|
|
|
|
|
|
|
|
|
|
const BASE = '/pda/v1/last-leg/return-receipts'
|
|
|
|
|
|
|
|
|
|
function count(value) {
|
|
|
|
|
const result = Number(value || 0)
|
|
|
|
|
return Number.isFinite(result) ? Math.max(0, result) : 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeItem(item = {}) {
|
|
|
|
|
return {
|
|
|
|
|
skuCode: String(item.skuCode || ''),
|
|
|
|
|
productNameZh: String(item.productNameZh || ''),
|
|
|
|
|
expectedQuantity: count(item.expectedQuantity),
|
|
|
|
|
receivedQuantity: count(item.receivedQuantity)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 15:01:17 +08:00
|
|
|
function isNotFound(error) {
|
|
|
|
|
const code = String(error?.code || '').toUpperCase()
|
|
|
|
|
return code === 'A0404' || code === '404'
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:43:11 +08:00
|
|
|
export function normalizeReturnReceiptStart(value = {}) {
|
|
|
|
|
return {
|
|
|
|
|
returnNo: String(value.returnNo || ''),
|
|
|
|
|
status: String(value.status || ''),
|
|
|
|
|
expectedItems: Array.isArray(value.expectedItems) ? value.expectedItems.map(normalizeItem) : []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeReturnSkuCandidates(value = {}) {
|
|
|
|
|
return {
|
|
|
|
|
skuCode: String(value.skuCode || ''),
|
|
|
|
|
candidates: Array.isArray(value.candidates) ? value.candidates.map((item) => ({
|
|
|
|
|
returnNo: String(item.returnNo || ''),
|
|
|
|
|
customerReferenceNo: String(item.customerReferenceNo || ''),
|
|
|
|
|
status: String(item.status || ''),
|
|
|
|
|
skuExpectedQuantity: count(item.skuExpectedQuantity),
|
|
|
|
|
skuReceivedQuantity: count(item.skuReceivedQuantity),
|
|
|
|
|
orderExpectedQuantity: count(item.orderExpectedQuantity),
|
|
|
|
|
orderReceivedQuantity: count(item.orderReceivedQuantity)
|
|
|
|
|
})) : []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeReturnReceiptResult(value = {}) {
|
|
|
|
|
return {
|
|
|
|
|
returnNo: String(value.returnNo || ''),
|
|
|
|
|
receiptNo: String(value.receiptNo || ''),
|
|
|
|
|
receiptBatchNo: String(value.receiptBatchNo || ''),
|
|
|
|
|
cartonMark: String(value.cartonMark || ''),
|
|
|
|
|
orderStatus: String(value.orderStatus || ''),
|
|
|
|
|
fullyMatched: Boolean(value.fullyMatched),
|
|
|
|
|
expectedQuantity: count(value.expectedQuantity),
|
|
|
|
|
receivedQuantity: count(value.receivedQuantity),
|
|
|
|
|
signedAt: value.signedAt || null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function startReturnReceipt(returnNo) {
|
2026-09-11 15:01:17 +08:00
|
|
|
try {
|
|
|
|
|
return normalizeReturnReceiptStart(await overseasRequest('POST', `${BASE}/start`, { returnNo: String(returnNo || '').trim() }))
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (isNotFound(error)) return null
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findReturnSkuCandidates(skuCode) {
|
2026-09-11 15:01:17 +08:00
|
|
|
const normalizedSkuCode = String(skuCode || '').trim()
|
|
|
|
|
try {
|
|
|
|
|
return normalizeReturnSkuCandidates(await overseasRequest('POST', `${BASE}/sku-candidates`, { skuCode: normalizedSkuCode }))
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (isNotFound(error)) return normalizeReturnSkuCandidates({ skuCode: normalizedSkuCode, candidates: [] })
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function confirmReturnReceipt(request) {
|
|
|
|
|
return normalizeReturnReceiptResult(await overseasRequest('POST', `${BASE}/confirm`, request))
|
|
|
|
|
}
|