石广澎
2023-11-29 20913c80c3f5fc8e533cb92b90e6f20bcd68e032
admin-web/src/views/stock/procure/purchaseOrder/edit.vue
@@ -1,5 +1,5 @@
<template>
  <win-md class="stock-edit" :title="`${setting.title}采购入库`" @close="close" :width="'800px'">
  <win-md class="stock-edit" :title="`${setting.title}采购入库`" @close="close" :width="'800px'" :loading="loading">
    <el-form class="form" ref="ruleForm" :model="formData" :rules="rules" label-width="120px">
      <div class="main-w">
        <el-row :gutter="24" class="headerHeight">
@@ -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"
@@ -135,14 +131,32 @@
                {{ scope.row.unit }}
              </template>
            </el-table-column>
            <el-table-column prop="price" label="单价(元)" align="center">
            <el-table-column prop="priceYuan" 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}].priceYuan`"
                  :rules="rules.priceYuan"
                >
                  <el-input
                    type="number"
                    v-model.number="scope.row.priceYuan"
                    @change="priceChange($event, goodsIndex, scope.$index)"
                  ></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>
@@ -170,61 +184,99 @@
      </div>
    </el-form>
    <div slot="footer" align="center" class="dialog-footer">
      <my-button name="取消" site="form" @click="close"/>
      <my-button name="保存" site="form" @click="handleSubmit"/>
      <my-button name="取消" site="form" @click="close" />
      <my-button name="保存" site="form" @click="handleSubmit" />
    </div>
  </win-md>
</template>
<script>
import {
  goodsModel,
  goodsTemplate,
  procureAdd,
  procureDetail,
  procureEdit,
  selectTenantWarehouse,
  goodsModel,
  procureDetail,
} from '@/api/stock/procure/purchaseOrder';
import { getTree } from '@/api/foudation/classification';
import {getTree} from '@/api/foudation/classification';
import MyButton from '@/components/myButton/myButton';
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 {getUploadUrl} from '@/utils/base';
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) => {
      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:[{
        label:'集采',
        value: '1'
      },{
        label:'自采',
        value: '2'
      }],
      fileList:[],
      loading: true,
      buyTypeOptions: [
        {
          label: '集采',
          value: '1',
        },
        {
          label: '自采',
          value: '2',
        },
      ],
      fileList: [],
      warehouses: [], // 入库仓库列表
      categoryOptions: [], // 物品分类列表
      modelList: [], //型号列表
      formData: {
        procureDoc:'',
        procureDoc: '',
        warehouseId: '', // 入库仓库id
        procureTime: '', // 采购时间
        buyType: '2', // 采购方式(1:集采;2=自采)
        procureGoods: [],
      },
      goodsItem: {
        whFormProcureId: '', // 采购单ID
        baseCategoryIds: '', // 分类编号数组
        baseCategoryId: '', // 分类编号
        baseGoodsTemplateId: '', // 物品模版编号
@@ -238,9 +290,9 @@
      },
      modelsItem: {
        baseGoodsModelsId: '', // 规格型号编号
        price: 0, // 单价(采购需要,调拨不需要)
        counts: 0, // 操作数量
        supplier: '', // 供应商
        priceYuan: null, // 单价(元)
        price: null, // 单价(分 采购需要,调拨不需要)
        counts: null, // 操作数量
        unit: null, //单位
      },
      rules: {
@@ -250,15 +302,16 @@
        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' }],
        priceYuan: [{ validator: checkPrice, trigger: 'blur' }],
        counts: [{ validator: checkCounts, trigger: 'blur' }],
      },
      uploadSettings: {
        title: '上传',
        max: 20, // 最大大小,单位M
        num: 10, // 支持上传图片个数
        accept: '.jpg,.png', // 限制格式
        tip: '', // 提示 默认:`只能上传${this.defaultSettings.num}个${this.defaultSettings.accept}文件,且不超过${this.defaultSettings.max}kb`
        uploadUrl: getUploadUrl(), // 上传路径
        multiple: true, // 是否支持批量上传
@@ -267,45 +320,55 @@
      },
    };
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {
    this.init()
    this.init();
  },
  methods: {
    async init() {
      this.getWarehouseList();
      this.getgoodsTemplate()
      this.getgoodsTemplate();
      this.getgoodsModel();
      // 获取物品分类列表
      const treeRes = await getTree();
      this.categoryOptions = this.removeEmptyChildren(treeRes);
      if (this.setting.id) {
        const detail = await procureDetail({ id:this.setting.id });
        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)
        if (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拼接物品分类列表
          let pIds = this.findParentIds(this.categoryOptions, item.baseCategoryId);
          item.baseCategoryIds = [...pIds, 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
          item.models.forEach((child, childIndex) => {
            this.$set(this.formData.procureGoods[index].models[childIndex], 'priceYuan', child.price / 100);
          });
          return item;
        });
      } else {
        this.formData.procureGoods.push(JSON.parse(JSON.stringify(this.goodsItem)));
      }
      this.visible = true;
      this.loading = false;
    },
    // 获取入库仓库列表
    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;
@@ -332,7 +395,7 @@
      goodsTemplate({ categoryId: id || '' }).then((res) => {
        if (index || index == 0) {
          this.$set(this.formData.procureGoods[index], 'goodsOptions', res);
        }else {
        } else {
          this.goodsTemplatelAll = res;
        }
      });
@@ -369,12 +432,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];
      // 根据选中分类请求物品名称列表
@@ -383,11 +446,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);
    },
@@ -412,10 +475,15 @@
      this.formData.procureGoods[index].models.splice(delIndex, 1);
    },
    // 价格input监听 转为分
    priceChange(e, goodsIndex, index) {
      this.formData.procureGoods[goodsIndex].models[index].price = e * 100;
    },
    // 上传
    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);
    },
    // 点击新增物品
@@ -432,7 +500,7 @@
    handleSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          console.log('this.formData', this.formData);
          this.loading = true;
          if (!this.setting.id) {
            procureAdd(this.formData)
              .then((res) => {
@@ -441,7 +509,7 @@
                this.$emit('search');
              })
              .catch((err) => {
                console.log('create err', err);
                this.loading = false;
                this.$message.error('保存失败');
              });
          } else {
@@ -452,7 +520,7 @@
                this.$emit('search');
              })
              .catch((err) => {
                console.log('edit err', err);
                this.loading = false;
                this.$message.error('保存失败');
              });
          }
@@ -463,48 +531,12 @@
    },
    close() {
      this.formData ={
      this.formData = {
        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');
    },
  },
};