into.vue 13.1 KB
<template>
  <div>
    <div class="container">
      <div class="table-header">
        <h2 class="title">采购明细</h2>
        <el-button type="primary" size="small" @click="addLine">添加一行</el-button>
      </div>
      <el-form ref="addForm" :model="formdata" size="small" :rules="rules">
        <GLTable ref="orderRef" :tableData="formdata.tableData" :noDataImg="url" :searchForm="tableSearchForm" :tableParams="tableParams" :hasPage="false">
          <template slot="upgradeGoodsId" slot-scope="scope">
            <el-form-item style="margin-bottom: 0px" :prop="'tableData.' + scope.index + '.upgradeGoodsId'" :rules="rules.upgradeGoodsId">
              <el-select style="width: 100%" v-model="scope.scope.upgradeGoodsId" placeholder="请选择" @change="getGoodsName(scope.scope, scope.scope.upgradeGoodsId)">
                <el-option v-for="item in goodsList" :key="item.upgradeGoodsId" :label="item.goodsName" :value="item.upgradeGoodsId"></el-option>
              </el-select>
            </el-form-item>
          </template>
          <template slot="goodsNumber" slot-scope="scope">
            <el-form-item style="margin-bottom: 0px" :prop="'tableData.' + scope.index + '.goodsNumber'" :rules="rules.goodsNumber">
              <el-input v-model="scope.scope.goodsNumber" placeholder="请输入" @blur="handleCalculate(scope.scope, 'goodsNumber')"></el-input>
            </el-form-item>
          </template>
          <template slot="upgradePurchasePrice" slot-scope="scope">
            <el-form-item style="margin-bottom: 0px" :prop="'tableData.' + scope.index + '.upgradePurchasePrice'" :rules="rules.upgradePurchasePrice">
              <el-input v-model="scope.scope.upgradePurchasePrice" placeholder="请输入" @blur="handleCalculate(scope.scope, 'upgradePurchasePrice')"></el-input>
            </el-form-item>
          </template>
          <template slot="goodsTotalPrice" slot-scope="scope">
            <el-form-item style="margin-bottom: 0px" :prop="'tableData.' + scope.index + '.goodsTotalPrice'" :rules="rules.goodsTotalPrice">
              <el-input v-model="scope.scope.goodsTotalPrice" placeholder="请输入" @blur="handleCalculate(scope.scope, 'goodsTotalPrice')"></el-input>
            </el-form-item>
          </template>
          <!-- 入库仓库 -->
          <template slot="putYqMerchantId" slot-scope="scope">
            <el-form-item style="margin-bottom: 0px" :prop="'tableData.' + scope.index + '.putYqMerchantId'" :rules="rules.putYqMerchantId">
              <el-select style="width: 100%" v-model="scope.scope.putYqMerchantId" placeholder="请选择" @change="handleSelect(scope.scope, scope.scope.putYqMerchantId)">
                <el-option v-for="item in storeUsageList" :key="item.merchantNo" :label="item.merchantName" :value="item.merchantNo"></el-option>
              </el-select>
            </el-form-item>
          </template>
          <!-- 入库门店 -->
          <template slot="putMerchantId" slot-scope="scope">
            <el-form-item style="margin-bottom: 0px" :prop="'tableData.' + scope.index + '.putMerchantId'" :rules="rules.putMerchantId">
              <el-select style="width: 100%" v-model="scope.scope.putMerchantId" placeholder="请选择" @change="handleStoreSelect(scope.scope, scope.scope.putMerchantId)">
                <el-option v-for="item in scope.scope.refByYqShopList" :key="item.merchantId" :label="item.merchantName" :value="item.merchantId"></el-option>
              </el-select>
            </el-form-item>
          </template>
          <div class="table-btns" slot="auth-button" slot-scope="scope">
            <el-button type="text" @click="handleDelete(scope.index)">删除</el-button>
          </div>
        </GLTable>
      </el-form>
    </div>
    <GLFooter style="margin-top: 16px">
      <el-button @click="goBack" size="medium">取消</el-button>
      <el-button @click="handleSave(1)" type="primary" plain size="medium">保存</el-button>
      <el-button type="primary" @click="handleSave(2)" size="medium">保存并入库</el-button>
    </GLFooter>
  </div>
</template>
<script>
import BigNumber from 'bignumber.js'
export default {
  data() {
    var checkPrice = (rule, value, callback) => {
      if (isNaN(+value)) {
        callback(new Error('请输入数字值'))
      } else {
        if (value <= 0 || value > 999999) {
          callback(new Error('仅支持输入0~999999'))
        } else {
          callback()
        }
      }
    }
    return {
      orderCode: this.$route.query.orderCode || '',
      rules: {
        upgradeGoodsId: [{ required: true, message: '请选择商品名称', trigger: 'change' }],
        goodsTotalPrice: [
          { required: true, message: '请输入采购总额', trigger: 'blur' },
          { validator: checkPrice, trigger: 'blur' }
        ],

        goodsNumber: [
          { required: true, message: '请输入采购数量', trigger: 'blur' },
          { validator: checkPrice, trigger: 'blur' }
        ],
        upgradePurchasePrice: [
          { required: true, message: '请输入采购单价', trigger: 'blur' },
          { validator: checkPrice, trigger: 'blur' }
        ],
        putYqMerchantId: [{ required: true, message: '请选择入库仓库', trigger: 'change' }],
        putMerchantId: [{ required: true, message: '请选择入库门店', trigger: 'change' }]
      },
      formdata: {
        tableData: [{ upgradeGoodsId: '', type: '', goodsName: '', goodsNumber: '', upgradePurchasePrice: '', goodsTotalPrice: '', putYqMerchantId: '', putYqMerchantName: '', putMerchantId: '', putMerchantName: '' ,     refByYqShopList: []}]
      },
      tableParams: {
        tableOption: true,
        tableColumns: [
          {
            label: '商品名称',
            prop: 'upgradeGoodsId',
            addHtml: 'upgradeGoodsId'
          },
          {
            label: '商品编码',
            prop: 'type'
          },
          {
            label: '所属系列',
            prop: 'goodsName'
          },
          {
            label: '采购数量',
            prop: 'goodsNumber',
            addHtml: 'goodsNumber'
          },
          {
            label: '采购单价',
            prop: 'upgradePurchasePrice',
            addHtml: 'upgradePurchasePrice'
          },
          {
            label: '采购总额/元',
            prop: 'goodsTotalPrice',
            addHtml: 'goodsTotalPrice'
          },
          {
            label: '入库仓库',
            prop: 'putYqMerchantId',
            addHtml: 'putYqMerchantId'
          },
          {
            label: '入库门店',
            prop: 'putMerchantId',
            addHtml: 'putMerchantId'
          }
        ]
      },
      goodsList: [],
      storeUsageList: [],
      refByYqShopList: []
    }
  },
  created() {
    this.getfwsGoodsList()
    this.getMerchantList()
    if (this.orderCode) this.getData()
  },
  methods: {
    getMerchantList() {
      this.$apis.purchaseShopping.getMerchantList({ mainPartnerMerchant: true }).then(res => {
        this.storeUsageList = res
        if (this.storeUsageList.length == 1) this.defaultSelect()
      })
    },
    defaultSelect() {
      let {
        formdata: { tableData },
        storeUsageList
      } = this
      tableData.forEach(item => {
        item.putYqMerchantName = storeUsageList[0].merchantName
        item.putYqMerchantId = storeUsageList[0].merchantNo
      })
      this.getRefByYqShopList(tableData[0], storeUsageList[0].merchantNo)
    },
    getRefByYqShopList(data, value) {
      this.$apis.purchaseShopping.get4SRefByYqShopList({ scene: 1, merchantId: value }).then(res => {
        data.refByYqShopList = res
        if (data) {
          data.refByYqShopList.unshift({
            merchantName: data.putYqMerchantName,
            merchantId: Number(data.putYqMerchantId)
          })
        }
      })
    },
    handleSave(saveOrWarehousing) {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          let purchaseOrderGoodsList = []
          this.formdata.tableData.forEach(item => {
            purchaseOrderGoodsList.push({
              upgradeGoodsId: item.upgradeGoodsId,
              goodsName: item.goodsName,
              goodsNumber: item.goodsNumber,
              upgradePurchasePrice: item.upgradePurchasePrice,
              goodsTotalPrice: item.goodsTotalPrice,
              putYqMerchantId: item.putYqMerchantId,
              putYqMerchantName: item.putYqMerchantName,
              putMerchantId: item.putMerchantId,
              putMerchantName: item.putMerchantName
            })
          })
          let params = {
            saveOrWarehousing,
            purchaseOrderGoodsList
          }
          if (this.orderCode) params.orderCode = this.orderCode
          if (saveOrWarehousing == 2) {
            this.$confirm('入库后自动更新对应的库存且不可撤回,可在入库管理中查看记录。是否确定入库?', '入库提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$apis.product.addSelfOrder(params).then(res => {
                this.$notify({
                  type: 'success',
                  title: '操作成功'
                })
                this.goBack()
              })
            })
          } else {
            this.$apis.product.addSelfOrder(params).then(res => {
              this.$notify({
                type: 'success',
                title: '操作成功'
              })
              this.goBack()
            })
          }
        }
      })
    },
    getData() {
      this.$apis.purchaseShopping.selectByOrderCode({ orderCode: this.orderCode }).then(res => {
        res.forEach(item => {
          item.putYqMerchantId = String(item.putYqMerchantId)
          item.refByYqShopList = []
          this.getRefByYqShopList(item, item.putYqMerchantId)
        })
        this.$nextTick(() => {
          this.formdata.tableData = res
        })
      })
    },
    handleDelete(index) {
      if (this.formdata.tableData.length == 1) return
      this.formdata.tableData.splice(index, 1)
    },
    getfwsGoodsList() {
      this.$apis.product.getfwsGoodsList({}).then(res => {
        this.goodsList = res
      })
    },
    getGoodsName(data, id) {
      this.goodsList.forEach(item => {
        if (item.upgradeGoodsId == id) {
          data.type = item.type
          data.goodsName = item.goodsCodeName
        }
      })
    },

    handleSelect(data, id) {
      this.storeUsageList.forEach(item => {
        if (item.merchantNo == id) {
          data.putYqMerchantName = item.merchantName
          data.putMerchantId = ''
        }
      })
      this.getRefByYqShopList(data, id)
    },
    handleStoreSelect(data, id) {
      data.refByYqShopList.forEach(item => {
        if (item.merchantId == id) {
          data.putMerchantName = item.merchantName
        }
      })
    },
    handleCalculate(data, key) {
      if (key == 'goodsNumber') {
        data[key] = data[key] ? new BigNumber(data[key]).dp(0).toNumber() : ''
        data.goodsTotalPrice =
          data.upgradePurchasePrice && data.goodsNumber
            ? new BigNumber(data.upgradePurchasePrice)
                .times(data.goodsNumber)
                .dp(2)
                .toNumber()
            : ''
        return
      }

      if (!data.goodsNumber) return
      data[key] = data[key] ? new BigNumber(data[key]).dp(2).toNumber() : ''
      let params = {
        upgradePurchasePrice: data => {
          data.goodsTotalPrice = data.upgradePurchasePrice
            ? new BigNumber(data.upgradePurchasePrice)
                .times(data.goodsNumber)
                .dp(2)
                .toNumber()
            : ''
        },
        goodsTotalPrice: data => {
          data.upgradePurchasePrice = data.goodsTotalPrice
            ? new BigNumber(data.goodsTotalPrice)
                .div(data.goodsNumber)
                .dp(2)
                .toNumber()
            : ''
        }
      }
      params[key](data)
    },

    addLine() {
      let {
        storeUsageList,
        formdata: { tableData }
      } = this
      tableData.unshift({
        refByYqShopList: [],
        upgradeGoodsId: '',
        type: '',
        goodsName: '',
        goodsNumber: '',
        upgradePurchasePrice: '',
        goodsTotalPrice: '',
        putYqMerchantId: storeUsageList.length == 1 ? storeUsageList[0].merchantNo : '',
        putYqMerchantName: storeUsageList.length == 1 ? storeUsageList[0].merchantName : '',
        putMerchantId: '',
        putMerchantName: ''
      })
      if (storeUsageList.length == 1) {
        this.getRefByYqShopList(tableData[0], tableData[0].putYqMerchantId)
      }
    },
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  background: #fff;
  padding: 20px;
  .table-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
  }
  .title {
    font-weight: 600;
    font-size: 16px;
    color: #333333;
    margin-bottom: 15px;
  }
  /deep/ .el-form-item__error {
    padding-top: 2px;
    z-index: 9999999;
    position: static;
  }
  /deep/ td.el-table__cell {
    height: 64px;
  }
}
</style>