invoice.js
6.26 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
/**
* 订单结算页-发票
* @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;
$('.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 (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 $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');
});
$el.on('click', '.invoice-close', function() {
$('.btn-close', $el).trigger('click');
});
}
function bindInvoiceInfo($el, info) {
info = info || {};
if (info.titleId) {
$('.rbt-' + info.titleId).trigger('click');
if (info.titleId === 2) {
$('#company-name', $el).val(info.titleName || '');
$('#company-tax-num', $el).val(info.taxNumber || '');
} else {
$('#personal-name', $el).val(info.titleName || '');
}
}
$('#receiver-phone', $el).val(info.receiver || defaultReceiver.hide || '');
}
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 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;
$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,
taxNumber: invoiceInfo.taxNumber || '',
receiver: invoiceInfo.receiver === defaultReceiver.hide ? defaultReceiver.full : invoiceInfo.receiver
};
};