Quellcode durchsuchen

feat: 优化收款计划的折让金额计算逻辑,确保物品清单回显时正确同步优惠后总金额

xieyong vor 2 Wochen
Ursprung
Commit
6dc2d163b6

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

@@ -486,6 +486,32 @@
         // 立即更新价格,无需设置isLoadingFromApi为true
         this.refreshprice();
       },
+      // 获取比例换算金额的基准(优惠后总金额)
+      // 取值顺序:
+      // 1. 父组件通过 setDiscountAmount() 传入的 discountAmount(由合同/订单的
+      //    「优惠后总金额」驱动,是首选来源);
+      // 2. info.payAmount(销售订单/采购订单用这个字段);
+      // 3. info.discountTotalPrice(合同用这个字段);
+      // 4. info.totalAmount / info.totalPrice(只剩总金额时的最后兜底)。
+      // 逐级回退是为了防住两类坏情况:
+      // a) 来源单据不含 payAmount 时 Number(undefined)=NaN,会把计划金额写成 "NaN";
+      // b) 父组件在物品清单异步回显期间广播了 0,把基准清零,比例算出来的金额变成 0。
+      getBaseAmount() {
+        const candidates = [
+          this.discountAmount,
+          this.info && this.info.payAmount,
+          this.info && this.info.discountTotalPrice,
+          this.info && this.info.totalAmount,
+          this.info && this.info.totalPrice
+        ];
+        for (const value of candidates) {
+          const amount = Number(value);
+          if (!Number.isNaN(amount) && amount > 0) {
+            return amount;
+          }
+        }
+        return 0;
+      },
       typeChange(val, index = null) {
         // console.log(val, index, '55555');
         if (val) {
@@ -514,7 +540,10 @@
       async ratioInput(val, index = null) {
         val = Number(val);
         if (isNaN(val)) val = 0;
-        this.discountAmount = Number(this.info.payAmount);
+        // 换算基准为优惠后总金额,不能直接取 info.payAmount:
+        // 报价单/核价单/商机等来源的表单不带该字段,Number(undefined) 为 NaN
+        const discountAmount = this.getBaseAmount();
+        this.discountAmount = discountAmount;
         try {
           if (index != null) {
             // 计算含本次新输入值的累计比例
@@ -534,7 +563,7 @@
 
             // 计算当前行计划金额
             let newval = (val / 100).toFixed(2);
-            let price = formatPrice(this.discountAmount * newval);
+            let price = formatPrice(discountAmount * newval);
             this.$set(this.form.datasource[index], 'price', price);
 
             // 计算累计金额(含当前行),单位换算为元(避免浮点精度问题)
@@ -547,7 +576,7 @@
                 ) / 100;
 
             // 多期时累计金额不能超过 discountAmount,使用 0.01 容差避免精度问题
-            const discountAmt = Number(this.discountAmount);
+            const discountAmt = discountAmount;
             if (totalAmount - discountAmt > 0.01) {
               this.$message.error('总金额不能超过优惠后总金额');
               this.$set(this.form.datasource[index], 'ratio', null);
@@ -562,7 +591,7 @@
             }
           } else {
             let newval = (val / 100).toFixed(2);
-            let price = formatPrice(this.discountAmount * newval);
+            let price = formatPrice(discountAmount * newval);
             return price;
           }
         } catch (error) {
@@ -742,7 +771,7 @@
               item.typeName = '交货款';
               item.ratio = 100;
               // 比例 100% 时金额等于优惠后总金额;同步算出避免切回"是"时 price 留空
-              this.discountAmount = Number(this.info.payAmount);
+              this.discountAmount = this.getBaseAmount();
               item.price = formatPrice(this.discountAmount);
               item.key = i + 1;
               item.issueNumber = i + 1;

+ 24 - 7
src/views/contractManage/contractBook/components/addDialog.vue

@@ -1855,28 +1855,45 @@
 
         this.form = contractVO;
         this.form.HJQD = this.form.partbName;
+        // 先同步优惠后总金额,再生成收款计划,保证首期 100% 比例时金额按新总额计算
+        this.$refs.paymentCollectionPlanRef &&
+          this.$refs.paymentCollectionPlanRef.setDiscountAmount(
+            contractVO.discountTotalPrice
+          );
         this.issueNumberChange(this.form.issueNumber);
-        this.$refs.inventoryTable &&
-          this.$refs.inventoryTable.putTableValue(row);
+        // 物品清单回显是异步的,先启动回显,稍后等它结束再收口金额
+        const tableReady = this.$refs.inventoryTable
+          ? this.$refs.inventoryTable.putTableValue(row)
+          : Promise.resolve();
         // const receiptData = {
         //     receiptPaymentList: row.receiptPaymentList,
         //     payAmount: contractVO.discountTotalPrice
         //   }
         // this.$refs.paymentCollectionPlanRef &&
         //   this.$refs.paymentCollectionPlanRef.putTableValue(receiptData);
-        this.$refs.paymentCollectionPlanRef &&
-          this.$refs.paymentCollectionPlanRef.setDiscountAmount(
-            contractVO.discountTotalPrice
-          );
         this.getEnterprise();
         await this.getLinkInfo(
           contractVO.type == '2' ? contractVO.partbId : contractVO.partaId
         );
-        if (contractVO.discountTotalPrice) {
+        // 等物品清单回显结束:回显过程中 inventoryTable 会按行数据汇总「优惠后总金额」
+        // 并广播给收款计划,行数据缺折让字段时汇总为 0,会把收款计划的换算基准清零
+        //(比例有值、计划金额却算成 0)。回显结束后按合同上的优惠后总金额重算各行折让
+        // 并同步基准,保证基准最终等于合同总额。
+        await tableReady;
+        if (this.$refs.inventoryTable && contractVO.discountTotalPrice) {
           this.$refs.inventoryTable.allPrice = contractVO.totalPrice;
           this.$refs.inventoryTable.discountInputByOrder(
             contractVO.discountTotalPrice
           );
+          this.$set(
+            this.form,
+            'discountTotalPrice',
+            contractVO.discountTotalPrice
+          );
+          this.$refs.paymentCollectionPlanRef &&
+            this.$refs.paymentCollectionPlanRef.setDiscountAmount(
+              contractVO.discountTotalPrice
+            );
         }
         this.$nextTick(() => {
           this.getrequireUser(contractVO.requireDeptId);

+ 8 - 4
src/views/purchasingManage/purchaseOrder/components/addDialogNew.vue

@@ -1774,10 +1774,14 @@
         this.$refs.paymentCollectionPlanRef?.setDiscountAmount(this.form.payAmount);
       },
       //产品清单修改单价或者数量重置优惠金额
-      orderDiscountAmount(val = 0) {
-        console.log('orderDiscountAmount~~~~', val);
-        this.form.payAmount = val;
-        this.$refs.paymentCollectionPlanRef.setDiscountAmount(val);
+      orderDiscountAmount(val = 0, totalPrice = 0) {
+        console.log('orderDiscountAmount~~~~', val, totalPrice);
+        // 与销售订单的 setCountAmount 保持一致:折让后金额汇总为 0 时回退到总金额。
+        // 否则物品清单回显/新增行期间的一次 0 汇总会把付款计划的换算基准清零,
+        // 表现为「输入比例但计划付款金额算不出来」。
+        const amount = Number(val) > 0 ? Number(val) : Number(totalPrice);
+        this.form.payAmount = amount;
+        this.$refs.paymentCollectionPlanRef.setDiscountAmount(amount);
         //this.form.payAmount = undefined
       },
 

+ 5 - 1
src/views/purchasingManage/purchaseOrder/components/inventoryTableNew.vue

@@ -1491,7 +1491,11 @@
         this.form.discountTotalPrice = getAllDiscountPrice(this.form.datasource) || 0;
         this.$store.commit('concact/setDiscountAmount', this.form.discountTotalPrice);
         this.$store.commit('order/setAllcountAmount', this.allPrice);
-        this.$emit('orderDiscountAmount', this.form.discountTotalPrice);
+        this.$emit(
+          'orderDiscountAmount',
+          this.form.discountTotalPrice,
+          this.allPrice
+        );
       },
 
       goodsPriceTypeChange(row, index) {

+ 7 - 0
src/views/saleManage/quotation/index.vue

@@ -535,6 +535,13 @@ export default {
       //获取详情
       const data = await getDetail(row.id);
       const contact = await contactDetail(data.contactId);
+      // 与合同台账「选择报价单」入口保持一致:物品行必须带上折让字段。
+      // 缺少折让字段时,物品清单回显后按行汇总出的「优惠后总金额」是 0,
+      // 并把这个 0 广播给收款计划,导致收款计划里输入比例算不出计划金额。
+      (data.quoteProductList || []).forEach((item) => {
+        this.$set(item, 'discountTotalPrice', Number(item.totalPrice));
+        this.$set(item, 'discountSinglePrice', Number(item.singlePrice));
+      });
       let obj = {
         contractVO: {
           categoryId: '',