/**
 * 开票申请采购确认页面JS工具
 *
 * Created by li.kui on 2020/9/3.
 */
'use strict';

/**
 * 数组元素去重
 *
 * @param arr 原数组
 * @returns {Array} 结果
 */
function unique(arr){
    // 新建一个数组来存放arr中的值
    var arr1 = [];
    for(var i = 0, len = arr.length; i < len; i++){
        if(arr1.indexOf(arr[i]) === -1){
            arr1.push(arr[i]);
        }
    }
    return arr1;
}


/**
 * 刷新返利表格底部合计
 */
function refreshTbRebateFooter() {
    // 返利数据
    var rebate = $(el.tbRebate).datagrid('getRows');
    var rebateAmount = new BigNumber(0);
    for(var j = 0; j < rebate.length; j++){
        var item2 = rebate[j];
        // 1:购方对供方返利 2:供方对购方返利
        if(Number(item2.rebateType) === 1){
            rebateAmount = rebateAmount.plus(item2.rebateAmount);
        }else {
            rebateAmount = rebateAmount.plus(item2.rebateAmount * -1);
        }
    }
    // 更新页脚行的值并刷新
    var trFooters = $(el.tbRebate).datagrid('getFooterRows');
    trFooters[0]['rebateAmount'] = rebateAmount.toFixed(2);
    $(el.tbRebate).datagrid('reloadFooter');
}


/**
 * 刷新扣款表格底部合计
 */
function refreshTbDeductionFooter() {
    // 扣款数据
    var deduction = $(el.tbDeduction).datagrid('getRows');
    var deductionAmount = new BigNumber(0);
    for(var k = 0; k < deduction.length; k++){
        var item3 = deduction[k];
        deductionAmount = deductionAmount.plus(item3.deductionAmount);
    }
    // 更新页脚行的值并刷新
    var tdFooters = $(el.tbDeduction).datagrid('getFooterRows');
    tdFooters[0]['deductionAmount'] = deductionAmount.toFixed(2);
    $(el.tbDeduction).datagrid('reloadFooter');
}


/**
 * 更新页面表格显示列值
 *
 * @param girdTr 表格行DOM
 * @param oriRowData 原表格行数据
 * @param columns 需要更新的列名
 * @param columnsData 列名对应的值
 */
function updatePageShowValue(girdTr, oriRowData, columns, columnsData) {
    // for(var i = 0; i < columns.length; i++){
    //     var value = columnsData[i], item = columns[i];
    //     if(Number(value) < 0){
    //         value = '<span style="color: red;">' + formatNumber(value, item.cent, 1) + '</span>'
    //     }else {
    //         value = formatNumber(value, item.cent, 1);
    //     }
    //     girdTr.siblings('td[field="' + item.column + '"]').find('div').html(value);
    //     oriRowData[item.column] = columnsData[i];
    // }

    for(var i = 0; i < columns.length; i++){
        var value = columnsData[i], column = columns[i], htmlValue = '';
        var valueNumber = new BigNumber(value);
        if(Number(value) < 0){
            htmlValue = '<span style="color: red;">' + valueNumber.toFormat() + '</span>'
        }else {
            htmlValue = valueNumber.toFormat();
        }
        girdTr.siblings('td[field="' + column + '"]').find('div').html(htmlValue);
        oriRowData[column] = valueNumber.toNumber();
    }
}


/**
 * 计算页面抬头相关金额
 */
function countPageHeadAmount() {
    // handleVoucherTail();

    var detail = $(el.tbDetails).datagrid('getRows'), rebate = "",
        deduction ="";
    // itAmount:开票含税总金额; inAmount:开票未税总金额
    var itAmount = new BigNumber(0), inAmount = new BigNumber(0);
    // rtAmount:返利含税金额 ; rntAmount:返利未税金额
    // var rtAmount = new BigNumber(0), rntAmount = new BigNumber(0);
    // dtAmount:扣款含税金额; dntAmount:扣款未税金额
    // var dtAmount = new BigNumber(0), dntAmount = new BigNumber(0);

    for (var i = 0; i < detail.length; i++) {
        var item1 = detail[i];
        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', item1);

        var notTaxAmount = item1.notTaxAmount;
        if(el.editIndex === rowIndex){
            var ntaEditor = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'notTaxAmount'}).target;
            notTaxAmount = ntaEditor.numberbox('getValue');
        }

        itAmount = itAmount.plus(item1.taxAmount);
        inAmount = inAmount.plus(notTaxAmount);
    }

    // for(var j = 0; j < rebate.length; j++){
    //     var item2 = rebate[j];
    //     // 1:购方对供方返利 2:供方对购方返利
    //     if(Number(item2.rebateType) === 1){
    //         rtAmount = rtAmount.plus(item2.rebateAmount);
    //         rntAmount = rntAmount.plus(item2.rntAmount);
    //     }else {
    //         rtAmount = rtAmount.plus(Number(item2.rebateAmount) * -1);
    //         rntAmount = rntAmount.plus(Number(item2.rntAmount) * -1);
    //     }
    // }

    // for(var k = 0; k < deduction.length; k++){
    //     var item3 = deduction[k];
    //     dtAmount = dtAmount.plus(item3.deductionAmount);
    //     dntAmount = dntAmount.plus(item3.dntAmount);
    // }

    // 发票含税金额 = 对账明细含税金额合计 - 含税扣款金额 + 含税返利金额
    $(el.invoiceNetAmount).textbox('setValue', inAmount.toFormat(2));//.plus(rntAmount).plus(dntAmount)
    $(el.invoiceTaxAmount).textbox('setValue', itAmount.toFormat(2));//.plus(rtAmount).plus(dtAmount)
    $(el.diffAmount).textbox('setValue', itAmount.minus(inAmount).toFormat(2));
    // $(el.rebateAmount).numberbox('setValue', rtAmount.toFixed(2));
    // $(el.rntAmount).val(rntAmount.toFixed(2));
    // $(el.deductionAmount).numberbox('setValue', dtAmount.toFixed(2));
    // $(el.dntAmount).val(dntAmount.toFixed(2));

    // var dif = Math.abs(Number($(el.invoiceNetAmount).textbox('getValue').replace(/,/g,'')) + Number($(el.invoiceTaxAmount).textbox('getValue').replace(/,/g,''))
    // - Number($(el.invoiceTotalAmount).textbox('getValue').replace(/,/g,'')) - Number($(el.invoiceTaxTotalAmount).textbox('getValue').replace(/,/g,''))).toFixed(2);
    // $(el.diffAmount).textbox('setValue', dif);



}


/**
 * 计算分摊金额
 *
 * @param isEdit 是否正在编辑 0:否 1:是
 */
function calculationShare(isEdit) {
    // 开票申请明细数据
    var rows = $(el.tbDetails).datagrid('getRows');

    // 分摊逻辑: 入库凭证能分摊的行含税金额合计和未税金额合计
    var invoiceNetAmount = new BigNumber(0), rntAmount = new BigNumber($(el.rntAmount).val()), dntAmount = new BigNumber($(el.dntAmount).val());

    // 开票申请中的税率
    var taxValue = 0;
    for (var i = 0; i < rows.length; i++) {
        var item1 = rows[i];
        var rowIndex1 = $(el.tbDetails).datagrid('getRowIndex', item1);
        if(Number(item1.shareFlag) === 1 && Number(item1.statementNum) > 0) {
            var notTaxAmount = item1.notTaxAmount;
            if(el.editIndex === rowIndex1){
                var ntaEditor = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'notTaxAmount'}).target;
                notTaxAmount = ntaEditor.numberbox('getValue');
            }

            invoiceNetAmount = invoiceNetAmount.plus(notTaxAmount);
        }

        taxValue = item1.taxValue;
    }

    // 后续计算时使用
    // var tax = new BigNumber(taxValue).dividedBy(100).plus(1);
    // if(Number(isEdit) === 0){
    //     addOrDelDataCountShare(rows, invoiceNetAmount, tax, rntAmount, dntAmount);
    // }else {
    //     editDataCountShare(rows, invoiceNetAmount, tax, rntAmount, dntAmount);
    // }

    // 处理尾差
    // var rebateAmount = new BigNumber($(el.rebateAmount).numberbox('getValue'));
    // var deductionAmount = new BigNumber($(el.deductionAmount).numberbox('getValue'));
    // handleDecimalDeviation(rebateAmount, rntAmount, deductionAmount, dntAmount);

    // 重新计算表体分摊后金额
    if(Number(isEdit) === 0){
        // againCountShareAfterAmount(rows);
    }else {

        // editAgainCountShareAfterAmount(rows);
    }
}


/**
 * 新增或删除时分摊计算
 */
function addOrDelDataCountShare(rows, itemNtaSum, tax, rntSumAmount, dntSumAmount) {
    for (var n = 0; n < rows.length; n++) {
        var item = rows[n];
        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', item);

        if(Number(item.shareFlag) === 1 && Number(item.statementNum) > 0){
            // 未税金额分摊比例
            var ntRatio = new BigNumber(item.notTaxAmount).div(itemNtaSum);
            // 如果存在两个明细的入库凭证相互冲销后有余数并且允许分摊数量不等于对账数量
            if(Number(item.shareNum) !== Number(item.statementNum)){
                ntRatio = new BigNumber(Number(item.shareNum) * Number(item.notTaxPrice)).div(itemNtaSum);
            }

            var shareRntAmount = ntRatio.mul(rntSumAmount);
            var shareRebateAmount = tax.mul(shareRntAmount);
            var shareDntAmount = ntRatio.mul(dntSumAmount);
            var shareDeductionAmount = tax.mul(shareDntAmount);

            $(el.tbDetails).datagrid('updateRow', {
                index: rowIndex,
                row: {
                    shareRebateAmount: shareRebateAmount.toFixed(2),
                    shareRntAmount: shareRntAmount.toFixed(2),
                    shareDeductionAmount: shareDeductionAmount.toFixed(2),
                    shareDntAmount: shareDntAmount.toFixed(2)
                }
            });
        }
    }
}


/**
 * 编辑时分摊计算
 */
function editDataCountShare(rows, itemNtaSum, tax, rntSumAmount, dntSumAmount) {
    for (var n = 0; n < rows.length; n++) {
        var item = rows[n];
        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', item);
        if(Number(item.shareFlag) === 1 && Number(item.statementNum) > 0){
            // 明细行中未税金额
            var itemNta = new BigNumber(item.notTaxAmount);
            if(el.editIndex === rowIndex){
                var ntaEditor = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'notTaxAmount'}).target;
                itemNta = new BigNumber(ntaEditor.numberbox('getValue'));
            }

            // 未税金额分摊比例
            var ntRatio = itemNta.div(itemNtaSum);
            // 如果存在两个明细的入库凭证相互冲销后有余数并且允许分摊数量不等于对账数量
            if(Number(item.shareNum) !== Number(item.statementNum)){
                // 直接转换会报错: Uncaught BigNumber Error: new BigNumber() number type has more than 15 significant digits
                var aa = Number(item.shareNum) * Number(item.notTaxPrice) + '';
                ntRatio = new BigNumber(aa).div(itemNtaSum);
            }

            var shareRntAmount = ntRatio.mul(rntSumAmount);
            var shareRebateAmount = tax.mul(shareRntAmount);
            var shareDntAmount = ntRatio.mul(dntSumAmount);
            var shareDeductionAmount = tax.mul(shareDntAmount);

            if(el.editIndex === rowIndex){
                // 更新非Editor列数据
                var columns1 = ['shareRebateAmount', 'shareDeductionAmount'];
                var columnsData1 = [Number(shareRebateAmount.toFixed(2)), Number(shareDeductionAmount.toFixed(2))];
                var ed1 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'diffTaxAmount'});
                updatePageShowValue($(ed1.target).parents('td[field="diffTaxAmount"]'), item, columns1, columnsData1);

                // 更新Editor列数据(initValue 不触发numberBox的onChange事件)
                var srnTarget1 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareRntAmount'}).target;
                srnTarget1.numberbox('initValue', Number(shareRntAmount.toFixed(2)));

                var sdnTarget1 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareDntAmount'}).target;
                sdnTarget1.numberbox('initValue', Number(shareDntAmount.toFixed(2)));

                item.shareRntAmount = shareRntAmount.toFixed(2);
                item.shareDntAmount = shareDntAmount.toFixed(2);
            }else {
                $(el.tbDetails).datagrid('updateRow', {
                    index: rowIndex,
                    row: {
                        shareRebateAmount: shareRebateAmount.toFixed(2),
                        shareRntAmount: shareRntAmount.toFixed(2),
                        shareDeductionAmount: shareDeductionAmount.toFixed(2),
                        shareDntAmount: shareDntAmount.toFixed(2)
                    }
                });
            }
        }
    }
}


/**
 * 重新计算表体分摊后金额
 *
 * @param rows 表格行数据
 */
function againCountShareAfterAmount(rows) {
    if(rows === ''){
        rows = $(el.tbDetails).datagrid('getRows');
    }

    // 分摊后到票未税金额; 分摊后到票含税金额; 分摊后到票税金
    var saNta = new BigNumber(0), saIta = new BigNumber(0), saTa = new BigNumber(0);

    // 计算行的分摊税金和分摊后到票金额
    for(var j = 0; j < rows.length; j++){
        var row = rows[j];
        // 计算行的分摊税金
        var sra = new BigNumber(row.shareRebateAmount), sda = new BigNumber(row.shareDeductionAmount);
        var srn = new BigNumber(row.shareRntAmount), sdn = new BigNumber(row.shareDntAmount);
        var sraTaxAmount = sra.minus(srn), sdaTaxAmount = sda.minus(sdn);

        // 分摊后到票相关金额
        var nta = new BigNumber(row.notTaxAmount), ta = new BigNumber(row.taxAmount);
        var shareAfterNta = nta.plus(srn).plus(sdn);
        var shareAfterIta = ta.plus(sra).plus(sda);
        var shareAfterTa = shareAfterIta.minus(shareAfterNta);

        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', row);
        $(el.tbDetails).datagrid('updateRow', {
            index: rowIndex,
            row: {
                shareAfterNta: shareAfterNta.toFixed(2),
                shareAfterIta: shareAfterIta.toFixed(2),
                shareAfterTa: shareAfterTa.toFixed(2),
                sraTaxAmount: sraTaxAmount.toFixed(2),
                sdaTaxAmount: sdaTaxAmount.toFixed(2)
            }
        });

        // 分摊后发票金额统计 依次计算未税、含税、税金
        saNta = saNta.plus(shareAfterNta);
        saIta = saIta.plus(shareAfterIta);
        saTa = saTa.plus(shareAfterTa);
    }

    // 更新页脚行的值并刷新
    var footers = $(el.tbDetails).datagrid('getFooterRows');
    footers[0]['shareAfterNta'] = saNta.toFixed(2);
    footers[0]['shareAfterTa'] = saTa.toFixed(2);
    footers[0]['shareAfterIta'] = saIta.toFixed(2);
    $(el.tbDetails).datagrid('reloadFooter');
}


/**
 * 正在编辑情况下重新计算表体分摊后金额
 *
 * @param rows 表格行数据
 */
function editAgainCountShareAfterAmount(rows) {
    if(rows === ''){
        rows = $(el.tbDetails).datagrid('getRows');
    }

    // 分摊后到票未税金额; 分摊后到票含税金额; 分摊后到票税金
    var saNta = new BigNumber(0), saIta = new BigNumber(0), saTa = new BigNumber(0);

    // 计算行的分摊税金和分摊后到票金额
    for(var j = 0; j < rows.length; j++) {
        var row = rows[j], rowIndex = $(el.tbDetails).datagrid('getRowIndex', row);
        var nta = new BigNumber(row.notTaxAmount), srn = new BigNumber(row.shareRntAmount), sdn = new BigNumber(row.shareDntAmount);
        if(el.editIndex === rowIndex){
            var srnTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareRntAmount'}).target;
            srn = new BigNumber(srnTarget.numberbox('getValue'));
            var sdnTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareDntAmount'}).target;
            sdn = new BigNumber(sdnTarget.numberbox('getValue'));
            var ntaTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'notTaxAmount'}).target;
            nta = new BigNumber(ntaTarget.numberbox('getValue'));
        }

        var ta = new BigNumber(row.taxAmount), sra = new BigNumber(row.shareRebateAmount), sda = new BigNumber(row.shareDeductionAmount);
        var sraTaxAmount = sra.minus(srn), sdaTaxAmount = sda.minus(sdn);

        // 分摊后到票相关金额
        var shareAfterNta = nta.plus(srn).plus(sdn);
        var shareAfterIta = ta.plus(sra).plus(sda);
        var shareAfterTa = shareAfterIta.minus(shareAfterNta);

        if (el.editIndex === rowIndex) {
            // 更新非Editor列数据
            var columns4 = ['shareAfterNta', 'shareAfterIta','shareAfterTa'];
            var columnsData4 = [
                Number(shareAfterNta.toFixed(2)),  Number(shareAfterIta.toFixed(2)),  Number(shareAfterTa.toFixed(2))
            ];
            var ed4 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'diffTaxAmount'});
            updatePageShowValue($(ed4.target).parents('td[field="diffTaxAmount"]'), row, columns4, columnsData4);

            // 更新Editor列数据(initValue 不触发numberBox的onChange事件)
            var srnTarget4 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'sraTaxAmount'}).target;
            srnTarget4.numberbox('initValue',  Number(sraTaxAmount.toFixed(2)));
            var sdnTarget4 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'sdaTaxAmount'}).target;
            sdnTarget4.numberbox('initValue',  Number(sdaTaxAmount.toFixed(2)));
        } else {
            $(el.tbDetails).datagrid('updateRow', {
                index: rowIndex,
                row: {
                    shareAfterNta: shareAfterNta.toFixed(2),
                    shareAfterIta: shareAfterIta.toFixed(2),
                    shareAfterTa: shareAfterTa.toFixed(2),
                    sraTaxAmount: sraTaxAmount.toFixed(2),
                    sdaTaxAmount: sdaTaxAmount.toFixed(2)
                }
            });
        }

        // 分摊后发票金额统计 依次计算未税、含税、税金
        saNta = saNta.plus(shareAfterNta);
        saIta = saIta.plus(shareAfterIta);
        saTa = saTa.plus(shareAfterTa);
    }

    // 更新页脚行的值并刷新
    var footers = $(el.tbDetails).datagrid('getFooterRows');
    footers[0]['shareAfterNta'] = saNta.toFixed(2);
    footers[0]['shareAfterTa'] = saTa.toFixed(2);
    footers[0]['shareAfterIta'] = saIta.toFixed(2);
    $(el.tbDetails).datagrid('reloadFooter');
}


/**
 * 刷新明细表格页脚
 */
function refreshTbDetailFooter() {
    // 开票申请明细
    var detail = $(el.tbDetails).datagrid('getRows');
    // 到票含税金额; 到票未税金额; 到票税额; 到票差异含税金额
    var itAmount = new BigNumber(0), inAmount = new BigNumber(0), iatAmount = new BigNumber(0), iadAmount = new BigNumber(0);
    // 分摊返利含税汇总金额; 分摊返利未税汇总金额; 分摊返利税金
    // var srSumAmount = new BigNumber(0), srnSumAmount = new BigNumber(0), srtSum = new BigNumber(0);
    // 分摊扣款含税汇总金额; 分摊扣款未税汇总金额; 分摊扣款税金
    // var sdSumAmount = new BigNumber(0), sdnSumAmount = new BigNumber(0), sdtSum = new BigNumber(0);
    // 分摊后到票未税金额; 分摊后到票含税金额; 分摊后到票税金
    // var saNta = new BigNumber(0), saIta = new BigNumber(0), saTa = new BigNumber(0);
    // 订单相关金额
    var asNtAmount = new BigNumber(0), asItAmount = new BigNumber(0), asDtAmount = new BigNumber(0);
    // 对账数量
    var statementNum = new BigNumber(0);
    for (var i = 0; i < detail.length; i++) {
        var item1 = detail[i];
        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', item1);

        var notTaxAmount = new BigNumber(item1.notTaxAmount), diffTaxAmount = new BigNumber(item1.diffTaxAmount), taxAmount = new BigNumber(item1.taxAmount);
        var srnAmount = new BigNumber(item1.shareRntAmount), sraTaxAmount = new BigNumber(item1.sraTaxAmount);
        var sdnAmount = new BigNumber(item1.shareDntAmount), sdaTaxAmount = new BigNumber(item1.sdaTaxAmount);
        if(el.editIndex === rowIndex){
            var editors = $(el.tbDetails).datagrid('getEditors', el.editIndex);
            for (var ed = 0; ed < editors.length; ed++) {
                var editor = editors[ed];
                if(editor.field === 'notTaxAmount'){
                    notTaxAmount = new BigNumber(editor.target.numberbox('getValue'));
                }
                if(editor.field === 'diffTaxAmount'){
                    diffTaxAmount = new BigNumber(editor.target.numberbox('getValue'));
                }
                if(editor.field === 'srnAmount'){
                    srnAmount = new BigNumber(editor.target.numberbox('getValue'));
                }
                if(editor.field === 'sraTaxAmount'){
                    sraTaxAmount = new BigNumber(editor.target.numberbox('getValue'));
                }
                if(editor.field === 'sdnAmount'){
                    sdnAmount = new BigNumber(editor.target.numberbox('getValue'));
                }
                if(editor.field === 'sdaTaxAmount'){
                    sdaTaxAmount = new BigNumber(editor.target.numberbox('getValue'));
                }
            }
        }

        inAmount = inAmount.plus(notTaxAmount);
        itAmount = itAmount.plus(taxAmount);
        iatAmount = iatAmount.plus(diffTaxAmount);
        iadAmount = iadAmount.plus(item1.iaDiffAmount);
        asNtAmount = asNtAmount.plus(item1.asNtAmount);
        asItAmount = asItAmount.plus(item1.asItAmount);
        asDtAmount = asDtAmount.plus(item1.asDtAmount);

        // 分摊返利金额统计 依次计算未税、含税、税金
        // srnSumAmount = srnSumAmount.plus(srnAmount);
        // srSumAmount = srSumAmount.plus(item1.shareRebateAmount);
        // srtSum = srtSum.plus(sraTaxAmount);

        // 分摊扣款金额统计 依次计算未税、含税、税金
        // sdnSumAmount = sdnSumAmount.plus(sdnAmount);
        // sdSumAmount = sdSumAmount.plus(item1.shareDeductionAmount);
        // sdtSum = sdtSum.plus(sdaTaxAmount);

        // 分摊后发票金额统计 依次计算未税、含税、税金
        // saNta = saNta.plus(item1.shareAfterNta);
        // saIta = saIta.plus(item1.shareAfterIta);

        // var isaTta = new BigNumber(item1.shareAfterIta), isaNta = new BigNumber(item1.shareAfterNta);
        // saTa = saTa.plus(isaTta.sub(isaNta));

        statementNum = statementNum.plus(item1.statementNum);
    }

    // 更新页脚行的值并刷新
    var footers = $(el.tbDetails).datagrid('getFooterRows');
    footers[0]['notTaxAmount'] = inAmount.toFixed(2);
    footers[0]['taxAmount'] = itAmount.toFixed(2);
    footers[0]['diffTaxAmount'] = iatAmount.toFixed(2);
    footers[0]['iaDiffAmount'] = iadAmount.toFixed(2);
    // footers[0]['shareRebateAmount'] = srSumAmount.toFixed(2);
    // footers[0]['sraTaxAmount'] = srtSum.toFixed(2);
    // footers[0]['shareRntAmount'] = srnSumAmount.toFixed(2);
    // footers[0]['shareDeductionAmount'] = sdSumAmount.toFixed(2);
    // footers[0]['sdaTaxAmount'] = sdtSum.toFixed(2);
    // footers[0]['shareDntAmount'] = sdnSumAmount.toFixed(2);
    // footers[0]['shareAfterNta'] = saNta.toFixed(2);
    // footers[0]['shareAfterTa'] = saTa.toFixed(2);
    // footers[0]['shareAfterIta'] = saIta.toFixed(2);
    footers[0]['asNtAmount'] = asNtAmount.toFixed(2);
    footers[0]['asItAmount'] = asItAmount.toFixed(2);
    footers[0]['asDtAmount'] = asDtAmount.toFixed(2);
    footers[0]['statementNum'] = statementNum.toNumber();
    $(el.tbDetails).datagrid('reloadFooter');
}


/**
 * 刷新开票申请明细底部返利相关统计
 */
function refreshRebShare(srn, sr, srt) {
    // 开票申请明细
    var detail = $(el.tbDetails).datagrid('getRows');
    // 分摊返利含税汇总金额; 分摊返利未税汇总金额; 分摊返利税金
    var srSumAmount = new BigNumber(0), srnSumAmount = new BigNumber(0), srtSum = new BigNumber(0);
    for (var i = 0; i < detail.length; i++) {
        var item = detail[i];
        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', item);
        if(el.editIndex === rowIndex){
            srnSumAmount = srnSumAmount.plus(srn);
            srSumAmount = srSumAmount.plus(sr);
            srtSum = srtSum.plus(srt);
        }else {
            srnSumAmount = srnSumAmount.plus(item.shareRntAmount);
            srSumAmount = srSumAmount.plus(item.shareRebateAmount);
            srtSum = srtSum.plus(item.sraTaxAmount);
        }
    }

    // 更新页脚行的值并刷新
    var footers = $(el.tbDetails).datagrid('getFooterRows');
    footers[0]['shareRebateAmount'] = srSumAmount.toFixed(2);
    footers[0]['sraTaxAmount'] = srtSum.toFixed(2);
    footers[0]['shareRntAmount'] = srnSumAmount.toFixed(2);
    $(el.tbDetails).datagrid('reloadFooter');

    $(el.rntAmount).val(srnSumAmount.toFixed(2));

    // editAgainCountShareAfterAmount(detail);
}


/**
 * 刷新开票申请明细底部扣款相关统计
 */
function refreshDedShare(sdn, sd, sdt) {
    // 开票申请明细
    var detail = $(el.tbDetails).datagrid('getRows');
    // 分摊扣款含税汇总金额; 分摊扣款未税汇总金额; 分摊扣款税金
    var sdSumAmount = new BigNumber(0), sdnSumAmount = new BigNumber(0), sdtSum = new BigNumber(0);
    for (var i = 0; i < detail.length; i++) {
        var item = detail[i];
        var rowIndex = $(el.tbDetails).datagrid('getRowIndex', item);

        if(el.editIndex === rowIndex){
            sdnSumAmount = sdnSumAmount.plus(sdn);
            sdSumAmount = sdSumAmount.plus(sd);
            sdtSum = sdtSum.plus(sdt);
        }else {
            sdnSumAmount = sdnSumAmount.plus(item.shareDntAmount);
            sdSumAmount = sdSumAmount.plus(item.shareDeductionAmount);
            sdtSum = sdtSum.plus(item.sdaTaxAmount);
        }
    }

    // 更新页脚行的值并刷新
    var footers = $(el.tbDetails).datagrid('getFooterRows');
    footers[0]['shareDeductionAmount'] = sdSumAmount.toFixed(2);
    footers[0]['sdaTaxAmount'] = sdtSum.toFixed(2);
    footers[0]['shareDntAmount'] = sdnSumAmount.toFixed(2);
    $(el.tbDetails).datagrid('reloadFooter');

    $(el.dntAmount).val(sdnSumAmount.toFixed(2));
    // editAgainCountShareAfterAmount(detail);
}


/**
 * 处理小数误差(返利含税金额 != 明细最终分摊金额相加)
 */
function handleDecimalDeviation(rebateAmount, rntAmount, deductionAmount, dntAmount) {
    var detail = $(el.tbDetails).datagrid('getRows');
    // srSumAmount:返利含税金额汇总; srnSumAmount:返利未税金额汇总; sdSumAmount:扣款含税金额汇总; sdnSumAmount:扣款未税金额汇总
    var srSumAmount = new BigNumber(0), srnSumAmount = new BigNumber(0), sdSumAmount = new BigNumber(0), sdnSumAmount = new BigNumber(0);

    for (var i = 0; i < detail.length; i++) {
        var item1 = detail[i], rowIndex = $(el.tbDetails).datagrid('getRowIndex', item1);
        var srn = new BigNumber(item1.shareRntAmount), sdn = new BigNumber(item1.shareDntAmount);
        if(el.editIndex === rowIndex){
            var srnTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareRntAmount'}).target;
            srn = new BigNumber(srnTarget.numberbox('getValue'));
            var sdnTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareDntAmount'}).target;
            sdn = new BigNumber(sdnTarget.numberbox('getValue'));
        }

        srnSumAmount = srnSumAmount.plus(srn);
        sdnSumAmount = sdnSumAmount.plus(sdn);
        srSumAmount = srSumAmount.plus(item1.shareRebateAmount);
        sdSumAmount = sdSumAmount.plus(item1.shareDeductionAmount);
    }

    // 含税返利误差; 未税返利误差
    // var rDeviation = rebateAmount.minus(srSumAmount), rnDeviation = rntAmount.minus(srnSumAmount);
    // if(!rDeviation.eq(0) || !rnDeviation.eq(0)){
    //     handleRebateDeviation(detail, rDeviation, rnDeviation);
    // }

    // 含税扣款误差; 未税扣款误差
    // var dDeviation = deductionAmount.minus(sdSumAmount), dnDeviation = dntAmount.minus(sdnSumAmount);
    // if(!dDeviation.eq(0) || !dnDeviation.eq(0)){
    //     handleDeductionDeviation(detail, dDeviation, dnDeviation);
    // }
}


/**
 * 处理返利分摊后的尾差
 *
 * @param detail 表格行数据
 * @param rDeviation 含税返利尾差
 * @param rnDeviation 未税返利尾差
 */
function handleRebateDeviation(detail, rDeviation, rnDeviation) {
    if(!rDeviation.eq(0)){
        var rowIndex1 = 0, shareRebateAmount1 = new BigNumber(0);
        for (var m1 = 0; m1 < detail.length; m1++) {
            shareRebateAmount1 = rDeviation.plus(detail[m1].shareRebateAmount);
            if (Number(detail[m1].shareFlag) === 1 && Number(detail[m1].statementNum) > 0) {
                rowIndex1 = m1;
                break;
            }
        }

        if(el.editIndex !== rowIndex1){
            $(el.tbDetails).datagrid('updateRow', {
                index: rowIndex1,
                row: {
                    shareRebateAmount: shareRebateAmount1.toFixed(2)
                }
            });
        }else {
            // 更新非Editor列数据
            var columns2 = ['shareRebateAmount'];
            var columnsData2 = [Number(shareRebateAmount1.toFixed(2))];
            var ed2 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'diffTaxAmount'});
            updatePageShowValue($(ed2.target).parents('td[field="diffTaxAmount"]'), detail[rowIndex1], columns2, columnsData2);
        }
    }

    if(!rnDeviation.eq(0)){
        var rowIndex3 = 0, shareRntAmount3 = new BigNumber(0);
        for (var m3 = 0; m3 < detail.length; m3++) {
            shareRntAmount3 = rnDeviation.plus(detail[m3].shareRntAmount);
            if (Number(detail[m3].shareFlag) === 1 && Number(detail[m3].statementNum) > 0) {
                rowIndex3 = m3;
                break;
            }
        }

        if(el.editIndex !== rowIndex3){
            $(el.tbDetails).datagrid('updateRow', {
                index: rowIndex3,
                row: {
                    shareRntAmount: shareRntAmount3.toFixed(2)
                }
            });
        }else {
            var srnTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareRntAmount'}).target;
            srnTarget.numberbox('initValue', Number(shareRntAmount3.toFixed(2)));
        }
    }
}


/**
 * 处理扣款分摊后的尾差
 *
 * @param detail 表格行数据
 * @param dDeviation 含税扣款尾差
 * @param dnDeviation 未税扣款尾差
 */
function handleDeductionDeviation(detail, dDeviation, dnDeviation) {
    if(!dDeviation.eq(0)){
        var rowIndex1 = 0, shareDeductionAmount1 = new BigNumber(0);
        for (var m1 = 0; m1 < detail.length; m1++) {
            shareDeductionAmount1 = dDeviation.plus(detail[m1].shareDeductionAmount);
            if (Number(detail[m1].shareFlag) === 1 && Number(detail[m1].statementNum) > 0) {
                rowIndex1 = m1;
                break;
            }
        }

        if(el.editIndex !== rowIndex1){
            $(el.tbDetails).datagrid('updateRow', {
                index: rowIndex1,
                row: {
                    shareDeductionAmount: shareDeductionAmount1.toFixed(2)
                }
            });
        }else {
            // 更新非Editor列数据
            var columns2 = ['shareDeductionAmount'];
            var columnsData2 = [Number(shareDeductionAmount1.toFixed(2))];
            var ed2 = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'diffTaxAmount'});
            updatePageShowValue($(ed2.target).parents('td[field="diffTaxAmount"]'), detail[rowIndex1], columns2, columnsData2);
        }
    }

    if(!dnDeviation.eq(0)){
        var rowIndex3 = 0, shareDntAmount3 = new BigNumber(0);
        for (var m3 = 0; m3 < detail.length; m3++) {
            shareDntAmount3 = dnDeviation.plus(detail[m3].shareDntAmount);
            if (Number(detail[m3].shareFlag) === 1 && Number(detail[m3].statementNum) > 0) {
                rowIndex3 = m3;
                break;
            }
        }

        if(el.editIndex !== rowIndex3){
            $(el.tbDetails).datagrid('updateRow', {
                index: rowIndex3,
                row: {
                    shareDntAmount: shareDntAmount3.toFixed(2)
                }
            });
        }else {
            var sdnTarget = $(el.tbDetails).datagrid('getEditor', {index: el.editIndex, field: 'shareDntAmount'}).target;
            sdnTarget.numberbox('initValue', Number(shareDntAmount3.toFixed(2)));
        }
    }
}


/**
 * 处理凭证尾差
 */
function handleVoucherTail() {
    // 需要处理统计行中 未税金额 != 含税金额 / (1 + 税率)
    var taxValue = new BigNumber(1), itSumAmount = new BigNumber(0), ntSumAmount = new BigNumber(0);
    var rows = $(el.tbDetails).datagrid('getRows');
    // maxAmount -> 表体最大含税金额; maxAmountRowIndex ->; maxDtAmount -> 表体最大税额;
    var maxAmount = new BigNumber(0), maxAmountRowIndex = -1, maxDtAmount = new BigNumber(0),
        // maxNtAmount ->表体最大未税金额; maxSdtAmount ->表体最大分摊后税额; maxSntAmount -> 表体最大分摊后未税金额;
        maxNtAmount = new BigNumber(0), maxSdtAmount = new BigNumber(0), maxSntAmount =  new BigNumber(0);
        for (var n = 0; n < rows.length; n++) {
        var item = rows[n];
        ntSumAmount = ntSumAmount.plus(item.notTaxAmount);
        itSumAmount = itSumAmount.plus(item.taxAmount);
        taxValue = new BigNumber(item.taxValue);

        if(maxAmount.eq(0)){
            maxAmount = new BigNumber(item.taxAmount);
            maxDtAmount = new BigNumber(item.diffTaxAmount);
            maxNtAmount = new BigNumber(item.notTaxAmount);
            maxSdtAmount = new BigNumber(item.shareAfterTa);
            maxSntAmount = new BigNumber(item.shareAfterNta);
            maxAmountRowIndex = $(el.tbDetails).datagrid('getRowIndex', item);
        }
        if(!maxAmount.eq(0) && maxAmount.lt(item.taxAmount) && Number(item.shareFlag) === 1){
            maxAmount = new BigNumber(item.taxAmount);
            maxDtAmount = new BigNumber(item.diffTaxAmount);
            maxNtAmount = new BigNumber(item.notTaxAmount);
            maxSdtAmount = new BigNumber(item.shareAfterTa);
            maxSntAmount = new BigNumber(item.shareAfterNta);
            maxAmountRowIndex = $(el.tbDetails).datagrid('getRowIndex', item);
        }
    }

    var tax = taxValue.div(100).plus(1);
    // 表体明细相加的含税金额除以税率减去表体明细相加的未税金额, 如果差异超过±0.01(包含±0.01), 需要将差异加到表体含税金额最大一行中
    var diff = itSumAmount.div(tax).sub(ntSumAmount);
    var diffNum = Number(diff.abs().toFixed(2));
    if(diffNum >= 0.01){
        var adtAmount = maxDtAmount.sub(diff).toFixed(2);
        var antAmount = maxNtAmount.plus(diff).toFixed(2);
        var sdtAmount = maxSdtAmount.sub(diff).toFixed(2);
        var sntAmount = maxSntAmount.plus(diff).toFixed(2);

        $(el.tbDetails).datagrid('updateRow', {
            index: maxAmountRowIndex,
            row: {
                diffTaxAmount: adtAmount,
                notTaxAmount: antAmount,
                shareAfterTa: sdtAmount,
                shareAfterNta: sntAmount
            }
        });
    }
}