orderApprovalFormat.js
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const orderFieldMaps = {
djOrderType: {
'': '订单类型',
1: '正常订单',
2: '打折策略',
3: '买赠策略',
4: '冲量活动',
5: '组合付款',
6: '内部结算'
},
lowPriceFlag: {
1: '低价订单'
}
}
function getFormattedFieldValue(value, format) {
const fieldMap = orderFieldMaps[format]
if (!fieldMap || value === undefined || value === null) return '-'
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,
normalizeOrderGoodsPrices
}