/**
 * 对账模块-编辑对账单时选择入库凭证数据JS
 *
 * Created by li.kui on 2020/8/3.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    asId: '#asId',
    alreadyExistId: '#alreadyExistId',
    orderType: '#orderType',
    supplierCode: '#supplierCode',
    companyCode: '#companyCode',
    factoryCode: '#factoryCode',
    currencyCode: '#currencyCode',
    inStockStartDate: '#inStockStartDate',
    inStockEndDate: '#inStockEndDate',
    taxCode: '#taxCode',
    voucherType: '#voucherType',
    receiveCode: '#receiveCode',
    receiveRowNo: '#receiveRowNo',
    poCode: '#poCode',
    poItemNo: '#poItemNo',
    materialCode: '#materialCode',
    materialName: '#materialName',
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    loadVoucherDataGird();
});


/**
 * 加载入库凭证数据
 */
function loadVoucherDataGird() {
    var searchHeight = $(el.searchForm).outerHeight(), orderType = $(el.orderType).val();

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待......',
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/finance/receipt/voucher/rest/provide/create/statement',
        singleSelect: false,
        rownumbers: true,
        pagination: false,
        queryParams: {
            asId: $(el.asId).val(),
            alreadyExistId: $(el.alreadyExistId).val(),
            supplierCode: $(el.supplierCode).val(),
            companyCode: $(el.companyCode).val(),
            factoryCode: $(el.factoryCode).val(),
            currencyCode: $(el.currencyCode).val(),
            postTimeStart: $(el.inStockStartDate).val(),
            postTimeEnd: $(el.inStockEndDate).val(),
            taxCode: $(el.taxCode).val(),
            purchaseGroup: $('#purchaseGroup').val(),
            voucherType: $(el.voucherType).val(),
            receiveType: orderType
        },
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'receiveCode',
                    title: '入库凭证',
                    width: 90
                },
                {
                    field: 'receiveRowNo',
                    title: '行号',
                    width: 60
                },
                {
                    field: 'receiveDate',
                    title: '入库日期',
                    width: 100
                },
                {
                    field: 'inOutFlag',
                    title: '出入库标识',
                    width: 85
                }
            ]
        ],
        columns: [
            [
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 90
                },
                {
                    field: 'materialName',
                    title: '物料名称',
                    width: 180
                },
                {
                    field: 'materialUnit',
                    title: '入库单位',
                    width: 70
                },
                {
                    field: 'receiveNum',
                    title: '入库数量',
                    width: 70,
                    formatter: function (value, row) {
                        if (row.voucherFlag === 'H') {
                            return '<span style="color: red;">' + value + '</span>';
                        }else {
                            return value;
                        }
                    }
                },
                {
                    field: 'surplusNum',
                    title: '剩余可对账数量',
                    width: 90,
                    formatter: function (value, row) {
                        if (row.voucherFlag === 'H') {
                            return '<span style="color: red;">' + value + '</span>';
                        }else {
                            return value;
                        }
                    }
                },
                {
                    field: 'notTaxPrice',
                    title: '未税单价',
                    width: 70
                },
                {
                    field: 'taxPrice',
                    title: '含税单价',
                    width: 70
                },
                {
                    field: 'taxCodeDesc',
                    title: '税率',
                    width: 160
                },
                {
                    field: 'notTaxAmount',
                    title: '不含税金额',
                    width: 100,
                    formatter: function (value, row) {
                        if (!value) {
                            return '';
                        }
                        if (row.voucherFlag === 'H') {
                            return '<span style="color: red;">' + formatNumber(value, 2, 1) + '</span>';
                        }else {
                            return formatNumber(value, 2, 1);
                        }
                    }
                },
                {
                    field: 'taxAmount',
                    title: '含税金额',
                    width: 100,
                    formatter: function (value, row) {
                        if (row.voucherFlag === 'H') {
                            return '<span style="color: red;">' + formatNumber(value, 2, 1) + '</span>';
                        }else {
                            return formatNumber(value, 2, 1);
                        }
                    }
                },
                {
                    field: 'diffTaxAmount',
                    title: '税额',
                    width: 100,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    // 累计开票金额 = 申请开票金额 + 采购确认金额 + 财务过账金额
                    field: 'invoicedAmount',
                    title: '累计开票金额',
                    width: 110,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    // 剩余可开票金额 = (入库数量-对账数量) * 订单含税单价
                    field: 'surplusAmount',
                    title: '剩余可开票金额',
                    width: 120,
                    formatter: function (value) {
                        return formatNumber(value, 2, 1);
                    }
                },
                {
                    field: 'poCode',
                    title: 'SAP订单号',
                    width: 90
                },
                {
                    field: 'poItemNo',
                    title: '订单行号',
                    width: 70
                },
                {
                    field: 'seekPgCodeName',
                    title: '询源采购组',
                    width: 120
                },
                {
                    field: 'paymentTermCodeName',
                    title: '付款条件',
                    width: 200
                },
                {
                    field: 'paymentWayCodeDesc',
                    title: '付款方式',
                    width: 200
                },
                // 承运商对账
                {
                    field: 'materialSupplierCode',
                    title: '材料商编码',
                    hidden: Number(orderType) === 1,
                    width: 90,
                    sortable: true
                },
                {
                    field: 'materialSupplierName',
                    title: '材料商名称',
                    hidden: Number(orderType) === 1,
                    width: 180,
                    sortable: true
                },
                {
                    field: 'freightType',
                    title: '运费类型',
                    hidden: Number(orderType) === 1,
                    width: 80,
                    sortable: true
                },
                // 集采订单信息
                {
                    field: 'jcPoCode',
                    title: '集采SAP订单号',
                    width: 110,
                    sortable: true
                },
                {
                    field: 'jcPoItemNo',
                    title: '行号',
                    width: 70,
                    sortable: true
                },
                {
                    field: 'jcFactoryCode',
                    title: '集采工厂',
                    width: 80,
                    sortable: true
                }
            ]
        ]
    });
}


/**
 * 确认选择
 *
 * @param callback 回调函数
 */
function confirmChose(callback) {
    var rows = $(el.tb).datagrid('getSelections');
    if(rows.length === 0){
        topWin.layer.alert('请选择入库凭证明细数据', {icon: 0});
        return false;
    }

    var rvIds = [];
    for(var i = 0; i < rows.length; i++){
        // 对账确认数量默认为剩余可对账数量
        rows[i].asQuantity = rows[i].surplusNum;
        rvIds.push(rows[i].receiveId);
    }

    // 校验选中的行是否保存
    var loading = topWin.layer.msg('校验选中的行是否保存, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/finance/receipt/voucher/rest/check/write/off',
        data: {
            supplierCode: $(el.supplierCode).val(),
            rvIds: JSON.stringify(rvIds),
            receiveType: $(el.orderType).val()
        },
        success: function () {
            topWin.layer.close(loading);
            callback(rows);
        },
        error: function (data) {
            topWin.layer.close(loading);
            var message = "需要选择以下凭证: </br>";
            topWin.layer.alert(message + data.responseJSON.message, {icon: 2});
        }
    });
}


/**
 * 数据查询
 */
function searchVoucherData() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams.receiveCode = $.trim($(el.receiveCode).val());
    queryParams.rowNo = $.trim($(el.receiveRowNo).val());
    queryParams.poCode = $.trim($(el.poCode).val());
    queryParams.poItemNo = $.trim($(el.poItemNo).val());
    queryParams.materialCode = $.trim($(el.materialCode).val());
    queryParams.materialName = $.trim($(el.materialName).val());
    queryParams.alreadyExistId = $.trim($(el.alreadyExistId).val());
    queryParams.supplierCode = $.trim($(el.supplierCode).val());
    queryParams.companyCode = $.trim($(el.companyCode).val());
    queryParams.factoryCode = $.trim($(el.factoryCode).val());
    queryParams.currencyCode = $.trim($(el.currencyCode).val());
    queryParams.receiveType = $.trim($(el.orderType).val());
    queryParams.postTimeStart = $.trim($(el.inStockStartDate).val());
    queryParams.postTimeEnd = $.trim($(el.inStockEndDate).val());
    $(el.tb).datagrid('load');
}


/**
 * 重置查询
 */
function resetVoucherSearch() {
    $(el.receiveCode).val('');
    $(el.receiveRowNo).val('');
    $(el.poCode).val('');
    $(el.poItemNo).val('');
    $(el.materialCode).val('');
    $(el.materialName).val('');

    searchVoucherData();
}