优化
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { createPriceEstimateBreakdown } from './priceEstimate.ts'
|
||||
|
||||
test('explains KG estimates and selects volume weight when it is larger', () => {
|
||||
assert.deepEqual(createPriceEstimateBreakdown(16, 0.6, 'KG'), {
|
||||
actualLabel: '实际毛重',
|
||||
actualQuantity: 16,
|
||||
convertedLabel: '体积重量',
|
||||
convertedQuantity: 100.2,
|
||||
convertedFormula: '0.6 m³ × 167',
|
||||
chargeableLabel: '计费重量',
|
||||
basisLabel: '采用体积重量',
|
||||
})
|
||||
})
|
||||
|
||||
test('explains CBM estimates and selects the larger converted quantity', () => {
|
||||
const result = createPriceEstimateBreakdown(400, 1, 'CBM')
|
||||
|
||||
assert.equal(result?.actualLabel, '实际体积')
|
||||
assert.equal(result?.convertedLabel, '重量折算体积')
|
||||
assert.equal(result?.convertedQuantity, 400 / 363)
|
||||
assert.equal(result?.convertedFormula, '400 KG ÷ 363')
|
||||
assert.equal(result?.chargeableLabel, '计费体积')
|
||||
assert.equal(result?.basisLabel, '采用重量折算体积')
|
||||
})
|
||||
|
||||
test('does not invent an explanation for an unsupported charge unit', () => {
|
||||
assert.equal(createPriceEstimateBreakdown(16, 0.6, 'PALLET'), null)
|
||||
})
|
||||
|
||||
test('keeps floating point artifacts out of the displayed formula', () => {
|
||||
assert.equal(createPriceEstimateBreakdown(16, 0.1 + 0.2, 'KG')?.convertedFormula, '0.3 m³ × 167')
|
||||
})
|
||||
@@ -0,0 +1,54 @@
|
||||
const KG_PER_CBM = 167
|
||||
const KG_PER_BILLABLE_CBM = 363
|
||||
|
||||
function formatFormulaNumber(value: number) {
|
||||
return value.toLocaleString('zh-CN', { maximumFractionDigits: 6, useGrouping: false })
|
||||
}
|
||||
|
||||
export interface PriceEstimateBreakdown {
|
||||
actualLabel: string
|
||||
actualQuantity: number
|
||||
convertedLabel: string
|
||||
convertedQuantity: number
|
||||
convertedFormula: string
|
||||
chargeableLabel: string
|
||||
basisLabel: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Explains the server's current first-leg chargeable-quantity rules. The server
|
||||
* response remains authoritative for the final chargeable quantity and amount.
|
||||
*/
|
||||
export function createPriceEstimateBreakdown(
|
||||
actualWeightKg: number,
|
||||
actualVolumeCbm: number,
|
||||
chargeUnit: string,
|
||||
): PriceEstimateBreakdown | null {
|
||||
if (chargeUnit === 'KG') {
|
||||
const volumeWeightKg = actualVolumeCbm * KG_PER_CBM
|
||||
return {
|
||||
actualLabel: '实际毛重',
|
||||
actualQuantity: actualWeightKg,
|
||||
convertedLabel: '体积重量',
|
||||
convertedQuantity: volumeWeightKg,
|
||||
convertedFormula: `${formatFormulaNumber(actualVolumeCbm)} m³ × ${KG_PER_CBM}`,
|
||||
chargeableLabel: '计费重量',
|
||||
basisLabel: actualWeightKg >= volumeWeightKg ? '采用实际毛重' : '采用体积重量',
|
||||
}
|
||||
}
|
||||
|
||||
if (chargeUnit === 'CBM') {
|
||||
const convertedVolumeCbm = actualWeightKg / KG_PER_BILLABLE_CBM
|
||||
return {
|
||||
actualLabel: '实际体积',
|
||||
actualQuantity: actualVolumeCbm,
|
||||
convertedLabel: '重量折算体积',
|
||||
convertedQuantity: convertedVolumeCbm,
|
||||
convertedFormula: `${formatFormulaNumber(actualWeightKg)} KG ÷ ${KG_PER_BILLABLE_CBM}`,
|
||||
chargeableLabel: '计费体积',
|
||||
basisLabel: actualVolumeCbm >= convertedVolumeCbm ? '采用实际体积' : '采用重量折算体积',
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user