Преглед на файлове

refactor(发货单): 优化发货单物品清单校验逻辑

liujt преди 7 месеца
родител
ревизия
ca75e569c1

+ 1 - 1
src/views/saleManage/saleOrder/invoice/components/addInvoiceDialog.vue

@@ -930,7 +930,7 @@ import {
             });
           }),
           new Promise((resolve, reject) => {
-            this.$refs.inventoryTableref.validateForm((valid) => {
+            this.$refs.inventoryTableref.validateFormOptimized((valid) => {
               if (!valid) {
                 reject(false);
               } else {

+ 131 - 9
src/views/saleManage/saleOrder/invoice/components/inventoryTable.vue

@@ -81,6 +81,7 @@
           <!-- <el-input v-model="scope.row.batchNo" v-no-chinese></el-input> -->
           <el-select
             v-model="scope.row.batchStockId"
+            clearable
             placeholder="请选择"
             @change="batchNoChange(scope.$index, scope.row)"
           >
@@ -1149,6 +1150,23 @@
 
       //批次号选择
       async batchNoChange(index, row) {
+        if(!row.batchStockId) {
+          this.$set(this.form.datasource[index], 'batchNo', '');
+          this.$set(this.form.datasource[index], 'warehouseNum', '');
+          const tempWarehouse = await getIdWarehouseList({
+            categoryId: this.form.datasource[index].productId,
+          })
+          // 有仓库列表
+          if (tempWarehouse?.length) {
+            console.log('666~~~~!!', index, tempWarehouse);
+            this.$set(
+              this.form.datasource[index],
+              'warehouseList',
+              tempWarehouse
+            );
+          }
+          return;
+        }
         const data = row.batchNoList.find(
           (item) => item.id == row.batchStockId
         );
@@ -1492,7 +1510,7 @@
         this.form.datasource.forEach(async (item, index) => {
           if (indexS.includes(index)) {
             if(!item.batchNo) {
-              console.log('111~~~~');
+              console.log('111~~~~', index);
               this.$set(
                 this.form.datasource[index],
                 'warehouseList',
@@ -1524,7 +1542,7 @@
                 size: -1
               });
 
-              console.log('templist~~~~', templist.list);
+              console.log('templist~~~~', index, templist.list);
 
               // 批次号
               this.$set(
@@ -1548,7 +1566,7 @@
         })
         // 如果有仓库列表
         if (warehouse?.length) {
-          console.log('222~~~~', item.batchNo);
+          console.log('222~~~~', index, item.batchNo);
           // 设置仓库列表
           this.$set(
             this.form.datasource[index],
@@ -1588,7 +1606,7 @@
           // 没有仓库ID,设置第一个仓库ID
           else {
              if (this.form.datasource[index]?.warehouseList?.length){
-              console.log('555~~~~');
+              console.log('555~~~~', index);
               this.$set(
                 this.form.datasource[index],
                 'warehouseId',
@@ -1631,7 +1649,7 @@
             size: -1
           });
           let batchNoList = templist?.list || [];
-          console.log('batchNoList~~~~', batchNoList);
+          console.log('batchNoList~~~~', index, batchNoList);
           // 设置批次列表
           this.$set(
             this.form.datasource[index],
@@ -1640,7 +1658,7 @@
           );
           // 查找批次号是否存在批次列表
           const batchNo = batchNoList.find(i => i.batchNo == item.batchNo);
-          console.log('batchNo~~~~', batchNo);
+          console.log('batchNo~~~~', index, batchNo);
           // 存在
           if (batchNo) {
             // 设置批次ID
@@ -1677,14 +1695,14 @@
         } 
         // 如果没有仓库列表
         else {
-          console.log('333~~~~');
+          console.log('333~~~~', index);
           // 通过产品编码查仓库
           const tempWarehouse = await getIdWarehouseList({
             categoryId: item.productId,
           })
           // 有仓库列表
           if (tempWarehouse?.length) {
-            console.log('666~~~~');
+            console.log('666~~~~', index, tempWarehouse);
             this.$set(
               this.form.datasource[index],
               'warehouseList',
@@ -1718,11 +1736,13 @@
             });
 
             let tempBatchNoList = templist1?.list || [];
-            tempBatchNoList = tempBatchNoList.push({
+            console.log('888~~~~', index, item.batchNo);
+            tempBatchNoList.push({
               batchNo: item.batchNo,
               id: 999,
               measureQuantity: 0
             })
+            console.log('tempBatchNoList~~~~', index, tempBatchNoList);
             // 批次号
             this.$set(
               this.form.datasource[index],
@@ -1775,10 +1795,18 @@
         //开始表单校验
         this.$refs.form.validate((valid, obj) => {
           let is = false;
+          let isBatchNo = false;
+          let isSuspend = false;
           this.form.datasource.forEach((item) => {
             if (Number(item.totalCount) > item.orderTotalCount) {
               is = true;
             }
+            if (item.batchStockId == 999) {
+              isBatchNo = true;
+            }
+            if(item.isSuspend == 1 && (Number(item.totalCount) > item.warehouseNum)) {
+              isSuspend = true;
+            }
           });
           // 退货单退货不校验数量,实物赔偿无法计算
           if (is && this.sourceType != 3) {
@@ -1787,6 +1815,73 @@
               callback(false);
             }
           }
+          if (isBatchNo) {
+            this.$message.warning('你填的批次号与库存批次号不相符,请重新选择批次号出库或删除批次号');
+            callback(false);
+          }
+          if (isSuspend) {
+            this.$message.warning('挂起产品发货数量不能大于库存');
+            callback(false);
+          }
+
+          if (obj) {
+            let messages = Object.keys(obj).map((key) => obj[key][0]);
+            if (messages.length > 0) {
+              this.$message.warning(messages[0].message);
+            }
+          }
+
+          callback(valid);
+        });
+      },
+
+      /**
+       * 优化后的表单校验方法 - 重构版本
+       * 添加了详细的注释、错误消息常量和更清晰的结构
+       */
+      validateFormOptimized(callback) {
+        // 使用常量存储错误消息
+        const ERROR_MESSAGES = {
+          QUANTITY_EXCEEDS_ORDER: '发货数量大于订单总数量',
+          BATCH_NUMBER_INVALID: '你填的批次号与库存批次号不相符,请重新选择批次号出库或删除批次号',
+          SUSPENDED_PRODUCT_OVERSTOCK: '挂起产品发货数量不能大于库存'
+        };
+
+        // 开始表单校验
+        this.$refs.form.validate((valid, obj) => {
+          // 收集所有校验错误
+          const errors = [];
+
+          // 遍历数据源进行业务规则校验
+          this.form.datasource.forEach((item) => {
+            // 检查数量是否超过订单数量(退货单除外)
+            if (this.isQuantityExceedsOrder(item)) {
+              if (this.sourceType != 3) { // 非退货单
+                errors.push({ type: 'quantity', message: ERROR_MESSAGES.QUANTITY_EXCEEDS_ORDER, stopValidation: this.isTotalCount == 1 });
+              }
+            }
+
+            // 检查批次号是否正确
+            if (this.isBatchNumberInvalid(item)) {
+              errors.push({ type: 'batch', message: ERROR_MESSAGES.BATCH_NUMBER_INVALID, stopValidation: true });
+            }
+
+            // 检查挂起产品库存
+            if (this.isSuspendedProductOverstock(item)) {
+              errors.push({ type: 'suspend', message: ERROR_MESSAGES.SUSPENDED_PRODUCT_OVERSTOCK, stopValidation: true });
+            }
+          });
+
+          // 如果存在错误,显示第一个错误并根据情况终止校验
+          if (errors.length > 0) {
+            this.$message.warning(errors[0].message);
+            if (errors[0].stopValidation) {
+              callback(false);
+              return;
+            }
+          }
+
+          // 显示表单字段错误(如果存在)
           if (obj) {
             let messages = Object.keys(obj).map((key) => obj[key][0]);
             if (messages.length > 0) {
@@ -1796,6 +1891,33 @@
 
           callback(valid);
         });
+      },
+
+      /**
+       * 检查数量是否超过订单数量
+       * @param {Object} item 数据项
+       * @returns {boolean} 是否超过
+       */
+      isQuantityExceedsOrder(item) {
+        return Number(item.totalCount) > item.orderTotalCount;
+      },
+
+      /**
+       * 检查批次号是否无效
+       * @param {Object} item 数据项
+       * @returns {boolean} 是否无效
+       */
+      isBatchNumberInvalid(item) {
+        return item.batchStockId == 999;
+      },
+
+      /**
+       * 检查挂起产品是否超过库存
+       * @param {Object} item 数据项
+       * @returns {boolean} 是否超过
+       */
+      isSuspendedProductOverstock(item) {
+        return item.isSuspend == 1 && Number(item.totalCount) > item.warehouseNum;
       }
     }
   };