form-dialog.vue
10.6 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
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
<template>
<el-dialog
:title="isEdit ? '编辑店内支出' : '添加店内支出'"
:visible.sync="visible"
:close-on-click-modal="false"
class="NCC-dialog NCC-dialog_center"
width="900px"
@close="handleClose"
>
<div class="form-content" v-loading="loading">
<el-form ref="form" :model="dataForm" :rules="rules" label-width="120px" label-position="right">
<el-form-item label="门店" prop="storeId">
<el-select v-model="dataForm.storeId" placeholder="请选择门店" clearable filterable style="width: 100%" @change="handleStoreChange">
<el-option v-for="store in storeOptions" :key="store.id" :label="store.dm" :value="store.id" />
</el-select>
</el-form-item>
<el-form-item v-show="false" label="门店名称" prop="storeName">
<el-input v-model="dataForm.storeName" placeholder="自动填充" readonly />
</el-form-item>
<el-form-item label="支出分类" prop="expenseCategoryId">
<el-select v-model="dataForm.expenseCategoryId" placeholder="请选择支出分类" clearable filterable style="width: 100%" @change="handleCategoryChange">
<el-option v-for="category in categoryOptions" :key="category.id" :label="category.typeName" :value="category.id" />
</el-select>
</el-form-item>
<el-form-item v-show="false" label="支出分类名称" prop="expenseCategoryName">
<el-input v-model="dataForm.expenseCategoryName" placeholder="自动填充" readonly />
</el-form-item>
<el-form-item label="支出日期" prop="expenseDate">
<el-date-picker
v-model="dataForm.expenseDate"
type="date"
placeholder="请选择支出日期"
format="yyyy-MM-dd"
value-format="timestamp"
style="width: 100%"
/>
</el-form-item>
<el-row :gutter="20">
<el-col :span="8">
<el-form-item label="单价" prop="unitPrice">
<el-input-number v-model="dataForm.unitPrice" placeholder="请输入单价" :precision="2" :min="0" :step="1" style="width: 100%" @change="calculateAmount" />
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="数量" prop="quantity">
<el-input-number v-model="dataForm.quantity" placeholder="请输入数量" :min="0" :step="1" style="width: 100%" @change="calculateAmount" />
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="金额" prop="amount">
<el-input-number v-model="dataForm.amount" placeholder="自动计算" :precision="2" :min="0" :step="1" style="width: 100%" :disabled="true" />
</el-form-item>
</el-col>
</el-row>
<el-form-item label="备注" prop="memo">
<el-input v-model="dataForm.memo" type="textarea" :rows="3" placeholder="请输入备注说明" maxlength="1000" show-word-limit />
</el-form-item>
<el-form-item label="附件" prop="attachment">
<el-upload
:action="uploadUrl"
:headers="uploadHeaders"
:file-list="fileList"
:on-success="handleUploadSuccess"
:on-remove="handleRemove"
:before-upload="beforeUpload"
multiple
:limit="9"
>
<el-button size="small" icon="el-icon-upload">选择文件</el-button>
<div slot="tip" class="el-upload__tip">支持多文件上传,最多9个文件,单个文件不超过10MB</div>
</el-upload>
</el-form-item>
</el-form>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="dataFormSubmit" :loading="submitLoading">确定</el-button>
</div>
</el-dialog>
</template>
<script>
import request from '@/utils/request'
export default {
data() {
return {
visible: false,
loading: false,
submitLoading: false,
isEdit: false,
dataForm: {
id: '',
storeId: '',
storeName: '',
expenseCategoryId: '',
expenseCategoryName: '',
expenseDate: '',
unitPrice: 0,
quantity: 0,
amount: 0,
memo: '',
attachment: [],
relatedReimbursementId: '',
relatedPurchaseRecordId: ''
},
rules: {
storeId: [
{ required: true, message: '请选择门店', trigger: 'change' }
],
expenseDate: [
{ required: true, message: '请选择支出日期', trigger: 'change' }
],
amount: [
{ required: true, message: '金额不能为空', trigger: 'blur' },
{ type: 'number', min: 0, message: '金额必须大于等于0', trigger: 'blur' }
]
},
storeOptions: [],
categoryOptions: [],
fileList: [],
uploadUrl: '',
uploadHeaders: {}
}
},
created() {
this.initStoreOptions()
this.initCategoryOptions()
this.initUploadConfig()
},
methods: {
init(id) {
this.visible = true
this.isEdit = !!id
this.loading = false
this.submitLoading = false
this.resetForm()
if (id) {
this.loadData(id)
}
},
resetForm() {
this.dataForm = {
id: '',
storeId: '',
storeName: '',
expenseCategoryId: '',
expenseCategoryName: '',
expenseDate: '',
unitPrice: 0,
quantity: 0,
amount: 0,
memo: '',
attachment: [],
relatedReimbursementId: '',
relatedPurchaseRecordId: ''
}
this.fileList = []
if (this.$refs.form) {
this.$refs.form.clearValidate()
}
},
loadData(id) {
this.loading = true
request({
url: `/api/Extend/LqStoreExpense/${id}`,
method: 'GET'
}).then(res => {
if (res.code == 200 && res.data) {
this.dataForm = {
id: res.data.id,
storeId: res.data.storeId,
storeName: res.data.storeName,
expenseCategoryId: res.data.expenseCategoryId || '',
expenseCategoryName: res.data.expenseCategoryName || '',
expenseDate: res.data.expenseDate ? new Date(res.data.expenseDate).getTime() : '',
unitPrice: res.data.unitPrice || 0,
quantity: res.data.quantity || 0,
amount: res.data.amount || 0,
memo: res.data.memo || '',
attachment: res.data.attachment || [],
relatedReimbursementId: res.data.relatedReimbursementId || '',
relatedPurchaseRecordId: res.data.relatedPurchaseRecordId || ''
}
// 处理附件列表
if (this.dataForm.attachment && this.dataForm.attachment.length > 0) {
this.fileList = this.dataForm.attachment.map(item => ({
name: item.name,
url: item.url,
fileId: item.fileId,
uid: item.fileId || Date.now() + Math.random()
}))
} else {
this.fileList = []
}
}
this.loading = false
}).catch(() => {
this.loading = false
})
},
initStoreOptions() {
request({
url: '/api/Extend/LqMdxx',
method: 'GET',
data: {
currentPage: 1,
pageSize: 1000
}
}).then(res => {
if (res.data && res.data.list) {
this.storeOptions = res.data.list
}
}).catch(() => {
this.storeOptions = []
})
},
initCategoryOptions() {
request({
url: '/api/Extend/LqReimbursementCategory',
method: 'GET',
data: {
currentPage: 1,
pageSize: 1000
}
}).then(res => {
if (res.data && res.data.list) {
this.categoryOptions = res.data.list
}
}).catch(() => {
this.categoryOptions = []
})
},
initUploadConfig() {
this.uploadUrl = this.define.comUploadUrl
this.uploadHeaders = {
Authorization: this.$store.getters.token
}
},
handleStoreChange(value) {
const store = this.storeOptions.find(s => s.id === value)
if (store) {
this.dataForm.storeName = store.dm
} else {
this.dataForm.storeName = ''
}
},
handleCategoryChange(value) {
const category = this.categoryOptions.find(c => c.id === value)
if (category) {
this.dataForm.expenseCategoryName = category.typeName
} else {
this.dataForm.expenseCategoryName = ''
}
},
calculateAmount() {
const unitPrice = Number(this.dataForm.unitPrice) || 0
const quantity = Number(this.dataForm.quantity) || 0
this.dataForm.amount = Number((unitPrice * quantity).toFixed(2))
},
handleUploadSuccess(res, file, fileList) {
if (res.code == 200) {
const fileItem = {
name: file.name,
fileId: res.data.name,
url: res.data.url || res.data.name
}
if (!this.dataForm.attachment) {
this.dataForm.attachment = []
}
this.dataForm.attachment.push(fileItem)
this.fileList = fileList
} else {
this.$message.error(res.msg || '文件上传失败')
this.fileList = fileList.filter(f => f.uid !== file.uid)
}
},
handleRemove(file, fileList) {
this.fileList = fileList
if (this.dataForm.attachment) {
this.dataForm.attachment = this.dataForm.attachment.filter(item => item.fileId !== file.fileId)
}
},
beforeUpload(file) {
const isLt10M = file.size / 1024 / 1024 < 10
if (!isLt10M) {
this.$message.error('上传文件大小不能超过 10MB!')
}
return isLt10M
},
dataFormSubmit() {
this.$refs.form.validate((valid) => {
if (valid) {
// 验证金额计算
if (this.dataForm.unitPrice && this.dataForm.quantity) {
const calculatedAmount = (parseFloat(this.dataForm.unitPrice) || 0) * (parseFloat(this.dataForm.quantity) || 0)
const inputAmount = parseFloat(this.dataForm.amount) || 0
if (Math.abs(calculatedAmount - inputAmount) > 0.01) {
this.$message.warning('金额必须等于单价乘以数量')
return
}
}
this.submitLoading = true
const submitData = {
storeId: this.dataForm.storeId,
storeName: this.dataForm.storeName,
expenseCategoryId: this.dataForm.expenseCategoryId || '',
expenseCategoryName: this.dataForm.expenseCategoryName || '',
expenseDate: this.dataForm.expenseDate ? new Date(this.dataForm.expenseDate).toISOString() : '',
unitPrice: parseFloat(this.dataForm.unitPrice) || 0,
quantity: parseInt(this.dataForm.quantity) || 0,
amount: parseFloat(this.dataForm.amount) || 0,
memo: this.dataForm.memo || '',
attachment: this.dataForm.attachment || [],
relatedReimbursementId: this.dataForm.relatedReimbursementId || '',
relatedPurchaseRecordId: this.dataForm.relatedPurchaseRecordId || ''
}
const url = this.isEdit
? `/api/Extend/LqStoreExpense/${this.dataForm.id}`
: '/api/Extend/LqStoreExpense'
const method = this.isEdit ? 'PUT' : 'POST'
request({
url,
method,
data: submitData
}).then(res => {
this.$message({
type: 'success',
message: res.msg || (this.isEdit ? '更新成功' : '创建成功'),
onClose: () => {
this.handleClose()
this.$emit('refresh')
}
})
}).catch(() => {
this.submitLoading = false
})
}
})
},
handleClose() {
this.visible = false
this.resetForm()
}
}
}
</script>
<style lang="scss" scoped>
.dialog-footer {
text-align: right;
}
</style>