/*--------------------------文档信息-------------------------------------------
///功能：      收款单查看单据
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//先定义一下全局变量
strTableP021303_skd = "Tp020301";

//查看单据的方法
function showDialogP021303_skd(strDivP021303_skd, strDjbh) {
    try {
        $(".operate").remove();                             //清除浮动按钮层
        $("#" + strDivP021303_skd).empty();       //清空弹出窗口内容

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP021303_skd, "收款单", false, false, strTableP021303_skd, "900");

        //添加表单元素到窗口中
        tableFormP021303_skd(strDivP021303_skd, strDjbh);

        //调用所查数据同时加载到所对应的控件
        myListP021303_skd(strDivP021303_skd, strDjbh);

        //重新定位dialog的位置
        //$("#" + strDivP021303_skd).dialog("option", "position", "center");

        //释放内存
        strDivP021303_skd = null;
        strDjbh = null;
    } catch (e) { }
}

//创建表单
function tableFormP021303_skd(strDivP021303_skd, strDjbh) {
    var strHeadP021303_skd = "";            //页头的初始默认为空
    var strTablep021303_skd = "";           //表格的初始是没有的
    var strDownP021303_skd = "";          //页尾的初始值为空
    var objP021303_skd = {};                  //存放表格的列配置
    var strPageHeader = [];                     //存放页头的列配置
    var strpageFooter = [];                      //存放页尾的列配置

    //加载编号的语句
    $("#idMust", $("#" + strDivP021303_skd)).append("NO:" + strDjbh);

    //获得jqGrid的列名和属性
    objP021303_skd = ParseData(window.external.DoOptions(strTableP021303_skd, 'GetTableConfig', '{}'));
    strPageHeader = objP021303_skd.pageHeader;
    strpageFooter = objP021303_skd.pageFooter;

    strHeadP021303_skd = "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {                        //创建页头
        if (strPageHeader[i]["name"] == "khmc" || strPageHeader[i]["name"] == "zhmc" || strPageHeader[i]["name"] == "zymc") {
            continue;
        }
        else if (strPageHeader[i]["name"] == "khdm") {
            strHeadP021303_skd += "<li class='inp_100'>";
            strHeadP021303_skd += strPageHeader[i]["xsmc"] + "：";
            strHeadP021303_skd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP021303_skd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP021303_skd += "</li>";
        }
        else {
            strHeadP021303_skd += "<li class='inp_70'>";
            strHeadP021303_skd += strPageHeader[i]["xsmc"] + "：";
            strHeadP021303_skd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP021303_skd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP021303_skd += "</li>";
        }
    }
    strHeadP021303_skd += "</ul>";
    $("#" + strDivP021303_skd + "_divform").append(strHeadP021303_skd);

    strTablep021303_skd += "<table id='Tp020301'></table>";     //创建表格
    $("#" + strDivP021303_skd + "_divform").append(strTablep021303_skd);

    strDownP021303_skd += "<ul>";
    for (var i = 0; i < strpageFooter.length; i++) {                            //创建页尾
        if (strpageFooter[i]["name"] == "czymc") {
            continue;
        }
        else if (strpageFooter[i]["name"] == "bz") {
            strDownP021303_skd += "<li class='bz'>";
            strDownP021303_skd += strpageFooter[i]["xsmc"] + "：";
            strDownP021303_skd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP021303_skd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP021303_skd += "</li>";
        }
        else {
            strDownP021303_skd += "<li class='inp_150'>";
            strDownP021303_skd += strpageFooter[i]["xsmc"] + "：";
            strDownP021303_skd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP021303_skd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP021303_skd += "</li>";
        }
    }
    strDownP021303_skd += "</ul>";
    $("#" + strDivP021303_skd + "_divform").append(strDownP021303_skd);

    //释放内存
    strHeadP021303_skd = null;
    strTablep021303_skd = null;
    strDownP021303_skd = null;
    objP021303_skd = null;
    strPageHeader = null;
    strpageFooter = null;
}

//从后台加载所查单据数据
function myListP021303_skd(strDivP021303_skd, strDjbh) {
    try {
        var objP021303_skd = {};                    //存放列配置
        var objResultP021303_skd = {};          //存放返回信息
        var myList = [];                                    //存放返回的数据
        var count = 5;                                      //表格中默认最少显示5条数据

        //获得jqGrid的列名和属性
        objP021303_skd = ParseData(window.external.DoOptions(strTableP021303_skd, 'GetTableConfig', '{}'));
        for (var i = 0; i < objP021303_skd.colModel.length; i++) {
            if (objP021303_skd.colModel[i].index == "cz") {
                objP021303_skd.colModel[i].hidden = true;
            }
        }
        //从后台取数据
        objResultP021303_skd = ParseData(window.external.DoOptions(strTableP021303_skd, 'GetList', "{\"djbh\":\"" + strDjbh + "\", \"cztype\":\"3\"}"));
        //创建表格
        createGrid_P021303_skd(strTableP021303_skd, objP021303_skd.colNames, objP021303_skd.colModel);

        //格式化表格中的数据
        $("#" + strTableP021303_skd).setColProp("ypzsl", { formatter: formatCountPlace });
        $("#" + strTableP021303_skd).setColProp("dj", { formatter: formatMoneyPlace });
        $("#" + strTableP021303_skd).setColProp("xsje", { formatter: formatMoneyPlace });
        $("#" + strTableP021303_skd).setColProp("wsk", { formatter: formatMoneyPlace });
        $("#" + strTableP021303_skd).setColProp("bcyh", { formatter: formatMoneyPlace });
        $("#" + strTableP021303_skd).setColProp("bcsk", { formatter: formatMoneyPlace });

        myList = objResultP021303_skd.Records;
        count = myList.length > 5 ? myList.length : 5;

        var ypzsl = 0, xsje = 0, wsk = 0, bcyh = 0, bcsk = 0;
        for (var i = 0; i < count; i++) {
            if (myList[i]) {
                if (myList[i].ypzsl) {                                      //销售数量存在时
                    ypzsl += parseFloat(myList[i].ypzsl);
                }                                                                                      
                if (myList[i].xsje) {                                       //销售金额存在时
                    xsje += parseFloat(myList[i].xsje);
                }
                if (myList[i].wsk) {                                        //未收金额存在时
                    wsk += parseFloat(myList[i].wsk);
                }
                if (myList[i].bcyh) {                                      //优惠金额存在时
                    bcyh += parseFloat(myList[i].bcyh);
                }
                if (myList[i].bcsk) {                                     //实收金额存在时
                    bcsk += parseFloat(myList[i].bcsk);
                }

                $("#" + strTableP021303_skd).addRowData(i + 1, myList[i]);        //添加后台数据行
            }
            else {                                                                                                  //添加空行
                $("#" + strTableP021303_skd).addRowData(i + 1, {});
            }
        }
        //调整表格的自适应的高度
        resizeGridScroll(strTableP021303_skd);

        //给收款日期绑定值
        $("#djdate", $("#" + strDivP021303_skd + "_divform")).val(objResultP021303_skd.djdate);
        //给客户绑定值
        $("#khdm", $("#" + strDivP021303_skd + "_divform")).val(objResultP021303_skd.khmc);
        //给收款账户绑定值
        $("#kmdm", $("#" + strDivP021303_skd + "_divform")).val(objResultP021303_skd.zhmc);
        //给经受人绑定值
        $("#zydm", $("#" + strDivP021303_skd + "_divform")).val(objResultP021303_skd.zymc);
        //给收款金额赋值
        if (objResultP021303_skd != "") {
            $("#djzje").val(parseFloat(objResultP021303_skd.djzje).toFixed(window.parent.strMoneyPlace));
        }
        //给制单人绑定值
        $("#czydm", $("#" + strDivP021303_skd + "_divform")).val(objResultP021303_skd.czymc);
        //给备注绑定值
        $("#bz", $("#" + strDivP021303_skd + "_divform")).val(escape(objResultP021303_skd.bz));

        //将最大id保存到grid
        $("#" + strTableP021303_skd).data("nextNum", count + 1);
    
        //合计行
        $("#" + strTableP021303_skd).footerData("set", { rn: "合计",
            ypzsl: ypzsl.toFixed(window.parent.strCountPlace),
            xsje: xsje.toFixed(window.parent.strMoneyPlace),
            wsk: wsk.toFixed(window.parent.strMoneyPlace),
            bcyh: bcyh.toFixed(window.parent.strMoneyPlace),
            bcsk: bcsk.toFixed(window.parent.strMoneyPlace)
        });

        //释放内存
        objP021303_skd = null;
        objResultP021303_skd = null;
        myList = null;
    } catch (e) { }
}

//数量的格式化
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

//money的格式化
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
-----------------------------------------------------------------*/
function createGrid_P021303_skd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        rownumbers: true,
        rownumWidth: 30,
        footerrow: true,
        autowidth: true
    });
    //给表格设置序号
    $("#" + strTableID).setLabel("rn", "序号");
    //设置表格高度
    $("#" + strTableID).setGridHeight(141);
}
