import Vue from 'vue'; import _ from 'lodash'; //改变数量 export function changeCount(row, countObj, noDiscountSingle, quoteType) { let total = row[countObj.countKey] || 0; let data = { ...row }; // 计算总数量(包装规格转换) if (row.packageDispositionList) { let endIndex = row.packageDispositionList.findIndex( (ite) => row[countObj.unitIdKey] == ite.id ); // 修正包装规格转换逻辑(确保层级相乘正确) for (let i = 1; i <= endIndex; i++) { // 从1开始,避免重复计算 total = Vue.prototype.$math.multiply( row.packageDispositionList[i].packageCell, total ); } } data['totalCount'] = total; // 转换后的总数量 // 计算重量(通用逻辑) setWeight(data); // 仅非生产加工类型(quoteType≠2)执行基于singlePrice的计算 if (quoteType !== 2) { data['discountSinglePrice'] = !noDiscountSingle ? data.singlePrice : data.discountSinglePrice; // 关键修复:用转换后的totalCount计算总价,而非原始数量 if (data.totalCount && data.singlePrice) { const count = Number(data.totalCount) || 0; // 使用转换后的总数量 const price = Number(data.singlePrice) || 0; data['totalPrice'] = Vue.prototype.$math.round( Vue.prototype.$math.multiply(count, price), 2 ); data['discountTotalPrice'] = !noDiscountSingle ? data.totalPrice : Vue.prototype.$math.round(Number(data.discountTotalPrice) || 0, 2); } else { data['totalPrice'] = 0; data['discountTotalPrice'] = 0; } } return data; } function setWeight(row) { if (row.weightUnit == row.measuringUnit) { row['totalWeight'] = row.totalCount; } else if (row.totalCount && row.singleWeight) { // 使用lodash的round函数保留4位小数并向上舍入 row['totalWeight'] = _.round( row.totalCount * row.singleWeight, 4, Math.ceil ); } else { row['totalWeight'] = 0; } } export function getAllPrice(arr) { let sum = 0; arr.forEach((item) => { if (item.totalPrice) { // 使用加法并保持精度 sum = Vue.prototype.$math.add(sum, Number(item.totalPrice) || 0); } }); return Vue.prototype.$math.round(sum, 2); } // //计算不含税单价 // function getNotaxSinglePrice(row) { // if (row.singlePrice && row.taxRate) { // row['notaxSinglePrice'] = parseFloat( // (row.singlePrice / (1 + row.taxRate / 100)).toFixed(2) // ); // } else { // row['notaxSinglePrice'] = ''; // } // } // //计算总金额 // function getNumTotalPrice(arr, noDiscountSingle) { // let sum = 0; // arr.forEach((r, index) => { // // if (r.singlePrice) { // // r.notaxSinglePrice=r.taxRate?r.singlePrice/(1-(r.taxRate/100)):r.singlePrice // // } // if (r.singlePrice && r.totalCount) { // Vue.set( // arr[index], // 'discountSinglePrice', // !noDiscountSingle ? r.singlePrice : r.discountSinglePrice // ); // Vue.set(arr[index], 'totalPrice', getAllPrice(r)); // Vue.set( // arr[index], // 'discountTotalPrice', // !noDiscountSingle ? r.totalPrice : r.discountTotalPrice // ); // sum += Number(r.totalPrice); // } else { // Vue.set(arr[index], 'totalPrice', 0); // Vue.set(arr[index], 'discountTotalPrice',0); // } // }); // return { allPrice: isNaN(sum) ? 0 : sum.toFixed(2), arr }; // } // //获取合计 // function getAllPrice(row) { // let num = 0; // if (row.pricingWay == 1) { // //按数量计价计算总金额 // num = Number(row.singlePrice) * Number(row.totalCount); // } // if (row.pricingWay == 2) { // //按重量计价计算总金额 // num = // Number(row.singlePrice) * // Number(row.totalCount) * // Number(row.singleWeight); // } // return isNaN(num) ? '' : num.toFixed(2); // } // function singleWeightChange(row) { // console.log(row, 'row'); // if (row && row.singleWeight && row.totalCount) { // row.totalWeight = (row.singleWeight * row.totalCount).toFixed(2) || 0; // } else { // row.totalWeight = ''; // } // }