/*--------------------------文档信息-------------------------------------------
///功能：      销项发票统计
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0413 = "Tp0413";    //表格ID
var objSelRowP0413 = {};           //选择行的数据
var intSelRowIDP0413 = 0;        //选择行的ID
var objWhereJsonP0413 = {};    //记录查询时的条件
var sortfieldP0413 = "";            //记录一下默认排序字段
var orderbyP0413 = "";            //记录一下默认的排序方式
var strRecordWhereJson = ""; //记录一下排序时查询的条件

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

//页面加载的事件
function pageLoad_P0413() {
    try {
        //查询类型
        loadSearchType_P0413();
        //选择日期范围
        loadDateExtent_P0413();
        //加载客户基本信息
        loadKhData_P0413();
        //加载商品基本信息
        loadSpData_P0413();
        //初始加载时隐藏药品
        $("#101").attr("style", "display:none");
        //加载数据
        searchDataClick_P0413();
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P0413(flag) {
    try {
        var strWhereJson = "";      //存放当前的查询条件
        var objConfig = {};              //存放当前的列配置
        var objResult = {};               //存放当前的查询结果
        if (!$("#cmbKhP0413").combobox("getValue")) {     //当前选择的客户不存在，赋值为【全部客户】！
            $("#cmbKhP0413").combobox("setValue", "");
        }
        if (!$("#cmbSpP0413").combobox("getValue")) {     //当选择的药品不存在，赋值为【全部药品】
            $("#cmbSpP0413").combobox("setValue", "");
        }
        //清空选择行信息
        intSelRowIDP0213 = 0;
        objSelRowP0213 = {};
        if (flag == "1") {
        }
        else {
            //查询条件的取得
            strWhereJson = "{\"sdate\":\"" + $("#dateStartTimeP0413").val() + "\",";
            strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0413").val() + "\",";
            if ($("#cbmSearchTypeP0413").combobox("getValue") == "按药品") {
                strWhereJson += "\"spdm\":\"" + $("#cmbSpP0413").combobox("getValue") + "\",";
            }
            strWhereJson += "\"khdm\":\"" + $("#cmbKhP0413").combobox("getValue") + "\"}";
            strRecordWhereJson = strWhereJson;
            //记录查询条件
            objWhereJsonP0413 = ParseData(strWhereJson.replace("}",
         ",\"dname\":\"" + $("#cmbDateExtentP0413").combobox("getValue") + "\",\"dname1\":\"" + $("#cmbDateExtentP0413").combobox("getText") + "\",\"spmc\":\"" + $("#cmbSpP0413").combobox("getText") + "\",\"khmc\":\"" + $("#cmbKhP0413").combobox("getText") + "\"}"));
        }

        objConfig = ParseData(window.external.DoOptions(strTableP0413, "GetTableConfig", "{}"));
        //格式化数据
        setData_P0413(objConfig.colModel);
        if ($("#cbmSearchTypeP0413").combobox("getValue") == "按客户") {
            if (flag != "1") {
                sortfieldP0413 = "";
                orderbyP0413 = "";
            }
            var strCollateWhereJson = strRecordWhereJson.replace("}", ",\"sortfield\":\"" + sortfieldP0413 + "\",\"orderby\":\"" + orderbyP0413 + "\"}");
            //创建表格
            objResult = ParseData(window.external.DoOptions(strTableP0413, "GetList", strCollateWhereJson));
            if (flag == "1"||flag == "2") {
                loadTreeData(strTableP0413, objResult.Records);
            }
            else {
                createTreeGrid_P0413(strTableP0413, objConfig.colNames, objConfig.colModel, objResult.Records, "khmc");
                //调整表格的大小
                resizeGrid(strTableP0413, true, false);
                loadTreeData(strTableP0413, objResult.Records);
            }
        }
        else {
            //创建表格
            objResult = ParseData(window.external.DoOptions(strTableP0413, "GetList", strWhereJson));
            createGrid_P0413(strTableP0413, objConfig.colNames, objConfig.colModel, objResult.Records);
        }
        //有滚动条时改变表格宽度
        resizeGridScroll(strTableP0413);
        //释放内存
        strWhereJson = null;
        objConfig = null;
        objResult = null;
    } catch (e) { }
}

//查看明细按钮事件
function viewDetail_P0413() {
    if (intSelRowIDP0413 > 0) {
        //改变标签的Name 
        $(window.parent.document).find("#span_413").text("往来明细");

        //选择的日期
        $(window.parent.document).find("#idIframe-41302").attr("dname", objWhereJsonP0413.dname);
        $(window.parent.document).find("#idIframe-41302").attr("sdate", objWhereJsonP0413.sdate);
        $(window.parent.document).find("#idIframe-41302").attr("edate", objWhereJsonP0413.edate);

        //供应商代码
        $(window.parent.document).find("#idIframe-41302").attr("khdm", objSelRowP0413.khdm ? objSelRowP0413.khdm : objWhereJsonP0413.khdm);
        $(window.parent.document).find("#idIframe-41302").attr("khmc", objSelRowP0413.khdm ? objSelRowP0413.khmc : objWhereJsonP0413.khmc);

        //药品代码
        $(window.parent.document).find("#idIframe-41302").attr("spdm", objSelRowP0413.spdm ? objSelRowP0413.spdm : objWhereJsonP0413.spdm);
        $(window.parent.document).find("#idIframe-41302").attr("spmc", objSelRowP0413.spdm ? objSelRowP0413.spmc : objWhereJsonP0413.spmc);

        //改变标签Iframe的Src
        $(window.parent.document).find("#idIframe-413").css("display", "none"); //隐藏统计页面
        $(window.parent.document).find("#idIframe-41302").css("display", ""); //显示明页面
        $(window.parent.document).find("#idIframe-41302").attr("src", "04/p041302.htm");

        //调整iframe的高度
        $(window.parent.document).find("#idIframe-41302").load(function () {
            var iHeight = window.parent.document.documentElement.clientHeight - window.parent._iFrmHeight;
            $(this).height(iHeight);
        });
    }
    else {
        //"请选择数据！"
        window.parent.cihoAlert(msg.other.selDataRow);
    }
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

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

//格式化表格中的数据的方法
function setData_P0413(objP0413) {
    for (var i = 0; i < objP0413.length; i++) {
        if (objP0413[i].name == "qcyk" || objP0413[i].name == "ykje" || objP0413[i].name == "yikje"
        || objP0413[i].name == "ykye" || objP0413[i].name == "yikje" || objP0413[i].name == "wkje"
        || objP0413[i].name == "bqje") {        //money的格式化
            objP0413[i].formatter = formatterP0413Money;
        }
        if (objP0413[i].name == "qczsl" || objP0413[i].name == "bqsl" ||
            objP0413[i].name == "wksl" || objP0413[i].name == "yiksl") {      //数量的格式化
            objP0413[i].formatter = formatterP0413Count;
        }
        if (objP0413[i].name == "xh") {     //隐藏序号列
            objP0413[i].hidden = true;
        }
    }
}

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

//加载查询类型的数据方法
function loadSearchType_P0413() {
    $("#cbmSearchTypeP0413").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        panelHeight: 42,
        editable: false,
        onSelect: function (option) {
            selectSearchType_P0413(option.dm);
        },
        enterDown: function () {
            selectSearchType_P0413($("#cbmSearchTypeP0413").combobox("getValue"));
        }
    });
    //加载数据
    $("#cbmSearchTypeP0413").combobox("loadData", [{ dm: "按客户", mc: "按客户", selected: true }, { dm: "按药品", mc: "按药品"}]);
}

//加载日期范围方法
function loadDateExtent_P0413() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0413", "dateEndTimeP0413");

    //添加选择事件
    $("#cmbDateExtentP0413").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0413", "dateEndTimeP0413");
        },
        enterDown: function () {
            dateExtentCalculation($("#cmbDateExtentP0413").combobox("getValue"), "dateStartTimeP0413", "dateEndTimeP0413");
        }
    });
    //加载数据
    $("#cmbDateExtentP0413").combobox("loadData",
    [{ dm: "本月", mc: "本月", selected: true },
    { dm: "上一月", mc: "上一月" },
    { dm: "本周", mc: "本周" },
    { dm: "过去三个月", mc: "过去三个月" },
    { dm: "本年", mc: "本年"}]);
}

//加载客户基本信息方法
function loadKhData_P0413() {
    $("#cmbKhP0413").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbKhP0413").combobox("getText") == "") {
                $("#cmbKhP0413").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbKhP0413").combobox("getText") == "全部客户") {
                $("#cmbKhP0413").combobox("setText", "");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    //加载数据
    var objProvider = getComboboxData("Tkhxx2");
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbKhP0413").combobox("loadData", objProvider);
    //释放内存
    objProvider = null;
}

//加载药品基本信息方法
function loadSpData_P0413() {
    $("#cmbSpP0413").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbSpP0413").combobox("getText") == "") {
                $("#cmbSpP0413").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbSpP0413").combobox("getText") == "全部药品") {
                $("#cmbSpP0413").combobox("setText", "");
            }
        },
        enterDown: function () { }
    });

    //加载数据
    var objDrug = getComboboxData("Tspxx2");
    if (objDrug == "{}") {
        objDrug = [];
    }
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbSpP0413").combobox("loadData", objDrug);
    //释放内存
    objDrug = null;
}

//普通表格的计算合计方法
function totalCompute_P0413() {
    //设置合计行的数据
    var iQcsl = parseFloat($("#" + strTableP0413).getCol("qczsl", false, "sum")).toFixed(window.parent.strCountPlace);      //期初数量
    var fQcje = parseFloat($("#" + strTableP0413).getCol("qcyk", false, "sum")).toFixed(window.parent.strMoneyPlace);    //期初金额 
    var iBqsl = parseFloat($("#" + strTableP0413).getCol("bqsl", false, "sum")).toFixed(window.parent.strCountPlace);       //应收数量
    var fBqje = parseFloat($("#" + strTableP0413).getCol("bqje", false, "sum")).toFixed(window.parent.strMoneyPlace);    //应收金额
    var iYksl = parseFloat($("#" + strTableP0413).getCol("yiksl", false, "sum")).toFixed(window.parent.strCountPlace);       //应开数量
    var fYkje = parseFloat($("#" + strTableP0413).getCol("yikje", false, "sum")).toFixed(window.parent.strMoneyPlace);    //应开金额
    var iWksl = parseFloat($("#" + strTableP0413).getCol("wksl", false, "sum")).toFixed(window.parent.strCountPlace);      //未收数量
    var fWkje = parseFloat($("#" + strTableP0413).getCol("wkje", false, "sum")).toFixed(window.parent.strMoneyPlace);    //未收金额
    var intIDLength = $("#" + strTableP0413).getDataIDs();                                                                                                     //获取表格中的记录
    //要合计了
    if (intIDLength.length > 0) {                          //当表格中有数据的时候，合计行数据正常格式化
        $("#" + strTableP0413).footerData("set", { rn: "合计", qczsl: iQcsl, qcyk: fQcje, bqsl: iBqsl, bqje: fBqje, yiksl: iYksl, yikje: fYkje, wksl: iWksl, wkje: fWkje }, true);
    }
    else {                                                              //当表格没有记录的时候，合计行中的数据都格式化成空
        $("#" + strTableP0413).footerData("set", { rn: "合计", qczsl: "&nbsp;", qcyk: "&nbsp;", bqsl: "&nbsp;", bqje: "&nbsp;", yiksl: "&nbsp;", yikje: "&nbsp;", wksl: "&nbsp;", wkje: "&nbsp;" }, false);
    }
}

//树的合计方法
function totalCompute_P0413p() {
    //设置合计行的数据
    $("#" + strTableP0413).setGridParam({
        gridComplete: function () {
            var objTable = $("#" + strTableP0413);      //存放表格对象
            var arrIds = objTable.getDataIDs();             //存放表格中数据的ID
            var fQcMoney = 0;                                    //默认期初金额为0
            var fBqykMoney = 0;                                 //默认本期应开为0
            var fYkMoney = 0;                                      //默认已开为0
            var fYkyeMoney = 0;                                   //默认余额为0
            if (arrIds.length > 0) {
                for (var i = 0; i < arrIds.length; i++) {
                    if (objTable.getCell(arrIds[i], "isLeaf") == "true") {
                        fQcMoney += parseFloat(objTable.getCell(arrIds[i], "qcyk"));
                        fBqykMoney += parseFloat(objTable.getCell(arrIds[i], "ykje"));
                        fYkMoney += parseFloat(objTable.getCell(arrIds[i], "yikje"));
                        fYkyeMoney += parseFloat(objTable.getCell(arrIds[i], "ykye"));
                    }
                }
                $("#" + strTableP0413).footerData("set", { khmc: "合计", qcyk: fQcMoney, ykje: fBqykMoney, yikje: fYkMoney, ykye: fYkyeMoney }, true);
            }
            else {
                $("#" + strTableP0413).footerData("set", { khmc: "合计", qcyk: "&nbsp;", ykje: "&nbsp;", yikje: "&nbsp;", ykye: "&nbsp;" }, false);
            }
        }
    });
    //释放内存
    objTable = null;
    arrIds = null;
    fQcMoney = null;
    fBqykMoney = null;
    fYkMoney = null;
    fYkyeMoney = null;
}

//查询类型的选择事件的方法
function selectSearchType_P0413(strValue) {
    //设置选择的查询类型
    if (strValue == "按客户") {        //按客户显示的页面
        strTableP0413 = "Tp0413";
        $("#divTp0413").css("display", "");
        $("#divTp041301").css("display", "none");
        $("#101").attr("style", "display:none");
    }
    else {          //按药品显示的页面
        strTableP0413 = "Tp041301";
        $("#divTp0413").css("display", "none");
        $("#divTp041301").css("display", "");
        $("#cmbSpP0413").combobox("setValue", "");
        $("#101").attr("style", "");
    }

    /*--------设置默认值--------*/
    objSelRowP0413 = {};
    intSelRowIDP0413 = 0;

    //加载数据
    searchDataClick_P0413();
}

//数量的格式化方法
function formatterP0413Count(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        cellvalue = 0;
    }
    return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
}

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


//当基本资料数据有修改时 刷新相关的combobox方法
function refreshMyCombobox(strModels) {
    if ($(window.parent.document).find('#idIframe-41302').length > 0) {
        $(window.parent.document.getElementById("idIframe-41302").contentWindow.document.getElementById("idRefreshCombobox")).val(strModels[0]);
        $(window.parent.document.getElementById("idIframe-41302").contentWindow.document.getElementById("idRefreshCombobox")).click();
    }
    for (var i = 0; i < strModels.length; i++) {
        var data = getComboboxData(strModels[i] + "2");

        if (strModels[i] == "Tkhxx") {
            data.unshift({ dm: "", mc: "全部客户", "selected": true });
            reLoadCombobox("cmbKhP0413", data);
        }
        if (strModels[i] == "Tspxx") {
            data.unshift({ dm: "", mc: "全部药品", "selected": true });
            reLoadCombobox("cmbSpP0413", data);
        }
    }
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
*****④objDataRecords：表格中需要加载的数据
-----------------------------------------------------------------*/
function createGrid_P0413(strTableID, objColNames, objColModel, objDataRecords) {
    $("#" + strTableID).GridUnload();       //删除旧表格
    if (objDataRecords == "{}") {              //当前数据源为空时，设置数据源变量为空对象
        objDataRecords = [];
    }
    //生成表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objColNames,
        colModel: objColModel,
        data: objDataRecords,
        rownumbers: true,
        autowidth: true,
        footerrow: true,
        gridview: true,
        rownumWidth: 30,
        cellsubmit: "clientArray",
        rowNum: objDataRecords.length,
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP0413 = rowId;
            objSelRowP0413 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0413();
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");

    //设置行号列的名称
    //$("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");

    //设置表格的高度
    //调整表格的大小
    resizeGrid(strTableP0413, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0413);
    //合计数据的计算
    totalCompute_P0413();
}

/*-------------------------------------------------------------------------------
///     ①strGridID:表格ID
///     ②objColNames:表格列名
///     ③objColModel:表格列属性
///     ④objJsonDatas:表格数据源，Json格式
///     ⑤strColExpandKey:伸缩列的字段名称
///     ⑥funcGridComplete:表格的gridComplete事件方法，此参数类型是方法对象
///     ⑦funcSelectRow:表格的onSelectRow事件方法，此参数类型是方法对象
///-----------------------------------------------------------------------------*/
function createTreeGrid_P0413(strTableID, objColNames, objColModel, objJsonDatas, strColExpandKey) {
    $("#" + strTableID).GridUnload();           //删除旧表格
    if (objJsonDatas == "{}") {                      //当数据集为空时，格式化数据集的空值
        objJsonDatas = [];
    }
    //创建表格
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: objJsonDatas[0],
        treeGrid: true,
        treedatatype: "local",
        treeGridModel: 'adjacency',
        ExpandColumn: strColExpandKey,
        ExpandColClick: false,
        editable: true,
        rownumbers: false,
        rowNum: 999999,
        sortname: "khdm",
        localReader: { id: "bh" },
        ExpandColClick: true,
        autowidth: true,
        footerrow: true,
        onSelectRow: function (rowId) {
            intSelRowIDP0413 = rowId;
            objSelRowP0413 = $(this).getRowData(rowId);
        },
        ondblClickRow: function () {
            viewDetail_P0413();
        },
        onSortCol: function (index, colindex, sortorder) {
            sortfieldP0413 = index;                         //排序字段
            orderbyP0413 = sortorder;                   //排序顺序
            searchDataClick_P0413("1");                     //加载数据
            return "stop";
        }
    }).parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
    //调整表格的大小
    resizeGrid(strTableP0413, true, false);
    //有滚动条时改变表格宽度
    resizeGridScroll(strTableP0413);
    //设计合计行
    totalCompute_P0413p();
}
