invoice.page.js 5.82 KB
/**
 * 我的发票
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 201/2/17
 */

var $ = require('yoho-jquery');
var Dialog = require('../common/dialog').Dialog;

var $hideDg = $('#invoice-hide-dg');

var detailTpl = require('hbs/home/invoice/detail.hbs');
var invoiceTpl = $hideDg.html();

var defaultReceiver;

require('../common');

$hideDg.remove();

function bindSupplyDialogEvent($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');
    });
}

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 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 (defaultReceiver && 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 viewInvoice(code) {
    if (!code) {
        return;
    }

    $.ajax({
        url: '/home/invoice/detail',
        type: 'GET',
        data: {orderCode: code}
    }).done(function(res) {
        if (res.code !== 200) {
            return;
        }

        new Dialog({
            className: 'invoice-me-page invoice-detail-dialog',
            content: detailTpl(res.data)
        }).show();
    });
}

function supplyInvoice(info, cb) {
    $.ajax({
        url: '/home/invoice/supply',
        type: 'POST',
        data: info
    }).done(function(res) {
        var _dg, _btn;

        cb && cb(res); // eslint-disable-line

        if (res.code === 200) {
            if (res.data.issueType === 2) {
                _btn = {
                    name: '查看发票',
                    cb: function() {
                        viewInvoice(res.data.order_code);
                    }
                };
            } else {
                _btn = {name: '我知道了'};
            }

            _dg = new Dialog({
                className: 'invoice-me-page supply-success-dialog',
                content: '<h2>提交成功</h2><p>您的服务申请已提交,您可以在我的发票中下载电子发票</p>',
                btns: [{
                    id: 'close-dg',
                    name: _btn.name,
                    btnClass: ['close-dg'],
                    cb: function() {
                        _dg.close();
                        _btn.cb && _btn.cb();
                    }
                }]
            }).show();
        }
    });
}

$('#me-invoice-orders').on('click', '.supply-invoice', function() {
    var $this = $(this);
    var dg = new Dialog({
        className: 'invoice-me-page invoice-supply-dialog',
        content: invoiceTpl,
        btns: [{
            id: 'sure-apply',
            name: '提交',
            btnClass: ['sure-apply'],
            cb: function() {
                var info = packInvoiceInfo(dg.$el);

                if (!validateInvoice(dg.$el, info)) {
                    return;
                }

                info.orderCode = $(this).data('id');
                supplyInvoice(info, function(data) {
                    if (data.code !== 200) {
                        return;
                    }

                    dg.close();

                    if (+data.issueType === 2) {
                        $this.removeClass('supply-invoice').addClass('view-invoice').text('查看发票');
                    } else {
                        $this.removeClass('supply-invoice').addClass('on-invoice').text('开票中');
                    }
                });
            }
        }, {
            id: 'cancel-apply',
            name: '取消',
            btnClass: ['cancel-apply'],
            cb: function() {
                dg.close();
            }
        }]
    }).show();

    // 事件绑定
    bindSupplyDialogEvent(dg.$el);
}).on('click', '.view-invoice', function() {
    viewInvoice($(this).data('id'));
});