invoice.js 6.33 KB
/**
 * 订单结算页-发票
 * @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 cleanHtml = require('../../../../utils/cleanHtml');

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 += '&nbsp;&nbsp;&nbsp;&nbsp;' + cleanHtml.htmlEncode(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
    };
};