invoice.js
5.53 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
/**
* 发票
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/9/6
*/
var $ = require('yoho-jquery');
var Dialog = require('../../common/dialog').Dialog;
var invoiceTpl = $('#invoice-chose-tpl').html();
var $withInvoice = $('#with-invoice');
var $withoutInvoice = $('#without-invoice');
// 发票信息验证
function validateInvoice($el) {
var pass = true;
var name = $.trim($('#company-name', $el).val());
var $receiverTip;
var mobile;
// 发票抬头为单位
if ($('input[name="invoice-title"]:checked', $el).val() === '2' && name === '') {
pass = false;
$('.invoice-title-tip', $el).removeClass('hide');
} else {
$('.invoice-title-tip', $el).addClass('hide');
}
// 收票人手机号
if ($('.el-invoice', $el).hasClass('focus')) {
$receiverTip = $('.receiver-tip', $el);
mobile = $.trim($('#receiver-phone', $el).val());
if (mobile === '') {
$receiverTip.removeClass('hide').find('em').html('请填写手机号码');
pass = false;
} else if (!/^[0-9]{11}$/.test(mobile)) {
$receiverTip.removeClass('hide').find('em').html('手机号码不正确');
pass = false;
} else {
$receiverTip.addClass('hide');
}
}
return pass;
}
function pkgInvoice() {
var $contentChecked = $('input[name="invoice-content"]:checked');
var pkg = {
invocesType: $('.el-invoice').hasClass('focus') ? '2' : '1',
invoicesTitle: $('input[name="invoice-title"]').prop('checked') ? '个人' : $('#company-name').val(),
invoicesContent: $contentChecked.val(),
invoicesContentName: $contentChecked.data('name')
};
if (pkg.invocesType === '1') {
pkg.receiver = $('#receiver-phone').val();
}
return pkg;
}
function bindInvoiceInfo(info) {
if (info) {
// 发票类型
if (info.invocesType === '2') {
$('.pa-invoice, .el-invoice').toggleClass('focus');
$('.invoice-content').removeClass('el-content');
} else {
// 电子发票
$('#receiver-phone').val(info.receiver);
}
// 发票抬头
if (!info.invoicesTitle || info.invoicesTitle === '个人') {
$('input[name="invoice-title"][value="1"]').prop('checked', true);
} else {
$('input[name="invoice-title"][value="2"]').prop('checked', true);
$('.company-row').removeClass('hide').find('.company-name').val(info.invoicesTitle);
}
// 发票内容
$('input[name="invoice-content"][value="' + info.invoicesContent + '"]').prop('checked', true);
}
}
function bindInvoiceEvent() {
var $invoiceTypes = $('.invoice-type li');
var $companyRow = $('.company-row');
// 发票类型切换
$invoiceTypes.click(function() {
var $this = $(this);
if ($this.hasClass('focus')) {
return;
}
$invoiceTypes.toggleClass('focus');
$('.invoice-content').toggleClass('el-content');
});
// 发票抬头
$('input[name="invoice-title"]').change(function() {
if ($(this).val() === '2') {
$companyRow.removeClass('hide');
} else {
$companyRow.addClass('hide');
}
});
}
function invoiceFactory(info) {
var invoice = new Dialog({
content: invoiceTpl,
closeIcon: false,
className: 'invoice',
btns: [
{
id: 'save-invoice',
name: '保存发票信息',
btnClass: ['save-invoice'],
cb: function() {
var pkg;
var p;
if (validateInvoice(invoice.$el)) {
pkg = pkgInvoice();
$withInvoice.removeClass('hide').find('em').html(
'<i>' + (pkg.invocesType === '1' ? '电子发票' : '纸质发票') +
'</i><i>' + pkg.invoicesTitle + '</i><i>' + pkg.invoicesContentName + '</i>'
);
$withoutInvoice.addClass('hide');
for (p in pkg) {
if (pkg.hasOwnProperty(p)) {
$withInvoice.data(p, pkg[p]);
}
}
invoice.close();
}
}
},
{
id: 'cancel-invoice',
name: '取消',
btnClass: ['cancel-invoice'],
cb: function() {
invoice.close();
}
}
]
});
bindInvoiceEvent();
bindInvoiceInfo($.extend({
receiver: $('#address-used').data('mobile')
}, info));
return invoice;
}
$('#invoice-radio').click(function(e) {
e.preventDefault();
invoiceFactory().show();
});
$('#modify-invoice').click(function() {
invoiceFactory({
invocesType: $withInvoice.data('invocesType'),
invoicesTitle: $withInvoice.data('invoicesTitle'),
invoicesContent: $withInvoice.data('invoicesContent'),
receiver: $withInvoice.data('receiver')
}).show();
});
// 获取发票信息
exports.getInvoice = function() {
if ($withInvoice.hasClass('hide')) {
return;
}
return {
invoicesType: $withInvoice.data('invocesType'),
invoicesTitle: $withInvoice.data('invoicesTitle'),
invoicesContent: $withInvoice.data('invoicesContent'),
receiver: $withInvoice.data('receiver')
};
};