/*
功能：采购单
创建人：李乐
*/

var strTableP010101 = "Tp010101";
var strDivP010101 = "divP010101";
var cols = ["spdm", "a1", "a2"];
var compareData;
var cellX, cellY;

//====================打开采购页面==================
//state:1 修改
//state:2 新增

function openBuy(state) {
    try {
        //变量声明
        var tbId = strTableP010101, dlgId = strDivP010101;

        //窗口对象
        var $dlgId = $("#" + dlgId);

        creatDiv_ShowDlgHaveTbl(dlgId, "采购单", false, true, tbId, 900);
        createElements(tbId, dlgId);
        initGridSet(tbId, dlgId);
        initInputsProp(tbId, dlgId);

        //设置采购单的状态：1，修改；2，新增
        $dlgId.data("state", state);

        //加载combobox下拉选项
        loadComboData(dlgId);

        var mylist = [];

        //状态为修改时，去后台请求数据
        if (state == 2) {
            $("#idMust", $dlgId).text("NO:" + objSelRowP0101.djbh);
            var record = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"' + objSelRowP0101.djbh + '"}'));
            if (record && record.ReturnCode == 0) {

                //为采购日期赋值，并且采购日期不可编辑
                $("#cgtime", $dlgId).val(record.djdate).attr("disabled", true);

                //为供应商combobox赋值
                var $gys = $("#gysdm", $dlgId);
                $gys.combobox("setValue", record.gysdm);
                if ($gys.combobox("getText") == record.gysdm) {
                    $gys.combobox("setText", record.gysmc);
                }

                //为职员combobox赋值
                var $zy = $("#zydm", $dlgId);
                $zy.combobox("setValue", record.zydm);
                if ($zy.combobox("getText") == record.zydm) {
                    $zy.combobox("setText", record.zymc);
                }

                //为仓库combobox赋值
                var $ck = $("#ckdm", $dlgId);
                $ck.combobox("setValue", record.ckdm);
                if ($ck.combobox("getText") == record.ckdm) {
                    $ck.combobox("setText", record.ckmc);
                }

                //为付款账户combobox赋值
                var $km = $("#kmdm", $dlgId);
                $km.combobox("setValue", record.kmdm);
                if ($km.combobox("getText") == record.kmdm) {
                    $km.combobox("setText", record.kmmc);
                }

                //如果优惠为有效的数字，则格式化并赋值
                if (record.a11 * 1) {
                    $("#a11", $dlgId).val((record.a11 * 1).toFixed(window.parent.strMoneyPlace));
                }

                //如果实付金额为有效的数字，则格式化并赋值
                if (record.jiner) {
                    $("#jiner", $dlgId).val((record.jiner * 1).toFixed(window.parent.strMoneyPlace));
                }

                //为备注赋值，并逆转义
                $("#a9", $dlgId).val(escape(record.a9));
                $("#sfkdate", $dlgId).val(record.sfkdate);
                $("#czydm", $dlgId).val(record.czymc);

                //将grid中的数据赋值到加载数组中
                if (record.Records instanceof Array) {
                    mylist = record.Records;
                }

                //将单据编号，供应商，经手人，仓库，付款账户，制单人，保存到对话框中，保存时要用到
                $dlgId.data("data", { "djbh": objSelRowP0101.djbh, "gysdm": record.gysdm, "gysmc": record.gysmc, "zydm": record.zydm, "zymc": record.zymc, "ckdm": record.ckdm, "ckmc": record.ckmc, "kmdm": record.kmdm, "kmmc": record.kmmc, "czydm": record.czydm, "cyzmc": record.czymc });

                $gys = $zy = $ck = $km = null;
            } else {
                window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
            }
        } else {

            //状态为新增时，为控件赋默认值
            $("#cgtime", $dlgId).val(getNowDate());
            $("#czydm", $dlgId).val(window.parent.objLoginInfo.name);
            $dlgId.data("data", {});
        }

        //为grid加载数据
        loadGridData(tbId, mylist);

        if (state == 2) {
            compareData = getCompareData(tbId, cols);
        }

        //设置窗口是否被修改，刚刚打开时为false
        $("#" + dlgId + "_divform").data("change", false);
        setTimeout('$("#a11", "#" + strDivP010101).focus();', 100);

        $(".layoutpanel").css("z-index", "50000009");
    } catch (e) { }
}

//计算footer统计信息==============在grid中调用
function sumGridTotal(tbId) {
    var $tbId = $("#" + tbId);
    var ids = $tbId.getDataIDs();
    var ypsl = 0, ypje = 0, kpsl = 0, kpje = 0;
    for (var i in ids) {
        var item = $tbId.getRowData(ids[i]);
        ypsl += isNaN(item.a1) ? 0 : item.a1 * 1;
        ypje += isNaN(item.sunmoney) ? 0 : item.sunmoney * 1;
        if (item.daipiao == "true") {
            kpsl += isNaN(item.a4) ? 0 : item.a4 * 1;
            kpje += isNaN(item.a6) ? 0 : item.a6 * 1;
        }
    }
    $tbId.footerData("set", { rn: "合计", a1: ypsl, sunmoney: ypje, a4: kpsl, a6: kpje }, true);
    $tbId = null;
}

//删除窗口中的grid数据
function delDjRow(tbId, rowId) {
    try {
        var $tbId = $("#" + tbId);
        $tbId.saveCell(cellX, cellY);
        var delItem = $tbId.getRowData(rowId);
        var vCols = $tbId.data("vCols");
        if (delItem.zt && !isRowEmpty(delItem, vCols)) {                                                                   //如果有商品代码
            window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (strRet) {
                if (msgBox.dlgRst.Yes == strRet) {
                    if (delItem.glsl * 1 > 0) {
                        window.parent.cihoAlert(msg.del.delForbid, msgBox.dlgBtn.Ok);
                    } else {
                        var $grid = $("#" + tbId);
                        var row = $grid.getRowData(rowId);
                        if (row.mxguid) {                              //如果存在mxguid则保存到records数组里
                            var records = $grid.data("records");
                            records.push('{"mxguid":"' + row.mxguid + '","zt":"3"}');
                            $grid.data("records", records);
                            changeZt(tbId);
                        }
                        $grid.delRowData(rowId);
                        sumGridTotal(tbId);
                        resizeGridScroll(tbId);
                    }
                }
            }
                );
        }
        else {
            $tbId.delRowData(rowId);
            if (delItem.mxguid) {                                   //如果存在mxguid则保持到records数组里
                var records = $tbId.data("records");
                records.push('{"mxguid":"' + delItem.mxguid + '","zt":"3"}');
                $tbId.data("records", records);
                changeZt(tbId);                                      //改变窗口的状态
            }
            resizeGridScroll(tbId);
            sumGridTotal(tbId);
        }
    } catch (e) { }
}

//改变对话框或数据行的状态
//如果只提供表格id，则只改变窗口的状态标志
function changeZt(tbId, rowId) {
    if (tbId == strTableP010101) {
        $("#" + strDivP010101 + "_divform").data("change", true);
    }
    if (rowId) {
        var $tbId = $("#" + tbId);
        var item = $tbId.getRowData(rowId);
        if (item.mxguid) {
            $tbId.setCell(rowId, "zt", 2);                               //2代表更新
        } else {
            $tbId.setCell(rowId, "zt", 1);                               //1代表新增
        }
    }
}

//添加单据空行
function addNewRow(tbId) {
    try {
        $tbId = $("#" + tbId);
        var nextNum = $tbId.data("nextNum") + 1;
        $tbId.addRowData(nextNum, {});
        $tbId.data("nextNum", nextNum);
        resizeGridScroll(tbId);
    } catch (e) { }
}

//为删除按钮注册事件
function bindDelEvent(tbId) {
    $("#" + tbId).delegate("tr[id]:visible", "hover", function (e) {
        if (e.type == "mouseenter") {
            var btn = '<a style="color:red;cursor:pointer;text-decoration:none" onclick="delDjRow(\'' + tbId + '\',' + this.id + ');return false;">删除</a>';
            $("#" + tbId).setCell(this.id, "cz", btn);
        }
        if (e.type == "mouseleave") {
            $("#" + tbId).setCell(this.id, "cz", null);
        }
    });
}

//创建表单元素
function createInputs(arr) {
    var strForm = "<ul>";
    for (i = 0; i < arr.length; i++) {
        var Item = arr[i];
        if (!Item.hidden) {
            if (Item.edittype == "text") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "' ";
                if (!Item.editable) { strForm += " disabled='disabled'"; }
                if (Item.index != "a9") {
                    strForm += " maxlength='50' ";
                }
                strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
                strForm += "</li>";
            } else if (Item.edittype == "select") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<select name='" + Item.index + "' id='" + Item.index + "'";
                strForm += " class='easyui-combobox' >";
                strForm += "</select>";
                strForm += "</li>";
            }
        }
    }
    strForm += "</ul>";
    return strForm;
}

function initGridSet(tbId, dlgId) {
    if (tbId == strTableP010101) {
        $("#" + tbId).setGridParam({
            afterEditCell: function (rowId, cellname, value, iRow, iCol) {
                try {
                    cellX = iRow; cellY = iCol;
                    var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
                    switch (cellname) {

                        //=======================================药品名称====================================                                                                                                                                                                               

                        case "spmc":
                            item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 }); //药品名称combobox

                            //-------------------------为药品combobox加载数据------------------------
                            var yplist = getComboboxData("Tspxx");
                            yplist.push({ "mc": "<font color='red'>添加药品</font>", "dm": "-1" });
                            item.combobox("loadData", yplist);                                      //药品combobox加载选项信息

                            //------------------------为药品combobox设置值----------------------------
                            var ypdm = $(this).getCell(rowId, "spdm");                              //药品代码隐藏列
                            item.combobox("setValue", ypdm);                                        //设置combobox的值
                            if (item.combobox("getText") == ypdm) { item.combobox("setText", value); }

                            item.combobox('focus');                                                 //设置焦点
                            item.combobox({                                                         //定义药品下拉框的事件
                                onChange: function (newValue, oldValue) {
                                    var $tbId = $("#" + tbId);
                                    if (!newValue) { $tbId.setCell(rowId, "spdm", null); }          //如果没有选中值就为药品代码赋空
                                    else {

                                        var data = $("#" + dlgId).data("data");                              //获取修改时保存到grid里面的信息
                                        $tbId.setCell(rowId, "spdm", newValue);

                                        //=========================设置和获取带票信息=========================

                                        var dp = $tbId.getCell(rowId, "daipiao");
                                        if (!dp) { $tbId.setCell(rowId, "daipiao", "是"); }         //如果没有带票信息就赋默认值为否
                                        dp = $tbId.getCell(rowId, "daipiao") == "true" ? true : false;                       //获取最终的带票信息

                                        //=========================设置和获取供应商信息=========================

                                        var $gys = $("#gysdm", "#" + dlgId);
                                        var gysdm = $gys.combobox("getValue");
                                        var gysmc = $gys.combobox("getText");
                                        if (gysmc == data.gysmc) { gysdm = data.gysdm; }            //修改时，如果供应商停用，则用修改时的供应商代码

                                        var yplist = getComboboxData("Tspxx");

                                        //=========================获取后台请求跟踪信息=======================
                                        var request = false;
                                        if (gysdm) {
                                            var s = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"spdm":"' + newValue + '","gysdm":"' + gysdm + '","fb":"2"}'));

                                            //------------------成功请求到跟踪信息-----------------------

                                            if (s && s.ReturnCode == 0 && s.Records instanceof Array && s.Records.length > 0) {
                                                request = true;

                                                $tbId.setCell(rowId, "a2", s.Records[0].a1 ? s.Records[0].a1 : null);                                //保存药品单价

                                                //如果带票就设置带票信息
                                                if (dp) {
                                                    $tbId.setCell(rowId, "a5", s.Records[0].a2 ? s.Records[0].a2 : null);                            //保存开票单位单价
                                                    var ss = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", "")); //开票单位信息
                                                    if (ss && ss.ReturnCode == 0 && ss.Records instanceof Array) {
                                                        for (var i in ss.Records) {
                                                            if (ss.Records[i].dm == s.Records[0].kpdwdm) {
                                                                $tbId.setCell(rowId, "kpdwdm", ss.Records[i].dm);                    //开票单位代码
                                                                $tbId.setCell(rowId, "kpdwmc", ss.Records[i].mc);                    //开票单位名称
                                                                // $tbId.setCell(rowId, "a6", ss.Records[i].kpsl);                      //开票税率
                                                                var sss = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"kpdwdm":"' + s.Records[0].kpdwdm + '","fb":"1"}'));
                                                                if (sss && sss.ReturnCode == 0) {                                         //保存代扣税率
                                                                    $tbId.setCell(rowId, "a8", sss.dksl);
                                                                }
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        if (!request) {
                                            for (var i in yplist) {
                                                if (yplist[i].dm == newValue) {     //如果药品代码相同，则获取最低单价
                                                    $tbId.setCell(rowId, "a2", yplist[i].a13 ? yplist[i].a13 : null);
                                                    if (dp) { $tbId.setCell(rowId, "a5", yplist[i].a13 ? yplist[i].a13 : null); }
                                                    break;
                                                }
                                            }
                                        }

                                        //====================由于每选择一种药品单价会不同，所以必须重新计算金额==================

                                        //---------------------计算药品金额---------------

                                        var ypNum = $tbId.getCell(rowId, "a1");
                                        var ypPrice = $tbId.getCell(rowId, "a2");
                                        if (/^\d+(\.\d+)?$/.test(ypNum) && /^\d+(\.\d+)?$/.test(ypPrice)) {
                                            $tbId.setCell(rowId, "sunmoney", ypNum * ypPrice);
                                        } else {
                                            $tbId.setCell(rowId, "sunmoney", null);
                                        }

                                        //--------------------计算开票金额--------------
                                        if (dp) {
                                            var kpNum = $tbId.getCell(rowId, "a4");                                  //开票数量
                                            var kpPrice = $tbId.getCell(rowId, "a5");                                //开票价格
                                            if (!kpPrice) { $tbId.setCell(rowId, "a5", ypPrice); }
                                            kpPrice = $tbId.getCell(rowId, "a5");
                                            if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                                $tbId.setCell(rowId, "a6", kpNum * kpPrice);
                                            } else {
                                                $tbId.setCell(rowId, "a6", null);
                                            }
                                        }
                                    }
                                    sumGridTotal(tbId);
                                    changeZt(tbId, rowId);
                                },

                                //弹出添加药品对话框
                                onSelect: function (item) {
                                    if (item.dm == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { model: "Tspxx", jquery: $(this) });
                                    }
                                },
                                afterChange: function (newValue, newText) {
                                    if (newValue == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { model: "Tspxx", jquery: $(this) });
                                        ciho.showTypeDialog();
                                    }
                                }
                            });
                            break;
                        case "kpdwmc":

                            //---------------根据带票信息、开票下拉框是否可用----------------
                            var dp = $(this).getCell(rowId, "daipiao");
                            if (dp == "false" || !dp) { $(this).restoreCell(iRow, iCol); return; }

                            item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 });

                            //======================加载下拉数据======================
                            var kpdwlist = getComboboxData("Tkpdwxx");
                            kpdwlist.push({ "mc": "<font color='red'>添加收票单位</font>", "dm": "-1" });
                            item.combobox("loadData", kpdwlist);

                            //======================为combobox赋值====================
                            var kpdwdm = $(this).getCell(rowId, "kpdwdm");
                            item.combobox("setValue", kpdwdm);
                            if (item.combobox("getText") == kpdwdm) { item.combobox("setText", value); }

                            item.combobox('focus');
                            item.combobox({
                                onChange: function (newValue, oldValue) {
                                    var $tbId = $("#" + tbId);
                                    if (!newValue) { $tbId.setCell(rowId, "kpdwdm", null); }
                                    else {
                                        $tbId.setCell(rowId, "kpdwdm", newValue);

                                        //----------------------去后台请求抵扣税率--------------------
                                        var s = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"kpdwdm":"' + newValue + '","fb":"1"}'));
                                        if (s && s.ReturnCode == 0) { $tbId.setCell(rowId, "a8", s.dksl); }
                                        changeZt(tbId, rowId);
                                    }
                                },
                                onSelect: function (item) {
                                    if (item.dm == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "收票单位", table: "Tflxx" }, { model: "Tkpdwxx", jquery: $(this) });
                                        $(this).combobox("setValue", "");
                                    }
                                },
                                afterChange: function (newValue, newText) {
                                    if (newValue == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "收票单位", table: "Tflxx" }, { model: "Tkpdwxx", jquery: $(this) });
                                        ciho.showTypeDialog();
                                        $(this).combobox("setValue", "");
                                    }
                                }
                            });
                            break;
                        case "daipiao":            //是否带票下拉框
                            item.combobox({
                                valueField: "dm",
                                textField: "mc",
                                width: _iWidth80,
                                panelHeight: 42,
                                editable: false,
                                onSelect: function (option) {
                                    var $tbId = $("#" + tbId);
                                    if (option.dm == "true") {
                                        var data = $("#" + dlgId).data("data");

                                        //------------------设置供应商信息-----------------
                                        var $gys = $("#gysdm", "#" + dlgId);
                                        var gysdm = $gys.combobox("getValue");
                                        var gysmc = $gys.combobox("getText");
                                        if (gysmc == data.gysmc) { gysdm = data.gysdm; }

                                        var ypdm = $tbId.getCell(rowId, "spdm");

                                        var request = false;

                                        if (ypdm && gysdm) {
                                            var s = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"spdm":"' + ypdm + '","gysdm":"' + gysdm + '","fb":"2"}'));
                                            if (s && s.ReturnCode == 0 && s.Records instanceof Array && s.Records.length > 0) {
                                                request = true;

                                                //为开票单价赋值
                                                $tbId.setCell(rowId, "a5", s.Records[0].a2 ? s.Records[0].a2 : null);                                                                    //把单价带出来
                                                $tbId.setCell(rowId, "kpdwdm", s.Records[0].kpdwdm);                                                                         //开票单位代码赋值


                                                var ss = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", "")); //开票单位信息
                                                if (ss && ss.ReturnCode == 0 && ss.Records instanceof Array) {
                                                    for (var i in ss.Records) {
                                                        if (ss.Records[i].dm == s.Records[0].kpdwdm) {
                                                            $tbId.setCell(rowId, "kpdwdm", ss.Records[i].dm);                    //开票单位代码
                                                            $tbId.setCell(rowId, "kpdwmc", ss.Records[i].mc);                    //开票单位名称
                                                            // $tbId.setCell(rowId, "a6", ss.Records[i].kpsl);                      //开票税率
                                                            var sss = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"kpdwdm":"' + s.Records[0].kpdwdm + '","fb":"1"}'));
                                                            if (sss && sss.ReturnCode == 0) {                                         //保存代扣税率
                                                                $tbId.setCell(rowId, "a8", sss.dksl);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        var ypNum = $tbId.getCell(rowId, "a1");
                                        var ypPrice = $tbId.getCell(rowId, "a2");

                                        $tbId.setCell(rowId, "a4", ypNum);
                                        if (!request) {
                                            $tbId.setCell(rowId, "a5", ypPrice);
                                        }

                                        var kpNum = $tbId.getCell(rowId, "a4");                  //开票数量
                                        var kpPrice = $tbId.getCell(rowId, "a5");                  //开票价格
                                        if (!kpPrice) { $tbId.setCell(rowId, "a5", ypPrice); }
                                        kpPrice = $tbId.getCell(rowId, "a5");                  //开票价格
                                        if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                            $tbId.setCell(rowId, "a6", kpNum * kpPrice);                  //开票价格
                                        } else {
                                            $tbId.setCell(rowId, "a6", null);                  //开票价格
                                        }
                                    } else {
                                        $tbId.setCell(rowId, "a4", null);
                                        $tbId.setCell(rowId, "a5", null);
                                        $tbId.setCell(rowId, "a6", null);
                                        $tbId.setCell(rowId, "kpdwmc", null);
                                        $tbId.setCell(rowId, "kpdwdm", null);
                                        $tbId.setCell(rowId, "a8", null);               //税率
                                    }
                                    $tbId.saveCell(iRow, iCol);
                                    sumGridTotal(tbId);
                                    changeZt(tbId, rowId);                          //改变状态该行的状态
                                }
                            });
                            var dplist = [{ dm: "true", mc: "是" }, { dm: "false", mc: "否"}];
                            item.combobox("loadData", dplist);
                            item.combobox("setValue", value);
                            item.combobox('focus');
                            break;
                        case "a1":                   //药品数量文本框
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                        case "a4":                   //开票数量文本框
                            var dp = $(this).getCell(rowId, "daipiao");
                            if (dp == "false" || !dp) { $(this).restoreCell(iRow, iCol); return; }
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                        case "a2":                  //药品价格文本框
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                        case "a5":                   //开票价格文本框
                            var dp = $(this).getCell(rowId, "daipiao");
                            if (dp == "false" || !dp) { $(this).restoreCell(iRow, iCol); return; }
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                    }
                    $(".layoutpanel").css("z-index", "50000009");
                } catch (e) { }
            },
            afterSaveCell: function (rowId, cellname, value, iRow, iCol) {
                try {
                    switch (cellname) {
                        case "a1":                  //药品数量文本框
                        case "a2":                  //药品价格文本框

                            //==========================计算药品金额=======================
                            var ypNum = $(this).getCell(rowId, "a1");
                            var ypPrice = $(this).getCell(rowId, "a2");

                            if (/^\d+(\.\d+)?$/.test(ypNum) && /^\d+(\.\d+)?$/.test(ypPrice)) {
                                $(this).setCell(rowId, "sunmoney", ypNum * ypPrice);
                            } else {
                                $(this).setCell(rowId, "sunmoney", null);
                            }

                            var dp = $("#" + tbId).getCell(rowId, "daipiao") == "true" ? true : false;
                            if (dp) {
                                var kpNum = $(this).getCell(rowId, "a4");
                                var kpPrice = $(this).getCell(rowId, "a5");

                                $(this).setCell(rowId, "a4", kpNum ? kpNum : ypNum);

                                $(this).setCell(rowId, "a5", kpPrice ? kpPrice : ypPrice);

                                var kpNum = $(this).getCell(rowId, "a4");
                                var kpPrice = $(this).getCell(rowId, "a5");
                                if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                    $(this).setCell(rowId, "a6", kpNum * kpPrice);
                                } else {
                                    $(this).setCell(rowId, "a6", null);
                                }
                            }

                            sumGridTotal(tbId);
                            break;
                        case "a4":                   //开票数量文本框
                        case "a5":                   //开票价格文本框

                            //======================计算开票金额======================

                            var kpNum = $(this).getCell(rowId, "a4");
                            var kpPrice = $(this).getCell(rowId, "a5");
                            if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                $(this).setCell(rowId, "a6", kpNum * kpPrice);               //开票价格
                            } else {
                                $(this).setCell(rowId, "a6", null);
                            }
                            sumGridTotal(tbId);
                            break;
                    }
                } catch (e) { }
            }
        });
    }
    addColFormater(tbId);
    $("#" + tbId).data("records", []);
    bindDelEvent(tbId);
    $("#" + tbId).setGridHeight(141);
    //$("#gbox_" + tbId).css("height", "157px");
}

function addColFormater(tbId) {
    var $tbId = $("#" + tbId);
    $tbId.setColProp("a1", { formatter: formatCountPlace });
    $tbId.setColProp("a5", { formatter: formatPricePlace });
    $tbId.setColProp("a2", { formatter: formatPricePlace });
    $tbId.setColProp("a4", { formatter: formatCountPlace });
    $tbId.setColProp("sunmoney", { formatter: formatMoneyPlace });
    $tbId.setColProp("a6", { formatter: formatMoneyPlace });
    $tbId.setColProp("spmc", { editoptions: { custom_element: my_input, custom_value: my_value} });
    $tbId.setColProp("kpdwmc", { editoptions: { custom_element: my_input, custom_value: my_value} });
    $tbId.setColProp("daipiao", { unformat: unformatIs, formatter: formatIs, editoptions: { custom_element: my_input, custom_value: my_value} });
    $tbId = null;
}

function initInputsProp(tbId, dlgId) {
    //为采购日期、付款日期绑定日期控件
    inputAndBindData("cgtime", false, "#" + dlgId);
    inputAndBindData("sfkdate", false, "#" + dlgId);

    var $dlgId = $("#" + dlgId);

    //设置控件外观宽度
    $("#a9", $dlgId).parent().addClass("bz");
    $("#cgtime,#sfkdate", $dlgId).parent().addClass("inp_70");

    //为combobox下拉框加载选项。并注册事件
    $("#kmdm", $dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth80,
        onSelect: function (option) {
            var $dlgId = $("#" + dlgId);
            $("#" + tbId).saveCell(cellX, cellY);
            var $jiner = $("#jiner", $dlgId);
            var $yh = $("#a11", $dlgId);
            var $sjkdate = $("#sfkdate", $dlgId);
            var cgtime = $("#cgtime", $dlgId).val();
            if (option.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "付款账户", table: "Tflxx" }, { model: "Tkjkm", jquery: $(this) });
                $(this).combobox("setValue", "");
            } else if (option.dm) {                     //如果选择了付款账户
                if (!$jiner.val() * 1) {
                    var yh = isNaN($yh.val()) ? 0 : $yh.val() * 1;
                    var sfje = $("#" + tbId).footerData("get").sunmoney - yh;
                    $jiner.val(sfje.toFixed(window.parent.strMoneyPlace));
                }
                if (!$sjkdate.val()) {
                    if (checkDatePattern(cgtime)) {
                        $sjkdate.val(cgtime);
                    }
                }
            } else {
                $jiner.val(""); $sjkdate.val("");
            }
            changeZt(tbId);
            $jiner = $yh = $sjkdate = null;
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "付款账户", table: "Tflxx" }, { model: "Tkjkm", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            }
        }
    });
    $("#ckdm", $dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth150,
        onSelect: function (item) {
            if (item.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "仓库", table: "Tflxx" }, { model: "TCkxx", jquery: $(this) });
                $(this).combobox("setValue", "");
            }
            changeZt(tbId);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "仓库", table: "Tflxx" }, { model: "TCkxx", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            }
        }
    });
    $("#zydm", $dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth150,
        onSelect: function (item) {
            if (item.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "经手人", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                $(this).combobox("setValue", "");
            }
            changeZt(tbId);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "经手人", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            }
        }
    });
    $("#gysdm", $dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth150,
        onSelect: function (item) {
            if (item.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "供应商", table: "Tflxx" }, { model: "Tgysxx", jquery: $(this) });
                $(this).combobox("setValue", "");
            }
            changeZt(tbId);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "供应商", table: "Tflxx" }, { model: "Tgysxx", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            }
        }
    });

    //设置制单人为不可编辑
    $("#czydm", $dlgId).attr("disabled", true);

    //为优惠、实付金额绑定数字控件
    $("#a11,#jiner", $dlgId).css("ime-mode", "disabled").autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "", mNum: 9 });

    //为实付金额、优惠、备注、采购日期、付款日期 注册事件
    $("#jiner,#a11,#a9,#cgtime,#sfkdate", $dlgId).change(function () { changeZt(tbId); }).focus(function () { $(this).select(); });

    $dlgId = null;
}

//加载combobox下拉选项
function loadComboData(dlgId) {
    var $dlgId = $("#" + dlgId);
    var zylist = getComboboxData("TZyxx");
    var cklist = getComboboxData("TCkxx");
    var kmlist = getComboboxData("TKjkm");
    var gyslist = getComboboxData("Tgysxx");
    zylist.push({ "mc": "<font color='red'>添加经手人</font>", "dm": "-1" });
    cklist.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
    gyslist.push({ "mc": "<font color='red'>添加供应商</font>", "dm": "-1" });
    kmlist.push({ "mc": "<font color='red'>添加付款账户</font>", "dm": "-1" });
    $("#kmdm", $dlgId).combobox("loadData", kmlist);
    $("#kmdm", $dlgId).combobox({ panelHeight: getComHeight(kmlist) });
    $("#zydm", $dlgId).combobox("loadData", zylist);
    $("#ckdm", $dlgId).combobox("loadData", cklist);
    if (cklist.length == 2) {
        $("#ckdm", $dlgId).combobox("setValue", cklist[0].dm);
    }
    $("#gysdm", $dlgId).combobox("loadData", gyslist);
    $dlgId = null;
}


//创建窗口input元素和grid
function createElements(tbId, dlgId) {
    var $dlgId = $("#" + dlgId);

    //后台获取grid配置信息
    var config = ParseData(window.external.DoOptions(tbId, "GetTableConfig", "{}"));

    //生成头部的表单元素
    var header = createInputs(config.pageHeader);

    //生成脚部的表单元素
    var footer = createInputs(config.pageFooter);

    //将头部、grid占位符、脚部添加到采购单中
    $("#" + dlgId + "_divform").append(header + '<table id="' + tbId + '"></table>' + footer);

    //在grid占位符上创建grid
    createDjGrid(tbId, config.colNames, config.colModel);

    //根据列配置信息，查询出显示并且可编辑的列名称，并保存到grid上
    var vCols = [];
    for (var i in config.colModel) {
        if (config.colModel[i].editable && !config.colModel[i].hidden) {
            vCols.push(config.colModel[i].index);
        }
    }
    $("#" + tbId).data("vCols", vCols);
    $("#" + tbId).setLabel("rn", "序号");
    $(".ui-jqgrid-htable th:visible:last", $dlgId).html('<img id="btnAdd"  style="cursor:pointer"  title="添加行" src="../../Content/Default/images/edit_add.gif" onclick="addNewRow(\'' + tbId + '\');return false; "/>');
    $dlgId = null;
}

//创建单据grid
function createDjGrid(tbId, cNames, cModel) {
    $("#" + tbId).jqGrid({
        datatype: "local",
        // height: "100%",
        autowidth: true,
        colNames: cNames,
        colModel: cModel,
        rownumbers: true,
        footerrow: true,
        sortable: false,
        rownumWidth: 30,
        cellEdit: true,
        cellsubmit: "clientArray"
    });
}

//加载窗口grid数据，并计算合计
function loadGridData(tbId, mylist) {
    var count = mylist.length > 5 ? mylist.length : 5;
    var ypsl = 0, ypje = 0, kpsl = 0, kpje = 0, $tbId = $("#" + tbId);
    for (var i = 0; i < count; i++) {
        if (mylist[i]) {                                                                         //添加后台数据行
            ypsl += mylist[i].a1 * 1;
            ypje += mylist[i].sunmoney * 1;
            if (mylist[i].daipiao == "是") {                                         //计算带票合计
                kpsl += mylist[i].a4 * 1;
                kpje += mylist[i].a6 * 1;
            }
            $tbId.addRowData(i + 1, mylist[i]);      //添加后台数据行
        }
        else {                                                                                     //添加空行
            $tbId.addRowData(i + 1, {});
        }
    }
    $tbId.footerData("set", { rn: "合计", a1: ypsl, sunmoney: ypje, a4: kpsl, a6: kpje }, true);
    $tbId.data("nextNum", count + 1);
    resizeGridScroll(tbId);
    $tbId = null;
}

//strType：1，保存退出
//strType：2，保存新增

function afterSaveClick(dlgId, afterSave) {
    try {
        var $tbId = $("#" + strTableP010101);
        var isChange = $("#" + dlgId + "_divform").data("change");
        $tbId.saveCell(cellX, cellY);
        if ($("#" + dlgId).data("state") == 2 && isChange) {
            var ids = $tbId.getDataIDs();
            for (var i = 0; i < ids.length; i++) {
                var item = $tbId.getRowData(ids[i]);
                if (item.glsl * 1 && (item.a1 * 1 != item.glsl * 1)) {
                    window.parent.cihoAlert(msg.other.pihaoNoSame, msgBox.dlgBtn.Ok, function () {
                        if (!authMyBillsP010101(dlgId, afterSave)) { return false; }
                        saveMyDataTp010101(dlgId, afterSave);
                    });
                    return;
                }
            }
            if (!authMyBillsP010101(dlgId, afterSave)) { return false; }
            saveMyDataTp010101(dlgId, afterSave);
        } else {
            if (!authMyBillsP010101(dlgId, afterSave)) { return false; }
            saveMyDataTp010101(dlgId, afterSave);
        }
    } catch (e) { }
}

//保存单据，
//afterSave：1：保存退出
//                  2：保存增加
function saveMyDataTp010101(dlgId, afterSave) {
    try {
        var tbId = strTableP010101;
        var dlgId = strDivP010101;
        var $tbId = $("#" + tbId);
        var $dlgId = $("#" + dlgId);

        var ids = $tbId.getDataIDs();
        var footerData = $tbId.footerData("get");

        //用来存放点击删除按钮后的行信息，只有删除信息
        var records = $tbId.data("records");

        var state = $dlgId.data("state");
        var data = $dlgId.data("data");
        var vCols = $tbId.data("vCols");
        var count = 0;
        var kp = false;
        var spdms = [];

        //用来存放grid中的行，也可能包含删除信息
        var rows = [];

        for (var i = 0; i < ids.length; i++) {
            var item = $tbId.getRowData(ids[i]);
            if (item.spdm) {
                spdms.push(item.spdm);
            }
            if (item.zt) {                                  //只有修改过或新增的数据保存到数据库    2：修改，1：新增，3：删除
                var rowEmpty = isRowEmpty(item, vCols);
                if (!rowEmpty) {
                    count++;
                    var dp, zt;
                    if (item.daipiao == "false") {
                        item.a4 = item.a5 = item.a6 = item.kpdwdm = "";
                    } else {
                        kp = true;
                    }
                    if (state == 2) { zt = ',"zt":"' + item.zt + '"'; } else { zt = ""; }
                    var record = '{"mxguid":"' + item.mxguid + '","spdm":"' + item.spdm + '","a1":"' + item.a1 + '","a2":"' + item.a2 + '","a4":"' + item.a4 + '","a5":"' + item.a5 + '","a8":"' + item.a8 + '","a6":"' + item.a6 + '","kpdwdm":"' + item.kpdwdm + '","a3":"' + item.daipiao + '"' + zt + '}';
                    rows.push(record);
                } else if (item.mxguid) {
                    rows.push('{"mxguid":"' + item.mxguid + '","zt":"3"}');
                }
            }
            //item = null;
        }
        if (count == 0) {
            window.parent.cihoAlert(msg.cg.cgdjNotEmp, msgBox.dlgBtn.Ok);
            // $tbId = $dlgId = ids = footerData = records = data = spdms = null;
            return false;
        }

        var inputs = [];
        var $gys = $("#gysdm", $dlgId);
        var $zy = $("#zydm", $dlgId);
        var $km = $("#kmdm", $dlgId);
        var $ck = $("#ckdm", $dlgId);
        var yh = $("#a11", $dlgId).val();

        inputs.push('"a7":"' + kp + '"');
        inputs.push('"djtypebh":"101"');

        if ($gys.combobox("getValue")) {
            inputs.push('"gysdm":"' + $gys.combobox("getValue") + '"');
        } else if ($gys.combobox("getText") == data.gysmc) {
            inputs.push('"gysdm":"' + data.gysdm + '"');
        }

        if ($zy.combobox("getValue")) {
            inputs.push('"zydm":"' + $zy.combobox("getValue") + '"');
        } else if ($zy.combobox("getText") == data.zymc) {
            inputs.push('"zydm":"' + data.zydm + '"');
        }

        if ($ck.combobox("getValue")) {
            inputs.push('"ckdm":"' + $ck.combobox("getValue") + '"');
        } else if ($ck.combobox("getText") == data.ckmc) {
            inputs.push('"ckdm":"' + data.ckdm + '"');
        }

        if ($km.combobox("getValue")) {
            inputs.push('"kmdm":"' + $km.combobox("getValue") + '"');
            inputs.push('"a11":"' + yh + '"');                                               //优惠
        } else if ($km.combobox("getText") == data.kmmc) {
            inputs.push('"kmdm":"' + data.kmdm + '"');
            inputs.push('"a11":"' + yh + '"');
        } else {
            inputs.push('"a11":""');
        }

        var djdate = $("#cgtime", $dlgId).val();

        inputs.push('"djdate":"' + djdate + '"');
        inputs.push('"sfkdate":"' + $("#sfkdate", $dlgId).val() + '"');
        inputs.push('"a5":"' + $("#jiner", $dlgId).val() + '"');                         //付款金额
        inputs.push('"a9":"' + trim($("#a9", $dlgId).val()) + '"');                            //备注

        inputs.push('"a1":"' + spdms.join("\\\\") + '"');
        inputs.push('"a8":"' + footerData.a6 + '"');
        inputs.push('"a2":"' + footerData.a1 + '"');
        inputs.push('"a4":"' + footerData.sunmoney + '"');

        if (state == 1) {//新增
            inputs.push('"spdm":"' + spdms[0] + '"');
            inputs.push('"djbh":"' + djdate + '"');
            inputs.push('"czydm":"' + window.parent.objLoginInfo.code + '"');
        } else if (state == 2) {//修改
            inputs.push('"djbh":"' + objSelRowP0101.djbh + '"');
            inputs.push('"czydm":"' + data.czydm + '"');
        }

        var jsonStr = '{' + inputs + ',"Records":[' + rows.concat(records) + ']}';

        var s;
        if (state == 1) {//新增
            s = ParseData(window.external.DoOptions(tbId, "Add", jsonStr));
        } else if (state == 2) {//修改
            s = ParseData(window.external.DoOptions(tbId, "Edit", jsonStr));
        }

        if (s.ReturnCode == 0) {
            if (state == 2) {
                if (compareData != getCompareData(tbId, cols)) {
                    var dodata = '{"fb":"3","djdate":"' + djdate + '"}';
                    window.external.DoOptions(tbId, "DoOthers", dodata);
                }
            }
            if (afterSave == 2) {   //保存新增
                changeGridState(tbId, dlgId);
            } else {                //保存退出
                cihoDlgClose(dlgId);
            }
            loadGridData_P0101();
        }
        else {
            window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
        }
        // s = $tbId = $dlgId = $ck = $gys = $zy = $km = ids = footerData = records = data = spdms = inputs = null;
    } catch (e) { }
}

function changeGridState(tbId, dlgId) {
    try {
        var $tbId = $("#" + tbId);
        var $dlgId = $("#" + dlgId);

        $tbId.clearGridData();
        $tbId.data("records", []);
        $dlgId.data("data", {});
        $dlgId.data("state", 1);
        $("#" + dlgId + "_divform").data("change", false);
        $("#idMust", $dlgId).text("");
        $("#cgtime", $dlgId).attr("disabled", false).val(getNowDate());
        $("#czydm", $dlgId).attr("disabled", true).val(window.parent.objLoginInfo.name);
        $("#kmdm,#zydm,#gysdm", $dlgId).combobox("setValue", "");
        var cklist = $("#ckdm", $dlgId).combobox("getData");
        if (cklist.length == 2) {
            $("#ckdm", $dlgId).combobox("setValue", cklist[0].dm);
        } else {
            $("#ckdm", $dlgId).combobox("setValue", "");
        }
        $("#jiner,#a11,#a9,#sfkdate", $dlgId).val("");
        loadGridData(tbId, []);

        setTimeout('$("#a11", "#" + strDivP010101).focus();', 100);
    } catch (e) { }
}

//保存单据，
//afterSave：1：保存退出
//                  2：保存增加
function authMyBillsP010101(dlgId, afterSave) {
    var $tbId = $("#" + strTableP010101);
    var vCols = $tbId.data("vCols");
    var sunmoney = $tbId.footerData("get").sunmoney * 1;
    var kpje = $tbId.footerData("get").kpje * 1;
    var maxAmount = window.top._MaxPerBill;
    if (sunmoney > maxAmount || kpje > maxAmount) {
        window.parent.cihoAlert(msg.other.djhjExtent, msgBox.dlgBtn.Ok);        //alert("单据金额过大！");
        //$tbId = null;
        return false;
    }

    var $dlgId = $("#" + dlgId);

    var data = $dlgId.data("data");
    var $gys = $("#gysdm", $dlgId);
    var gysdm = $gys.combobox("getValue");
    var gysmc = $gys.combobox("getText");
    if (!gysdm && gysmc != data.gysmc) {
        window.parent.cihoAlert(msg.sz.gys.nameEmp, msgBox.dlgBtn.Ok, function () {
            $("#gysdm", "#" + dlgId).combobox('focus');
            // $tbId = $gys = data = null;
        });          //alert("请选择供应商!");
        $tbId = $gys = data = null;
        return false;
    }

    var $zy = $("#zydm", $dlgId);
    var zydm = $zy.combobox("getValue");
    var zymc = $zy.combobox("getText");
    if (!zydm && zymc != data.zymc) {
        window.parent.cihoAlert(msg.sz.jsr.empOrNotExit, msgBox.dlgBtn.Ok, function () {
            $("#zydm", "#" + dlgId).combobox('focus');
            //$tbId = $gys = $zy = data = null;
        });          //alert("请选择经手人！");
        $tbId = $gys = $zy = data = null;
        return false;
    }

    var $ck = $("#ckdm", $dlgId);
    var ckdm = $ck.combobox("getValue");
    var ckmc = $ck.combobox("getText");
    if (!ckdm && ckmc != data.ckmc) {
        window.parent.cihoAlert(msg.sz.ck.empOrNotExit, msgBox.dlgBtn.Ok, function () {
            $("#ckdm", "#" + dlgId).combobox('focus');
            //$tbId = $gys = $zy = $ck = data = null;
        });
        $tbId = $gys = $zy = $ck = data = null;
        return false;
    }

    var $cg = $("#cgtime", $dlgId);
    var cgtime = $cg.val();
    if (!cgtime || !checkDatePattern(cgtime)) {
        window.parent.cihoAlert(msg.cg.inputCgDt, msgBox.dlgBtn.Ok, function () {
            $("#cgtime", "#" + dlgId).select();
            // $tbId = $gys = $zy = $ck = $cg = data = null;
        });        //alert("请输入采购日期！");
        $tbId = $gys = $zy = $ck = $cg = data = null;
        return false;
    } else {
        var cgdate = cgtime.replace(/\-/g, "/");
        if (cgdate < window.top._startDate) {
            window.parent.cihoAlert(msg.other.cgrqExtent, null, function () {
                $("#cgtime", "#" + dlgId).select();
                //$tbId = $gys = $zy = $ck = $cg = data = null;
            });                               //alert("采购日期范围不对！");
            $tbId = $gys = $zy = $ck = $cg = data = null;
            return false;
        }
    }

    var $czy = $("#czydm", $dlgId);
    if (!$czy.val()) {
        window.parent.cihoAlert(msg.other.inputZdr, msgBox.dlgBtn.Ok, function () {
            $("#czydm", "#" + dlgId).select();
            //$tbId = $gys = $zy = $ck = $cg = $czy = data = null;
        });        //        alert("请输入制单人！");
        $tbId = $gys = $zy = $ck = $cg = $czy = data = null;
        return false;
    }

    var ids = $tbId.getDataIDs();
    for (var i = 0; i < ids.length; i++) {
        var item = $tbId.getRowData(ids[i]);
        if (item.zt && !isRowEmpty(item, vCols)) {              //只遍历有修改的行
            if (!item.spdm) {
                window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.yp.lnEmpOrNotExit, msgBox.dlgBtn.Ok);
                return false;
            }
            if (item.a1 * 1 <= 0) {
                window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.yp.lnAmountEmp, msgBox.dlgBtn.Ok);
                return false;
            }
            if (item.a2 * 1 <= 0) {
                window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.yp.lnPriceEmp, msgBox.dlgBtn.Ok);
                return false;
            }
            if (item.daipiao == "true") {
                if (item.a4 * 1 <= 0) {
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.fp.lnAmountEmp, msgBox.dlgBtn.Ok);
                    //                    alert("请输入第【" + (i + 1) + "】行的开票数量！");
                    return false;
                }
                if (item.a5 * 1 <= 0) {
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.fp.lnPriceEmp, msgBox.dlgBtn.Ok);
                    //                    alert("请输入第【" + (i + 1) + "】行的开票单价！");
                    return false;
                }
                if (!item.kpdwdm) {
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.kp.SpNoEmpty, msgBox.dlgBtn.Ok);
                    //alert("请输入第【" + (i + 1) + "】行的开票单价！");
                    return false;
                }
            }
        }
        //item = null;
    }

    var $km = $("#kmdm", $dlgId);
    var $sfkdate = $("#sfkdate", $dlgId);
    var $jiner = $("#jiner", $dlgId);

    var yh = $("#a11", $dlgId).val() * 1;                                         //优惠
    var jiner = $jiner.val();                                                 //实付金额
    var sfkdate = $sfkdate.val();

    var kmdm = $km.combobox("getValue");                                          //付款账户
    var kmmc = $km.combobox("getText");

    if (kmdm || kmmc || sfkdate || jiner || yh) {
        if (!jiner) {
            window.parent.cihoAlert(msg.other.inputFkje, msgBox.dlgBtn.Ok, function () {
                $("#jiner", "#" + dlgId).select();
            });
            $tbId = $gys = $zy = $ck = $cg = $czy = $km = $sfkdate = $jiner = ids = data = null;
            return false;
        }
        if (!sfkdate) {
            window.parent.cihoAlert(msg.dt.inputFkDt, msgBox.dlgBtn.Ok, function () {
                $("#sfkdate", "#" + dlgId).select();
            });
            $tbId = $gys = $zy = $ck = $cg = $czy = $km = $sfkdate = $jiner = ids = data = null;
            return false;
        } else {
            var fkdate = sfkdate.replace(/\-/g, "/");
            if (fkdate < window.top._startDate) {
                window.parent.cihoAlert(msg.other.fkrqExtent, msgBox.dlgBtn.Ok, function () {
                    $("#sfkdate", "#" + dlgId).select();
                });
                $tbId = $gys = $zy = $ck = $cg = $czy = $km = $sfkdate = $jiner = ids = data = null;
                return false;
            }
        }
        if (!kmdm && kmmc != data.kmmc) {
            window.parent.cihoAlert(msg.other.inputFkzh, null, function () {
                $("#kmdm", "#" + dlgId).combobox('focus');
            });
            return false;
        }
        if (jiner * 1 < 0) {
            window.parent.cihoAlert(msg.other.inputSfjeLessZero, msgBox.dlgBtn.Ok);
            return false;
        }
        var sfAddYh = yh + jiner * 1;                       //优惠
        var yf = $tbId.footerData("get").sunmoney * 1;      //应付
        if (sfAddYh > yf) {//"实付金额加优惠大于应付金额！"
            window.parent.cihoAlert(msg.other.sfjeAddYfjeMor, msgBox.dlgBtn.Ok, function () {
                $("#jiner", "#" + dlgId).select();
            });
            $tbId = $gys = $zy = $ck = $cg = $czy = $km = $sfkdate = $jiner = ids = data = null;
            return false;
        }

        //付款金额不一致
        if (window.parent.bShowMsgOfMoney && sfAddYh != yf) {
            window.parent.cihoAlert(msg.other.difMoney, msgBox.dlgBtn.YesNo, function (result) {
                if (result == msgBox.dlgRst.Yes) {
                    saveMyDataTp010101(dlgId, afterSave)
                }
            });
            $tbId = $gys = $zy = $ck = $cg = $czy = $km = $sfkdate = $jiner = ids = data = null;
            return false;
        }
    }
    return true;
}


function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}
function formatPricePlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strPricePlace);
    }
    else {
        return "&nbsp;";
    }
}
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}
function formatIs(cellvalue, options, row) {
    if (!cellvalue) { return "&nbsp;" } else { return cellvalue; }
}
function unformatIs(cellvalue, options) {
    if (cellvalue == "是") { return "true"; } else if (cellvalue == "否") { return "false"; } else { return ""; }
}
function my_input(value, options) {
    return $('<select name="' + options.name + '" id="' + options.id + '"></select>').addClass("easyui-combobox");
}
function my_value(ele) {
    return ele.combobox('getText');
}
function isRowEmpty(row, cols) {
    for (var i = 0; i < cols.length; i++) {
        if (!/^\s*$/.test(row[cols[i]])) {
            return false;
        }
    }
    return true;
}