/*--------------------------文档信息-------------------------------------------
///
///功能：    收款管理-收款单
///创建人：  李孟昌
///创建时间：2011年0月12日
///修改人：  王思波
///修改时间：2011年07月18日
///修改原因：功能实现有问题
///详细说明：--------------
///
-----------------------------------------------------------------------------*/

//本页全局变量
var strTableP020301 = "Tp020301";           //表格ID
var strDialogP020301 = "divP020301";        //窗口ID
var strDelGuidP020301 = "";                 //删除数据ID
var strOldItemP020301 = "";                 //影响预提费用的数据
var intRowCountP020301 = 5;                 //表格默认行数
var objConfigP020301 = {};                  //窗口元素信息
var objDataListP020301 = { Records: [] };   //窗口内容的信息
var objCellPlaceP020301 = { iRow: 0, iCol: 0, value: 0 }; //编辑状态单元格位置，未收款金额

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

//弹出收款单窗口方法
function showDialog_P020301(strCallType, strSaveFlag) {
    try {
        //创建窗口对象
        creatDiv_ShowDlgHaveTbl(strDialogP020301, "收款单", false, true, strTableP020301, 950);

        //设置保存标志
        $("#" + strDialogP020301 + "_divform").data("saveFlag", strSaveFlag);
        $("#" + strDialogP020301 + "_divform").data("callType", strCallType);

        //窗口元素信息的取得
        getPageConfig_P020301();

        //窗口元素的添加
        creatDialogItem_P020301();

        //窗口中数据的取得
        if (strSaveFlag == "2" || strCallType == "2") {
            getPageData_P020301(strCallType);
        }

        //加载表格数据(新增表单时，添加空行)
        loadGridData_P020301();

        //记录影响预提费用的信息
        if (strSaveFlag == "2") {
            strOldItemP020301 = getCompareData(strTableP020301, new Array("records", "del"));
            strOldItemP020301 += $("#djzje", "#" + strDialogP020301).val();
        }

    } catch (e) { }
}

//保存数据
function saveData_P020301(strFuncType) {
    var isValidateText = validateText_P020301();
    var strJsonData = "";   //JSON格式的字符串数据
    var objResult = "";     //保存结果

    //验证通过后才可保存
    if (isValidateText) {
        strJsonData = getDataJson_P020301(); //保存信息的取得
        objResult = saveData(strTableP020301, strJsonData, $("#" + strDialogP020301 + "_divform").data("saveFlag")); //保存数据

        //保存成功后的操作
        if (objResult.ReturnCode == 0) {
            //保存数据之后的操作
            afterSave_P020301();

            //保存新增
            if (strFuncType == "2") {
                clearPageData_P020301(); //清空页面的信息
                setTimeout(' $("#djzje", "#" + strDialogP020301).focus();', 100);
            }
            //保存退出
            else {
                cihoDlgClose(strDialogP020301); //关闭窗口
            }
        }
        else {
            window.parent.cihoAlert(objResult.MSG); //保存失败时的提示信息
        }
    }

    //释放内存
    isValidateText = null;
    strJsonData = null;
    objResult = null;
}

//删除数据行方法
function deleteDataRow_P020301(rowid) {
    try {
        //保存当前单元格
        $("#" + strTableP020301).saveCell(objCellPlaceP020301.iRow, objCellPlaceP020301.iCol);

        if (checkRowNull_P020301(rowid) == false) {
            //提示用户是否删除数据
            window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
                if (msgBox.dlgRst.Yes == result) {
                    //记录删除的数据GUID
                    if ($("#" + strTableP020301).getCell(rowid, "mxguid") != "") {
                        if (strDelGuidP020301 != "") {
                            strDelGuidP020301 += "/" + $("#" + strTableP020301).getCell(rowid, "mxguid");
                        }
                        else {
                            strDelGuidP020301 = $("#" + strTableP020301).getCell(rowid, "mxguid");
                        }
                    }

                    //从表格移除此行
                    $("#" + strTableP020301).delRowData(rowid);

                    //设置合计数
                    sumTotal_P020301();
                }
            });
        }
        else {
            $("#" + strTableP020301).delRowData(rowid); //从表格移除此行
        }

        //调整滚动条(拖动列宽时，出现双向滚动条)
        resizeGridScroll(strTableP020301);
    } catch (e) { }
}

//自动结算按钮事件
function autoCashin_P020301() {
    try {
        //保存当前单元格
        $("#" + strTableP020301).saveCell(objCellPlaceP020301.iRow, objCellPlaceP020301.iCol);

        //自动结算功能的实现
        if (objDataListP020301.Records.length <= 0) {
            window.parent.cihoAlert(msg.skd.dataNotEmp);
        }
        else if ($("#djzje", "#" + strDialogP020301).val() == "") {
            window.parent.cihoAlert(msg.skd.skJeNotEmp);
        }
        else {
            autoCashinFun_P020301();
        }
    } catch (e) { }
}

//单元格编辑前的事件
function beforeEditCell_P020301(rowid, cellname, value, iRow, iCol) {
    //记录单元格索引值
    objCellPlaceP020301.iRow = iRow;
    objCellPlaceP020301.iCol = iCol;

    //计算最初的未收金额
    objCellPlaceP020301.value = ($(this).getCell(rowid, "wsk") == "" ? 0 : parseFloat($(this).getCell(rowid, "wsk"))) +   //未收
                                ($(this).getCell(rowid, "bcyh") == "" ? 0 : parseFloat($(this).getCell(rowid, "bcyh"))) + //优惠
                                ($(this).getCell(rowid, "bcsk") == "" ? 0 : parseFloat($(this).getCell(rowid, "bcsk")));  //实收
}

//单元格编辑事件
function afterEditCell_P020301(rowid, cellname, value, iRow, iCol) {
    if (rowid.indexOf("blank") < 0) {
        var objCellItem = $("#" + rowid + " :input[name='" + cellname + "']", this); //获得单元格对象

        //内容编辑时居右显示
        objCellItem.css("text-align", "right");

        //设置单元格中的控件属性
        if (cellname == "bcsk") {
            objCellItem.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "", aNeg: '-' }).focus(function () { this.select(); }).css("ime-mode", "disabled");
        }
        else {
            objCellItem.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
        }

        //释放内存
        objCellItem = null;
    }
    else {
        $(this).restoreCell(iRow, iCol); //单元格不可编辑状态的设置
    }
}

//单元格保存事件
function afterSaveCell_P020301(rowid, cellname, value, iRow, iCol) {
    var intPrivilegeMoney = $(this).getCell(rowid, "bcyh") == "" ? 0 : parseFloat($(this).getCell(rowid, "bcyh"));  //优惠
    var intRealityMoney = $(this).getCell(rowid, "bcsk") == "" ? 0 : parseFloat($(this).getCell(rowid, "bcsk"));    //实收
    var intNotMoney = objCellPlaceP020301.value - intPrivilegeMoney - intRealityMoney;  //计算未收金额
    var strCellTitle = ""; //提示信息

    //更新未收金额
    $(this).setCell(rowid, "wsk", intNotMoney.toFixed(window.parent.strMoneyPlace));

    //单元格的提示信息设置
    if ($(this).getCell(rowid, "records") == "" && $(this).getCell(rowid, "del") == "") {
        strCellTitle = $(".name", "#" + rowid).text() + " | 优惠:" + intPrivilegeMoney.toFixed(window.parent.strMoneyPlace) + " | 实收:" + intRealityMoney.toFixed(window.parent.strMoneyPlace);
        $(this).setCell(rowid, "glxsymc", "", "", { title: strCellTitle }); //关联销售员
    }

    //窗口信息变量标志
    $("#" + strDialogP020301 + "_divform").data("change", true);

    //设置合计数
    sumTotal_P020301();

    //释放内存
    intPrivilegeMoney = null;
    intRealityMoney = null;
    intNotMoney = null;
    strCellTitle = null;
}

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


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

//添加表单元素到窗口中
function creatDialogItem_P020301() {
    var objPageHeader = objConfigP020301.pageHeader;
    var objPageFooter = objConfigP020301.pageFooter;
    var strItemHtml = "";

    strItemHtml = "<ul>"; //页头元素
    for (var intI = 0; intI < objPageHeader.length; intI++) {
        if (objPageHeader[intI].hidden == false) {
            if (objPageHeader[intI].edittype == "text") {
                strItemHtml += "<li class=\"inp_70\">";
                strItemHtml += objPageHeader[intI].xsmc + "：<input id=\"" + objPageHeader[intI].name + "\" name=\"" + objPageHeader[intI].name + "\" type=\"text\"/>";
                strItemHtml += "</li>";
            }
            else if (objPageHeader[intI].edittype == "select") {
                strItemHtml += "<li>";
                strItemHtml += objPageHeader[intI].xsmc + "：<select id=\"" + objPageHeader[intI].name + "\" name=\"" + objPageHeader[intI].name + "\" class=\"easyui-combobox\"></select>";
                strItemHtml += "</li>";
            }
        }
    }
    strItemHtml += "    <li class=\"fil_btn\">";
    strItemHtml += "        <a id=\"btnAutoCashin\" href=\"#\" onclick=\"autoCashin_P020301()\">自动结算</a>";
    strItemHtml += "    </li>";
    strItemHtml += "</ul>";
    strItemHtml += "<table id=\"" + strTableP020301 + "\"></table>"; //表格元素
    strItemHtml += "<ul>"; //页尾元素
    for (var intJ = 0; intJ < objPageFooter.length; intJ++) {
        if (objPageFooter[intJ].hidden == false) {
            if (objPageFooter[intJ].name == "bz") {
                strItemHtml += "<li class=\"bz\">";
                strItemHtml += objPageFooter[intJ].xsmc + "：<input id=\"" + objPageFooter[intJ].name + "\" name=\"" + objPageFooter[intJ].name + "\" type=\"text\" onkeypress=\"return checkValidate(event)\" onpaste=\"return validatePaste()\"/>";
                strItemHtml += "</li>";
            }
            else {
                strItemHtml += "<li class=\"inp_70\">";
                strItemHtml += objPageFooter[intJ].xsmc + "：<input id=\"" + objPageFooter[intJ].name + "\" name=\"" + objPageFooter[intJ].name + "\" value=\"" + window.parent.objLoginInfo.name + "\" type=\"text\" disabled=\"disabled\"/>";
                strItemHtml += "</li>";
            }
        }
    }
    strItemHtml += "</ul>";
    $("#" + strDialogP020301 + "_divform").append(strItemHtml);

    //绑定日期控件
    inputAndBindData("djdate", false, "#" + strDialogP020301, function () { $("#" + strDialogP020301 + "_divform").data("change", true); });
    $("#djdate", "#" + strDialogP020301).val(formatDate(new Date()));

    //绑定数字控件
    $("#djzje", "#" + strDialogP020301).autoNumeric({ mDec: window.parent.strPricePlace, aSep: "", aNeg: '-' }).focus(function () { this.select(); }).css("ime-mode", "disabled");

    //加载下拉框控件的信息
    loadClient_P020301();       //客户
    loadAccount_P020301();      //收款帐户
    loadDutyPerson_P020301();   //经手人
    $(".layoutpanel").css("z-index", "50000009");

    //默认焦点的设置
    setTimeout(' $("#djzje", "#" + strDialogP020301).focus();', 100);

    //创建表格
    createGrid_P020301();

    //给控件添加变量事件
    $(":input", $("#" + strDialogP020301 + "_divform")).change(function () {
        $("#" + strDialogP020301 + "_divform").data("change", true);
    });

    //释放内存
    objPageHeader = null;
    objPageFooter = null;
    strItemHtml = null;
}

//客户下拉框信息的加载
function loadClient_P020301() {
    //设置属性
    $("#khdm", "#" + strDialogP020301).combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        //选择事件
        onSelect: function (option) {
            //查询收款信息
            objDataListP020301 = ParseData(window.external.DoOptions(strTableP020301, "GetList", "{\"khdm\":\"" + option.dm + "\",\"cztype\":\"1\"}"));
            loadGridData_P020301();
            //内容变更标志
            $("#" + strDialogP020301 + "_divform").data("change", true);
        },
        //内容变更前事件
        afterChange: function (newValue, newText) {
            $("#khdm", "#" + strDialogP020301).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

    //加载数据
    $("#khdm", "#" + strDialogP020301).combobox("loadData", getComboboxData("Tkhxx1"));
}

//收款账户下拉框信息的加载
function loadAccount_P020301() {
    //设置属性
    $("#kmdm", "#" + strDialogP020301).combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        //选择事件
        onSelect: function (option) {
            $("#" + strDialogP020301 + "_divform").data("change", true);
        },
        //内容变更前事件
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "帐户", table: "" }, { jquery: $("#kmdm", "#" + strDialogP020301), model: "Tkjkm" });
                ciho.showTypeDialog();
            }
        }
    });

    //加载数据
    var objAccount = getComboboxData("TKjkm");
    objAccount.push({ "mc": "添加帐户", "dm": "-1" });
    $("#kmdm", "#" + strDialogP020301).combobox("loadData", objAccount);
    $("#kmdm", "#" + strDialogP020301).combo_ciho("layoutpanel").find("div.combobox-item[value='-1']").css("color", "red");

    //释放内存
    objAccount = null;
}

//经手人下拉框信息的加载
function loadDutyPerson_P020301() {
    //设置属性
    $("#zydm", "#" + strDialogP020301).combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        //选择事件
        onSelect: function (option) {
            $("#" + strDialogP020301 + "_divform").data("change", true);
        },
        //内容变更前事件
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "经手人", table: "" }, { jquery: $("#zydm", "#" + strDialogP020301), model: "TZyxx" });
                ciho.showTypeDialog();
            }
        }
    });

    //加载数据
    var objEmployee = getComboboxData("TZyxx");
    objEmployee.push({ "mc": "添加经手人", "dm": "-1" });
    $("#zydm", "#" + strDialogP020301).combobox("loadData", objEmployee);
    $("#zydm", "#" + strDialogP020301).combo_ciho("layoutpanel").find("div.combobox-item[value='-1']").css("color", "red");

    //释放内存
    objEmployee = null;
}

//窗口元素信息的取得
function getPageConfig_P020301() {
    //页面元素配置信息的取得
    objConfigP020301 = ParseData(window.external.DoOptions(strTableP020301, "GetTableConfig", "{}"));

    //添加格式化方法
    for (var intI = 0; intI < objConfigP020301.colModel.length; intI++) {
        switch (objConfigP020301.colModel[intI].name) {
            case "ypzsl": //销售数量
                objConfigP020301.colModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != undefined && cellvalue != "" && ((rowObject.djtypebh != "301" && rowObject.djtypebh != "302") || parseFloat(cellvalue) > 0)) {
                        return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                    }
                    else {
                        return "&nbsp;";
                    }
                };
                break;
            case "dj": //单价
                objConfigP020301.colModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != undefined && cellvalue != "" && rowObject.djtypebh != "301" && rowObject.djtypebh != "302") {
                        return parseFloat(cellvalue).toFixed(window.parent.strPricePlace);
                    }
                    else {
                        return "&nbsp;";
                    }
                };
                break;
            case "xsje": //金额
            case "wsk": //未收
            case "bcyh": //优惠
            case "bcsk": //实收
                objConfigP020301.colModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != undefined && cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                    }
                    else {
                        return "&nbsp;";
                    }
                };
                break;
        }
    }
}

//窗口数据的取得
function getPageData_P020301(strCallType) {
    //收款单信息的取得
    objDataListP020301 = ParseData(window.external.DoOptions(strTableP020301, "GetList", "{\"djbh\":\"" + (strCallType == "2" ? objSelRowP0201.djbh : objSelRowP0203.djbh) + "\",\"cztype\":\"" + strCallType + "\"}"));

    //客户(编辑状态、销售单添加状态)
    $("#khdm", "#" + strDialogP020301).combobox("setValue", objDataListP020301.khdm);
    $("#khdm", "#" + strDialogP020301).combobox("setText", objDataListP020301.khmc);

    //编辑状态时进行赋值
    if ($("#" + strDialogP020301 + "_divform").data("saveFlag") == "2") {
        //隐藏自动结算按钮
        $("#btnAutoCashin", "#" + strDialogP020301).css("display", "none");
        //单据编号
        $("#idMust", $("#" + strDialogP020301)).append("NO:" + objSelRowP0203.djbh);
        //日期
        $("#djdate", "#" + strDialogP020301).val(objDataListP020301.djdate);
        $("#djdate", "#" + strDialogP020301).attr("disabled", true);
        //客户
        $("#khdm", "#" + strDialogP020301).combobox("disable");
        //收款帐户
        $("#kmdm", "#" + strDialogP020301).combobox("setValue", objDataListP020301.kmdm);
        $("#kmdm", "#" + strDialogP020301).combobox("setText", objDataListP020301.zhmc);
        //经手人
        $("#zydm", "#" + strDialogP020301).combobox("setValue", objDataListP020301.zydm);
        $("#zydm", "#" + strDialogP020301).combobox("setText", objDataListP020301.zymc);
        //收款金额
        $("#djzje", "#" + strDialogP020301).val(parseFloat(objDataListP020301.djzje).toFixed(window.parent.strMoneyPlace))
        //制单人
        $("#czydm", "#" + strDialogP020301).val(objDataListP020301.czymc);
        //备注
        $("#bz", "#" + strDialogP020301).val(escape(objDataListP020301.bz));
    }
}

//查询收款信息
function loadGridData_P020301() {
    var objDataGrid = $("#" + strTableP020301); //表格对象
    var intRowLength = objDataListP020301.Records.length <= intRowCountP020301 ? intRowCountP020301 : objDataListP020301.Records.length; //表格行数
    var strCellHtml = "";   //单元格的内容
    var strCellTitle = "";  //单元格提示信息
    var intSaleCount = 0;   //销售数量
    var intSaleMoney = 0;   //销售金额
    var intNotRetake = 0;   //未收金额
    var intPrivilege = 0;   //优惠金额
    var intProceeds = 0;    //实收金额

    //清空旧数据
    $("#" + strTableP020301).clearGridData();
    //添加新数据
    for (var intI = 0; intI < intRowLength; intI++) {
        if (intI < objDataListP020301.Records.length) {
            //单元格气泡信息的设置
            if ((objDataListP020301.Records[intI].fpyh != "" && objDataListP020301.Records[intI].fpyh != "0") || (objDataListP020301.Records[intI].fpss != "" && objDataListP020301.Records[intI].fpss != "0")) {
                strCellTitle = getCellTitle_P020301(objDataListP020301.Records[intI].xsmxguid);
            }
            else {
                strCellTitle = objDataListP020301.Records[intI].glxsymc + " | 优惠:0.00" + " | 实收:0.00";
            }

            //添加回款分配按钮
            strCellHtml = "<div class=\"name\">" + (objDataListP020301.Records[intI].glxsymc.indexOf("\\") < 0 ? objDataListP020301.Records[intI].glxsymc : objDataListP020301.Records[intI].glxsymc.split("\\")[0] + "...");
            strCellHtml += "    <div class=\"add\">";
            strCellHtml += "        <a title=\"回款分配\" href=\"#\" onclick=\"showDialog_P020302('" + (intI + 1).toString() + "')\"></a>";
            strCellHtml += "    </div>";
            strCellHtml += "</div>";
            objDataListP020301.Records[intI].glxsymc = strCellHtml;

            //加载数据
            objDataGrid.addRowData((intI + 1).toString(), objDataListP020301.Records[intI]);

            //设置单据的字体样式
            objDataGrid.setCell((intI + 1).toString(), "xsdjdate", "", { color: "#CCC" });   //单据日期
            objDataGrid.setCell((intI + 1).toString(), "xsdjbh", "", { color: "#CCC" });     //单据编号
            objDataGrid.setCell((intI + 1).toString(), "ywlx", "", { color: "#CCC" });       //业务类型
            objDataGrid.setCell((intI + 1).toString(), "spmc", "", { color: "#CCC" });       //药品名称
            objDataGrid.setCell((intI + 1).toString(), "dj", "", { color: "#CCC" });         //药品单价
            objDataGrid.setCell((intI + 1).toString(), "ypzsl", "", { color: "#CCC" });      //销售数量
            objDataGrid.setCell((intI + 1).toString(), "xsje", "", { color: "#CCC" });       //销售金额
            objDataGrid.setCell((intI + 1).toString(), "wsk", "", { color: "#CCC" });        //未收金额
            objDataGrid.setCell((intI + 1).toString(), "glxsymc", "", "", { title: strCellTitle }); //关联销售员

            //计算合计
            intSaleCount += objDataListP020301.Records[intI].ypzsl == "" ? 0 : parseFloat(objDataListP020301.Records[intI].ypzsl);  //销售数量
            intSaleMoney += objDataListP020301.Records[intI].xsje == "" ? 0 : parseFloat(objDataListP020301.Records[intI].xsje);    //销售金额
            intNotRetake += objDataListP020301.Records[intI].wsk == "" ? 0 : parseFloat(objDataListP020301.Records[intI].wsk);      //未收金额
            intPrivilege += objDataListP020301.Records[intI].bcyh == "" ? 0 : parseFloat(objDataListP020301.Records[intI].bcyh);    //优惠金额
            intProceeds += objDataListP020301.Records[intI].bcsk == "" ? 0 : parseFloat(objDataListP020301.Records[intI].bcsk);     //实收金额
        }
        else {
            objDataGrid.addRowData("blank_" + (intI + 1).toString(), {});
        }
    }

    //设置数据合计值
    objDataGrid.footerData("set", { rn: "合计",
        ypzsl: intSaleCount.toFixed(window.parent.strCountPlace),   //销售数量
        xsje: intSaleMoney.toFixed(window.parent.strMoneyPlace),    //销售金额
        wsk: intNotRetake.toFixed(window.parent.strMoneyPlace),     //未收金额
        bcyh: intPrivilege.toFixed(window.parent.strMoneyPlace),    //优惠金额
        bcsk: intProceeds.toFixed(window.parent.strMoneyPlace)      //实收金额
    }, false);

    //调整滚动条
    resizeGridScroll(strTableP020301);

    //释放内存
    objDataGrid = null;
    intRowLength = null;
    strCellHtml = null;
    strCellTitle = null;
    intSaleCount = null;
    intSaleMoney = null;
    intNotRetake = null;
    intPrivilege = null;
    intProceeds = null;
}

//计算合计数据
function sumTotal_P020301() {
    var objDataGrid = $("#" + strTableP020301);
    var arrDataID = objDataGrid.getDataIDs();
    var intSaleCountSum = 0;    //销售数量
    var intSaleMoneySum = 0;    //销售金额
    var intNotRetakeSum = 0;    //未收金额
    var intPrivilegeSum = 0;    //优惠金额
    var intProceedsSum = 0;     //实收金额

    //计算合计
    for (var intI = 0; intI < arrDataID.length; intI++) {
        if (arrDataID[intI].indexOf("blank") < 0) {
            intSaleCountSum += objDataGrid.getCell(arrDataID[intI], "ypzsl") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "ypzsl"));
            intSaleMoneySum += objDataGrid.getCell(arrDataID[intI], "xsje") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "xsje"));
            intNotRetakeSum += objDataGrid.getCell(arrDataID[intI], "wsk") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "wsk"));
            intPrivilegeSum += objDataGrid.getCell(arrDataID[intI], "bcyh") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "bcyh"));
            intProceedsSum += objDataGrid.getCell(arrDataID[intI], "bcsk") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "bcsk"));
        }
    }
    //设置合计
    objDataGrid.footerData("set", {
        ypzsl: intSaleCountSum.toFixed(window.parent.strCountPlace), //销售数量
        xsje: intSaleMoneySum.toFixed(window.parent.strMoneyPlace), //销售金额
        wsk: intNotRetakeSum.toFixed(window.parent.strMoneyPlace),  //未收金额
        bcyh: intPrivilegeSum.toFixed(window.parent.strMoneyPlace), //优惠金额
        bcsk: intProceedsSum.toFixed(window.parent.strMoneyPlace)   //实收金额
    }, false);

    //释放内存
    objDataGrid = null;
    arrDataID = null;
    intSaleCountSum = null;
    intSaleMoneySum = null;
    intNotRetakeSum = null;
    intPrivilegeSum = null;
    intProceedsSum = null;
}

//保存成功后的操作
function afterSave_P020301() {
    //重新加载表格数据
    if ($("#" + strDialogP020301 + "_divform").data("callType") == "2") {
        searchDataClickP0201(); //销售管理
    }
    else {
        loadGridData_P0203(); //收款管理
    }

    //修改预提单
    if ($("#" + strDialogP020301 + "_divform").data("saveFlag") == "2") {
        var strNewItemInfo = getCompareData(strTableP020301, new Array("records", "del"));
        strNewItemInfo += $("#djzje", "#" + strDialogP020301).val();
        if (strNewItemInfo != strOldItemP020301) {
            ParseData(window.external.DoOptions(strTableP020301, "DoOthers", "{djbh:\"" + objSelRowP0203.djbh + "\"}"));
        }
    }

    //释放内存
    freeMemory_P020301();
}

//清空页面的信息
function clearPageData_P020301() {
    //保存类型
    $("#" + strDialogP020301 + "_divform").data("saveFlag", "1");

    //单据编号
    $("#idMust", $("#" + strDialogP020301)).empty();

    //隐藏自动结算按钮
    $("#btnAutoCashin", "#" + strDialogP020301).css("display", "");
    //日期
    $("#djdate", "#" + strDialogP020301).val(formatDate(new Date()));
    $("#djdate", "#" + strDialogP020301).attr("disabled", false);
    //客户
    $("#khdm", "#" + strDialogP020301).combobox("setValue", "");
    $("#khdm", "#" + strDialogP020301).combobox("enable");
    //收款帐户
    $("#kmdm", "#" + strDialogP020301).combobox("setValue", "");
    //经手人
    $("#zydm", "#" + strDialogP020301).combobox("setValue", "");
    //收款金额
    $("#djzje", "#" + strDialogP020301).val("")
    //制单人
    $("#czydm", "#" + strDialogP020301).val(window.parent.objLoginInfo.name);
    //备注
    $("#bz", "#" + strDialogP020301).val("");

    //清空表格
    loadGridData_P020301();

    //内容变更标志的更新
    $("#" + strDialogP020301 + "_divform").data("change", false);
}

//自动结算
function autoCashinFun_P020301() {
    var objDataGrid = $("#" + strTableP020301); //表格对象
    var arrDataID = objDataGrid.getDataIDs();   //数据行ID
    var intIncomeMoney = parseFloat($("#djzje", "#" + strDialogP020301).val()); //收款金额
    var intNotMoney = 0;        //未收
    var intPrivilegeMoney = 0;  //优惠
    var intRealityMoney = 0;    //实收
    var intNotMoneySum = 0;     //未收合计
    var intRealityMoneySum = 0; //实收合计
    var strCellTitle = "";      //提示信息

    for (var intI = 0; intI < arrDataID.length; intI++) {
        //只计算有数据的行
        if (arrDataID[intI].indexOf("blank") < 0) {
            //表格中已有的金额取得
            intNotMoney = objDataGrid.getCell(arrDataID[intI], "wsk") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "wsk"));          //未收
            intPrivilegeMoney = objDataGrid.getCell(arrDataID[intI], "bcyh") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "bcyh"));  //优惠
            intRealityMoney = objDataGrid.getCell(arrDataID[intI], "bcsk") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "bcsk"));   //实收
            intNotMoney = intNotMoney + intPrivilegeMoney + intRealityMoney; //计算当前最初的未收金额(当优惠、实收已经输入金额时，未收单元格的金额不是最初未收金额)

            //实收金额
            if (intIncomeMoney > 0 && intIncomeMoney >= intNotMoney) {
                intRealityMoney = intNotMoney; //实收金额
                intIncomeMoney -= intNotMoney; //结算后的收款金额
            }
            else {
                intRealityMoney = intIncomeMoney;   //收款金额
                intIncomeMoney = 0;                 //结算后的收款金额
            }
            intNotMoney = intNotMoney - intRealityMoney;    //未收金额
            intPrivilegeMoney = 0;                          //优惠金额
            objDataGrid.setCell(arrDataID[intI], "wsk", intNotMoney.toFixed(window.parent.strMoneyPlace));          //未收
            objDataGrid.setCell(arrDataID[intI], "bcyh", intPrivilegeMoney.toFixed(window.parent.strMoneyPlace));   //优惠
            objDataGrid.setCell(arrDataID[intI], "bcsk", intRealityMoney.toFixed(window.parent.strMoneyPlace));     //实收

            //当用户没有操作回款分配时，才进行设置
            if (objDataGrid.getCell(arrDataID[intI], "records") == "" && objDataGrid.getCell(arrDataID[intI], "del") == "") {
                strCellTitle = $(".name", "#" + arrDataID[intI]).text() + " | 优惠:0.00" + " | 实收:" + objDataGrid.getCell(arrDataID[intI], "bcsk");
                objDataGrid.setCell(arrDataID[intI], "glxsymc", "", "", { title: strCellTitle }); //单元格的提示信息设置
            }

            //计算合计
            intNotMoneySum += intNotMoney;
            intRealityMoneySum += intRealityMoney;
        }
    }
    //更新合计
    objDataGrid.footerData("set", { wsk: intNotMoneySum.toFixed(window.parent.strMoneyPlace), bcyh: intPrivilegeMoney.toFixed(window.parent.strMoneyPlace), bcsk: intRealityMoneySum.toFixed(window.parent.strMoneyPlace) }, false);

    //释放内存
    objDataGrid = null;         //表格对象
    arrDataID = null;           //数据行ID
    intIncomeMoney = null;      //收款金额
    intNotMoney = null;         //未收
    intPrivilegeMoney = null;   //优惠
    intRealityMoney = null;     //实收
    intNotMoneySum = null;      //未收合计
    intRealityMoneySum = null;  //实收合计
    strCellTitle = null;        //提示信息
}

//添加行的鼠标移动事件
function addMouseMove_P020301() {
    $("#" + strTableP020301).delegate("tr[id]:visible", "hover", function (e) {
        if (e.type == "mouseenter") {
            $("#" + strTableP020301).setCell(this.id, "cz", "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"deleteDataRow_P020301('" + this.id + "');return false;\">删除</a>");
        }
        if (e.type == "mouseleave") {
            $("#" + strTableP020301).setCell(this.id, "cz", "&nbsp;");
        }
    });
}

//单元格提示信息的取得
function getCellTitle_P020301(strGuid) {
    var objResult = ParseData(window.external.DoOptions("Tp020302", "GetList", "{'xsmxguid':'" + strGuid + "','djbh':'" + objSelRowP0203.djbh + "'}")); //回款分配信息的取得
    var strCellTitle = "";

    //组建单元格的气泡信息
    for (var intI = 0; intI < objResult.Records.length; intI++) {
        if (strCellTitle != "") {
            strCellTitle += "\n" + objResult.Records[intI].zymc + " | 优惠:" + parseFloat(objResult.Records[intI].a2).toFixed(window.parent.strMoneyPlace) + " | 实收:" + parseFloat(objResult.Records[intI].a1).toFixed(window.parent.strMoneyPlace);
        }
        else {
            strCellTitle = objResult.Records[intI].zymc + " | 优惠:" + parseFloat(objResult.Records[intI].a2).toFixed(window.parent.strMoneyPlace) + " | 实收:" + parseFloat(objResult.Records[intI].a1).toFixed(window.parent.strMoneyPlace);
        }
    }

    //释放内存
    objResult = null;

    return strCellTitle;
}

//保存信息的JSON字符串的取得
function getDataJson_P020301() {
    var objDataGrid = $("#" + strTableP020301); //表格对象
    var arrDataID = objDataGrid.getDataIDs();   //数据行ID
    var strSaveFlag = $("#" + strDialogP020301 + "_divform").data("saveFlag"); //保存标志
    var strJsonDetail = ""; //回款分配信息
    var strJsonData = "";   //保存信息
    var strDelFlag = "";    //删除标志
    var intAllocate = 0;    //分配金额
    var intPrivilege = 0;   //优惠金额

    //明细信息的取得
    for (var intI = 0; intI < arrDataID.length; intI++) {
        if (objDataGrid.getCell(arrDataID[intI], "bcsk") != "" && parseFloat(objDataGrid.getCell(arrDataID[intI], "bcsk")) != 0) {
            if (strJsonDetail != "") {
                strJsonDetail += ",";
            }
            strJsonDetail += "{mxguid:\"" + objDataGrid.getCell(arrDataID[intI], "mxguid") + "\",";
            strJsonDetail += "xsdjbh:\"" + objDataGrid.getCell(arrDataID[intI], "xsdjbh") + "\",";
            strJsonDetail += "xsmxguid:\"" + objDataGrid.getCell(arrDataID[intI], "xsmxguid") + "\",";
            strJsonDetail += "xsdjtypebh:\"" + objDataGrid.getCell(arrDataID[intI], "xsdjtypebh") + "\",";
            strJsonDetail += "bcyh:\"" + objDataGrid.getCell(arrDataID[intI], "bcyh") + "\",";
            strJsonDetail += "bcsk:\"" + objDataGrid.getCell(arrDataID[intI], "bcsk") + "\",";
            strJsonDetail += "glxsy:\"" + objDataGrid.getCell(arrDataID[intI], "glxsy") + "\",";
            strJsonDetail += "del:\"" + objDataGrid.getCell(arrDataID[intI], "del") + "\",";
            strJsonDetail += "records:[";
            //已分配回款
            if (objDataGrid.getCell(arrDataID[intI], "records") != "" || ($("#" + strDialogP020301 + "_divform").data("saveFlag") == "2" && objDataGrid.getCell(arrDataID[intI], "glxsy") != "")) {
                //分配销售员信息
                strJsonDetail += objDataGrid.getCell(arrDataID[intI], "records");

                //其它销售员信息
                intPrivilege = parseFloat(objDataGrid.getCell(arrDataID[intI], "bcyh")) - parseFloat(objDataGrid.getCell(arrDataID[intI], "fpyh"));
                intAllocate = parseFloat(objDataGrid.getCell(arrDataID[intI], "bcsk")) - parseFloat(objDataGrid.getCell(arrDataID[intI], "fpss"));
                if (intAllocate > 0 || intPrivilege > 0) {
                    if (objDataGrid.getCell(arrDataID[intI], "records") != "") {
                        strJsonDetail += ",";
                    }
                    strJsonDetail += "{zydm:\"\",";
                    strJsonDetail += "a1:\"" + intAllocate.toString() + "\",";
                    strJsonDetail += "a2:\"" + intPrivilege.toString() + "\",";
                    strJsonDetail += "xsmxguid:\"" + objDataGrid.getCell(arrDataID[intI], "xsmxguid") + "\"}";
                }

                //其他销售员只有优惠金额时，清空优惠金额后删除其他销售员信息
                if (intAllocate == 0 && parseFloat(objDataGrid.getCell(arrDataID[intI], "bcyh")) == 0) {
                    strDelFlag = "1";
                }
            }
            //销售员为空时的默认分配
            else if (objDataGrid.getCell(arrDataID[intI], "glxsy") == "") {
                //其它销售员信息
                strJsonDetail += "{zydm:\"\",";
                strJsonDetail += "a1:\"" + objDataGrid.getCell(arrDataID[intI], "bcsk") + "\",";
                strJsonDetail += "a2:\"" + objDataGrid.getCell(arrDataID[intI], "bcyh") + "\",";
                strJsonDetail += "xsmxguid:\"" + objDataGrid.getCell(arrDataID[intI], "xsmxguid") + "\"}";
            }
            //销售不为空的默认分配
            else if ($("#" + strDialogP020301 + "_divform").data("saveFlag") == "1") {
                //默认销售员信息
                strJsonDetail += "{zydm:\"" + objDataGrid.getCell(arrDataID[intI], "glxsy") + "\",";
                strJsonDetail += "a1:\"" + objDataGrid.getCell(arrDataID[intI], "bcsk") + "\",";
                strJsonDetail += "a2:\"" + objDataGrid.getCell(arrDataID[intI], "bcyh") + "\",";
                strJsonDetail += "xsmxguid:\"" + objDataGrid.getCell(arrDataID[intI], "xsmxguid") + "\"}";
            }
            strJsonDetail += "],flag:\"" + strDelFlag + "\"}";

            //删除类型
            strDelFlag = "";
        }
    }

    //收款单信息的取得
    strJsonData += "{djbh:\"" + (strSaveFlag == "1" ? "" : objSelRowP0203.djbh) + "\",";
    strJsonData += "djdate:\"" + $("#djdate", "#" + strDialogP020301).val() + "\",";
    strJsonData += "khdm:\"" + $("#khdm", "#" + strDialogP020301).combobox("getValue") + "\",";
    strJsonData += "kmdm:\"" + $("#kmdm", "#" + strDialogP020301).combobox("getValue") + "\",";
    strJsonData += "zydm:\"" + $("#zydm", "#" + strDialogP020301).combobox("getValue") + "\",";
    strJsonData += "czydm:\"" + (strSaveFlag == "1" ? window.parent.objLoginInfo.code : objSelRowP0203.czydm) + "\",";
    strJsonData += "bz:\"" + trim($("#bz", "#" + strDialogP020301).val()) + "\",";
    strJsonData += "delete:\"" + strDelGuidP020301 + "\",";
    strJsonData += "Records:[" + strJsonDetail + "]}";

    //释放内存
    objDataGrid = null;    //表格对象
    arrDataID = null;      //数据行ID
    strSaveFlag = null;    //保存标志
    strJsonDetail = null;  //回款分配信息
    intAllocate = null;    //分配金额
    intPrivilege = null;   //优惠金额

    //返回值
    return strJsonData;
}

//创建表格
function createGrid_P020301() {
    $("#" + strTableP020301).jqGrid({
        datatype: "local",
        sortname: "id",
        rowNum: 99999,
        rownumWidth: 30,
        rownumbers: true,
        autowidth: true,
        footerrow: true,
        gridview: true,
        cellEdit: true,
        cellsubmit: "clientArray",
        colNames: objConfigP020301.colNames,
        colModel: objConfigP020301.colModel,
        beforeEditCell: beforeEditCell_P020301,
        afterEditCell: afterEditCell_P020301,
        afterSaveCell: afterSaveCell_P020301
    });

    //设置序号列的名称
    $("#" + strTableP020301).setLabel("rn", "序号");

    //设置表格高度
    $("#" + strTableP020301).setGridHeight(141);

    //添加行的鼠标移动事件
    addMouseMove_P020301();
}

//数据行是否为空的验证
function checkRowNull_P020301(rowid) {
    var arrColData = $("#" + rowid + " td:visible");
    var isResult = false;

    //判断指定行可见列的内容是否为空
    for (var intJ = 1; intJ < arrColData.length - 1; intJ++) {
        if (trim((arrColData[intJ].innerText)) != "") {
            isResult = false;
            break;
        }
        else {
            isResult = true;
        }
    }

    //释放内存
    arrColData = null;

    //返回值
    return isResult;
}

//数据的验证
function validateText_P020301() {
    var objDataGrid = {};       //表格对象
    var arrDataID = [];         //数据行ID
    var strChittyDate = $("#djdate", "#" + strDialogP020301).val(); //单据日期

    //保存当前单元格
    $("#" + strTableP020301).saveCell(objCellPlaceP020301.iRow, objCellPlaceP020301.iCol);

    //表单日期
    if ($("#djdate", "#" + strDialogP020301).val() == "") {
        window.parent.cihoAlert(msg.skd.djdateNotEmp, msgBox.dlgBtn.Ok, function () { $("#djdate", "#" + strDialogP020301).focus(); });
        return false;
    }
    //客户
    else if ($("#khdm", "#" + strDialogP020301).combobox("getText") == "" || $("#khdm", "#" + strDialogP020301).combobox("getValue") == undefined) {
        window.parent.cihoAlert(msg.skd.khdmNotEmp, msgBox.dlgBtn.Ok, function () { $("#khdm", "#" + strDialogP020301).combobox("focus"); });
        return false;
    }
    //付款帐户
    else if ($("#kmdm", "#" + strDialogP020301).combobox("getText") == "" || $("#kmdm", "#" + strDialogP020301).combobox("getValue") == undefined) {
        window.parent.cihoAlert(msg.skd.kmdmNotEmp, msgBox.dlgBtn.Ok, function () { $("#kmdm", "#" + strDialogP020301).combobox("focus"); });
        return false;
    }
    //职员
    else if ($("#zydm", "#" + strDialogP020301).combobox("getText") == "" || $("#zydm", "#" + strDialogP020301).combobox("getValue") == undefined) {
        window.parent.cihoAlert(msg.skd.zydmNotEmp, msgBox.dlgBtn.Ok, function () { $("#zydm", "#" + strDialogP020301).combobox("focus"); });
        return false;
    }
    //收款金额
    else if ($("#djzje", "#" + strDialogP020301).val() == "" || parseFloat($("#djzje", "#" + strDialogP020301).val()) == 0) {
        window.parent.cihoAlert(msg.skd.skJeNotEmp, msgBox.dlgBtn.Ok, function () { $("#djzje", "#" + strDialogP020301).focus(); });
        return false;
    }
    //单据日期是否在有效范围内
    else if (strChittyDate.replace(/-/g, "/") < window.parent._startDate) {
        window.parent.cihoAlert(msg.other.djrqExtent, msgBox.dlgBtn.Ok, function () { $("#djdate", "#" + strDialogP020301).focus(); });
        return false;
    }
    //收款金额与实收金额的合计是否相等
    else if (parseFloat($("#djzje", "#" + strDialogP020301).val()) != parseFloat($("#" + strTableP020301).footerData("get").bcsk)) {
        var intContrast = parseFloat($("#djzje", "#" + strDialogP020301).val()) - parseFloat($("#" + strTableP020301).footerData("get").bcsk)
        window.parent.cihoAlert(msg.skd.skBuDeng + "<br>(相差：" + intContrast + "元)", msgBox.dlgBtn.Ok, function () { $("#djzje", "#" + strDialogP020301).focus(); });
        return false;
    }
    else {
        //表格中的信息验证
        objDataGrid = $("#" + strTableP020301);
        arrDataID = objDataGrid.getDataIDs();

        //循环表格
        for (var intI = 0; intI < arrDataID.length; intI++) {
            if (arrDataID[intI].indexOf("blank") < 0) {
                //优惠
                if (parseFloat(objDataGrid.getCell(arrDataID[intI], "fpyh")) > parseFloat(objDataGrid.getCell(arrDataID[intI], "bcyh"))) {
                    window.parent.cihoAlert(msg.skd.skdYh.replace("NO", (intI + 1).toString()));
                    return false;
                }

                //实收
                if (parseFloat(objDataGrid.getCell(arrDataID[intI], "fpss")) > parseFloat(objDataGrid.getCell(arrDataID[intI], "bcsk"))) {
                    window.parent.cihoAlert(msg.skd.skdJe.replace("NO", (intI + 1).toString()));
                    return false;
                }
            }
        }
    }

    //释放内存
    strChittyDate = null;       //单据日期
    objDataGrid = null;         //表格对象
    arrDataID = null;           //数据行ID

    //返回值
    return true;
}

//释放全局变量的内存
function freeMemory_P020301() {
    strDelGuidP020301 = ""; //删除数据ID
    strOldItemP020301 = ""; //影响预提费用的数据
    objConfigP020301 = {};  //窗口元素信息
    objDataListP020301 = { Records: [] }; //窗口内容的信息
    objCellPlaceP020301 = { iRow: 0, iCol: 0, wsk: 0 }; //编辑状态单元格位置，未收款金额
}

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