Răsfoiți Sursa

refactor(销售/采购): 统一价格格式化并提高浮点数计算精度

liujt 3 luni în urmă
părinte
comite
b0ed8e9647

+ 6 - 5
src/BIZComponents/paymentCollectionPlan/Index.vue

@@ -222,6 +222,7 @@
 <script>
   import { emailReg, phoneReg, numberReg } from 'ele-admin';
   import { paymentTypeOp } from '@/enum/dict';
+  import { formatPrice } from '@/BIZComponents/setProduct.js';
   export default {
     props: {
       delDetailIds: Array,
@@ -351,11 +352,11 @@
         ];
       },
       allRatio() {
-        return this.form.datasource.reduce((acc, cur) => acc + Number(cur.ratio), 0).toFixed(2);
+        return formatPrice(this.form.datasource.reduce((acc, cur) => acc + Number(cur.ratio), 0));
       },
       allPrice() {
         // 使用分进行计算,避免浮点数精度问题
-        return (this.form.datasource.reduce((acc, cur) => acc + Math.round(Number(cur.price) * 100), 0) / 100).toFixed(2);
+        return formatPrice(this.form.datasource.reduce((acc, cur) => acc + Math.round(Number(cur.price) * 100), 0) / 100);
       },
     },
     watch: {
@@ -404,7 +405,7 @@
           }
           let newval = (val / 100).toFixed(2);
           // console.log('newval~~~', newval)
-          let price = (this.discountAmount * newval).toFixed(2);
+          let price = formatPrice(this.discountAmount * newval);
           // console.log(newval, price, index, '88888');
           if (index != null) {
             // console.log(newval, price, index, '999999');
@@ -464,7 +465,7 @@
             // 直接计算价格,避免使用async/await的forEach
             let val = Number(r.ratio);
             let newval = (val / 100).toFixed(2);
-            let price = (this.discountAmount * newval).toFixed(2);
+            let price = formatPrice(this.discountAmount * newval);
             newData[i] = {
               ...r,
               price: price
@@ -496,7 +497,7 @@
             const lastPrice = Math.round((discountAmount - otherPriceSum) * 100) / 100;
             newData[lastIndex] = {
               ...newData[lastIndex],
-              price: lastPrice.toFixed(2)
+              price: formatPrice(lastPrice)
             };
             console.log('adjusted last item price:', lastPrice);
           }

+ 12 - 10
src/views/purchasingManage/purchaseOrder/components/addDialogNew.vue

@@ -767,6 +767,7 @@
   import PaymentCollectionPlan from '@/BIZComponents/paymentCollectionPlan/Index.vue'
   import { shippingModePurchaseOp, transactionMethodsOp } from '@/enum/dict.js';
   import { formatDate } from '@/utils/dateUtils';
+  import { formatPrice } from '@/BIZComponents/setProduct.js';
 
   export default {
     mixins: [dictMixins],
@@ -1521,6 +1522,7 @@
       },
       discountInput() {
         this.$refs.inventoryTable.discountInputByOrder(this.form.payAmount);
+        this.$refs.paymentCollectionPlanRef?.setDiscountAmount(this.form.payAmount);
       },
       //产品清单修改单价或者数量重置优惠金额
       orderDiscountAmount(val = 0) {
@@ -2182,23 +2184,23 @@
             return;
           }
 
-          // 使用分进行计算,避免浮点数精度问题
-          let receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 100), 0) / 100;
+          // 使用之一进行计算,避免浮点数精度问题(对应4位小数)
+          let receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 10000), 0) / 10000;
           const payAmount = +commitData.payAmount;
           
           if(+ratioSum == 100) {
-            // 使用分进行计算,避免浮点数精度问题
-            const difference = Math.round((payAmount - receiptPaymentListSum) * 100) / 100;
-            if (Math.abs(difference) >= 0.01 && commitData.receiptPaymentList.length > 0) {
+            // 使用之一进行计算,避免浮点数精度问题
+            const difference = Math.round((payAmount - receiptPaymentListSum) * 10000) / 10000;
+            if (Math.abs(difference) >= 0.0001 && commitData.receiptPaymentList.length > 0) {
               const lastIndex = commitData.receiptPaymentList.length - 1;
-              // 使用分进行计算,避免浮点数精度问题
-              const newPrice = Math.round((+commitData.receiptPaymentList[lastIndex].price + difference) * 100) / 100;
-              commitData.receiptPaymentList[lastIndex].price = newPrice.toFixed(2);
-              receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 100), 0) / 100;
+              // 使用之一进行计算,避免浮点数精度问题
+              const newPrice = Math.round((+commitData.receiptPaymentList[lastIndex].price + difference) * 10000) / 10000;
+              commitData.receiptPaymentList[lastIndex].price = formatPrice(newPrice);
+              receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 10000), 0) / 10000;
             }
           }
 
-          if(Math.abs(+receiptPaymentListSum - payAmount) > 0.01){
+          if(Math.abs(+receiptPaymentListSum - payAmount) > 0.0001){
             this.$message.error('计划收款金额合计与优惠后金额不一致');
             return;
           }

+ 11 - 10
src/views/saleManage/saleOrder/components/addDialogNew.vue

@@ -780,6 +780,7 @@
   import personSelect from '@/components/CommomSelect/person-select.vue';
   import PaymentCollectionPlan from '@/BIZComponents/paymentCollectionPlan/Index.vue'
   import { shippingModeOp, transactionMethodsOp } from '@/enum/dict.js';
+  import { formatPrice } from '@/BIZComponents/setProduct.js';
   export default {
     mixins: [dictMixins],
     components: {
@@ -1952,26 +1953,26 @@
               return;
             }
 
-            // 使用分进行计算,避免浮点数精度问题
-            let receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 100), 0) / 100;
+            // 使用之一进行计算,避免浮点数精度问题(对应4位小数)
+            let receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 10000), 0) / 10000;
             const payAmount = +commitData.payAmount;
             
             if(+ratioSum == 100) {
-              // 使用分进行计算,避免浮点数精度问题
-              const difference = Math.round((payAmount - receiptPaymentListSum) * 100) / 100;
+              // 使用之一进行计算,避免浮点数精度问题
+              const difference = Math.round((payAmount - receiptPaymentListSum) * 10000) / 10000;
               console.log('difference~~~', difference);
-              if (Math.abs(difference) >= 0.01 && commitData.receiptPaymentList.length > 0) {
+              if (Math.abs(difference) >= 0.0001 && commitData.receiptPaymentList.length > 0) {
                 const lastIndex = commitData.receiptPaymentList.length - 1;
-                // 使用分进行计算,避免浮点数精度问题
-                const newPrice = Math.round((+commitData.receiptPaymentList[lastIndex].price + difference) * 100) / 100;
-                commitData.receiptPaymentList[lastIndex].price = newPrice.toFixed(2);
-                receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 100), 0) / 100;
+                // 使用之一进行计算,避免浮点数精度问题
+                const newPrice = Math.round((+commitData.receiptPaymentList[lastIndex].price + difference) * 10000) / 10000;
+                commitData.receiptPaymentList[lastIndex].price = formatPrice(newPrice);
+                receiptPaymentListSum = commitData.receiptPaymentList.reduce((acc, cur) => acc + Math.round(+cur.price * 10000), 0) / 10000;
               }
             }
 
             console.log('receiptPaymentListSum~~~', +receiptPaymentListSum, payAmount);
 
-            if(Math.abs(+receiptPaymentListSum - payAmount) > 0.01){
+            if(Math.abs(+receiptPaymentListSum - payAmount) > 0.0001){
               this.$message.error('计划收款金额合计与优惠后金额不一致');
               return;
             }