haoyahui
2023-11-16 94de1745b4b69eeeb1ac8c5d3508ce87258afc25
admin-web/src/views/stock/procure/purchaseOrder/edit.vue
@@ -65,11 +65,7 @@
              <el-form-item
                label="物品名称"
                :prop="`procureGoods[${goodsIndex}].baseGoodsTemplateId`"
                :rules="{
                  required: true,
                  message: '请选择',
                  trigger: 'change',
                }"
                :rules="rules.baseGoodsTemplateId"
              >
                <el-select
                  v-model="goodsItem.baseGoodsTemplateId"
@@ -137,12 +133,26 @@
            </el-table-column>
            <el-table-column prop="price" label="单价(元)" align="center">
              <template slot-scope="scope">
                <el-input type="number" v-model="scope.row.price"></el-input>
                <el-form-item
                  label-width="0"
                  style="margin-bottom: 0"
                  :prop="`procureGoods[${goodsIndex}].models[${scope.$index}].price`"
                  :rules="rules.price"
                >
                  <el-input type="number" v-model.number="scope.row.price"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="counts" label="采购数量" align="center">
              <template slot-scope="scope">
                <el-input type="number" v-model="scope.row.counts"></el-input>
                <el-form-item
                  label-width="0"
                  style="margin-bottom: 0"
                  :prop="`procureGoods[${goodsIndex}].models[${scope.$index}].counts`"
                  :rules="rules.counts"
                >
                  <el-input v-model.number="scope.row.counts"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
@@ -189,29 +199,69 @@
import winMd from '@/components/win/win-md';
import upload from '@/components/upload/index';
import { getUploadUrl,getDownUrl} from '@/utils/base';
import SettingIplatform from '../../../../../public/static/config';
import { findParentIds } from '@/utils/index';
import { mapGetters } from 'vuex';
export default {
  components: { MyButton, winMd, upload },
  props: {
    setting: {
      type: Object,
      default: () => {
      }
    }
      default: () => {},
    },
  },
  data() {
    // 自定义校验
    // 物品重复校验
    var checkGoodsTemplateId = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请选择'));
      }
      let temp = this.formData.procureGoods.filter((v) => v.baseGoodsTemplateId == value);
      if (!temp || temp.length!=1) {
        return callback(new Error('已选过此物品'));
      }
      callback();
    };
    // 金额校验
    var checkPrice = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入'));
      }
      if (value == 0) {
        this.$message.warning('数字值不能为0');
        return callback(new Error('数字值不能为0'));
      }
      callback();
    };
    // 数量校验
    var checkCounts = (rule, value, callback) => {
      console.log(value)
      if (!value) {
        return callback(new Error('请输入'));
      }
      if (!Number.isInteger(value)) {
        this.$message.warning('请输入数字值');
        return callback(new Error('请输入数字值'));
      }
      if (value == 0) {
        this.$message.warning('数字值不能为0');
        return callback(new Error('数字值不能为0'));
      }
      callback();
    };
    return {
      visible: false,
      loading: false,
      buyTypeOptions:[{
      buyTypeOptions: [
        {
        label:'集采',
        value: '1'
      },{
          value: '1',
        },
        {
        label:'自采',
        value: '2'
      }],
          value: '2',
        },
      ],
      fileList:[],
      warehouses: [], // 入库仓库列表
      categoryOptions: [], // 物品分类列表
@@ -237,9 +287,8 @@
      },
      modelsItem: {
        baseGoodsModelsId: '', // 规格型号编号
        price: 0, // 单价(采购需要,调拨不需要)
        counts: 0, // 操作数量
        supplier: '', // 供应商
        price: null, // 单价(采购需要,调拨不需要)
        counts: null, // 操作数量
        unit: null, //单位
      },
      rules: {
@@ -249,8 +298,10 @@
        procureDoc: [{ required: true, message: '请上传', trigger: 'change' }],
        baseCategoryIds: [{ required: true, message: '请选择', trigger: 'change' }],
        baseGoodsTemplateId: [{ required: true, message: '请选择', trigger: 'change' }],
        baseGoodsTemplateId: [{ validator: checkGoodsTemplateId, trigger: ['blur', 'change'] }],
        modelsIds: [{ required: true, message: '请选择', trigger: 'change' }],
        price: [{ validator: checkPrice, trigger: 'blur' }],
        counts: [{ validator: checkCounts, trigger: 'blur' }],
      },
      uploadSettings: {
@@ -266,13 +317,16 @@
      },
    };
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {
    this.init()
    this.init();
  },
  methods: {
    async init() {
      this.getWarehouseList();
      this.getgoodsTemplate()
      this.getgoodsTemplate();
      this.getgoodsModel();
      // 获取物品分类列表
      const treeRes = await getTree();
@@ -281,30 +335,33 @@
        const detail = await procureDetail({ id:this.setting.id });
        this.formData = Object.assign(this.formData, detail);
        if(this.formData.procureDoc) {
          this.fileList = JSON.parse(this.formData.procureDoc)
          this.fileList = JSON.parse(this.formData.procureDoc);
        }
        this.$set(this.formData,'buyType',this.formData.buyType.toString())
        this.$set(this.formData, 'buyType', this.formData.buyType.toString());
        this.formData.procureTime = this.formData.procureTime.toString();
        this.formData.procureGoods.map((item, index) => {
          // 根据子集ID拼接物品分类列表
          item.baseCategoryIds = this.findParentIds(this.categoryOptions, item.baseCategoryId);
          this.$set(this.formData.procureGoods[index],'modelsIds',item.models.map((v) => v.baseGoodsModelsId))
          item.baseCategoryIds = findParentIds(this.categoryOptions, item.baseCategoryId);
          this.$set(
            this.formData.procureGoods[index],
            'modelsIds',
            item.models.map((v) => v.baseGoodsModelsId),
          );
          this.getgoodsTemplate(item.baseCategoryId, index);
          this.getgoodsModel(item.baseGoodsTemplateId, index);
          return item
          return item;
        });
        console.log('this.formData.procureGoods',this.formData.procureGoods)
      } else {
        this.formData.procureGoods.push(JSON.parse(JSON.stringify(this.goodsItem)));
      }
      this.visible = true;
    },
    // 获取入库仓库列表
    getWarehouseList() {
      selectTenantWarehouse()
      selectTenantWarehouse({ agencyId: this.userInfo.tenantId })
        .then((res) => {
          this.warehouses = res;
          if (!this.setting.id && res.length) {
            this.formData.procureGoods.push(JSON.parse(JSON.stringify(this.goodsItem)));
          }
          if (this.warehouses.length && !this.formData.warehouseId) {
            // 默认选中第一个仓库
            this.formData.warehouseId = this.warehouses[0].id;
@@ -368,12 +425,12 @@
    // 物品分类选择
    categoryChange(e, index) {
      this.formData.procureGoods[index].goodsOptions = []
      this.formData.procureGoods[index].baseGoodsTemplateId = ''
      this.formData.procureGoods[index].goodsTemplateName = ''
      this.formData.procureGoods[index].modelsOptions = []
      this.formData.procureGoods[index].modelsIds = []
      this.formData.procureGoods[index].models = []
      this.formData.procureGoods[index].goodsOptions = [];
      this.formData.procureGoods[index].baseGoodsTemplateId = '';
      this.formData.procureGoods[index].goodsTemplateName = '';
      this.formData.procureGoods[index].modelsOptions = [];
      this.formData.procureGoods[index].modelsIds = [];
      this.formData.procureGoods[index].models = [];
      this.formData.procureGoods[index].baseCategoryId = e[e.length - 1];
      // 根据选中分类请求物品名称列表
@@ -382,11 +439,11 @@
    // 物品名称列表
    goodsTemplateChange(e, index) {
      this.formData.procureGoods[index].modelsOptions = []
      this.formData.procureGoods[index].modelsIds = []
      this.formData.procureGoods[index].models = []
      this.formData.procureGoods[index].modelsOptions = [];
      this.formData.procureGoods[index].modelsIds = [];
      this.formData.procureGoods[index].models = [];
      this.formData.procureGoods[index].goodsTemplateName = this.getGoodsTemplateName(e)
      this.formData.procureGoods[index].goodsTemplateName = this.getGoodsTemplateName(e);
      // 根据选中物品名称id获取规格型号列表
      this.getgoodsModel(e, index);
    },
@@ -413,8 +470,8 @@
    // 上传
    uploadChange() {
      let arr = this.$refs.uploadRef.fileList
      this.formData.procureDoc = JSON.stringify(arr)
      let arr = this.$refs.uploadRef.fileList;
      this.formData.procureDoc = JSON.stringify(arr);
    },
    // 点击新增物品
@@ -466,44 +523,8 @@
        warehouseId: '', // 入库仓库id
        procureTime: '', // 采购时间
        procureGoods: [],
      }
      this.$emit('close')
    },
    // 通过子集id查到所以相关父级id并返回数组
    findParentIds(dataSource, nodeId) {
      const parentIds = [nodeId]; // 用于存储所有父节点ID的数组
      // 定义一个递归函数,用于遍历整棵树并查找子节点的所有父节点
      function traverse(node, nodeId) {
        if (node.id === nodeId) {
          // 如果当前节点的ID等于子节点的ID,则表示已经找到了子节点,可以开始向上查找父节点
          return true; // 返回true表示已经找到了子节点
        }
        if (node.children) {
          // 如果当前节点有子节点,则继续遍历子节点
          for (const childNode of node.children) {
            if (traverse(childNode, nodeId)) {
              // 如果在子节点中找到了子节点的父节点,则将当前节点的ID添加到父节点ID数组中,并返回true表示已经找到了子节点
              parentIds.unshift(node.id);
              return true;
            }
          }
        }
        return false; // 如果当前节点不是子节点的父节点,则返回false
      }
      // 从根节点开始遍历整棵树,并调用递归函数查找子节点的所有父节点
      for (const node of dataSource) {
        if (traverse(node, nodeId)) {
          // 如果在当前节点的子树中找到了子节点的父节点,则直接退出循环
          break;
        }
      }
      return parentIds; // 返回所有父节点ID的数组
      };
      this.$emit('close');
    },
  },
};