/*--------------------------文档信息-------------------------------------------
///功能：      进项发票明细
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP041201 = "Tp041201";               //表格ID
var strPageP041201 = "divPageP041201";    //分页层的ID
var objSelRowP041201 = {};                         //选择行的数据
var intSelRowIDP041201 = 0;                       //选择行的ID
var objPageParamP041201 = { sortfield: "dkdate", orderby: "asc", where: "", flag: 1 };     //分页控件使用

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    //$("#" + strDialog).dialog("close");
    cihoDlgClose(strDialog);                 //关闭弹出层
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载事件
function pageLoad_P041201() {
    try {
        //选择日期范围
        dateExtent_P041201();
        //给开票单位赋值
        loadSpdwData_P041201();
        //加载表格数据(触发查询按钮的点击事件)
        //获得表格的列属性
        objConfig = ParseData(window.external.DoOptions(strTableP041201, "GetTableConfig", "{}"));
        //格式化表中的数据
        setData_P041201(objConfig.colModel);
        //创建表格
        createGrid(strTableP041201, objConfig.colNames, objConfig.colModel, [], null, null, null, null);
        //添加事件
        addGridEvent_P041201();
        //调整表格的自适应列宽度
        resizeGrid(strTableP041201, false, true);
        //创建分页
        createPage(strPageP041201, strTableP041201);
        //加载表格数据(触发查询按钮的点击事件)
        searchDataClick_P041201();
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P041201() {
    try {
        var objResult = {};                 //存放当前的查询结果

        if (!$("#cmbSpdwP041201").combobox("getValue")) {  //"当前选择的药品不存在，赋值为【全部收票单位】！"
            $("#cmbSpdwP041201").combobox("setValue", "");
        }
        //查询条件的取得
        objPageParamP041201.where = mySearchWhereJson();
        objPageParamP041201.sortfield = "dkdate";
        objPageParamP041201.orderby = "asc";
        objPageParamP041201.flag = 1;
        loadGridData_Page(objPageParamP041201);
        //释放内存
        objResult = null;
    } catch (e) { }
}

//返回按钮的事件
function pageBack_P041201() {
    //改变标签的Name
    $(window.parent.document).find("#span_412").text("进项发票抵扣统计");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-412").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-41201").css("display", "none"); //隐藏明细页面
    $(window.parent.document).find("#idIframe-41201").attr("src", "");
}

//查看单据的事件
function viewReceipt_P041201() {
    if (intSelRowIDP041201 > 0) {
        if (objSelRowP041201.djbh.substr(0, 4) == "JXFP") {  //进项发票的查看单据
            showDialogP041201_jxfpdkd("divP041201", objSelRowP041201.djbh);
        }
    }
    else {
        //"请选择数据！"
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

//表格中的事件
function addGridEvent_P041201() {
    $("#" + strTableP041201).setGridParam({
        onSelectRow: function (rowid) {
            intSelRowIDP041201 = rowid;
            objSelRowP041201 = $(this).getRowData(rowid);
        },
        ondblClickRow: function () {
            viewReceipt_P041201();
        },
        onSortCol: function (index, colindex, sortorder) {
            objPageParamP041201.sortfield = index;
            objPageParamP041201.orderby = sortorder;
            objPageParamP041201.flag = "";
            loadGridData_Page(objPageParamP041201);
            return "stop";
        }
    });
}
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//格式化表格中的数据
function setData_P041201(objP041201) {
    for (var i = 0; i < objP041201.length; i++) {
        if (objP041201[i].name == "fpje" || objP041201[i].name == "dkje") {     //money的格式化
            objP041201[i].formatter = formatterP041201Money;
        }
        if (objP041201[i].name == "dksl") {     //数量的格式化
            objP041201[i].formatter = formatterP041201Percent;
        }
    }
}

//日期范围修改方法
function dateTimeChange_P041201() {
    if ($("#cmbDateExtentP041201").combobox("getText") != "自定义") {
        $("#cmbDateExtentP041201").combobox("setValue", "");
        $("#cmbDateExtentP041201").combobox("setText", "自定义");
    }
}


//选择日期范围
function dateExtent_P041201() {
    dateExtentCalculation("本月", "dateStartTimeP041201", "dateEndTimeP041201");
    $("#cmbDateExtentP041201").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP041201", "dateEndTimeP041201");
        },
        enterDown: function () {
            dateExtentCalculation($("#cmbDateExtentP041201").combobox("getValue"), "dateStartTimeP041201", "dateEndTimeP041201");
        }
    });
    //加载数据
    $("#cmbDateExtentP041201").combobox("loadData",
    [{ dm: "本月", mc: "本月", selected: true },
    { dm: "上一月", mc: "上一月" },
    { dm: "本周", mc: "本周" },
    { dm: "过去三个月", mc: "过去三个月" },
    { dm: "本年", mc: "本年"}]);
    var strDateItem = $(window.parent.document).find("#idIframe-41201").attr("dname");
    //设置默认日期为本月
    if (strDateItem != "") {
        $("#cmbDateExtentP041201").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP041201", "dateEndTimeP041201");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-41201").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-41201").attr("edate");
        $("#cmbDateExtentP041201").combobox("setText", "自定义");
        $("#dateStartTimeP041201").val(strStartTime);
        $("#dateEndTimeP041201").val(strEndTime);
    }
    //释放内存
    strDateItem = null;
    strStartTime = null;
    strEndTime = null;
}

//给收票单位加载数据
function loadSpdwData_P041201() {
    $("#cmbSpdwP041201").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbSpdwP041201").combobox("getText") == "") {
                $("#cmbSpdwP041201").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbSpdwP041201").combobox("getText") == "全部收票单位") {
                $("#cmbSpdwP041201").combobox("setText", "");
            }
        }
    });

    //加载数据
    var objDrug = getComboboxData("Tkpdwxx2");
    var strDrugID = $(window.parent.document).find("#idIframe-41201").attr("kpdwdm");
    var strDrugText = $(window.parent.document).find("#idIframe-41201").attr("kpdwmc");
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部收票单位", "selected": true });
    $("#cmbSpdwP041201").combobox("loadData", objDrug);
    //设定默认值
    $("#cmbSpdwP041201").combobox("setValue", strDrugID);
    $("#cmbSpdwP041201").combobox("setText", strDrugText);
    //释放内存
    objDrug = null;
    strDrugID = null;
}

//计算合计的方法
function totalCompute_P041201() {
    //设置合计行的数据
    var iFpMoney = parseFloat($("#" + strTableP041201).getCol("fpje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    var iDkMoney = parseFloat($("#" + strTableP041201).getCol("dkje", false, "sum")).toFixed(window.parent.strMoneyPlace);
    $("#" + strTableP041201).footerData("set", { rn: "合计", fpje: iFpMoney, dkje: iDkMoney }, true);
}

//money格式化方法
function formatterP041201Money(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
}

// 百分比的格式化方法
function formatterP041201Percent(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
}


//当基本资料数据有修改时 刷新相关的combobox方法
function refreshMyCombobox() {
    var strModels = $("#idRefreshCombobox").val();
    var data = getComboboxData(strModels + "2");

    if (strModels == "Tkpdwxx") {
        data.unshift({ dm: "", mc: "全部收票单位", "selected": true });
        reLoadCombobox("cmbSpdwP041201", data);
    }
    //释放内存
    strModels = null;
    data = null;
}

//查询条件的取得
function mySearchWhereJson() {
    var strWhereJson = "";                  //存放查询条件的变量
    //查询条件的取得
    strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP041201").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP041201").val() + "\",";
    strWhereJson += "\"kpdwdm\":\"" + $("#cmbSpdwP041201").combobox("getValue") + "\"}";
    return strWhereJson.replace(/[{}]/g, "");
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
