minSellPrice.js
5.15 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { isMarketingGoods } from './marketingFlag'
export function isBelowMinSellPrice(goods, value) {
if (!goods || goods.validMinSellPrice !== 'Y' || goods.minSellPrice == null || goods.minSellPrice === '') {
return false
}
return parseFloat(value) < parseFloat(goods.minSellPrice)
}
export function getGoodsDisplayName(goods) {
if (!goods) return ''
return goods.goodsAlias || goods.upgradeGoodsName || goods.goodsInfo?.goodsName || goods.baseGoodsName || ''
}
// 活动价:活动商品加入订单时商品列表 sellPrice 的快照(oldPrice),不随用户改价变化
export function getGoodsActivityPrice(goods, orderItem) {
if (!goods || !isMarketingGoods(goods, orderItem)) return undefined
if (goods.oldPrice == null || goods.oldPrice === '') return undefined
return goods.oldPrice
}
function buildSingleApprovalItem(item, goods, number) {
if (isMarketingGoods(goods, item)) return null
const sellPrice = normalizeGoodsPrice(goods.sellPrice)
if (!isBelowMinSellPrice(goods, sellPrice)) return null
return {
type: 'single',
name: getGoodsDisplayName(goods),
sellPrice,
activityPrice: getGoodsActivityPrice(goods, item),
number: number || '1',
sourceItem: item
}
}
function getPackageDisplayTotal(item, orderTypeCode) {
const orderType = String(orderTypeCode || '')
let total = 0
item.packageGoods.goodsList.forEach(goods => {
const sellPrice = parseFloat(goods.sellPrice || 0)
if (orderType === '1' || orderType === '4') {
total += sellPrice
} else if (goods.finalPricePerUnit != null) {
total += parseFloat(goods.finalPricePerUnit || 0)
} else {
total += sellPrice
}
})
return total
}
function buildPackageApprovalItem(item, orderTypeCode) {
const children = (item.packageGoods.goodsList || [])
.map(goods => {
if (isMarketingGoods(goods, item)) return null
const sellPrice = normalizeGoodsPrice(goods.sellPrice)
if (!isBelowMinSellPrice(goods, sellPrice)) return null
return {
name: getGoodsDisplayName(goods),
sellPrice,
activityPrice: getGoodsActivityPrice(goods, item),
number: item.packageGoods.number || '1',
sourceGoods: goods
}
})
.filter(Boolean)
if (!children.length) return null
return {
type: 'package',
name: item.packageGoods.packageAlias || item.packageGoods.packageName || '',
sellPrice: getPackageDisplayTotal(item, orderTypeCode),
number: item.packageGoods.number || '1',
children,
sourceItem: item
}
}
export function collectBelowMinSellPriceItems(orderObj) {
if (!orderObj) return []
const productFields = ['frontGear', 'frontWindow', 'rearGear', 'rearWindow', 'sunRoof', 'package', 'skuGoods']
const result = []
productFields.forEach(field => {
const list = orderObj[field]
if (!Array.isArray(list)) return
list.forEach(item => {
if (item.goodsFlag == 1 && item.goods) {
const approvalItem = buildSingleApprovalItem(item, item.goods, item.goods.number)
if (approvalItem) result.push(approvalItem)
return
}
if (item.goodsFlag == 2) {
if (item.multipleSpecificationsGoods?.goods) {
const approvalItem = buildSingleApprovalItem(
item,
item.multipleSpecificationsGoods.goods,
item.multipleSpecificationsGoods.number
)
if (approvalItem) result.push(approvalItem)
return
}
if (item.goods) {
const approvalItem = buildSingleApprovalItem(item, item.goods, item.goods.number)
if (approvalItem) result.push(approvalItem)
}
return
}
if (item.goodsFlag == 3 && item.packageGoods) {
const approvalItem = buildPackageApprovalItem(item, orderObj.orderTypeCode)
if (approvalItem) result.push(approvalItem)
}
})
})
return result
}
export function hasBelowMinSellPriceGoods(orderObj) {
return collectBelowMinSellPriceItems(orderObj).length > 0
}
export function shouldUseMinPriceApproval(orderTypeCode) {
return String(orderTypeCode || '') === '1'
}
export function applyLegacyMinSellPriceClamp(goods, value) {
let price = normalizeGoodsPrice(value)
if (isBelowMinSellPrice(goods, price)) {
uni.showToast({
title: '单价不可低于商品最低价',
icon: 'none'
})
price = String(goods.minSellPrice)
}
return price
}
export function normalizeGoodsPrice(value) {
let price = value
if (!price || String(price).trim() === '') {
price = '0'
}
price = String(price).replace(/[^\d.]/g, '')
if (price.split('.').length > 2) {
price = price.slice(0, price.lastIndexOf('.')) + price.slice(price.lastIndexOf('.') + 1)
}
if (price.includes('.')) {
const parts = price.split('.')
if (parts[1].length > 2) {
parts[1] = parts[1].substring(0, 2)
price = parts.join('.')
}
}
if (parseFloat(price) > 100000) {
price = '100000'
}
if (parseFloat(price) < 0 || parseFloat(price) == 0) {
price = '0'
}
return price
}
export function getSkuGoodsPriceTarget(item) {
if (item?.multipleSpecificationsGoods?.goods) {
return item.multipleSpecificationsGoods.goods
}
return item?.goods || null
}