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

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

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP041302_xxfpd, "销项发票", false, false, strTableP041302_xxfpd);

        //添加表单元素到窗口中
        tableFormP041302_xxfpd(strDivP041302_xxfpd, strDjbh, strZt);

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

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

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

//创建表单
function tableFormP041302_xxfpd(strDivP041302_xxfpd, strDjbh, strZt) {
    var strHeadP041302_xxfpd = "";          //页头默认为空
    var strTablep041302_xxfpd = "";          //表格默认为空 
    var strDownP041302_xxfpd = "";          //页尾默认为空
    var objP041302_xxfpd = {};                  //表格的列配置
    var strPageHeader = [];                         //页头配置
    var strpageFooter = [];                          //页尾的配置
    //加载编号的语句
    if (strZt == "0") {
        $("#idMust", $("#" + strDivP041302_xxfpd)).append("(未送)");
    }
    else if (strZt == "1") {
        $("#idMust", $("#" + strDivP041302_xxfpd)).append("(已送)");
    }
    else if (strZt == "2") {
        $("#idMust", $("#" + strDivP041302_xxfpd)).append("(退票)");
    }
    //获得jqGrid的列名和属性
    objP041302_xxfpd = ParseData(window.external.DoOptions(strTableP041302_xxfpd, 'GetTableConfig', '{}'));

    strPageHeader = objP041302_xxfpd.pageHeader;
    strpageFooter = objP041302_xxfpd.pageFooter;

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**绘制表单 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    strHeadP041302_xxfpd += "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {            //创建页头
        if (strPageHeader[i]["name"] == "kpdwmc" || strPageHeader[i]["name"] == "khmc") {
            continue;
        }
        if (strPageHeader[i]["name"] == "sffxskp") {
            strHeadP041302_xxfpd += "<li class='unline'>";
            strHeadP041302_xxfpd += "<input type='checkbox'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP041302_xxfpd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP041302_xxfpd += strPageHeader[i]["xsmc"];
            strHeadP041302_xxfpd += "</li>";
        }
        else if (strPageHeader[i]["name"] == "kpsl") {
            strHeadP041302_xxfpd += "<li class='inp_70'>";
            strHeadP041302_xxfpd += strPageHeader[i]["xsmc"] + "：";
            strHeadP041302_xxfpd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP041302_xxfpd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>%"; //限制输入长度
            strHeadP041302_xxfpd += "</li>";
        }
        else {
            strHeadP041302_xxfpd += "<li class='inp_70'>";
            strHeadP041302_xxfpd += strPageHeader[i]["xsmc"] + "：";
            strHeadP041302_xxfpd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP041302_xxfpd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP041302_xxfpd += "</li>";
        }
    }
    strHeadP041302_xxfpd += "</ul>";
    $("#" + strDivP041302_xxfpd + "_divform").append(strHeadP041302_xxfpd);

    strTablep041302_xxfpd += "<table id='Tp040202'></table>";               //创建表格
    $("#" + strDivP041302_xxfpd + "_divform").append(strTablep041302_xxfpd);

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

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**绘制表单 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    //释放内存
    strHeadP041302_xxfpd = null;
    strTablep041302_xxfpd = null;
    strDownP041302_xxfpd = null;
    objP041302_xxfpd = null;
    strPageHeader = null;
    strpageFooter = null;
}

//从后台加载数据到前台表单实现展示功能
function myListP041302_xxfpd(strDivP041302_xxfpd, strDjbh) {
    try {
        var objP041302_xxfpd = {};                  //存放表格的列配置
        var objResultP041302_xxfpd = {};        //存放返回信息
        var myList = [];                                      //存放返回的数据
        var count = 5;                                        //默认最少显示5条数据
        //获得jqGrid的列名和属性
        objP041302_xxfpd = ParseData(window.external.DoOptions(strTableP041302_xxfpd, 'GetTableConfig', '{}'));

        for (var i = 0; i < objP041302_xxfpd.colModel.length; i++) {
            if (objP041302_xxfpd.colModel[i].index == "cz") {
                objP041302_xxfpd.colModel[i].hidden = true;
            }
        }
        objResultP041302_xxfpd = ParseData(window.external.DoOptions(strTableP041302_xxfpd, 'GetList', '{"djbh":"' + strDjbh + '"}'));

        //创建表格
        createGrid_P041302_xxfpd(strTableP041302_xxfpd, objP041302_xxfpd.colNames, objP041302_xxfpd.colModel);

        //格式化表格中的数据
        $("#" + strTableP041302_xxfpd).setColProp("sl", { formatter: formatCountPlace });
        $("#" + strTableP041302_xxfpd).setColProp("dj", { formatter: formatMoneyPlace });
        $("#" + strTableP041302_xxfpd).setColProp("je", { formatter: formatMoneyPlace });

        myList = objResultP041302_xxfpd.Records;                //返回的数据赋值给【myList】
        count = myList.length > 5 ? myList.length : 5;             //表格默认最少显示5条数据

        //添加行并为行分配id，并计算合计
        var xxfpje = 0;
        for (var i = 0; i < count; i++) {
            if (myList[i]) {
                if (myList[i].je) {                                                      //当销项发票金额存在时
                    xxfpje += parseFloat(myList[i].je);
                }
                $("#" + strTableP041302_xxfpd).addRowData(i + 1, myList[i]);      //添加后台数据行
            }
            else {                                                        //添加空行
                $("#" + strTableP041302_xxfpd).addRowData(i + 1, {});
            }
        }

        //调整表格列宽度自适应 
        resizeGridScroll(strTableP041302_xxfpd);

        //给收票日期绑定值
        $("#djdate", $("#" + strDivP041302_xxfpd + "_divform")).val(objResultP041302_xxfpd.djdate);
        //给收票单位赋值
        $("#kpdwdm", $("#" + strDivP041302_xxfpd + "_divform")).val(objResultP041302_xxfpd.kpdwmc);
        //给开票税率赋值
        $("#kpsl", $("#" + strDivP041302_xxfpd + "_divform")).val((parseFloat(objResultP041302_xxfpd.kpsl) * 100).toFixed(window.parent.strPercentPlace));
        //给客户赋值
        $("#khdm", $("#" + strDivP041302_xxfpd + "_divform")).val(objResultP041302_xxfpd.khmc);
        //给发票编号赋值
        $("#fpbh", $("#" + strDivP041302_xxfpd + "_divform")).val(objResultP041302_xxfpd.fpbh);
        //给发票金额赋值
        if (objResultP041302_xxfpd.a1 != "") {
            $("#a1", $("#" + strDivP041302_xxfpd + "_divform")).val(parseFloat(objResultP041302_xxfpd.a1).toFixed(window.parent.strMoneyPlace));
        }
        //给给非售票开票赋值
        $("sffxskp", $("#" + strDivP041302_xxfpd + "_divform")).attr(objResultP041302_xxfpd.sffxskp);
        //是否非销售开票
        if (objResultP041302_xxfpd.sffxskp == "False") {
            $("#sffxskp", $("#" + strDivP041302_xxfpd + "_divform")).attr("checked", false);
        }
        else {
            $("#sffxskp", $("#" + strDivP041302_xxfpd + "_divform")).attr("checked", true);
        }
        //给制单人赋值
        $("#czydm", $("#" + strDivP041302_xxfpd + "_divform")).val(objResultP041302_xxfpd.czymc);
        //给备注赋值
        $("#bz", $("#" + strDivP041302_xxfpd + "_divform")).val(escape(objResultP041302_xxfpd.bz));

        //加上合计行
        $("#" + strTableP041302_xxfpd).footerData("set", { rn: "合计", je: xxfpje.toFixed(window.parent.strMoneyPlace) });

        //释放内存
        objP041302_xxfpd = null;
        objResultP041302_xxfpd = 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_P041302_xxfpd(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);
}