invoice.js 5.53 KB
/**
 * 发票
 * @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')
    };
};