invoice-update.vue
9.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
<template>
<Modal v-model="visible" :title="title" :loading="inLoading">
<Form ref="modal" :model="formItem" :label-width="80" :rules="rules">
<FormItem label="订单编号">
<span>{{ formItem.orderCode }}</span>
</FormItem>
<FormItem label="发票类型">
<RadioGroup v-model="formItem.type">
<Radio :label="ELECTRONIC">电子发票</Radio>
<Radio :label="PAPER">纸质发票</Radio>
<Radio :label="CANCELLED">作废</Radio>
</RadioGroup>
</FormItem>
<template v-if="formItem.type !== CANCELLED">
<FormItem v-show="formItem.type === ELECTRONIC" label="上传PDF" prop="pdfUrl">
<PDFUpload v-model="formItem.pdfUrl" @input="onPDFUrlChang" />
</FormItem>
<FormItem v-show="formItem.type === PAPER" label="物流信息">
<RadioGroup v-model="formItem.logisticsSwitch">
<Radio :label="LOGISTICS_YES">需要物流</Radio>
<Radio :label="LOGISTICS_NO">不需要物流</Radio>
</RadioGroup>
</FormItem>
<template v-if="formItem.logisticsSwitch === LOGISTICS_YES && formItem.type === PAPER">
<FormItem prop="logisticsId" class="logistics-company">
<Select v-model="formItem.logisticsId">
<Option v-for="(item, idx) in expressList" :key="idx" :value="item.id">
{{ item.companyName }}
</Option>
</Select>
</FormItem>
<FormItem prop="expressNumber" class="express-number">
<i-input v-model="formItem.expressNumber" type="text" placeholder="物流单号"></i-input>
</FormItem>
</template>
</template>
</Form>
<div slot="footer" class="modal-footer">
<span v-if="formItem.status !== UNOPEN"><i>*</i> 在第三方开票系统冲红或作废后更新保存发票信息</span>
<Button type="primary" :loading="inLoading" @click="update">保存</Button>
<Button @click="close">关闭</Button>
</div>
</Modal>
</template>
<script>
import _ from 'lodash';
import { InvoiceStatusName2Id, InvoiceTypeName2Id, LogisticsTypeName2Id } from '../store/constant';
import InvoiceService from 'services/finance/invoice-service';
import TradeService from 'services/trade/trade-service';
import PDFUpload from './pdf-upload';
export default {
name: 'UpdateInvoice',
components: {
PDFUpload,
},
data() {
return {
visible: false,
inLoading: false,
title: '',
// constants
UNOPEN: InvoiceStatusName2Id.UNOPEN,
CANCELLED: InvoiceStatusName2Id.CANCELLED,
...InvoiceTypeName2Id,
LOGISTICS_NO: LogisticsTypeName2Id.NO,
LOGISTICS_YES: LogisticsTypeName2Id.YES,
// api result data
expressList: [],
formItem: {},
// rules
rules: {
pdfUrl: {
trigger: 'change',
validator: (rule, value, callback) => {
if (this.formItem.type === InvoiceTypeName2Id.ELECTRONIC) {
if (!value) {
return callback(new Error('请上传发票PDF文件'));
}
}
callback();
},
},
logisticsId: {
trigger: 'change',
validator: (rule, value, callback) => {
if (this.formItem.type === InvoiceTypeName2Id.PAPER) {
if (!value) {
return callback(new Error('请选择物流公司'));
}
}
callback();
},
},
expressNumber: {
trigger: 'blur',
validator: (rule, value, callback) => {
if (this.formItem.type === InvoiceTypeName2Id.PAPER) {
if (!value) {
return callback(new Error('请输入物流单号'));
}
if (!/^[\w\d]{10,20}$/i.test(value)) {
// 10位-20位字符,只能是数字或者字母
return callback(new Error('请输入有效的物流单号:10位~20位数字或字母'));
}
}
callback();
},
},
},
};
},
created() {
this.invoiceAPI = new InvoiceService();
this.tradeAPI = new TradeService();
this.tradeAPI.allotExpressCompList().then(res => {
const list = res.data;
this.expressList = list;
// create expressId2Name map
const ExpressId2Name = list.reduce((acc, item) => {
acc[item.id] = item.companyName;
return acc;
}, {});
this.$watch(
'formItem.logisticsId',
newVal => {
if (newVal === '' || newVal == null) {
this.formItem.logisticsCompanyName = '';
} else {
this.formItem.logisticsCompanyName = ExpressId2Name[newVal] || '';
}
},
{
immediate: true,
}
);
});
},
methods: {
setTitle(invoice) {
if (invoice.status === InvoiceStatusName2Id.UNOPEN) {
this.title = '录入发票';
} else {
this.title = '发票详情';
}
},
show(invoice) {
// this.$refs.modal.resetFields();
this.setTitle(invoice);
// initial data
this.invoiceData = invoice;
this.formItem = {
pdfUrl: null,
logisticsId: null,
expressNumber: null,
logisticsCompanyName: null,
logisticsSwitch: this.LOGISTICS_YES,
...invoice,
};
// transfer CANCELLED state to invoice type
if (this.formItem.status === InvoiceStatusName2Id.CANCELLED) {
this.formItem.type = InvoiceStatusName2Id.CANCELLED;
}
this.visible = true;
},
/**
* 前端可控制的状态转移
* Q:
* 这个作废是什么意思?
* 选择作废后,下面的 pdf 或物流信息怎么处理
* 如果发票状态是未开发票,可以先作废吗
* 选作废后,是不是就不可以编辑当前发票的状态了
*
* A:
* 1.作废就是不提供发票了,之前提供的发票也不展示了
* 2.删除
* 3.可以,表示不提供发票
* 4.可以编辑,作废只是状态之一,不是终点
*
* so: rules
*
* [1]未开发票->已经开发票
* [2]退货未处理->退货已经处理
* [3]作废->已开发票(针对退款额为0)
* [4]作废->退货已经处理((针对退款额不为0)
*
* [5]非作废状态都可作废(*->作废)
* 退货未处理->作废
* 退货已经处理->作废
* 未开发票->作废
* 已开发票->作废
* [6] 已开票->已开票, 退货已经处理->退货已经处理(更新发票信息)
* return: true: 可以提交, false: 不必提交(当前状态为作废, 选择状态还是作废)
*/
updateState(invoice) {
const { status: currentStatus } = this.invoiceData;
const isCancled = invoice.type === InvoiceStatusName2Id.CANCELLED;
if (isCancled) {
if (currentStatus === InvoiceStatusName2Id.CANCELLED) {
return false;
}
// 作废, 删除关联数据 [5]
_.assign(invoice, {
type: InvoiceTypeName2Id.ELECTRONIC,
pdfUrl: null,
logisticsSwitch: null,
logisticsId: null,
expressNumber: null,
status: InvoiceStatusName2Id.CANCELLED,
});
} else {
let nextStatus;
switch (currentStatus) {
case InvoiceStatusName2Id.UNOPEN:
nextStatus = InvoiceStatusName2Id.OPNED; // [1]
break;
case InvoiceStatusName2Id.REJECT_UNHANDLED:
nextStatus = InvoiceStatusName2Id.REJECT_HANDLED; // [2]
break;
case InvoiceStatusName2Id.CANCELLED:
if (this.invoiceData.returnAmount === 0) {
nextStatus = InvoiceStatusName2Id.OPNED; // [3]
} else {
nextStatus = InvoiceStatusName2Id.REJECT_HANDLED; // [4]
}
break;
default:
nextStatus = currentStatus; // [6]
}
invoice.status = nextStatus;
}
return true;
},
validate(callback) {
if (this.$refs.modal.fields.length !== 0) {
return this.$refs.modal.validate(callback);
}
return callback(true);
},
update() {
// validate
this.validate(valid => {
if (valid) {
// only whitelist
const params = _.pick(this.formItem, [
'shopId',
'orderCode',
'status',
'step',
// issueDate
'pdfUrl',
'type',
'logisticsId',
'logisticsCompanyName',
'expressNumber',
'logisticsSwitch',
]);
if (!this.updateState(params)) {
this.close();
return;
}
this.inLoading = true;
this.invoiceAPI
.update(params)
.then(() => {
this.inLoading = false;
this.$Message.success('更新成功');
this.$emit('updated', params);
this.close();
})
.catch(() => {
this.inLoading = false;
this.$Message.warning('更新失败');
});
}
});
},
close() {
this.visible = false;
},
onPDFUrlChang() {
this.$refs.modal.validateField('pdfUrl');
},
},
};
</script>
<style lang="scss" scoped>
.modal-footer {
& > span {
float: left;
line-height: 32px;
}
i {
color: red;
}
}
.file-upload {
display: inline-block;
}
.logistics-company {
display: inline-block;
min-width: 20em;
}
.express-number {
display: inline-block;
/deep/ .ivu-form-item-content {
margin-left: 20px !important;
}
}
</style>