inventory-form.vue
7.58 KB
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
<template>
<el-dialog title="添加库存" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center"
lock-scroll width="800px">
<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" :rules="rules">
<el-row :gutter="15">
<el-col :span="12">
<el-form-item label="产品" prop="productId">
<el-select v-model="dataForm.productId" placeholder="请选择产品" clearable filterable
:style='{"width":"100%"}' :disabled="!!defaultProductId" @change="onProductChange">
<el-option v-for="item in productOptions" :key="item.id" :label="item.productName"
:value="item.id" />
</el-select>
</el-form-item>
</el-col>
<el-col :span="12" v-show="false">
<el-form-item label="入库类型" prop="stockInType">
<el-select v-model="dataForm.stockInType" placeholder="请选择入库类型" clearable
:style='{"width":"100%"}' @change="onStockInTypeChange">
<el-option label="普通入库" :value="1" />
<el-option label="采购入库" :value="2" />
</el-select>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="数量" prop="quantity">
<el-input-number v-model="dataForm.quantity" placeholder="请输入数量" :min="1" :precision="0"
:style='{"width":"100%"}' @change="calculatePurchaseAmount" />
</el-form-item>
</el-col>
<el-col :span="12" v-if="dataForm.stockInType === 2">
<el-form-item label="采购单价" prop="purchaseUnitPrice">
<el-input-number v-model="dataForm.purchaseUnitPrice" placeholder="请输入采购单价" :min="0"
:precision="2" :style='{"width":"100%"}' @change="calculatePurchaseAmount" />
</el-form-item>
</el-col>
<el-col :span="12" v-if="dataForm.stockInType === 2">
<el-form-item label="采购总金额">
<el-input v-model="purchaseAmountDisplay" placeholder="系统自动计算" readonly
:style='{"width":"100%"}' />
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="入库时间" prop="stockInTime">
<el-date-picker v-model="dataForm.stockInTime" type="date" placeholder="请选择入库时间"
value-format="yyyy-MM-dd" format="yyyy-MM-dd" :style='{"width":"100%"}' />
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="生产日期" prop="productionDate">
<el-date-picker v-model="dataForm.productionDate" type="date" placeholder="请选择生产日期"
value-format="yyyy-MM-dd" format="yyyy-MM-dd" :style='{"width":"100%"}' />
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="保质期(天)" prop="shelfLife">
<el-input-number v-model="dataForm.shelfLife" placeholder="请输入保质期" :min="1" :precision="0"
:style='{"width":"100%"}' />
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="批次号" prop="batchNumber">
<el-input v-model="dataForm.batchNumber" placeholder="请输入批次号" clearable
:style='{"width":"100%"}' />
</el-form-item>
</el-col>
</el-row>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false">取 消</el-button>
<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
</span>
</el-dialog>
</template>
<script>
import request from '@/utils/request'
export default {
data() {
return {
visible: false,
btnLoading: false,
defaultProductId: '',
dataForm: {
productId: '',
stockInType: 2,
quantity: 1,
stockInTime: '',
productionDate: '',
shelfLife: 365,
batchNumber: '',
purchaseUnitPrice: null
},
productOptions: [],
rules: {
productId: [
{ required: true, message: '请选择产品', trigger: 'change' }
],
quantity: [
{ required: true, message: '请输入数量', trigger: 'blur' }
],
stockInTime: [
{ required: true, message: '请选择入库时间', trigger: 'change' }
],
// purchaseUnitPrice: [
// {
// validator: (rule, value, callback) => {
// if (this.dataForm.stockInType === 2) {
// if (!value || value <= 0) {
// callback(new Error('采购入库时,采购单价必须大于0'))
// } else {
// callback()
// }
// } else {
// callback()
// }
// },
// trigger: 'blur'
// }
// ]
}
}
},
computed: {
purchaseAmountDisplay() {
if (this.dataForm.stockInType === 2 && this.dataForm.purchaseUnitPrice && this.dataForm.quantity) {
const amount = this.dataForm.purchaseUnitPrice * this.dataForm.quantity
return '¥' + amount.toFixed(2)
}
return ''
}
},
methods: {
init(productId) {
this.visible = true
this.defaultProductId = productId || ''
this.dataForm = {
productId: productId || '',
stockInType: 2,
quantity: 1,
stockInTime: '',
productionDate: '',
shelfLife: 365,
batchNumber: '',
purchaseUnitPrice: null
}
this.initProductOptions()
},
initProductOptions() {
// 如果传入了产品ID,只显示该产品;否则只显示上架的产品
const query = {
currentPage: 1,
pageSize: 1000
}
// 如果传入了产品ID,则查询该产品;否则只查询上架的产品
if (this.defaultProductId) {
query.id = this.defaultProductId
} else {
query.onShelfStatus = 1 // 只获取上架的产品
}
request({
url: '/api/Extend/LqProduct/GetList',
method: 'GET',
data: query
}).then(res => {
if (res.code == 200 && res.data && res.data.list) {
if (this.defaultProductId) {
// 如果传入了产品ID,只显示该产品
this.productOptions = res.data.list
} else {
// 只显示上架的产品
this.productOptions = res.data.list.filter(product => product.onShelfStatus === 1)
}
}
}).catch(() => {
this.productOptions = []
})
},
onProductChange() {
// 产品选择变化时的处理
},
onStockInTypeChange() {
// 入库类型变化时,重置采购单价
if (this.dataForm.stockInType !== 2) {
this.dataForm.purchaseUnitPrice = null
}
// 清除验证状态
this.$nextTick(() => {
this.$refs.elForm && this.$refs.elForm.clearValidate('purchaseUnitPrice')
})
},
calculatePurchaseAmount() {
// 自动计算采购总金额(通过计算属性 purchaseAmountDisplay 实现)
// 计算属性会自动响应数据变化,无需手动更新
},
dataFormSubmit() {
this.$refs.elForm.validate((valid) => {
if (!valid) {
return false
}
this.btnLoading = true
const requestData = {
productId: this.dataForm.productId,
stockInType: this.dataForm.stockInType,
quantity: this.dataForm.quantity,
stockInTime: this.dataForm.stockInTime,
productionDate: this.dataForm.productionDate,
shelfLife: this.dataForm.shelfLife,
batchNumber: this.dataForm.batchNumber
}
// 如果是采购入库,添加采购单价(采购总金额由后端自动计算)
if (this.dataForm.stockInType === 2) {
requestData.purchaseUnitPrice = this.dataForm.purchaseUnitPrice
}
request({
url: '/api/Extend/LqInventory/Create',
method: 'POST',
data: requestData
}).then(res => {
this.btnLoading = false
this.$message({
type: 'success',
message: res.msg || '添加成功',
onClose: () => {
this.visible = false
this.$emit('refresh')
}
})
}).catch(() => {
this.btnLoading = false
})
})
}
}
}
</script>
<style lang="scss" scoped>
</style>