invoice.page.js
5.82 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
/**
* 我的发票
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 201/2/17
*/
var $ = require('yoho-jquery');
var Dialog = require('../common/dialog').Dialog;
var $hideDg = $('#invoice-hide-dg');
var detailTpl = require('hbs/home/invoice/detail.hbs');
var invoiceTpl = $hideDg.html();
var defaultReceiver;
require('../common');
$hideDg.remove();
function bindSupplyDialogEvent($el) {
var $titleWrap = $('.invoice-title', $el),
$personalRow = $('.personal-row', $el),
$companyRow = $('.company-row', $el);
$titleWrap.on('click', '.radio-btn', function() {
var $this = $(this),
id = $this.data('id');
if ($this.hasClass('on')) {
return;
}
if (id * 1 === 2) {
$companyRow.removeClass('hide');
$personalRow.addClass('hide');
} else {
$companyRow.addClass('hide');
$personalRow.removeClass('hide');
}
$titleWrap.find('.on').removeClass('on');
$this.addClass('on');
});
}
function packInvoiceInfo($el) {
var resData = { // 5.8.1需求,只支持电子发票(type: 2),发票内容只能开明细(id:12)
invocesType: 2,
contentId: 12,
contentName: '明细'
},
receiver = $('#receiver-phone', $el).val();
resData.titleId = $('.invoice-title .on', $el).data('id') || 1;
if (resData.titleId * 1 === 1) {
resData.titleName = $.trim($('#personal-name', $el).val()) || '个人';
} else {
resData.titleName = $.trim($('#company-name', $el).val());
resData.taxNumber = $.trim($('#company-tax-num', $el).val());
}
if (receiver) {
resData.receiver = receiver;
}
return resData;
}
function validateInvoice($el, info) {
var pass = true;
var $receiverTip;
// 发票抬头
if (!info.titleName) {
pass = false;
$('.company-name-tip', $el).removeClass('hide');
} else {
$('.company-name-tip', $el).addClass('hide');
}
if (info.titleId === 2) {
if (!info.taxNumber) {
pass = false;
$('.company-tax-tip', $el).removeClass('hide');
} else {
$('.company-tax-tip', $el).addClass('hide');
}
}
// 收票人手机号
if (info.invocesType * 1 === 2) {
$receiverTip = $('.receiver-tip', $el);
if (!info.receiver) {
$receiverTip.removeClass('hide').find('em').html('请填写手机号码');
pass = false;
} else if (defaultReceiver && info.receiver === defaultReceiver.hide) {
$receiverTip.addClass('hide');
} else if (!/^[0-9]{11}$/.test(info.receiver)) {
$receiverTip.removeClass('hide').find('em').html('请输入正确手机号');
pass = false;
} else {
$receiverTip.addClass('hide');
}
}
return pass;
}
function viewInvoice(code) {
if (!code) {
return;
}
$.ajax({
url: '/home/invoice/detail',
type: 'GET',
data: {orderCode: code}
}).done(function(res) {
if (res.code !== 200) {
return;
}
new Dialog({
className: 'invoice-me-page invoice-detail-dialog',
content: detailTpl(res.data)
}).show();
});
}
function supplyInvoice(info, cb) {
$.ajax({
url: '/home/invoice/supply',
type: 'POST',
data: info
}).done(function(res) {
var _dg, _btn;
cb && cb(res); // eslint-disable-line
if (res.code === 200) {
if (res.data.issueType === 2) {
_btn = {
name: '查看发票',
cb: function() {
viewInvoice(res.data.order_code);
}
};
} else {
_btn = {name: '我知道了'};
}
_dg = new Dialog({
className: 'invoice-me-page supply-success-dialog',
content: '<h2>提交成功</h2><p>您的服务申请已提交,您可以在我的发票中下载电子发票</p>',
btns: [{
id: 'close-dg',
name: _btn.name,
btnClass: ['close-dg'],
cb: function() {
_dg.close();
_btn.cb && _btn.cb();
}
}]
}).show();
}
});
}
$('#me-invoice-orders').on('click', '.supply-invoice', function() {
var $this = $(this);
var dg = new Dialog({
className: 'invoice-me-page invoice-supply-dialog',
content: invoiceTpl,
btns: [{
id: 'sure-apply',
name: '提交',
btnClass: ['sure-apply'],
cb: function() {
var info = packInvoiceInfo(dg.$el);
if (!validateInvoice(dg.$el, info)) {
return;
}
info.orderCode = $(this).data('id');
supplyInvoice(info, function(data) {
if (data.code !== 200) {
return;
}
dg.close();
if (+data.issueType === 2) {
$this.removeClass('supply-invoice').addClass('view-invoice').text('查看发票');
} else {
$this.removeClass('supply-invoice').addClass('on-invoice').text('开票中');
}
});
}
}, {
id: 'cancel-apply',
name: '取消',
btnClass: ['cancel-apply'],
cb: function() {
dg.close();
}
}]
}).show();
// 事件绑定
bindSupplyDialogEvent(dg.$el);
}).on('click', '.view-invoice', function() {
viewInvoice($(this).data('id'));
});