Commit 3c69f9c2 by 张立顺

fix 价格精度问题修复

1 parent 1f6f9eab
......@@ -108,7 +108,10 @@ import dayjs from 'dayjs'
import { ImagePreview } from 'vant'
import orderApprovalApi from '@/api/orderApproval'
import mockOrderDetailRes from './mockData'
import { getFormattedFieldValue as formatFieldValue } from './orderApprovalFormat'
import {
getFormattedFieldValue as formatFieldValue,
normalizeOrderGoodsPrices
} from './orderApprovalFormat'
import GoodsOrderItem from './components/goodsOrderItem.vue'
import ApprovalPopup from './components/approvalPopup.vue'
import store from '@/store'
......@@ -232,17 +235,7 @@ export default {
return processed
},
addPackagePrice(orderDTO) {
const dto = { ...orderDTO }
if (Array.isArray(dto.packageGoodsList)) {
dto.packageGoodsList = dto.packageGoodsList.map(pkg => {
let total = 0
if (Array.isArray(pkg.packageGoodsOrderList)) {
total = pkg.packageGoodsOrderList.reduce((sum, item) => sum + (item.finalPrice || 0), 0)
}
return { ...pkg, packagePrice: total }
})
}
return dto
return normalizeOrderGoodsPrices(orderDTO)
},
async loadOrderDetail(orderCode) {
this.showLoading()
......
......@@ -19,6 +19,75 @@ function getFormattedFieldValue(value, format) {
return Object.prototype.hasOwnProperty.call(fieldMap, value) ? fieldMap[value] : '-'
}
const orderMoneyFields = [
'finalPrice',
'singleProductFinalPrice',
'singleProductSalesPrice',
'totalPrice',
'upgradePrice'
]
function toMoneyCents(value) {
if (value === null || value === undefined || value === '') return null
const amount = Number(value)
if (!Number.isFinite(amount)) return null
const correction = amount < 0 ? -Number.EPSILON : Number.EPSILON
return Math.round((amount + correction) * 100)
}
function normalizeMoney(value) {
const cents = toMoneyCents(value)
return cents === null ? value : cents / 100
}
function normalizeGoodsLine(line = {}) {
return orderMoneyFields.reduce(
(result, field) => {
if (Object.prototype.hasOwnProperty.call(result, field)) {
result[field] = normalizeMoney(result[field])
}
return result
},
{ ...line }
)
}
function sumFinalPrices(goodsList = []) {
const totalCents = goodsList.reduce((total, goods) => {
if (Number(goods.discountType) === 4) return total
const cents = toMoneyCents(goods.finalPrice)
return cents === null ? total : total + cents
}, 0)
return totalCents / 100
}
/**
* 统一审批详情普通商品、套餐子商品和套餐汇总的金额精度。
* 套餐总额直接累加非赠品子项的权威 finalPrice,不通过单价和数量反算。
* @param {object} orderDTO 订单商品数据
* @returns {object} 不修改源对象的金额展示数据
*/
function normalizeOrderGoodsPrices(orderDTO = {}) {
const result = { ...orderDTO }
if (Array.isArray(result.subOrderList)) {
result.subOrderList = result.subOrderList.map(normalizeGoodsLine)
}
if (Array.isArray(result.packageGoodsList)) {
result.packageGoodsList = result.packageGoodsList.map(pkg => {
const packageGoodsOrderList = Array.isArray(pkg.packageGoodsOrderList)
? pkg.packageGoodsOrderList.map(normalizeGoodsLine)
: []
return {
...pkg,
packageGoodsOrderList,
packagePrice: sumFinalPrices(packageGoodsOrderList)
}
})
}
return result
}
module.exports = {
getFormattedFieldValue
getFormattedFieldValue,
normalizeOrderGoodsPrices
}
const assert = require('assert')
const { getFormattedFieldValue } = require('../src/views/orderApproval/orderApprovalFormat')
const {
getFormattedFieldValue,
normalizeOrderGoodsPrices
} = require('../src/views/orderApproval/orderApprovalFormat')
assert.strictEqual(getFormattedFieldValue(0, 'lowPriceFlag'), '-')
assert.strictEqual(getFormattedFieldValue(1, 'lowPriceFlag'), '低价订单')
......@@ -7,4 +10,42 @@ assert.strictEqual(getFormattedFieldValue(undefined, 'lowPriceFlag'), '-')
assert.strictEqual(getFormattedFieldValue(null, 'lowPriceFlag'), '-')
assert.strictEqual(getFormattedFieldValue(2, 'lowPriceFlag'), '-')
const source = {
subOrderList: [
{
upgradePrice: 1520,
singleProductSalesPrice: 999.99,
singleProductFinalPrice: 999.9900000000001
}
],
packageGoodsList: [
{
packageGoodsOrderList: [
{
finalPrice: 285.62,
number: 1,
singleProductFinalPrice: 285.62000000000006
},
{
finalPrice: 2142.74,
number: 1,
singleProductFinalPrice: 2142.74
},
{
discountType: 4,
finalPrice: 100,
singleProductFinalPrice: 100
}
]
}
]
}
const normalized = normalizeOrderGoodsPrices(source)
assert.strictEqual(normalized.subOrderList[0].singleProductFinalPrice, 999.99)
assert.strictEqual(normalized.packageGoodsList[0].packagePrice, 2428.36)
assert.strictEqual(normalized.packageGoodsList[0].packageGoodsOrderList[0].singleProductFinalPrice, 285.62)
assert.strictEqual(source.packageGoodsList[0].packagePrice, undefined)
assert.strictEqual(source.packageGoodsList[0].packageGoodsOrderList[0].singleProductFinalPrice, 285.62000000000006)
console.log('orderApprovalFormat tests passed')
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!