test: normalize finance container allocation sources
This commit is contained in:
@@ -9,6 +9,24 @@ function rows(value) { return Array.isArray(value) ? value : Array.isArray(value
|
||||
function dateOffset(days) { const value = new Date(); value.setUTCDate(value.getUTCDate() + days); return value.toISOString().slice(0, 10) }
|
||||
function blocker(context, name, evidence) { context.addStep(MODULE, name, null, false, evidence, OPTIONS) }
|
||||
|
||||
export function normalizeFinanceFixtureSources(sources, orders, allocations, customerId, marker) {
|
||||
const orderById = new Map(rows(orders).map((order) => [id(order?.orderId ?? order?.id), order]))
|
||||
const allocationOrders = rows(allocations).map((allocation) => orderById.get(id(allocation?.orderId))).filter(Boolean)
|
||||
return rows(sources).map((source) => {
|
||||
const sourceOrder = orderById.get(id(source?.orderId))
|
||||
|| (source?.billingBasis === 'CONTAINER_SHARED' ? allocationOrders[0] : null)
|
||||
return {
|
||||
...source,
|
||||
customerId: id(source?.customerId ?? customerId) || null,
|
||||
marker: source?.marker ?? marker,
|
||||
orderId: id(source?.orderId ?? sourceOrder?.orderId ?? sourceOrder?.id) || null,
|
||||
waybillId: id(source?.waybillId ?? sourceOrder?.waybillId) || null,
|
||||
shipmentId: id(source?.shipmentId ?? sourceOrder?.shipmentId) || null,
|
||||
orderNo: source?.orderNo ?? sourceOrder?.orderNo ?? null,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function createOrder(context, suffix, pricingProfile = null) {
|
||||
if (context.financeFixtureHooks?.createOrder) return context.financeFixtureHooks.createOrder(context, suffix, pricingProfile)
|
||||
const originalMarker = context.marker
|
||||
@@ -438,22 +456,28 @@ export async function run(context) {
|
||||
context.assert(MODULE, '取得订单真实运单标识', null,
|
||||
measured.length > 0 && measured.every((item) => item.waybillId || item.shipmentId || item.orderNo),
|
||||
'使用订单域真实 waybillId、shipmentId 或 orderNo,不使用数据库主键冒充运单标识', OPTIONS)
|
||||
const fixtureOrders = measured.map((item) => ({ orderId: id(item.orderId), orderNo: item.orderNo || null,
|
||||
shipmentId: item.shipmentId ? id(item.shipmentId) : null,
|
||||
waybillId: item.waybillId ? id(item.waybillId) : (item.shipmentId ? id(item.shipmentId) : null), inboundId: id(item.inboundId),
|
||||
actualWeightKg: item.actualWeightKg, totalVolumeCbm: item.totalVolumeCbm,
|
||||
businessDate: fixtures.find((fee) => fee.orderId === id(item.orderId))?.businessDate || null }))
|
||||
const fixtureSources = normalizeFinanceFixtureSources(fixtures, fixtureOrders, allocations, customerId, context.marker)
|
||||
const invalidFixtureSources = fixtureSources.filter((source) => !source.feeSourceId || !source.customerId
|
||||
|| !source.businessDate || !(source.waybillId || source.shipmentId || source.orderNo) || source.marker !== context.marker)
|
||||
context.assert(MODULE, '输出完整 Finance 来源契约', null, invalidFixtureSources.length === 0,
|
||||
`sources=${fixtureSources.length}, invalid=${invalidFixtureSources.length};字段必须来自本轮费用来源、客户、订单/运单和真实柜次分摊`, OPTIONS)
|
||||
context.createdResources.financeFullFixtures = {
|
||||
marker: context.marker, customerId,
|
||||
prerequisites: {
|
||||
container: containerOwned && Boolean(containerFixture?.feeSourceId) && allocations.length > 0
|
||||
? { ready: true, category: null }
|
||||
: { ready: false, category: 'container-prerequisite', evidence: '缺少同 marker 已完成柜次、有效 containerBookingId、按柜费用来源或真实分摊' },
|
||||
financeSources: fixtures.length > 0
|
||||
financeSources: fixtureSources.length > 0 && invalidFixtureSources.length === 0
|
||||
? { ready: true, category: null }
|
||||
: { ready: false, category: 'finance-source-prerequisite', evidence: '本轮 marker 未产生可消费 Finance 费用来源' },
|
||||
: { ready: false, category: 'finance-source-prerequisite', evidence: '本轮 marker 未产生字段完整的可消费 Finance 费用来源' },
|
||||
},
|
||||
orders: measured.map((item) => ({ orderId: id(item.orderId), orderNo: item.orderNo || null,
|
||||
shipmentId: item.shipmentId ? id(item.shipmentId) : null,
|
||||
waybillId: item.waybillId ? id(item.waybillId) : (item.shipmentId ? id(item.shipmentId) : null), inboundId: id(item.inboundId),
|
||||
actualWeightKg: item.actualWeightKg, totalVolumeCbm: item.totalVolumeCbm,
|
||||
businessDate: fixtures.find((fee) => fee.orderId === id(item.orderId))?.businessDate || null })),
|
||||
feeSources: fixtures,
|
||||
orders: fixtureOrders,
|
||||
feeSources: fixtureSources,
|
||||
container: containerFixture ? { containerBookingId: containerFixture.containerBookingId,
|
||||
containerNo: containerFixture.containerNo } : { containerBookingId: null, containerNo: null },
|
||||
allocations, supplier: supplier?.id ? { supplierId: id(supplier.id), partnerId: id(supplier.id), supplierName: supplier.name || null } : null,
|
||||
|
||||
Reference in New Issue
Block a user