///<reference path="/Scripts/ExtPB_Net.js" />
///<reference path="/Scripts/MyExtPB.js" />
///<reference path="/Scripts/ExtPB_Dll_Pb_Http.js" />
///<reference path="/Scripts/MyExtPb_Input.js" />

function W_YuKaiPiaoTongJi() {

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var tab_1 = new TabControl(requestor);
    var sle_zdbm = new SingleLineEdit(requestor);
    var sle_zdr = new SingleLineEdit(requestor);
    var ddlb_wczt = new DropDownListBox(requestor);
    var ddlb_kplx = new DropDownListBox(requestor);
    var ddlb_pjzl = new DropDownListBox(requestor);
    var sle_fphm = new SingleLineEdit(requestor);
    var sle_kpje = new SingleLineEdit(requestor);
    var sle_jbr = new SingleLineEdit(requestor);
    var dp_2 = new DatePicker(requestor);
    var sle_wldw = new SingleLineEdit(requestor);
    var dp_1 = new DatePicker(requestor);
    var dw_1 = new DataWindow(requestor);
    var dw_mkfhjb = new DataWindow(requestor);
    var dw_2 = new DataWindow(requestor);
    var dw_3 = new DataWindow(requestor);
    var tb_1 = new ToolStrip(requestor);

    if (typeof (this.bind) == "function") var baseBind = this.bind;
    this.bind = function (win) {
        self.Instance = win;
        if (typeof (baseBind) == "function") baseBind(win);

        if (requestor == null) requestor = new PBWindow();
        requestor.Attach(win);
        if (tab_1 == null) tab_1 = new TabControl(requestor);
        tab_1.Attach(win.Control("tab_1"));
        if (sle_zdbm == null) sle_zdbm = new SingleLineEdit(requestor);
        sle_zdbm.Attach(win.Control("sle_zdbm"));
        if (sle_zdr == null) sle_zdr = new SingleLineEdit(requestor);
        sle_zdr.Attach(win.Control("sle_zdr"));
        if (ddlb_wczt == null) ddlb_wczt = new DropDownListBox(requestor);
        ddlb_wczt.Attach(win.Control("ddlb_wczt"));
        if (ddlb_kplx == null) ddlb_kplx = new DropDownListBox(requestor);
        ddlb_kplx.Attach(win.Control("ddlb_kplx"));
        if (ddlb_pjzl == null) ddlb_pjzl = new DropDownListBox(requestor);
        ddlb_pjzl.Attach(win.Control("ddlb_pjzl"));
        if (sle_fphm == null) sle_fphm = new SingleLineEdit(requestor);
        sle_fphm.Attach(win.Control("sle_fphm"));
        if (sle_kpje == null) sle_kpje = new SingleLineEdit(requestor);
        sle_kpje.Attach(win.Control("sle_kpje"));
        if (sle_jbr == null) sle_jbr = new SingleLineEdit(requestor);
        sle_jbr.Attach(win.Control("sle_jbr"));
        if (dp_2 == null) dp_2 = new DatePicker(requestor);
        dp_2.Attach(win.Control("dp_2"));
        if (sle_wldw == null) sle_wldw = new SingleLineEdit(requestor);
        sle_wldw.Attach(win.Control("sle_wldw"));
        if (dp_1 == null) dp_1 = new DatePicker(requestor);
        dp_1.Attach(win.Control("dp_1"));
        if (dw_1 == null) dw_1 = new DataWindow(requestor);
        dw_1.Attach(win.Control("dw_1"));
        if (dw_mkfhjb == null) dw_mkfhjb = new DataWindow(requestor);
        dw_mkfhjb.Attach(win.Control("dw_mkfhjb"));
        if (dw_2 == null) dw_2 = new DataWindow(requestor);
        dw_2.Attach(win.Control("dw_2"));
        if (dw_3 == null) dw_3 = new DataWindow(requestor);
        dw_3.Attach(win.Control("dw_3"));
        if (tb_1 == null) tb_1 = new ToolStrip(requestor);
        tb_1.Attach(win.Control("tb_1"));

        if (typeof (self.PostOpen) == "function")
            win.AttachEvent("window", "PostOpen", self.PostOpen);

        if (typeof (self.Zdbm) == "function")
            win.AttachEvent("cb_zdbm", "Clicked", self.Zdbm);

        if (typeof (self.Zdr) == "function")
            win.AttachEvent("cb_zdr", "Clicked", self.Zdr);

        if (typeof (self.Jbr) == "function")
            win.AttachEvent("cb_jbr", "Clicked", self.Jbr);

        if (typeof (self.Wldw) == "function")
            win.AttachEvent("cb_wldw", "Clicked", self.Wldw);

        if (typeof (self.dw_DoubleClicked) == "function")
            win.AttachEvent("dw_1", "DoubleClicked", self.dw_DoubleClicked);

        if (typeof (self.Hz_RowFocusChanged) == "function")
            win.AttachEvent("dw_1", "RowFocusChanged", self.Hz_RowFocusChanged);

        if (typeof (self.dw_2_doubleClicked) == "function")
            win.AttachEvent("dw_2", "DoubleClicked", self.dw_2_doubleClicked);

        if (typeof (self.dw_2_editChanged) == "function")
            win.AttachEvent("dw_2", "EditChanged", self.dw_2_editChanged);

        if (typeof (self.dw_2_itemChanged) == "function")
            win.AttachEvent("dw_2", "ItemChanged", self.dw_2_itemChanged);

        if (typeof (self.dw_3_DropDown) == "function")
            win.AttachEvent("dw_3", "DropDown", self.dw_3_DropDown);

        if (typeof (self.dw_3_editChanged) == "function")
            win.AttachEvent("dw_3", "EditChanged", self.dw_3_editChanged);

        if (typeof (self.dw_3_itemChanged) == "function")
            win.AttachEvent("dw_3", "ItemChanged", self.dw_3_itemChanged);

        if (typeof (self.dw_3_itemFocusChange) == "function")
            win.AttachEvent("dw_3", "ItemFocusChanged", self.dw_3_itemFocusChange);

        if (typeof (self.dw_3_keyDown) == "function")
            win.AttachEvent("dw_3", "KeyDown", self.dw_3_keyDown);

        if (typeof (self.Query) == "function")
            win.AttachEvent("btn_Query", "Clicked", self.Query);

        if (typeof (self.PostOpen) == "function")
            win.AttachEvent("btn_Clear", "Clicked", self.PostOpen);

        if (typeof (self.Audit) == "function")
            win.AttachEvent("btn_Audit", "Clicked", self.Audit);

        if (typeof (self.KaiPiao) == "function")
            win.AttachEvent("btn_KaiPiao", "Clicked", self.KaiPiao);

        if (typeof (self.YEdit) == "function")
            win.AttachEvent("btn_YEdit", "Clicked", self.YEdit);

        if (typeof (self.Edit) == "function")
            win.AttachEvent("btn_Edit", "Clicked", self.Edit);

        if (typeof (self.InsertRow) == "function")
            win.AttachEvent("btn_InsertRow", "Clicked", self.InsertRow);

        if (typeof (self.DeleteRow) == "function")
            win.AttachEvent("btn_DeleteRow", "Clicked", self.DeleteRow);

        if (typeof (self.Save) == "function")
            win.AttachEvent("btn_Save", "Clicked", self.Save);

        if (typeof (self.Cancel) == "function")
            win.AttachEvent("btn_Cancel", "Clicked", self.Cancel);

        if (typeof (self.Exit) == "function")
            win.AttachEvent("btn_Exit", "Clicked", self.Exit);

    }

    if (typeof (this.unbind) == "function") var baseUnBind = this.unbind;
    this.unbind = function () {
        self.Instance = null;
        if (typeof (this.Release) == "function") this.Release();
        if (typeof (baseUnBind) == "function") baseUnBind();

        tab_1.Detach(); tab_1 = null;
        sle_zdbm.Detach(); sle_zdbm = null;
        sle_zdr.Detach(); sle_zdr = null;
        ddlb_wczt.Detach(); ddlb_wczt = null;
        ddlb_kplx.Detach(); ddlb_kplx = null;
        ddlb_pjzl.Detach(); ddlb_pjzl = null;
        sle_fphm.Detach(); sle_fphm = null;
        sle_kpje.Detach(); sle_kpje = null;
        sle_jbr.Detach(); sle_jbr = null;
        dp_2.Detach(); dp_2 = null;
        sle_wldw.Detach(); sle_wldw = null;
        dp_1.Detach(); dp_1 = null;
        dw_1.Detach(); dw_1 = null;
        dw_mkfhjb.Detach(); dw_mkfhjb = null;
        dw_2.Detach(); dw_2 = null;
        dw_3.Detach(); dw_3 = null;
        tb_1.Detach(); tb_1 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

    //以上代码由设计器自动生成,请不要手工修改
    //ExtPB.Net JavaScript End  设计器查找标志,请不要删除或修改

    var curW_OPEN_BMDA = new OpenWindowQueryBase();

    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    
    
    var curYhbh = "";
    var EditFlag = "";
    var lsh = "";
    var fplsh = "";
    var dwc = new DataWindowChild();
    var webReq = new WebRequestor();
    webReq.SetWebServiceUrl("/Service/ServiceCaiWu.ashx");

    //#region PostOpen
    this.PostOpen = function () {
        curYhbh = requestor.GetParm("curYhbh");
        sle_kpje.SetText("");
        sle_fphm.SetText("");
        sle_wldw.SetText("");
        sle_jbr.SetText("");
        sle_zdr.SetText("");
        sle_zdbm.SetText("");
        ddlb_wczt.SetText("全部");
        ddlb_kplx.SetText("全部");
        ddlb_pjzl.SetText("全部");
        var myDate = new Date();
        var nian = myDate.getFullYear().toString();
        var yue = myDate.getMonth();
        var ri = "01";
        var objDate = new Date(nian, yue, ri);
        dp_1.SetValue(objDate.getVarDate())
        dp_2.SetValue(UtcDateFormatToString(new Date().getVarDate()));
       
    }
    //#endregion

    //#region Function 呼出数据窗口的函数群 
    this.Wldw = function (x, y, row, dwoName) {
        dwoName == "wldw";
        curW_OPEN_BMDA.SetDataWindow(W_YuKaiPiaoTongJi, "wldw", "dwmc", "M", self)   // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_wldw_fl_tree&listDw=dw_pub_wldw_list&treeRootLabel=分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA, null);
    }

    this.Zdbm = function (x, y, row, dwoName) {
        dwoName == "bmbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_YuKaiPiaoTongJi, "bmbh", "bmmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_bmda_fl_tree&listDw=dw_pub_list_bmda&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle=multiple&curQxlx=all"), curW_OPEN_BMDA_NEW, null);
    }

    this.Jbr = function (x, y, row, dwoName) {
        dwoName == "ygbh";
        curW_OPEN_BMDA.SetDataWindow(W_YuKaiPiaoTongJi, "ygbh", "ygmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_ryda_zw_tree&listDw=dw_pub_ygda_list&treeRootLabel=职务分类&treeData=csbh&treeLabel=csmc&treeParentData=fcsbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA, null);
    }

    this.Zdr = function (x, y, row, dwoName) {
        dwoName == "zdr";
        curW_OPEN_BMDA.SetDataWindow(W_YuKaiPiaoTongJi, "zdr", "ygmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_ryda_zw_tree&listDw=dw_pub_ygda_list1&treeRootLabel=职务分类&treeData=csbh&treeLabel=csmc&treeParentData=fcsbh&treeLeave=islast&selectStyle=multiple"), curW_OPEN_BMDA, null);
    }
    //#endregion

    //#region 选中后赋值
    this.SetSleValue = function (difBh, val) {
        if ("''" == val)
            val = "";

        if ("bmbh" == difBh)
            sle_zdbm.SetText(val);
        else if ("zdr" == difBh)
            sle_zdr.SetText(val);
        else if ("ygbh" == difBh)
            sle_jbr.SetText(val);
        else if ("wldw" == difBh)
            sle_wldw.SetText(val);
    }

    //#endregion

    //#region query
    this.Query = function () {
        var curKssj = UtcDateFormatToString(dp_1.GetValue());
        var curJssj = UtcDateFormatToString(dp_2.GetValue());
        var sqlWhere = " CONVERT(date,kprq,120) between '" + curKssj + "' and '" + curJssj + "' ";
        if (sle_kpje.GetText().length > 0) {
            sqlWhere += " and (kpje <= CONVERT(float," + sle_kpje.GetText() + ")) ";
        }
        if (sle_fphm.GetText().length > 0) {
            sqlWhere += " and (fphm like '%" + sle_fphm.GetText() + "%') ";
        }
        if (ddlb_pjzl.GetText() != "全部") {
            sqlWhere += " and (pjzl = '" + (ddlb_pjzl.GetText().split("]"))[0].substring(1, 10) + "') ";
        }
        if (ddlb_kplx.GetText() != "全部") {
            sqlWhere += " and (kplx = '" + (ddlb_kplx.GetText().split("]"))[0].substring(1, 10) + "') ";
        }
        if (ddlb_wczt.GetText() != "全部") {
            sqlWhere += " and (fhjb.fhmc = '" + ddlb_wczt.GetText() + "') ";
        }
        if (sle_zdbm.GetText().length > 0) {
            sqlWhere += " and (bmbh in (" + self.DealWords(sle_zdbm.GetText()) + ")) ";
        }
        if (sle_wldw.GetText().length > 0) {
            sqlWhere += " and (wldw in (" + self.DealWords(sle_wldw.GetText()) + ")) ";
        }
        if (sle_jbr.GetText().length > 0) {
            sqlWhere += " and (jbr in (" + self.DealWords(sle_jbr.GetText()) + ")) ";
        }
        if (sle_zdr.GetText().length > 0) {
            sqlWhere += " and (kpr in (" + self.DealWords(sle_zdr.GetText()) + ")) ";
        }

        dw_1.RetrieveEx(sqlWhere);
    }
    //#endregion

    //#region Function DealWords
    this.DealWords = function (words) {

        var temChar = words.toString().charAt(0);
        if (!(temChar == "'"))
            words = "'" + words + "'";
        return words;
    }
    //#endregion

    this.Exit = function () {
        setTimeout("window.close()", 100);
    }

    //#region this.Hz_RowFocusChanged  汇总行改变的时候
    this.Hz_RowFocusChanged = function (row) {
        tb_1.SetEnabled("btn_KaiPiao", true);
        if (row == 0) {
            tb_1.SetEnabled("btn_Audit", false);
            tb_1.SetEnabled("btn_Edit", false);
            tb_1.SetEnabled("btn_YEdit", false);
            return;
        }
        var fhStatus = dw_1.GetItemString(row, "fhjb");
        if (fhStatus == "T") {
            tb_1.SetEnabled("btn_Audit", false);
            tb_1.SetEnabled("btn_Edit", true);
            tb_1.SetEnabled("btn_YEdit", false);
        } else {
            tb_1.SetEnabled("btn_Audit", true);
            tb_1.SetEnabled("btn_Edit", false);
            tb_1.SetEnabled("btn_YEdit", true);
        }

        var curFhjbs = dw_1.GetItemString(dw_1.GetRow(), "fhjb"); // 当前

        var curRowNo = dw_mkfhjb.Find("fhjb='" + curFhjbs + "'", 1, dw_mkfhjb.RowCount());  // 当前行
        tb_1.SetText("btn_Audit", dw_mkfhjb.GetItemString(curRowNo, "dygn"));
    }
    //#endregion

    //#region this.dw_DoubleClicked
    this.dw_DoubleClicked = function (x, y, row, dowName) {

        if (row > 0) {
            tab_1.SelectTab(2);
            var fplsh = dw_1.GetItemString(row, "fplsh");
            dw_2.Retrieve(fplsh);
            dw_2.Modify("DataWindow.Readonly=yes");
        }

    }
    //#endregion

    //#region this.SetEditMode
    this.SetEditMode = function (newMode) {

        var bEditFlag = false;

        if (newMode != "") {
            bEditFlag = true;
        }

        tab_1.SelectTab(2);
        dw_1.SetEnabled(!bEditFlag);
        tb_1.SetEnabled("btn_Save", bEditFlag);
        tb_1.SetEnabled("btn_Cancel", bEditFlag);

        if (newMode == "Add") {
            dw_2.SetEnabled(true);
            dw_3.SetEnabled(false);
            dw_2.Modify("DataWindow.Readonly=no");
            dw_2.SetFocus();
            tb_1.SetEnabled("btn_KaiPiao", false);
            tb_1.SetEnabled("btn_Edit", false);
            tb_1.SetEnabled("btn_Audit", false);
            tb_1.SetEnabled("btn_InsertRow", false);
            tb_1.SetEnabled("btn_DeleteRow", false);

        } else if (newMode == "Edit") {
            dw_2.SetEnabled(false);
            dw_3.SetEnabled(true);
            dw_2.Modify("DataWindow.Readonly=yes");
            dw_3.Modify("DataWindow.Readonly=no");
            dw_3.SetFocus();
            tb_1.SetEnabled("btn_KaiPiao", false);
            tb_1.SetEnabled("btn_Edit", false);
            tb_1.SetEnabled("btn_Audit", false);
            tb_1.SetEnabled("btn_InsertRow", true);
            tb_1.SetEnabled("btn_DeleteRow", true);
        } else {
            dw_2.SetEnabled(false);
            dw_3.SetEnabled(false);
            dw_2.Modify("DataWindow.Readonly=yes");
            dw_3.Modify("DataWindow.Readonly=yes");
            tb_1.SetEnabled("btn_KaiPiao", true);
            tb_1.SetEnabled("btn_Edit", false);
            tb_1.SetEnabled("btn_Audit", true);
            tb_1.SetEnabled("btn_InsertRow", false);
            tb_1.SetEnabled("btn_DeleteRow", false);
        }

    }
    //#endregion     

    //#region this.Cancel
    this.Cancel = function () {
        dw_1.RefreshDetails();
        tab_1.SelectTab(1);

        tb_1.SetEnabled("btn_InsertRow", false);
        tb_1.SetEnabled("btn_DeleteRow", false);
        tb_1.SetEnabled("btn_Save", false);
        tb_1.SetEnabled("btn_Cancel", false);
        dw_2.Modify("DataWindow.Readonly=yes");
        dw_3.Modify("DataWindow.Readonly=yes");
        if (dw_1.RowCount() == 0) {
            self.Hz_RowFocusChanged(0);
        } else {
            self.Hz_RowFocusChanged(1);
        }
        
        dw_1.SetEnabled(true);
    }
    //#endregion

    //#region kaipiao
    this.KaiPiao = function () {

        dw_2.Reset();
        dw_2.ScrollToRow(dw_2.InsertRow(0));
        var lsh = "";
        webReq.Invoke("GetKFPlsh");
        if (webReq.StatusCode() == 200) {
            lsh = webReq.ResponseText();
        } else {
            alert(webReq.ResponseText());
            return;
        }
        dw_2.SetItem(1, "fplsh", lsh);
        dw_2.SetFocus();
        dw_2.SetItem(1, "kpr", curYhbh);
       
        self.SetEditMode("Add");
        EditFlag = "Add";
    }
    //#endregion

    //#region 关联单据
    this.Edit = function () {
        var row = dw_1.GetRow();
        if (row <= 0) {
            alert("请先选择一行数据！");
            return;
        }
        fplsh = dw_1.GetItemString(row, "fplsh");

        dw_1.ReSelectRow(row);
        dw_1.RefreshDetails();
        dw_2.Retrieve(fplsh);
       
      
//        dw_2.SetItem(dw_2.GetRow(), "fplsh", lsh);
//        dw_2.SetItem(dw_2.GetRow(), "lydh", fplsh);
        dw_2.SetItem(dw_2.GetRow(), "ykp", "0");

        dw_3.SetDataObject("da_fp_hz_mx");
        dw_3.GetChild("djlx", dwc);
        dwc.Retrieve("fp_djlb");

        dw_3.Reset();
        dw_3.ScrollToRow(dw_3.InsertRow(0));
        dw_3.SetFocus();
        self.SetEditMode("Edit");
        EditFlag = "Edit";
    }
    //#endregion

    //#region 修改
    this.YEdit = function () {
        var row = dw_1.GetRow();
        if (row <= 0) {
            alert("请先选择一行数据！");
            return;
        }
        var fplsh = dw_1.GetItemString(row, "fplsh");
       // dw_2.SetDataObject("da_fp_hzxz_top");
        dw_2.GetChild("kplx", dwc);
        dwc.Retrieve("cw_pjlx");
        dw_2.GetChild("pjzl", dwc);
        dwc.Retrieve("cw_pjzl");
        dw_2.GetChild("kpr", dwc);
        dwc.Retrieve("%");
        dw_2.GetChild("jbr", dwc);
        dwc.Retrieve("%");
        dw_2.GetChild("bmbh", dwc);
        dwc.Retrieve("%", curYhbh, "all");

        dw_1.ReSelectRow(row);
        dw_1.RefreshDetails();
        dw_2.Retrieve(fplsh);
        //dw_mx.Retrieve(fplsh);
        dw_2.Modify("fphm.protect='1'");
        self.SetEditMode("Add");
        EditFlag = "Edit";
    }
    //#endregion

    //#region Save
    this.Save = function () {
        if (EditFlag == "Add") {
            //#region 开空票
            dw_2.AcceptText();

            if (dw_2.ModifiedCount() <= 0) {
                alert("没有数据需要保存!");
                return;
            }

            if (dw_2.AcceptText() != 1) {
                return;
            }

            var curPzh = dw_2.GetItemString(1, "fplsh");

            //进行数据数据校验
            if (dw_2.ModifiedCount() > 0) {
                if (!dw_2.CheckData("*", ""))
                    return;
            }

            var dw_2_data = dw_2.GetChanges();
            if (dw_2_data == "")
                return;

            webReq.Invoke("KFaPiao_Save", "dwChanges=" + dw_2_data + "&dwObjects=" + dw_2.GetDataObject());

            if (webReq.StatusCode() == 200) {

                dw_2.ResetUpdate();
                //刷新 
                dw_1.Refresh();

                //找到相应的记录
                var findRow = dw_1.Find("fplsh='" + curPzh + "' ", 1, dw_1.RowCount());
                if (findRow > 0) {
                    dw_1.ScrollToRow(findRow);
                }

                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);

                self.SetEditMode("");
            }
            else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
                dw_2.SetFocus();
            }
            //#endregion
        }
        else if (EditFlag == "Edit") {
            //#region 关联单据
            webReq.Invoke("GetFPlsh");
            if (webReq.StatusCode() == 200) {
                lsh = webReq.ResponseText();
            } else {
                alert(webReq.ResponseText());
                return;
            }
            dw_2.AcceptText();
            dw_3.AcceptText();

            if ((dw_2.ModifiedCount() + dw_3.DeletedCount() + dw_3.ModifiedCount()) <= 0) {
                alert("没有数据需要保存!");
                return;
            }

            if (dw_2.AcceptText() != 1) {
                return;
            }
            if (dw_3.AcceptText() != 1) {
                return;
            }
            //  更新行号 
            for (row = 1; row <= dw_3.RowCount(); row++) {

                if (dw_3.GetItemString(row, "djbh") == "" || dw_3.GetItemNumber(row, "kpje") == 0) {
                    dw_3.DeleteRow(row);
                }
            }
            if (dw_3.RowCount() == 0) {
                alert("无明细记录!");
                dw_3.SetFocus();
                return;
            }

            for (row = 1; row <= dw_3.RowCount(); row++) {
                // 这个是为了有改变的值，无意思           
                dw_3.SetItem(row, "fplsh", lsh);
                dw_3.SetItem(row, "djlsh", lsh + "_" + self.formartStr(parseInt(row)));
            }

            //进行数据数据校验
            if (dw_2.ModifiedCount() > 0 || dw_3.GetItemString(dw_2.GetRow(), "fplsh") == "") {
                if (!dw_2.CheckData("*", ""))
                    return;
            }

            if (dw_3.ModifiedCount() > 0) {
                if (!dw_3.CheckData("*", ""))
                    return;
            }

            var dw_2_data = dw_2.GetChanges();
            var dw_3_data = dw_3.GetChanges();
            if (dw_2_data == "" && dw_3_data == "")
                return;

            webReq.Invoke("KFaPiaoMX_Save", "dw_Master=" + dw_2_data + "&dw_Detail=" + dw_3_data + "&hzDwName=" + dw_2.GetDataObject() + "&mxDwName=" + dw_3.GetDataObject() + "&lsh=" + lsh);

            if (webReq.StatusCode() == 200) {

                dw_2.ResetUpdate();
                dw_3.ResetUpdate();
                dw_3.Retrieve("");

                //刷新 
                dw_1.Refresh();

                //找到相应的记录
                //                var findRow = dw_1.Find("fplsh='" + curPzh + "' ", 1, dw_1.RowCount());
                //                if (findRow > 0) {
                //                    dw_1.ScrollToRow(findRow);
                //                }

                requestor.MessageBox("提示", "数据保存成功!", ICON.Information);

                self.SetEditMode("");
                if (dw_1.RowCount() == 0) {
                    self.Hz_RowFocusChanged(0);
                } else {
                    self.Hz_RowFocusChanged(1);
                }
                tab_1.SelectTab(1);
            }
            else {
                requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
                dw_2.SetFocus();
            }
            //#endregion
        }

    }
    //#endregion

    //#region this.formartStr  
    this.formartStr = function (n) {

        var curN = n + "";
        var leftLength = 6 - (curN.length);
        var ret = "";
        for (var i = 1; i <= leftLength; i++) {
            ret = ret + "0";
        }
        return (ret + curN);
    }
    //#endregion  

    //#region Audit
    this.Audit = function () {
        var curShjb = "F";

        if (dw_1.GetRow() < 1) {
            alert("请先选择一笔数据进行审核");
            return;
        }
        var fph = dw_1.GetItemString(dw_1.GetRow(), "fplsh");
        if (fph.length < 1) {
            alert("凭证号异常");
            return;
        }
        if (!confirm("您确定要审核凭证号为[" + fph + "]的单据吗？")) {
            return;
        }
        var curFhjb = dw_1.GetItemString(dw_1.GetRow(), "fhjb"); // 当前
        var curFhjbStr = self.GetAuditStatus(curFhjb);
        var curToFhjb = curFhjbStr.split("#")[0];
        var curToShowName = dw_mkfhjb.GetItemString(curFhjbStr.split("#")[1], "fhmc");
        webReq.Invoke("FaPiao_Audit", "curFplsh=" + fph + "&curShjb=" + curToFhjb);
        if (webReq.StatusCode() == 200) {

            //#region 刷新汇总表
            dw_1.SetRedraw(false);
            dw_1.SetProcessing(true);

            self.Query();
            //找到相应的记录
            var findRow = dw_1.Find("fplsh='" + fph + "' ", 1, dw_1.RowCount());
            if (findRow > 0) {
                dw_1.ScrollToRow(findRow);
            }
            dw_1.SetProcessing(false);
            dw_1.SetRedraw(true);
            //#endregion

            // 下一笔将要显示在按钮上的功能
            var curNextFhjbStr = self.GetAuditStatus(curToFhjb);
            var curToButtonShowName = dw_mkfhjb.GetItemString(curFhjbStr.split("#")[1], "dygn");
            tb_1.SetText("btn_Audit", curToButtonShowName);
        }
    }
    //#endregion

    //#region 审核级别
    this.GetAuditStatus = function (curFhjb) {
        if (curFhjb == "T") {
            tb_1.SetEnabled("btn_Audit", false);
            return;
        }
        var fhjbRow = dw_mkfhjb.Find("fhjb='" + curFhjb + "'", 1, dw_mkfhjb.RowCount());  // 当前行

        var curShjbRow = 0;
        if (dw_mkfhjb.RowCount() - 2 == 0) {  //没有级别，只有2条数据
            curShjb = "T";
            curShjbRow = dw_mkfhjb.Find("fhjb='T'", 1, dw_mkfhjb.RowCount());  // 当前行
        } else {
            for (var i = 1; i <= dw_mkfhjb.RowCount() - 2; i++) {
                if (parseInt(curFhjb == "F" ? "0" : curFhjb) < parseInt(dw_mkfhjb.GetItemString(i, "fhjb"))) {
                    curShjb = dw_mkfhjb.GetItemString(i, "fhjb");
                    curShjbRow = i;
                    break;
                } else if (i == dw_mkfhjb.RowCount() - 2) {
                    curShjb = "T";
                    curShjbRow = dw_mkfhjb.Find("fhjb='T'", 1, dw_mkfhjb.RowCount());  // 当前行
                }
            }
        }
        return curShjb + "#" + curShjbRow
    }
    //#endregion

    //#region this.dw_2_doubleClicked  双击弹出窗口

    this.dw_2_doubleClicked = function (x, y, row, dwoName) {
        if (dwoName == "bmbh") {
            curW_OPEN_BMDA.SetDataWindow(dw_2, "bmbh", "bmmc")  // 这个用来给子窗口Set当前数据窗口
            requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_bmda_fl_tree&listDw=dw_pub_list_bmda&treeRootLabel=部门分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&curQxlx=all"), curW_OPEN_BMDA, null);
        }
        if (dwoName == "wldw") {

            curW_OPEN_BMDA.SetDataWindow(dw_2, "wldw", "dwmc")   // 这个用来给子窗口Set当前数据窗口
            requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base.OpenWindowQueryBase&treeDw=dw_da_wldw_fl_tree&listDw=dw_pub_wldw_list&treeRootLabel=分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast"), curW_OPEN_BMDA, null);
        }
    }
    //#endregion 

    //#region this.dw_2_editChanged  自动查供应商

    this.dw_2_editChanged = function (row, dwoName, data) {
        if (dwoName == "wldw") {
            dw_2.GetChild(dwoName, dwc);
            if (data != "") {
                dwc.Retrieve("00", "%" + data + "%", curYhbh);
            }
            else {
                dwc.Retrieve("00", "%", curYhbh);
            }
        } else if (dwoName == "bmbh") {
            dw_2.GetChild(dwoName, dwc);
            if (data != "") {
                dwc.Retrieve("%" + data + "%", curYhbh, "all");
            }
            else {
                dwc.Retrieve("%", curYhbh, "all");
            }
        } else if (dwoName == "jbr") {
            dw_2.GetChild(dwoName, dwc);
            if (data != "") {
                dwc.Retrieve("%" + data + "%");
            }
            else {
                dwc.Retrieve("%");
            }
        } 
    }
    //#endregion 

    //#region this.dw_2_itemChanged  检查供应商数据完整性
    this.dw_2_itemChanged = function (row, dwoName, data) {
        if (dwoName == "wldw") {
            dw_2.GetChild(dwoName, dwc);
            var findRow = dwc.Find("wldw='" + data + "' or dwmc='" + data + "'", 1, dwc.RowCount());
            if (findRow <= 0) {
                requestor.MessageBox("提示", "未找到编号为[" + data + "]的记录，请检查!", ICON.StopSign);
                return 1;
            } else {
                dw_2.SetItem(row, "wldw", dwc.GetItemString(findRow, "wldw"));
            }
        }
        if (dwoName == "bmbh") {
            dw_2.GetChild(dwoName, dwc);
            var findRow = dwc.Find("bmbh='" + data + "' or bmmc='" + data + "'", 1, dwc.RowCount());
            if (findRow <= 0) {
                requestor.MessageBox("提示", "未找到部门编号为[" + data + "]的记录，请检查!", ICON.StopSign);
                return 1;
            } else {
                dw_2.SetItem(row, "bmbh", dwc.GetItemString(findRow, "bmbh"));
            }
        }
        if (dwoName == "jbr") {
            dw_2.GetChild(dwoName, dwc);
            var findRow = dwc.Find("ygbh='" + data + "' or ygmc='" + data + "'", 1, dwc.RowCount());
            if (findRow <= 0) {
                requestor.MessageBox("提示", "未找到经办人为[" + data + "]的记录，请检查!", ICON.StopSign);
                return 1;
            } else {
                dw_2.SetItem(row, "jbr", dwc.GetItemString(findRow, "ygbh"));
            }
        }
    }
    //#endregion 

    //#region this.dw_3_keyDown 如果是在最后一行的金额列按回车，则自动增加一行

    this.dw_3_keyDown = function (keyCode, keyFlags) {

        if ((keyCode == 13 || keyCode == 39) && dw_3.GetColumnName() == "bzxx") {

            if (dw_3.GetItemNumber(dw_3.GetRow(), "kpje") == 0) {
                requestor.MessageBox("提示", "请为当前行输入金额", 1);
                dw_3.SetColumn("kpje");
                dw_3.SetFocus();
            } else if (dw_3.GetRow() == dw_3.RowCount()) {

                dw_3.ScrollToRow(dw_3.InsertRow(0));

                dw_3.SetColumn("djlx");
                dw_3.SetFocus();
            }
            return 1;
        }
        else if (keyCode == 37) {
            dw_3.SetColumn("djlx");
            dw_3.SetFocus();
            return 1;
        }

    }
    //#endregion

    //#region this.dw_3_DropDown
    this.dw_3_DropDown = function () {

        var row = dw_3.GetRow();
        if (row <= 0)
            return;

        var dwoName = dw_3.GetColumnName();
        if (dwoName == "djbh") {
            if (dw_3.GetItemString(row, "djlx") == "") {
                requestor.MessageBox("提示", "请先选择单据类型!", ICON.Information);
                dw_3.SetColumn("djlx");
                dw_3.SetFocus();
            } else {
                dw_3.GetChild("djbh", dwc);
                dwc.Retrieve(dw_3.GetItemString(row, "djlx"), "%");
                findRow = dwc.Find("djbh='" + dw_3.GetItemString(row, dwoName) + "'", 1, dwc.RowCount());
                if (findRow > 0) {
                    if (findRow == 1)
                        dwc.SelectRow(1, true);
                    else
                        dwc.ScrollToRow(findRow);
                }
            }
        }
    }
    //#endregion

    //#region this.dw_3_itemFocusChange
    this.dw_3_itemFocusChange = function (row, dwoName, data) {
        if (dwoName == "djbh") {
            if (dw_3.GetItemString(row, "djlx") == "") {
                requestor.MessageBox("提示", "请先选择单据类型!", ICON.Information);
                dw_3.SetColumn("djlx");
                dw_3.SetFocus();
            } else {
                dw_3.GetChild("djbh", dwc);
                dwc.Retrieve(dw_3.GetItemString(row, "djlx"), "%");
            }
        }

    }
    //#endregion

    //#region this.dw_3_itemChanged
    this.dw_3_itemChanged = function (row, dwoName, data) {
        if (dwoName == "djbh") {
            if (dw_3.GetItemString(row, "djlx") == "") {
                requestor.MessageBox("提示", "请先选择单据类型!", ICON.Information);
                dw_3.SetColumn("djlx");
                dw_3.SetFocus();
            }
            else {
                dw_3.GetChild("djbh", dwc);
                var findRow = dwc.Find("pzh='" + data + "' ", 1, dwc.RowCount());
                if (findRow <= 0) {
                    requestor.MessageBox("提示", "未找到单据编号为[" + data + "]的记录，请检查!", ICON.StopSign);
                    return 1;
                } else {
                    dw_3.SetItem(row, "kdrq", dwc.GetItemDate(findRow, "kdrq"));
                    dw_3.SetItem(row, "djje", dwc.GetItemNumber(findRow, "zjje"));
                }
            }
        }
    }
    //#endregion

    //#region this.dw_3_editChanged
    this.dw_3_editChanged = function (row, dwoName, data) {
        if (dwoName == "djbh") {
            if (dw_3.GetItemString(row, "djlx") == "") {
                requestor.MessageBox("提示", "请先选择单据类型!", ICON.Information);
                dw_3.SetColumn("djlx");
                dw_3.SetFocus();
            } else {
                dw_3.GetChild("djbh", dwc);
                if (data != "") {
                    dwc.Retrieve(dw_3.GetItemString(row, "djlx"), "%" + data + "%");

                }
                else {
                    dwc.Retrieve(dw_3.GetItemString(row, "djlx"), "%");
                }
            }
        }
    }
    //#endregion
 }