import Vue from 'vue'; // 保留指定位数小数并去掉多余的0 export function formatPrice(price, decimals = 4) { return price.toFixed(decimals).replace(/\.?0+$/, ''); } // 安全格式化数字(兼容 $math 未注入的场景) function safeFormat(value) { if (Vue.prototype.$math && Vue.prototype.$math.format) { return Vue.prototype.$math.format(value, 14) } return Number(value) } //改变数量 export function changeCount(row, countObj, noDiscountSingle) { console.log(row, countObj, noDiscountSingle); let total = row[countObj.countKey] || 0; let data = row; if (row.packageDispositionList) { let endIndex = row.packageDispositionList.findIndex( (ite) => row[countObj.unitIdKey] == ite.id ); for (; 0 < endIndex; endIndex--) { total = safeFormat(row.packageDispositionList[endIndex].packageCell * total) } } data['totalCount'] = total; data['discountSinglePrice'] = !noDiscountSingle ? data.singlePrice : data.discountSinglePrice; setWeight(data); if (row[countObj.countKey] && row.singlePrice) { data['totalPrice'] = row[countObj.countKey] * row.singlePrice; data['discountTotalPrice'] = !noDiscountSingle ? data.totalPrice : data.discountTotalPrice; } else { data['totalPrice'] = 0; data['discountTotalPrice'] = 0; } // getNotaxSinglePrice(data); return data; // if (row) { // singleWeightChange(row, index); // } // return getNumTotalPrice(arr, noDiscountSingle); } //改变数量 export function changeCountNew(row, countObj, noDiscountSingle, weightType) { let data = JSON.parse(JSON.stringify(row)); console.log('changeCountNew~~', data); let total = data[countObj.countKey] || 0; let _endIndex = 0; //计算单重需要 if (data.packageDispositionList) { let endIndex = data.packageDispositionList.findIndex( (ite) => data[countObj.unitIdKey] == ite.id ); _endIndex = endIndex; let packageData = data.packageDispositionList.find( (ite) => data[countObj.unitIdKey] == ite.id ); data[countObj.unitKey] = packageData.conversionUnit; if (data.weightUnit == data.measuringUnit && data.singleWeight) { data.packageDispositionList[1].packageCell = data.singleWeight; } for (; 0 < endIndex; endIndex--) { total = safeFormat(data.packageDispositionList[endIndex].packageCell * total) } } data['totalCount'] = total; data['discountSinglePrice'] = data.singlePrice && data.discountRatio ? formatPrice(+data.singlePrice * (+data.discountRatio /100)) : data.singlePrice; if (weightType == 'totalWeight') { console.log('weightType~~', 1); setSingleWeight(data, countObj, _endIndex); } else { console.log('weightType~~', 2); setWeight(data, countObj); } data['totalPrice'] = 0; data['discountTotalPrice'] = 0; if (data.pricingWay == 2 || data.pricingWay == 3) { if (data.totalWeight && data.singlePrice) { data['totalPrice'] = formatPrice(data.totalWeight * data.singlePrice); } } else { if (data[countObj.countKey] && data.singlePrice) { data['totalPrice'] = formatPrice(data[countObj.countKey] * data.singlePrice); } } data['discountTotalPrice'] = data['totalPrice'] && data.discountRatio ? formatPrice(+data['totalPrice'] * (data.discountRatio /100)) : data['totalPrice']; return data; } function setWeight(row) { if (row.weightUnit == row.measuringUnit) { row['totalWeight'] = row.totalCount; } else if (row.totalCount && row.singleWeight) { row['totalWeight'] = Number((row.totalCount * row.singleWeight).toFixed(2)); } else { row['totalWeight'] = 0; } } export function getAllPrice(arr) { let sum = 0; arr.forEach((item) => { if (item.totalPrice) { sum += Number(item.totalPrice); } }); return isNaN(sum) ? 0 : sum.toFixed(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 = ''; // } // }