2026-09-10 22:43:11 +08:00
|
|
|
import { overseasRequest } from './overseas.js'
|
|
|
|
|
|
|
|
|
|
const BASE = '/pda/v1/last-leg/return-inventory'
|
|
|
|
|
const encoded = (value) => encodeURIComponent(String(value || '').trim())
|
|
|
|
|
const count = (value) => Number.isFinite(Number(value)) ? Math.max(0, Number(value)) : 0
|
|
|
|
|
|
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
|
|
|
function normalizeItems(items) {
|
|
|
|
|
return Array.isArray(items) ? items.map((item) => ({
|
|
|
|
|
skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''),
|
|
|
|
|
quantity: count(item.quantity ?? item.availableQuantity)
|
|
|
|
|
})) : []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizePackingPreview(value = {}) {
|
2026-10-08 15:34:47 +08:00
|
|
|
value = value && typeof value === 'object' ? value : {}
|
|
|
|
|
const normalizeCustomer = (customer = {}) => ({
|
|
|
|
|
customerId: customer.customerId == null ? '' : String(customer.customerId),
|
|
|
|
|
orders: Array.isArray(customer.orders) ? customer.orders.map((item) => ({
|
|
|
|
|
returnNo: String(item.returnNo || ''), customerReferenceNo: String(item.customerReferenceNo || ''), status: String(item.status || '')
|
|
|
|
|
})) : [],
|
|
|
|
|
skus: Array.isArray(customer.skus) ? customer.skus.map((item) => ({
|
|
|
|
|
skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''), availableQuantity: count(item.availableQuantity)
|
|
|
|
|
})) : []
|
|
|
|
|
})
|
|
|
|
|
const result = {
|
2026-09-10 22:43:11 +08:00
|
|
|
customerKey: value.customerId == null ? '' : String(value.customerId),
|
|
|
|
|
orders: Array.isArray(value.orders) ? value.orders.map((item) => ({
|
|
|
|
|
returnNo: String(item.returnNo || ''), customerReferenceNo: String(item.customerReferenceNo || ''), status: String(item.status || '')
|
|
|
|
|
})) : [],
|
|
|
|
|
skus: Array.isArray(value.skus) ? value.skus.map((item) => ({
|
|
|
|
|
skuCode: String(item.skuCode || ''), productNameZh: String(item.productNameZh || ''), availableQuantity: count(item.availableQuantity)
|
|
|
|
|
})) : []
|
|
|
|
|
}
|
2026-10-08 15:34:47 +08:00
|
|
|
if (Array.isArray(value.customers)) result.customers = value.customers.map(normalizeCustomer).filter((item) => item.customerId)
|
|
|
|
|
else if (result.customerKey && result.orders.length) {
|
|
|
|
|
// Older service versions returned the single-customer shape. Keep the
|
|
|
|
|
// PDA behavior consistent by exposing it through the grouped contract.
|
|
|
|
|
result.customers = [{ customerId: result.customerKey, orders: result.orders, skus: result.skus }]
|
|
|
|
|
}
|
|
|
|
|
return result
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeReturnLabel(value = {}) {
|
|
|
|
|
return {
|
|
|
|
|
cartonMark: String(value.cartonMark || ''), sourceBatchNo: String(value.sourceBatchNo || ''),
|
|
|
|
|
sourceReturnNo: String(value.sourceReturnNo || ''), totalQuantity: count(value.totalQuantity),
|
|
|
|
|
length: count(value.length), width: count(value.width), height: count(value.height), weight: count(value.weight),
|
|
|
|
|
items: normalizeItems(value.items), generatedAt: value.generatedAt || null, printableHtml: String(value.printableHtml || '')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 15:34:47 +08:00
|
|
|
export async function findPackingOrders(returnNo = '', mode = 'RETURN_NO') {
|
2026-09-10 22:43:11 +08:00
|
|
|
const query = String(returnNo || '').trim()
|
2026-09-11 15:01:17 +08:00
|
|
|
try {
|
2026-10-08 15:34:47 +08:00
|
|
|
return normalizePackingPreview(await overseasRequest('GET', `${BASE}/packing/orders${query ? `?${mode === 'RETURN_NO' ? 'returnNo' : 'skuCode'}=${encoded(query)}` : ''}`))
|
2026-09-11 15:01:17 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
if (isNotFound(error)) return normalizePackingPreview()
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2026-09-10 22:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-08 15:34:47 +08:00
|
|
|
export async function listPackingPlans() {
|
|
|
|
|
const value = await overseasRequest('GET', `${BASE}/packing/plans`)
|
|
|
|
|
return Array.isArray(value) ? value.map(normalizePackingPlan) : []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function completePackingPlan(planNo, request) {
|
|
|
|
|
const value = await overseasRequest('POST', `${BASE}/packing/plans/${encoded(planNo)}/complete`, request)
|
|
|
|
|
return { planNo: String(value?.planNo || planNo), labels: Array.isArray(value?.labels) ? value.labels.map(normalizeReturnLabel) : [] }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizePackingPlan(value = {}) {
|
|
|
|
|
return { planNo: String(value.planNo || ''), customerId: String(value.customerId || ''), returnNos: Array.isArray(value.returnNos) ? value.returnNos.map(String) : [], sourceCartonMarks: Array.isArray(value.sourceCartonMarks) ? value.sourceCartonMarks.map(String) : [], sources: Array.isArray(value.sources) ? value.sources : [], mode: String(value.mode || ''), status: String(value.status || ''), boxes: Array.isArray(value.boxes) ? value.boxes : [], remark: String(value.remark || '') }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getPackingPlan(planNo) {
|
|
|
|
|
return normalizePackingPlan(await overseasRequest('GET', `${BASE}/packing/plans/${encoded(planNo)}`))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function scanReturnCarton(mark) {
|
|
|
|
|
return overseasRequest('GET', `${BASE}/cartons/${encoded(mark)}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function mergeReturnBoxes(request) {
|
|
|
|
|
const value = await overseasRequest('POST', `${BASE}/cartons/merge-boxes`, request)
|
|
|
|
|
return { labels: [normalizeReturnLabel(value?.label)] }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function packReturnSkus(request) {
|
|
|
|
|
const value = await overseasRequest('POST', `${BASE}/cartons/pack`, request)
|
|
|
|
|
return { labels: Array.isArray(value?.labels) ? value.labels.map(normalizeReturnLabel) : [] }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function putawayReturnCarton(mark, request) {
|
|
|
|
|
return overseasRequest('POST', `${BASE}/cartons/${encoded(mark)}/putaway`, request)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:43:11 +08:00
|
|
|
export async function mergeReturnCartons(request) {
|
|
|
|
|
const value = await overseasRequest('POST', `${BASE}/cartons/merge`, request)
|
|
|
|
|
return { cartonMark: String(value?.cartonMark || ''), skuCode: String(value?.skuCode || ''), totalQuantity: count(value?.totalQuantity), label: normalizeReturnLabel(value?.label) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getReturnCartonLabel(cartonMark) {
|
|
|
|
|
return normalizeReturnLabel(await overseasRequest('GET', `${BASE}/cartons/${encoded(cartonMark)}/label`))
|
|
|
|
|
}
|