invoice.js
6.75 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
/**
* 订单结算页-发票
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/12/29
*/
var $ = require('yoho-jquery');
var yas = require('../../common/data-yas');
var Dialog = require('../../common/dialog').Dialog;
var $invoiceRadio = $('#invoice-radio');
var invoiceTpl = $('#invoice-chose-tpl').html();
var invoiceInfo = {},
defaultReceiver = {};
function validateInvoice($el, info) {
var pass = true;
var $receiverTip;
// 发票抬头
if (!info.titleName) {
pass = false;
$('.invoice-title-tip', $el).removeClass('hide');
} else {
$('.invoice-title-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 (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 bindInvoiceEvent($el) {
var $invoiceTypeWrap = $('.invoice-type', $el),
$titleWrap = $('.invoice-title', $el),
$goodsTypeWrap = $('.invoice-goods-type', $el),
$receiver = $('.receiver', $el),
$companyRow = $('.company-row', $el);
$invoiceTypeWrap.on('click', 'li', function() {
var $this = $(this);
if ($this.hasClass('focus')) {
return;
}
if ($this.hasClass('el-invoice')) {
$receiver.removeClass('hide');
} else {
$receiver.addClass('hide');
}
$this.siblings('.focus').removeClass('focus');
$this.addClass('focus');
});
$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');
} else {
$companyRow.addClass('hide');
}
$titleWrap.find('.on').removeClass('on');
$this.addClass('on');
});
$goodsTypeWrap.on('click', '.radio-btn', function() {
var $this = $(this);
if ($this.hasClass('on')) {
return;
}
$goodsTypeWrap.find('.on').removeClass('on');
$this.addClass('on');
});
$el.on('click', '.invoice-close', function() {
$('.btn-close', $el).trigger('click');
});
}
function bindInvoiceInfo($el, info) {
info = info || {};
// 发票类型
if (info.invocesType === 1) {
$('.pa-invoice', $el).trigger('click');
}
if (info.titleId) {
$('.rbt-' + info.titleId).trigger('click');
if (info.titleId === 2 && info.titleName) {
$('#company-name', $el).val(info.titleName);
}
}
if (info.contentId) {
$('.rbc-' + info.contentId, $el).trigger('click');
}
$('#receiver-phone', $el).val(info.receiver || defaultReceiver.hide || '');
}
function packInvoiceInfo($el) {
var $goodsType = $('.invoice-goods-type .on', $el);
var resData = {},
receiver = $('#receiver-phone', $el).val();
if ($('.pa-invoice', $el).hasClass('focus')) {
resData.invocesType = 1;
} else {
resData.invocesType = 2;
}
resData.titleId = $('.invoice-title .on', $el).data('id') || 1;
if (resData.titleId * 1 === 1) {
resData.titleName = '个人';
} else {
resData.titleName = $('#company-name', $el).val();
}
resData.contentId = $goodsType.data('id');
resData.contentName = $goodsType.data('name');
if (receiver) {
resData.receiver = receiver;
}
return resData;
}
function setShowInvoiceInfo() {
var $dom = $invoiceRadio.next();
var _h = '';
if (!invoiceInfo.invocesType) {
$dom.addClass('hide');
return;
}
if (invoiceInfo.invocesType * 1 === 1) {
_h += '纸质发票';
} else {
_h += '电子发票';
}
_h += ' ' + invoiceInfo.titleName +
' ' + invoiceInfo.contentName;
$dom.removeClass('hide').find('span').html(_h);
}
function invoiceEditDialog(baseInfo) {
var invoice = new Dialog({
content: invoiceTpl,
className: 'ensure-invoice-dialog',
btns: [
{
id: 'save-invoice',
name: '保存发票信息',
btnClass: ['save-invoice'],
cb: function() {
var info = packInvoiceInfo(invoice.$el);
if (validateInvoice(invoice.$el, info)) {
$.extend(invoiceInfo, packInvoiceInfo(invoice.$el));
setShowInvoiceInfo();
invoice.close();
// 保存发票信息按钮埋点
yas.givePoint('YB_SC_INVOICE_INFO_SAVE', {
INVOICE_TYPE: invoiceInfo.invocesType,
INVOICE_TITLE: invoiceInfo.titleId,
INVOICE_CONTENT: invoiceInfo.contentId
});
}
}
},
{
id: 'cancel-invoice',
name: '取消',
btnClass: ['btn-close'],
cb: function() {
invoice.close();
if (!invoiceInfo.invocesType) {
$invoiceRadio.removeClass('on');
}
}
}
]
});
if (!defaultReceiver.full) {
defaultReceiver = invoice.$el.find('.receiver').data();
}
bindInvoiceEvent(invoice.$el);
bindInvoiceInfo(invoice.$el, baseInfo);
return invoice;
}
$invoiceRadio.click(function() {
$invoiceRadio.toggleClass('on');
if ($invoiceRadio.hasClass('on')) {
invoiceEditDialog(invoiceInfo).show();
// 发票选中时埋点
yas.givePoint('YB_SC_INVOICE_ISSUE');
} else {
invoiceInfo = {};
setShowInvoiceInfo();
}
});
$('#modify-invoice').click(function() {
invoiceEditDialog(invoiceInfo).show();
});
// 获取发票信息
exports.getInvoice = function() {
if (!$invoiceRadio.hasClass('on') || !invoiceInfo.invocesType) {
return;
}
return {
invoicesType: invoiceInfo.invocesType,
invoicesTitle: invoiceInfo.titleName,
invoicesContent: invoiceInfo.contentId,
receiver: invoiceInfo.receiver === defaultReceiver.hide ? defaultReceiver.full : invoiceInfo.receiver
};
};