/**
 * 对账模块-用于对账入库凭证查询首页JS
 *
 * Created by li.kui on 2020/7/31.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    receiveCode: '#receiveCode',
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',
    paymentExpireDateStart: '#paymentExpireDateStart',
    paymentExpireDateEnd: '#paymentExpireDateEnd',
    poCode: '#poCode',
    materialCode: '#materialCode',
    materialName: '#materialName',
    pgCode: '#pgCode',
    companyCode: '#companyCode',
    postTimeStart: '#postTimeStart',
    postTimeEnd: '#postTimeEnd',
    factoryCode: '#factoryCode',
    voucherType: '#voucherType',
    isAllowAccount: '#isAllowAccount',
    isReconcile: '#isReconcile',
    isSearchRepeat: '#isSearchRepeat',
    cvCode: '#cvCode',
    tb: '#tb',

    dataSyncDialog: '#dataSyncDialog',
    poTypeName: '#poTypeName',
    poType: '#poType',
    startPostTime: '#startPostTime',
    endPostTime: '#endPostTime',
    receiptVoucherNo: '#receiptVoucherNo',
    syncCompanyCode: '#syncCompanyCode',
    syncSupCode: '#syncSupCode',
    syncMaterialCode: '#syncMaterialCode',
    syncVoucherType: '#syncVoucherType',
    //定义初始列
    defaultColumns:[],

    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    initDict('movement_type,payment_method')
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll')
    initPageComBoBox();
    //定义初始列
    el.defaultColumns =  [
        {
            field: 'asNo',
            title: '对账单号',
            width: 120,
            sortable: true
        },
        {
            field: 'accountStatementFlag',
            title: '允许对账',
            width: 80,
            exportEnum:[
                {code:1,codeWithDesc:'允许'},
                {code:0,codeWithDesc:'不允许'},
            ],
            formatter: function (value) {
                if (value === 1) {
                    return '<span style="color: green;">允许</span>';
                }else {
                    return '<span style="color: red;">不允许</span>';
                }
            }
        },
        {
            field: 'mvCode',
            title: '物料凭证号',
            width: 90,
            sortable: true
        },
        {
            field: 'mvItemNo',
            title: '行号',
            width: 50,
            sortable: true
        },
        {
            field: 'mvYear',
            title: '年度',
            width: 100
        },
        {
            field: 'voucherType',
            title: '凭证类型',
            width: 100,
            exportEnum:[
                {code:1,codeWithDesc:'常规'},
                {code:2,codeWithDesc:'寄售'},
                {code:3,codeWithDesc:'费用'}
            ],
            formatter: function (value) {
                if (value === 1) {
                    return '常规';
                }else if (value === 2){
                    return '寄售';
                }else if (value === 3){
                    return '费用';
                }
            }
        },
        {
            field: 'mvPostDate',
            title: '凭证过账日期',
            width: 100,
            sortable: true,
            exportDateFormatter:'date',
            formatter: function (value) {
                if (!value) {
                    return '';
                }
                return moment(value).format('YYYY-MM-DD');
            }
        },
        {
            field: 'purPostDate',
            title: '质检入库日期',
            width: 100,
            sortable: true,
            exportDateFormatter:'date',
            formatter: function (value) {
                if (!value) {
                    return '';
                }
                return moment(value).format('YYYY-MM-DD');
            }
        },

        {
            field: 'voucherFlag',
            title: '入库标识',
            width: 70,
            sortable: true,
            exportEnum:[
                {code:'S',codeWithDesc:'入库'},
                {code:'H',codeWithDesc:'退货'}
            ],
            formatter: function (value) {
                if (value === 'S') {
                    return '<span style="color: green;">入库</span>';
                }else {
                    return '<span style="color: red;">退货</span>';
                }
            }
        },
        {
            field: 'extendFiled1',
            title: '免检标识',
            width: 100
        },
        {
            field: 'headUpText',
            title: '送货单号',
            width: 100
        },
        {
            field: 'materialCode',
            title: '物料编码',
            width: 90,
            sortable: true
        },
        {
            field: 'materialName',
            title: '物料名称',
            width: 280,
            sortable: true
        },
        {
            field: 'materialDesc',
            title: '物料描述',
            width: 180,
            sortable: true
        },
        // {
        //     field: 'specs',
        //     title: '物料规格',
        //     width: 280
        // },
        {
            field: 'poMeterUnit',
            title: '采购单位',
            width: 70,
            sortable: true
        },
        {
            field: 'mvNum',
            title: '入库数量',
            width: 70,
            sortable: true
        },
        {
            field: 'taxCode',
            title: '税率',
            width: 60,
            sortable: true
        },
        {
            field: 'taxDesc',
            title: '税率描述',
            width: 150,
            sortable: true
        },
        {
            field: 'notTaxPrice',
            title: '未税单价',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },
        {
            field: 'taxPrice',
            title: '含税单价',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },

        {
            field: 'deductionRatio',
            title: '其他折扣',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },
        {
            field: 'notTaxAmount',
            title: '未税金额',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },
        {
            field: 'taxAmount',
            title: '含税金额',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },
        {
            field: 'originalTaxAmount',
            title: '原含税金额',
            width: 90,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },
        {
            field: 'diffTaxAmount',
            title: '税额',
            width: 80,
            sortable: true,
            formatter: function (value) {
                return formatterPrice(value);
            }
        },
        {
            field: 'cvCode',
            title: '参考凭证号',
            width: 90
        },
        {
            field: 'cvItemNo',
            title: '参考凭证行号',
            width: 100
        },
        {
            field: 'cvYear',
            title: '参考凭证年度',
            width: 100
        },
        {
            field: 'invoicedCode',
            title: 'MIRO凭证号',
            width: 100
        },
        {
            field: 'paymentExpireDate',
            title: '付款到期日',
            width: 120,
            sortable: true,
            exportDateFormatter:'date',
            formatter: function (value, row) {
                if (!value) {
                    return '';
                }
                return '<span style="color: red;">' + moment(value).format('YYYY-MM-DD') + '</span>';
            }
        },
        // {
        //     field: 'invoicedNum',
        //     title: '发票过账数量',
        //     width: 90,
        //     sortable: true
        // },
        // {
        //     field: 'invoicedAmount',
        //     title: '发票过账金额',
        //     width: 90,
        //     sortable: true,
        //     formatter: function (value) {
        //         if (!value) {
        //             return '';
        //         }
        //         return formatNumber(value, 2, 1);
        //     }
        // },
        {
            field: 'qualityTestingNo',
            title: '质检单号',
            width: 90
        },
        // {
        //     field: 'zlsdh',
        //     title: 'SC订单号',
        //     width: 90
        // },
        {
            field: 'poCode',
            title: 'SAP订单号',
            width: 90,
            sortable: true
        },
        {
            field: 'poItemNo',
            title: '订单行号',
            width: 70,
            sortable: true
        },
        {
            field: 'poType',
            title: '订单类型编号',
            width: 90
        },
        {
            field: 'poTypeName',
            title: '订单类型名称',
            width: 100
        },
        {
            field: 'supplierCode',
            title: '供应商编码',
            width: 90,
            sortable: true
        },
        {
            field: 'supplierName',
            title: '供应商名称',
            width: 190,
            sortable: true
        },
        {
            field: 'companyName',
            title: '公司名称',
            width: 220,
            sortable: true,
            formatter: function (value, row) {
                if (value) {
                    return '(' + row.companyCode + ')' + value;
                }
                return '';
            }
        },
        {
            field: 'supplierName2',
            title: '物流公司',
            width: 220,
            sortable: true,
            formatter: function (value, row) {
                if (value) {
                    return '(' + row.supplierCode2 + ')' + value;
                }
                return '';
            }
        },
        {
            field: 'factoryCode',
            title: '工厂编码',
            width: 200,
            sortable: true,
            // formatter: function (value, row) {
            //     if (value) {
            //         return '(' + row.factoryCode + ')' + value;
            //     }
            //     return '';
            // }
        },
        {
            field: 'sapMoveType',
            title: '移动类型',
            width: 120,
            sortable: true,
            exportEnum:getDictList('movement_type'),
            formatter: function (value, row) {
                return getDicName('movement_type',value)
            }
        },
        // {
        //     field: 'unitPriceConvert',
        //     title: '价格单位',
        //     width: 70,
        //     sortable: true
        // },
        {
            field: 'currencyCode',
            title: '币种',
            width: 60,
            sortable: true
        },
        {
            field: 'pgCode',
            title: '采购组',
            width: 110,
            sortable: true,
            exportEnum:getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll'),
            formatter: function (value, row) {
                return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
            }
        },
        {
            field: 'supplierPurchaseGroupCode',
            title: '对账采购组',
            width: 110,
            sortable: true,
            exportEnum:getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll'),
            formatter: function (value, row) {
                return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll',value);
            }
        },
        // {
        //     field: 'seekPgName',
        //     title: '询源采购组',
        //     width: 110,
        //     sortable: true,
        //     formatter: function (value, row) {
        //         if (value) {
        //             return '(' + row.seekPgCode + ')' + value;
        //         }
        //         return '';
        //     }
        // },
        {
            field: 'paymentTermCode',
            title: '付款条件',
            width: 140,
            sortable: true,
            exportEnum:getDictList('payment_method'),
            formatter: function (value, row) {
                return getDicName('payment_method',value)
            }
        },
        {
            field: 'sapRemark',
            title: '行文本',
            width: 150,
            sortable: true
        },
        // {
        //     field: 'paymentWayDesc',
        //     title: '付款方式',
        //     width: 140,
        //     sortable: true,
        //     formatter: function (value, row) {
        //         if (value) {
        //             return '(' + row.paymentWayCode + ')' + value;
        //         }
        //         return '';
        //     }
        // },
        // {
        //     field: 'materialSupplierCode',
        //     title: '材料商编码',
        //     width: 90,
        //     sortable: true
        // },
        // {
        //     field: 'materialSupplierName',
        //     title: '材料商名称',
        //     width: 190,
        //     sortable: true
        // },
        // {
        //     field: 'freightType',
        //     title: '运费类型',
        //     width: 80,
        //     sortable: true
        // },
        // {
        //     field: 'jcMvCode',
        //     title: '集采物料凭证',
        //     width: 110,
        //     sortable: true
        // },
        // {
        //     field: 'jcMvItemNo',
        //     title: '行号',
        //     width: 70,
        //     sortable: true
        // },
        // {
        //     field: 'jcPoCode',
        //     title: '集采SAP订单号',
        //     width: 110,
        //     sortable: true
        // },
        // {
        //     field: 'jcPoItemNo',
        //     title: '行号',
        //     width: 70,
        //     sortable: true
        // },
        // {
        //     field: 'jcFactoryCode',
        //     title: '集采工厂',
        //     width: 80,
        //     sortable: true
        // },
        {
            field: 'syncTime',
            title: '同步时间',
            width: 130,
            sortable: true,
            exportDateFormatter: 'dateTime',
            formatter: function (value) {
                if (value) {
                    return moment(value).format("YYYY-MM-DD HH:mm:ss");
                }
                return '';
            }
        },
        {
            field: 'lastUpdateTime',
            title: '最后同步时间',
            width: 130,
            sortable: true,
            exportDateFormatter: 'dateTime',
            formatter: function (value) {
                if (value) {
                    return moment(value).format("YYYY-MM-DD HH:mm:ss");
                }
                return '';
            }
        }
    ];
    //从后台查询动态列
    let column = getRemoteColumnSort(columnSortCodeEnum.RECEIVE,el.defaultColumns);
    //赋值到表
    loadGrid(column);
});


/**
 * 初始化页面数据表格
 */
function loadGrid(columns) {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据加载中, 请耐心等待...',
        // url: g.ctx + 'fin/receipt/voucher/rest/list',
        height: el.bodyHeight - searchHeight,
        singleSelect: false,
        rownumbers: true,
        title:"<span style='color: red'>*请输入筛选条件，点击查询</span>",
        // 定义从服务器对数据进行排序
        remoteSort: true,
        // 定义是否允许多列排序
        // multiSort: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        // queryParams: {
        //     postTimeStart:$('#postTimeStart').datebox('getValue'),
        //     companyCode:$('#deptCode').val(),
        //     postTimeEnd:$('#postTimeEnd').datebox('getValue')
        // },
        frozenColumns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },

                // {
                //     field: 'voucherType',
                //     title: '凭证类型',
                //     width: 120,
                //     sortable: true,
                //     formatter: function (value) {
                //         // 1:材料商入库凭证 2:承运商对账凭证
                //         if (Number(value) === 1) {
                //             return '材料商入库凭证';
                //         }else {
                //             return '承运商对账凭证';
                //         }
                //     }
                // }
            ]
        ],
        columns: [columns],
        toolbar: [
            // {
            //     text: '数据同步',
            //     iconCls: 'icon-reload',
            //     handler: function () {
            //         dataSync();
            //     }
            // },
            // '-',
            {
                id: 'btn_excel',
                text: '导出Excel',
                iconCls: 'icon-download',
                handler: function () {
                    exportVoucherData();
                    setOperationLog("导出Excel按钮", "入库凭证查询");
                }
            },
            '-',
            {
                text: '修改其他折扣',
                iconCls: 'icon-import',
                id: 'icon_mark_batch_update',
                handler: function () {
                    var rows = $(el.tb).datagrid('getChecked');
                    if (rows.length <= 0) {
                        layer.msg("请选择需要修改的数据");
                        return;
                    }
                    var ids = [];
                    $.each(rows, function (idx, itm) {
                        ids.push(itm.id);
                    })
                    var updateRatioDialog = openDialog("updateRatioDialog", g.ctx+"pc/finance/receipt/voucher/updateRatioPage?ids=" + ids.join(","), {
                        title: '修改其他折扣',
                        width: 400,
                        height: '300',
                        cache: false,
                        modal: true,
                        border: false,
                        draggable: false,
                        shadow: false,
                        onClose: function () {
                            updateRatioDialog.dialog('destroy');
                        },
                        buttons: [{
                            text: '确定',
                            iconCls: 'icon-save',
                            handler: function () {
                                topWin.frames['updateRatioDialog'].page.save(() => {
                                    updateRatioDialog.dialog('close');
                                    $(el.tb).datagrid('reload');
                                });

                            }
                        }, {
                            text: '关闭',
                            iconCls: 'icon-cancel',
                            handler: function () {
                                updateRatioDialog.dialog('close');
                            }
                        }]
                    });
                }
            },
            //  "-",
            // {
            //     text: '批量导入',
            //     id: 'btn_pldr',
            //     iconCls: 'icon-import',
            //     handler: function () {
            //         importRatioExcel();
            //         var importRatioDialog = openDialog("importRatioDialog", g.ctx+"/finance/receipt/voucher/importRatioPage" , {
            //             title: '导入扣款比例',
            //             width: "500px",
            //             height: "70%",
            //             cache: false,
            //             modal: true,
            //             border: false,
            //             draggable: false,
            //             shadow: false,
            //             onClose: function () {
            //                 importRatioDialog.dialog('destroy');
            //             },
            //             buttons: [{
            //                 text: '确定',
            //                 handler: function () {
            //                     topWin.frames['updatePaymentTermDialog'].page.save(() => {
            //                         importRatioDialog.dialog('close');
            //                         $(el.tb).datagrid('reload');
            //                     });
            //
            //                 }
            //             }, {
            //                 text: '关闭',
            //                 handler: function () {
            //                     importRatioDialog.dialog('close');
            //                 }
            //             }]
            //         });
            //     }
            // },
            "-",
            {
                text: '批量价格调整',
                id: 'btn_pljgtz',
                iconCls: 'icon-import',
                handler: function () {
                    importTaxPriceExcel();
                }
            },
            "-",
            {
                text: '列设置',
                iconCls: 'icon-anchor',
                id: 'btn_column_sort',
                handler: function () {
                    openSortDialog({
                        //唯一编码
                        code: columnSortCodeEnum.RECEIVE,
                        //初始列（每次新增或删除列需要手动删除sys_column_sort表中对应code数据）
                        columns: el.defaultColumns,
                        callBack : function (newColumns) {
                            loadGrid(newColumns);
                        },
                    });
                }
            }
            // '-',
            // {
            //     text: '批量修改付款条件',
            //     iconCls: 'icon-import',
            //     id: 'icon_payment_batch_update',
            //     handler: function () {
            //         var rows = $(el.tb).datagrid('getChecked');
            //         if (rows.length <= 0) {
            //             layer.msg("请选择需要修改的数据");
            //             return;
            //         }
            //         var ids = [];
            //         $.each(rows, function (idx, itm) {
            //             ids.push(itm.id);
            //         })
            //         var updatePaymentTermDialog = openDialog("updatePaymentTermDialog", g.ctx+"/finance/receipt/voucher/updatePaymentTermPage?ids=" + ids.join(","), {
            //             title: '付款条件批量修改',
            //             width: 600,
            //             height: '800',
            //             cache: false,
            //             modal: true,
            //             border: false,
            //             draggable: false,
            //             shadow: false,
            //             onClose: function () {
            //                 updatePaymentTermDialog.dialog('destroy');
            //             },
            //             buttons: [{
            //                 text: '确定',
            //                 handler: function () {
            //                     topWin.frames['updatePaymentTermDialog'].page.save(() => {
            //                         updatePaymentTermDialog.dialog('close');
            //                         $(el.tb).datagrid('reload');
            //                     });
            //
            //                 }
            //             }, {
            //                 text: '关闭',
            //                 handler: function () {
            //                     updatePaymentTermDialog.dialog('close');
            //                 }
            //             }]
            //         });
            //     }
            // },
        ],
        onBeforeLoad:function (){
            displayButtonByUserRole('fin.receipt.voucher.index');
        },
        onLoadSuccess: function () {
            displayButtonByUserRole('fin.receipt.voucher.index');
            $(el.tb).datagrid('clearSelections');
        }
    });
}
/**
 * 导入
 */
function importRatioExcel() {
    openLayerWin("导入扣款比例", "/fin/receipt/voucher/importRatioPage", "500px", "70%");
}

/**
 * 导入
 */
function importTaxPriceExcel() {
    var importDialog = openQueryImportExcelDialog('pc/finance/receipt/voucher/rest/price/export',getQuery(),'导入其他折扣',function(file){
        let formData = new FormData();
        formData.append("file", file);
        var loading = topWin.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/finance/receipt/voucher/rest/price/import',
            method: "POST",
            data: formData,
            contentType: false,
            processData: false,
            success: function (data) {
                topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    $(el.tb).datagrid('reload');
                    importDialog.dialog('close');
                });
            },
            complete: function(data) {
                topWin.layer.close(loading);
            },
            error: function (data) {
                topWin.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}

function importRatioRow(){
    $(el.tb).datagrid('reload');
}

/**
 * 初始化页面下拉框
 */
function initPageComBoBox() {
    $(el.factoryCode).combobox({
        url: g.ctx + 'pc/sys/factory/rest/listAll',
        editable: false,
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        onLoadSuccess: function () { //加载完成后,设置选中第一项
            var val = $(this).combobox("getData");
            for (var item in val[0]) {
                if (item === "code") {
                    $(this).combobox("select", val[0][item]);
                }
            }
        }
    });
    // 初始化查询是凭证类型下拉框
    $(el.voucherType).combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '材料', value: 1},
            {label: '寄售', value: 2},
            {label: '费用', value: 3}
        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });

    // 是否对账下拉框
    $(el.isReconcile).combobox({
        valueField: 'value',
        textField: 'label',
        // value: -1,
        data: [
            {label: '是', value: 1},
            {label: '否', value: 2}
        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });

    // 是否对账下拉框
    $(el.isAllowAccount).combobox({
        valueField: 'value',
        textField: 'label',
        // value: -1,
        data: [
            {label: '允许', value: '1'},
            {label: '不允许', value: '0'}
        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });

    // 初始化同步时凭证类型下拉框
    $(el.syncVoucherType).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '全部', value: -1},
            {label: '材料商入库凭证', value: 1},
            {label: '承运商对账凭证', value: 2}
        ],
        editable: false,
        value: -1,
        panelHeight: 'auto'
    });


    // 初始化查询时公司代码下拉框
    $(el.companyCode).combobox({
        url: g.ctx + 'pc/sys/company/rest/listAll?isAuthority=1',
        panelHeight: 300,
        panelWidth: 340,
        editable: false,
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        onLoadSuccess: function () { //加载完成后,设置选中第一项
            if ($('#deptCode').val()){
                $(this).combobox("select", $('#deptCode').val());
            }
        }
    });

    // 初始化同步时公司代码下拉框
    $(el.syncCompanyCode).combobox({
        url: g.ctx + 'pc/sys/company/rest/listAll?isAuthority=0',
        panelHeight: 300,
        panelWidth: 340,
        valueField: 'companyCode',
        textField: 'companyCodeName',
        prompt: '--- 请选择 ---'
    });
}


/**
 * 数据同步
 */
function dataSync() {
    $(el.dataSyncDialog).dialog('open');
}


/**
 * 确定同步
 */
function confirmSync() {
    var startPostTime = $(el.startPostTime).datebox('getValue'), endPostTime = $(el.endPostTime).datebox('getValue'),
        receiptVoucher = $(el.receiptVoucherNo).textbox('getValue'), supplierCode = $(el.syncSupCode).textbox('getValue'),
        syncCompanyCode = $(el.syncCompanyCode).combobox('getValue'), syncMaterialCode = $(el.syncMaterialCode).textbox('getValue'),
        voucherType = $(el.syncVoucherType).combobox('getValue');

    if(!startPostTime){
        topWin.layer.alert('请选择凭证过账日期起始日', {icon: 0});
        return false;
    }

    if(!endPostTime){
        topWin.layer.alert('请选择凭证过账日期截止日', {icon: 0});
        return false;
    }

    if(moment(endPostTime).isBefore(startPostTime)){
        topWin.layer.alert('凭证过账日期起始日不能大于过账日期截止日', {icon: 0});
        return false;
    }

    // 凭证创建不能跨年
    var diff = moment(startPostTime).diff(moment(endPostTime), 'years');
    if(diff !== 0){
        topWin.layer.alert('凭证过账日期不能跨年度', {icon: 0});
        return false;
    }

    var load = topWin.layer.msg('数据同步中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
    $.ajax({
        type: 'POST',
        url: g.ctx + 'fin/receipt/voucher/rest/sync/warehouse/voucher',
        data: {
            startPostTime: startPostTime,
            endPostTime: endPostTime,
            receiptVoucher: receiptVoucher,
            supplierCode: supplierCode,
            companyCode: syncCompanyCode,
            materialCode: syncMaterialCode,
            voucherType: voucherType
        },
        success: function () {
            topWin.layer.close(load);
            closeSyncDialog();
            $(el.tb).datagrid('load');
        },
        error: function (data) {
            topWin.layer.close(load);
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}


/**
 * 关闭弹框
 */
function closeSyncDialog() {
    $(el.dataSyncDialog).dialog('close');
}


/**
 * 导出入库凭证数据
 */
function exportVoucherData() {
    openExportDialog('pc/finance/receipt/voucher/rest/export','tb', getQuery());
}


/**
 * 数据查询
 */
function searchData() {
    $(el.tb).datagrid('options').url = g.ctx + 'pc/finance/receipt/voucher/rest/list';
    $(el.tb).datagrid('options').queryParams = getQuery();
    $(el.tb).datagrid('load');
}


/**
 * 重置查询条件
 */
function resetSearch() {
    $(el.receiveCode).val('');
    $(el.supplierCode).val('');
    $(el.poType).val('');
    $(el.poTypeName).val('');
    $(el.poCode).val('');
    $(el.materialCode).val('');
    $(el.materialName).val('');
    // $(el.companyCode).combobox('setValue', '');
    $(el.pgCode).combobox('setValue', '');
    $(el.isReconcile).combobox('setValue', '');
    $(el.isAllowAccount).combobox('setValue', '');
    $(el.isSearchRepeat).prop('checked', false);
    $(el.postTimeStart).datebox('setValue', $('#beginTime').val());
    $(el.postTimeEnd).datebox('setValue', $('#endTime').val());
    $(el.paymentExpireDateStart).datebox('setValue', '');
    $(el.paymentExpireDateEnd).datebox('setValue', '');
    $(el.voucherType).combobox('setValue', -1);

    searchData();
}


/**
 * 获取查询条件
 */
function getQuery() {
    var queryParams = {};
    queryParams.receiveCode = $.trim($(el.receiveCode).val());
    queryParams.supplierCode = $.trim($(el.supplierCode).val());
    queryParams.supplierName = $.trim($(el.supplierName).val());
    queryParams.factoryCode = $(el.factoryCode).combobox('getValue');
    queryParams.poType = $.trim($(el.poType).val());
    queryParams.poTypeName = $.trim($(el.poTypeName).val());
    queryParams.poCode = $.trim($(el.poCode).val());
    queryParams.materialCode = $.trim($(el.materialCode).val());
    queryParams.materialName = $.trim($(el.materialName).val());
    queryParams.companyCode = $(el.companyCode).combobox('getValue');
    queryParams.isReconcile = $(el.isReconcile).combobox('getValue');
    queryParams.isAllowAccount = $(el.isAllowAccount).combobox('getValue');
    queryParams.isSearchRepeat = $(el.isSearchRepeat).prop('checked') ? "1" : "0";
    queryParams.pgCode = $(el.pgCode).combobox('getValue');
    queryParams.postTimeStart = $(el.postTimeStart).datebox('getValue');
    queryParams.postTimeEnd = $(el.postTimeEnd).datebox('getValue');
    queryParams.paymentExpireDateStart = $(el.paymentExpireDateStart).datebox('getValue');
    queryParams.paymentExpireDateEnd = $(el.paymentExpireDateEnd).datebox('getValue');
    queryParams.receiveType = $(el.voucherType).combobox('getValue');
    queryParams.cvCode = $.trim($(el.cvCode).val());

    return queryParams;
}