Просмотр исходного кода

refactor(setProduct): 简化数量计算逻辑,移除lodash依赖

yusheng 7 месяцев назад
Родитель
Сommit
1bd6ded0f2
1 измененных файлов с 27 добавлено и 45 удалено
  1. 27 45
      src/BIZComponents/setProduct.js

+ 27 - 45
src/BIZComponents/setProduct.js

@@ -1,65 +1,48 @@
 import Vue from 'vue';
-import _ from 'lodash';
 
 //改变数量
-export function changeCount(row, countObj, noDiscountSingle, quoteType) {
+export function changeCount(row, countObj, noDiscountSingle) {
   let total = row[countObj.countKey] || 0;
-  let data = { ...row };
-
-  // 计算总数量(包装规格转换)
+  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
+    for (; 0 < endIndex; endIndex--) {
+      total = Vue.prototype.$math.format(
+        row.packageDispositionList[endIndex].packageCell * total,
+        14
       );
     }
   }
-  data['totalCount'] = total; // 转换后的总数量
-
-  // 计算重量(通用逻辑)
-  setWeight(data);
 
-  // 仅非生产加工类型(quoteType≠2)执行基于singlePrice的计算
-  if (quoteType !== 2) {
-    data['discountSinglePrice'] = !noDiscountSingle
-      ? data.singlePrice
-      : data.discountSinglePrice;
+  data['totalCount'] = total;
+  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;
-    }
+  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);
 }
 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
-    );
+    row['totalWeight'] = row.totalCount * row.singleWeight;
   } else {
     row['totalWeight'] = 0;
   }
@@ -68,11 +51,10 @@ export function getAllPrice(arr) {
   let sum = 0;
   arr.forEach((item) => {
     if (item.totalPrice) {
-      // 使用加法并保持精度
-      sum = Vue.prototype.$math.add(sum, Number(item.totalPrice) || 0);
+      sum += Number(item.totalPrice);
     }
   });
-  return Vue.prototype.$math.round(sum, 2);
+  return isNaN(sum) ? 0 : sum.toFixed(2);
 }
 // //计算不含税单价
 // function getNotaxSinglePrice(row) {