fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<template>
  <div>
    <el-dialog :visible.sync="isShow" :title="title" width="70%" :modal-append-to-body="false"
               :close-on-click-modal="false">
      <div class="addAllot-style">
        <el-form size="mini" :model="tableForm" ref="tableForm">
          <el-row class="sel-style">
            <el-col class="formLayout" :span="8">
              <el-form-item label="调出渠道:" label-width="100px" prop="outChannelName"
                            :rules="formRules.outChannelName">
                <el-select v-model="tableForm.outChannelName" placeholder="请选择调出渠道" clearable
                           :popper-append-to-body="false" @change="changeDcChannel">
                  <el-option v-for="item in channelData"
                             :disabled="item.id === tableForm.inChannelId" :key="item.id"
                             :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col class="formLayout" :span="8">
              <el-form-item label="调入渠道:" label-width="100px" prop="inChannelName"
                            :rules="formRules.inChannelName">
                <el-select v-model="tableForm.inChannelName" placeholder="请选择调入渠道"
                           @change="changeDrChannel" clearable :popper-append-to-body="false">
                  <el-option v-for="item in channelData"
                             :disabled="item.id === tableForm.outChannelId" :key="item.id"
                             :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <list-condition-template ref="table" :form="tableForm" :dataKey="'spuNum'"
                                   :tableData="tableForm.tableData" :total="total"
                                   :isShowPage="false" defaultPadding="0">
            <template slot="operationSection">
              <el-button size="mini" type="primary" @click="handleAddCommodity">新增商品</el-button>
            </template>
            <template slot="columns">
              <el-table-column label="商品主编码" prop="spuNum" width="150px" show-overflow-tooltip>
              </el-table-column>
              <el-table-column label="商品名称" prop="spuName" min-width="180px" show-overflow-tooltip>
              </el-table-column>
              <el-table-column label="规格型号" prop="spec" width="140px">
              </el-table-column>
              <el-table-column label="剩余库存" prop="stock" width="100px" show-overflow-tooltip>
              </el-table-column>
              <el-table-column label="调拨数量" width="220px">
                <template scope="scope">
                  <el-form-item :prop="'tableData.'+ scope.$index + '.transferStock'" :rules="[
                        {
                          validator: vailTransferStock,
                          stock: scope.row.stock,
                        },
                        { 
                          required: true, message: '请输入调拨数量' 
                        },
                      ]">
                    <el-input v-model="scope.row.transferStock" placeholder="请输入调拨数量">
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" :width="`${$store.getters.colSize + 30}px`">
                <template scope="scope">
                  <wly-btn type="success" @click="handleDeleteItem(scope.$index)">删除</wly-btn>
                </template>
              </el-table-column>
            </template>
          </list-condition-template>
          <el-row>
            <el-col :span="16">
              <el-form-item label="调拨备注:" class="custom-form-style" label-width="80px"
                            prop="remark">
                <el-input v-model="tableForm.remark" type="textarea"
                          :autosize="{ minRows: 4, maxRows: 6 }" maxlength="200"
                          placeholder="请输入调拨备注" clearable>
                </el-input>
              </el-form-item>
              <el-form-item label="附件:" label-width="80px" prop="attachment">
                <upload-file :limitNumber='3' :uploadPath="'order'" :customTips="'当前限制选择3个文件'"
                             :accept="'.xls,.xlsx,.doc,.docx,.jpg,.jpeg,.png,.JPG,.JPEG'"
                             @handle-success="handleSuccess" @handle-remove="handleRemove"
                             :fileList="fileList" :isShow="isShow">
                  <template>仅支持.xls,.xlsx,.doc,.docx,.jpg,.jpeg,.png,.JPG,.JPEG的附件</template>
                </upload-file>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="save" type="primary" :loading="loading">保存
        </el-button>
        <el-button size="mini" @click="cancel">取消</el-button>
      </div>
    </el-dialog>
    <choose-products-dialog ref="chooseProducts" :channelId="tableForm.outChannelId"
                            :dialogVisible.sync="chooseProductsDialogVisible" title="选择商品"
                            @hand-selected-row-data="handSelectedRowData"
                            :selectedTable="tableForm.tableData">
    </choose-products-dialog>
  </div>
</template>
 
<script>
import uploadFile from '@/components/uploadFile/uploadFile.vue'
import chooseProductsDialog from '@/views/inventoryAllocation/components/chooseProductsDialog.vue'
import uploadFileApi from '@/api/uploadFile'
import InventoryAllocationApi from '@/api/stockManagement/inventoryAllocation'
import dictApi from '@/api/dict'
export default {
  name: "addAllotDialog",
  components: {
    uploadFile,
    chooseProductsDialog
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isShow: false,
      tableForm: {
        tableData: [],
        remark: null,
        attachment: null,//附件ID
        inChannelId: null,//调入渠道
        inChannelName: null,
        outChannelId: null,//调出渠道
        outChannelName: null,
      },
      total: 0,
      formRules: {
        'transferStock': [
          { required: true, message: '请输入调拨数量' },
          { pattern: /^\+?[1-9][0-9]*$/, message: '请输入大于0的正整数', trigger: 'change' },
        ],
        'inChannelName': [
          { required: true, message: '请选择调入渠道' },
        ],
        'outChannelName': [
          { required: true, message: '请选择调出渠道' },
        ],
      },
      channelData: [],
      fileList: [],
      loading: false,
      chooseProductsDialogVisible: false
    }
  },
  watch: {
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    dialogVisible: function (newShow, oldShow) {
      this.isShow = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    isShow: function (newDialogShow, oldDialogShow) {
      if (!newDialogShow) {
        this.$refs["tableForm"].resetFields();
        this.$emit('update:dialogVisible', newDialogShow)
        this.initData();
      }
    }
  },
  mounted() {
    this.getChannelData();
  },
  methods: {
    initData() {
      Object.keys(this.tableForm).forEach((key) => {
        if (key != 'tableData') {
          this.tableForm[key] = null
        } else {
          this.tableForm[key] = []
        }
      })
      this.fileList = [];
      this.total = 0;
      this.loading = false;
    },
    /**
     * 选中的调出渠道
     */
    changeDcChannel(val) {
      this.tableForm.outChannelId = val;
      this.tableForm.tableData = [];
    },
    /**
     * 选中的调入渠道
     */
    changeDrChannel(val) {
      this.tableForm.inChannelId = val;
    },
    /**
     * 获取渠道数据字典
     */
    async getChannelData() {
      let _arr = [];
      const res = await dictApi.getChanneldict()
      let _data = res.data;
      if (res.code === '0' && _data && _data.length > 0) {
        _data.forEach((item) => {
          _arr.push({
            id: item.dictCode,
            name: item.dictName,
            disabled: false,
          })
        })
        this.channelData = _arr;
      }
    },
    /**
      * 新增商品
      */
    handleAddCommodity() {
      if (!this.tableForm.outChannelId) {
        this.$message({
          message: '调出渠道为必填项,请选择',
          type: 'info'
        })
        return;
      }
      this.chooseProductsDialogVisible = true;
    },
    /**
     * 获取选中的商品
     */
    handSelectedRowData(data) {
      let newArr = [];
      if (this.tableForm.tableData.length) {
        //将原有的数据与新增的数据合并
        data.forEach((v1) => {
          let arr = this.tableForm.tableData.map((v2) => v2.spuNum);
          if (arr.indexOf(v1.spuNum) === -1) {
            this.tableForm.tableData = [...this.tableForm.tableData, ...[v1]]
          }
        })
        //反选,选中的数据点击取消选中的情况下
        this.tableForm.tableData.forEach((v1, index) => {
          let arr = data.map((v2) => v2.spuNum);
          if (arr.indexOf(v1.spuNum) != -1) {
            newArr = [...newArr, ...[v1]]
          }
        })
      } else {
        newArr = data;
      }
      this.tableForm.tableData = newArr;
    },
    /**
     * 删除商品
     */
    handleDeleteItem(index) {
      let _this = this;
      _this.$confirm('是否删除当前商品?')
        .then(_ => {
          _this.tableForm.tableData.splice(index, 1);
        })
        .catch(_ => { });
    },
    /**
  * 附件上传成功
  */
    handleSuccess(arr, id) {
      this.fileList = arr
      this.tableForm.attachment = id
    },
    /**
     * 删除附件
     */
    handleRemove(file) {
      uploadFileApi.removeFile({ key: file.id }).then(res => {
        const index = this.fileList.findIndex(item => item.id === file.id)
        if (index !== -1) {
          this.fileList.splice(index, 1)
        }
        if (!this.fileList.length) {
          this.tableForm.attachment = null
        }
      })
    },
    /**
     * 保存
     */
    save() {
      let _this = this;
      _this.$refs.tableForm.validate((valid) => {
        if (valid) {
          if (!_this.tableForm.tableData || _this.tableForm.tableData.length === 0) {
            _this.$message({
              message: '请选择调拨商品!',
              type: 'warning'
            })
            return;
          }
          _this.sendRequest();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    /**
     * 发送请求
     */
    sendRequest() {
      let arr = [];
      let { remark, outChannelId, inChannelId, attachment, tableData } = this.tableForm;
      let params = { remark: remark, inChannel: inChannelId, outChannel: outChannelId, attachment: attachment }
      if (tableData && tableData.length > 0) {
        tableData.forEach((item) => {
          arr.push({
            spuName: item.spuName,
            spuNum: item.spuNum,
            transferStock: item.transferStock,//调拨数量
          })
        })
      }
      params.spuList = arr;
      this.loading = true;
      InventoryAllocationApi.addAllot(params).then((res) => {
        if (res.code === '200') {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.$emit("close")
          this.isShow = false;
          this.loading = false;
        } else {
          this.loading = false;
        }
      }).catch((error) => {
        this.loading = false;
      })
    },
    cancel() {
      let _this = this;
      let isNull = _this.isSubmitData();
      if (isNull) {
        _this.$confirm('取消后当前调拨信息将清空,确认要取消吗?')
          .then(_ => {
            _this.isShow = false;
          })
          .catch(_ => { });
      } else {
        _this.isShow = false;
      }
    },
    isSubmitData() {
      let bool = false
      for (let i in this.tableForm) {
        if (i === 'tableData' && this.tableForm[i].length > 0) {
          bool = true;
          break;
        } else {
          if (this.tableForm[i] && i != 'tableData') {
            bool = true;
            break;
          }
        }
      }
      return bool;
    },
    /**
     *验证调拨库存
    */
    vailTransferStock(rule, value, callback) {
      const reg = /^\+?[1-9][0-9]*$/;
      if (!reg.test(value)) {
        callback(new Error('请输入大于0的正整数'))
      } else if (value > rule.stock) {
        callback(new Error('剩余库存不足'))
      } else {
        callback()
      }
    },
  }
}
</script>
 
<style lang="scss">
.addAllot-style {
  .main-b-style {
    padding: 0 !important;
  }
  .commonForm {
    margin-bottom: 0 !important;
  }
  .custom-form-style {
    margin-top: 20px;
  }
}
</style>