Commit 72284c02 by 李伟康

fix(BusinessInformation): 修复毛利率计算逻辑和输入过滤问题

- 修复毛利率计算中的除零和无限值边界情况处理
- 改进数值计算精度控制,使用原生除法替代NP库计算
- 修复组件数据绑定参数传递错误
- 移除预估交付数字段的最大长度限制
- 改进输入过滤逻辑,增加前导零去除功能
- 修复下拉选项显示逻辑,支持备用视图键值回退
1 parent 2c67261a
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
<template v-else> <template v-else>
<el-input <el-input
v-model="componentData[i.key]" v-model="componentData[i.key]"
@input="filterInputVal(i,$event,'data')" @input="filterInputVal(i,$event,'componentData')"
:maxlength="i.maxlength || -1" :maxlength="i.maxlength || -1"
></el-input> ></el-input>
</template> </template>
...@@ -131,9 +131,30 @@ export default { ...@@ -131,9 +131,30 @@ export default {
productDetails.forEach(item => { productDetails.forEach(item => {
revenue = NP.plus(revenue, item.revenue || 0) revenue = NP.plus(revenue, item.revenue || 0)
}) })
if (revenue === 0) return '0.00%'
const gpr = NP.times(NP.divide(djGrossProfit, revenue), 100) // 检查边界情况
return NP.round(gpr, 2) + '%' if (revenue === 0 || !isFinite(revenue)) return '0.00%'
if (!isFinite(djGrossProfit)) return '0.00%'
try {
// 使用原生除法,然后手动控制精度
const division = djGrossProfit / revenue
// 检查结果是否有效
if (!isFinite(division)) return '0.00%'
// 转换为百分比并保留2位小数(四舍五入)
const percentage = division * 100
const roundedPercentage = Math.round(percentage * 100) / 100
// 最终检查
if (!isFinite(roundedPercentage)) return '0.00%'
return roundedPercentage.toFixed(2) + '%'
} catch (error) {
console.error('毛利率计算错误:', error)
return '0.00%'
}
} }
}, },
data () { data () {
...@@ -144,7 +165,7 @@ export default { ...@@ -144,7 +165,7 @@ export default {
{ label: '嘀加毛利率', key: 'djGpr', type: 'text' }, { label: '嘀加毛利率', key: 'djGpr', type: 'text' },
], ],
businessInformation: [ businessInformation: [
{ label: '预估交付数/月', key: 'deliveryCount', filterRule: 'integerRange', min: 0, max: 999999, maxlength: 6 }, { label: '预估交付数/月', key: 'deliveryCount', filterRule: 'integerRange', min: 0, max: 999999 },
{ label: '预估触客率', key: 'customerTouchRate', unit: '%', filterRule: 'decimalRange', min: 0, max: 100, precision: 2 }, { label: '预估触客率', key: 'customerTouchRate', unit: '%', filterRule: 'decimalRange', min: 0, max: 100, precision: 2 },
{ label: '人员成本', key: 'personnelCost', filterRule: 'decimalRange', min: 0, max: 999999, precision: 2 }, { label: '人员成本', key: 'personnelCost', filterRule: 'decimalRange', min: 0, max: 999999, precision: 2 },
{ label: '房租', key: 'rentCost', filterRule: 'decimalRange', min: 0, max: 999999, precision: 2 }, { label: '房租', key: 'rentCost', filterRule: 'decimalRange', min: 0, max: 999999, precision: 2 },
......
...@@ -246,7 +246,7 @@ export default { ...@@ -246,7 +246,7 @@ export default {
if (!value || !i?.options?.length) return '-' if (!value || !i?.options?.length) return '-'
const option = i.options.find(item => item.value === value) const option = i.options.find(item => item.value === value)
return option?.label || value || '-' return option?.label || (i.viewKey && this.componentData[i.viewKey]) || value || '-'
}, },
getMultipleSelectText (i) { getMultipleSelectText (i) {
const values = this.componentData[i.key] const values = this.componentData[i.key]
......
...@@ -15,6 +15,11 @@ export default { ...@@ -15,6 +15,11 @@ export default {
// 只保留数字 // 只保留数字
let filtered = (val || '').replace(/\D/g, ''); let filtered = (val || '').replace(/\D/g, '');
// 去除前导零(但保留单独的0)
if (filtered.length > 1) {
filtered = filtered.replace(/^0+/, '');
}
// 转换为数字进行范围检查 // 转换为数字进行范围检查
const numValue = parseInt(filtered, 10); const numValue = parseInt(filtered, 10);
...@@ -32,10 +37,23 @@ export default { ...@@ -32,10 +37,23 @@ export default {
if (parts.length > 2) { if (parts.length > 2) {
filtered = parts[0] + '.' + parts.slice(1).join(''); filtered = parts[0] + '.' + parts.slice(1).join('');
} }
// 重新分割,确保处理正确
const newParts = filtered.split('.');
// 去除整数部分的前导零(但保留单独的0或小数点前的0)
if (newParts[0].length > 1) {
newParts[0] = newParts[0].replace(/^0+/, '');
}
// 限制小数位数 // 限制小数位数
if (parts.length === 2 && parts[1].length > i.precision) { if (newParts.length === 2 && newParts[1].length > i.precision) {
filtered = parts[0] + '.' + parts[1].substring(0, i.precision); newParts[1] = newParts[1].substring(0, i.precision);
} }
// 重新组合
filtered = newParts.join('.');
// 转换为数字进行范围检查 // 转换为数字进行范围检查
const numValue = parseFloat(filtered); const numValue = parseFloat(filtered);
// 如果超出最大值,限制为最大值 // 如果超出最大值,限制为最大值
...@@ -46,7 +64,6 @@ export default { ...@@ -46,7 +64,6 @@ export default {
filtered = String(i.max); filtered = String(i.max);
} }
} }
this.$set(this[keyName], i.key, filtered); this.$set(this[keyName], i.key, filtered);
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!