2026-09-20 15:24:04 +08:00
|
|
|
import { overseasDownload, overseasRequest, overseasUpload } from './overseas.js'
|
2026-09-10 22:43:11 +08:00
|
|
|
|
|
|
|
|
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 || ''),
|
2026-09-20 15:24:04 +08:00
|
|
|
expectedItems: Array.isArray(value.expectedItems) ? value.expectedItems.map(normalizeItem) : [],
|
|
|
|
|
completedPallets: Array.isArray(value.completedPallets) ? value.completedPallets.map((item) => ({ palletCode: String(item.palletCode || ''), skuCode: String(item.skuCode || ''), quantity: count(item.quantity), receiptBatchNo: String(item.receiptBatchNo || '') })) : []
|
|
|
|
|
,photos: Array.isArray(value.photos) ? value.photos.map((item) => ({ fileId: String(item.fileId || ''), fileName: String(item.fileName || '退货打托照片'), contentType: String(item.contentType || 'image/jpeg'), sizeBytes: count(item.sizeBytes) })) : []
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = {}) {
|
2026-09-30 14:53:47 +08:00
|
|
|
const result = {
|
2026-09-10 22:43:11 +08:00
|
|
|
returnNo: String(value.returnNo || ''),
|
|
|
|
|
receiptNo: String(value.receiptNo || ''),
|
|
|
|
|
receiptBatchNo: String(value.receiptBatchNo || ''),
|
2026-09-20 15:24:04 +08:00
|
|
|
palletCode: String(value.palletCode || ''),
|
2026-09-10 22:43:11 +08:00
|
|
|
orderStatus: String(value.orderStatus || ''),
|
|
|
|
|
fullyMatched: Boolean(value.fullyMatched),
|
|
|
|
|
expectedQuantity: count(value.expectedQuantity),
|
|
|
|
|
receivedQuantity: count(value.receivedQuantity),
|
|
|
|
|
signedAt: value.signedAt || null
|
|
|
|
|
}
|
2026-09-30 14:53:47 +08:00
|
|
|
if (value.cartonMark != null || value.printableHtml != null || value.totalBoxCount != null) {
|
|
|
|
|
result.cartonMark = String(value.cartonMark || '')
|
|
|
|
|
result.printableHtml = String(value.printableHtml || '')
|
|
|
|
|
result.totalBoxCount = count(value.totalBoxCount)
|
|
|
|
|
}
|
|
|
|
|
if (value.unitType != null || value.unitCode != null) {
|
|
|
|
|
result.unitType = String(value.unitType || (value.palletCode ? 'PALLET' : ''))
|
|
|
|
|
result.unitCode = String(value.unitCode || value.palletCode || '')
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(value.labels)) result.labels = value.labels.map((item) => ({ cartonMark: String(item.cartonMark || ''), printableHtml: String(item.printableHtml || '') })).filter((item) => item.cartonMark && item.printableHtml)
|
|
|
|
|
return result
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-09-30 14:53:47 +08:00
|
|
|
const value = String(request?.palletCode || '')
|
|
|
|
|
const encoded = value.startsWith('BOX:') || value.startsWith('PARCEL:')
|
|
|
|
|
? { ...request, unitType: value.slice(0, value.indexOf(':')), unitCode: value.slice(value.indexOf(':') + 1), palletCode: undefined }
|
|
|
|
|
: request
|
|
|
|
|
return normalizeReturnReceiptResult(await overseasRequest('POST', `${BASE}/confirm`, encoded))
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
2026-09-20 15:24:04 +08:00
|
|
|
|
|
|
|
|
export async function createReturnPallet(returnNo, skuCode) {
|
|
|
|
|
const value = await overseasRequest('POST', `${BASE}/pallets`, { returnNo: String(returnNo || '').trim(), skuCode: String(skuCode || '').trim() })
|
|
|
|
|
return { palletCode: String(value?.palletCode || value?.code || ''), printableHtml: String(value?.printableHtml || '') }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function checkReturnPallet(returnNo, palletCode) {
|
|
|
|
|
const value = await overseasRequest('POST', `${BASE}/pallets/check`, { returnNo: String(returnNo || '').trim(), palletCode: String(palletCode || '').trim() })
|
|
|
|
|
return { palletCode: String(value?.palletCode || ''), empty: Boolean(value?.empty), skuCodes: Array.isArray(value?.skuCodes) ? value.skuCodes.map(String) : [], quantity: count(value?.quantity) }
|
|
|
|
|
}
|
|
|
|
|
export const manuallyCompleteReturnReceipt = (returnNo, discrepancyReason) => overseasRequest('POST', `${BASE}/${encodeURIComponent(String(returnNo || '').trim())}/manual-complete`, { discrepancyReason: String(discrepancyReason || '').trim() })
|
|
|
|
|
export const uploadReturnPalletPhoto = (returnNo, filePath) => overseasUpload(`${BASE}/${encodeURIComponent(String(returnNo || '').trim())}/photos/files`, filePath)
|
|
|
|
|
export const downloadReturnPalletPhoto = (returnNo, fileId) => overseasDownload(`${BASE}/${encodeURIComponent(String(returnNo || '').trim())}/photos/files/${encodeURIComponent(String(fileId || '').trim())}/download`, { expectedContentTypePrefix: 'image/' })
|