///<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_KaiPiaoDanTongJi() {

    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_hz = new DataWindow(requestor);
    var dw_mkfhjb = new DataWindow(requestor);
    var dw_mx_top = new DataWindow(requestor);
    var dw_mx = 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_hz == null) dw_hz = new DataWindow(requestor);
        dw_hz.Attach(win.Control("dw_hz"));
        if (dw_mkfhjb == null) dw_mkfhjb = new DataWindow(requestor);
        dw_mkfhjb.Attach(win.Control("dw_mkfhjb"));
        if (dw_mx_top == null) dw_mx_top = new DataWindow(requestor);
        dw_mx_top.Attach(win.Control("dw_mx_top"));
        if (dw_mx == null) dw_mx = new DataWindow(requestor);
        dw_mx.Attach(win.Control("dw_mx"));
        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_hz_DoubleClicked) == "function")
            win.AttachEvent("dw_hz", "DoubleClicked", self.dw_hz_DoubleClicked);

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

        if (typeof (self.dw_mx_top_doubleClicked) == "function")
            win.AttachEvent("dw_mx_top", "DoubleClicked", self.dw_mx_top_doubleClicked);

        if (typeof (self.dw_mx_top_editChanged) == "function")
            win.AttachEvent("dw_mx_top", "EditChanged", self.dw_mx_top_editChanged);

        if (typeof (self.dw_mx_top_itemChanged) == "function")
            win.AttachEvent("dw_mx_top", "ItemChanged", self.dw_mx_top_itemChanged);

        if (typeof (self.dw_mx_top_keyDown) == "function")
            win.AttachEvent("dw_mx_top", "KeyDown", self.dw_mx_top_keyDown);

        if (typeof (self.dw_mx_DropDown) == "function")
            win.AttachEvent("dw_mx", "DropDown", self.dw_mx_DropDown);

        if (typeof (self.dw_mx_editChanged) == "function")
            win.AttachEvent("dw_mx", "EditChanged", self.dw_mx_editChanged);

        if (typeof (self.dw_mx_itemChanged) == "function")
            win.AttachEvent("dw_mx", "ItemChanged", self.dw_mx_itemChanged);

        if (typeof (self.dw_mx_itemFocusChange) == "function")
            win.AttachEvent("dw_mx", "ItemFocusChanged", self.dw_mx_itemFocusChange);

        if (typeof (self.dw_mx_keyDown) == "function")
            win.AttachEvent("dw_mx", "KeyDown", self.dw_mx_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.Edit) == "function")
            win.AttachEvent("btn_Edit", "Clicked", self.Edit);

        if (typeof (self.TuiPiao) == "function")
            win.AttachEvent("btn_TuiPiao", "Clicked", self.TuiPiao);

        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_hz.Detach(); dw_hz = null;
        dw_mkfhjb.Detach(); dw_mkfhjb = null;
        dw_mx_top.Detach(); dw_mx_top = null;
        dw_mx.Detach(); dw_mx = 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 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()));
        dw_mx_top.Modify("DataWindow.Readonly=yes");
        dw_mx.Modify("DataWindow.Readonly=yes");
    }
    //#endregion

	 //#region this.Hz_RowFocusChanged  汇总行改变的时候
    this.Hz_RowFocusChanged = function (row) {
        if (row > 0) {
            var fhStatus = dw_hz.GetItemString(row, "fhjb");
            tb_1.SetEnabled("btn_KaiPiao", true);
            tb_1.SetEnabled("btn_TuiPiao", true);
            tb_1.SetEnabled("btn_Query", true);
            tb_1.SetEnabled("btn_Filter", true);
            tb_1.SetEnabled("btn_Clear", true);
            if (fhStatus == "F") {
                tb_1.SetEnabled("btn_Audit", true);
                tb_1.SetEnabled("btn_Edit", true);
                // tb_1.SetEnabled("btn_TuiPiao", true);
            } else if (fhStatus == "T") {
                tb_1.SetEnabled("btn_Audit", false);
                tb_1.SetEnabled("btn_Edit", false);
                // tb_1.SetEnabled("btn_TuiPiao", false);
            } else {
                tb_1.SetEnabled("btn_Audit", true);
                // tb_1.SetEnabled("btn_TuiPiao", true);
                tb_1.SetEnabled("btn_Edit", false);
            }

            var curFhjbs = dw_hz.GetItemString(dw_hz.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 Function 呼出数据窗口的函数群 
	 this.Wldw = function (x, y, row, dwoName) {
	     dwoName == "wldw";
	     curW_OPEN_BMDA.SetDataWindow(W_KaiPiaoDanTongJi, "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_KaiPiaoDanTongJi, "bmbh", "bmmc", "M", self)  // 这个用来给子窗口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&selectStyle=multiple&curQxlx=all"), curW_OPEN_BMDA_NEW, null);
	 }

	 this.Jbr = function (x, y, row, dwoName) {
	     dwoName == "ygbh";
	     curW_OPEN_BMDA.SetDataWindow(W_KaiPiaoDanTongJi, "ygbh", "ygmc", "M", self)  // 这个用来给子窗口Set当前数据窗口
	     requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&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_KaiPiaoDanTongJi, "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 = " 1=1 and  CONVERT(date,kprq,120) between '" + curKssj + "' and '" + curJssj + "' ";

	     if (sle_kpje.GetText().length > 0) {// && (isNaN(sle_kpje.GetText()))
	     	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 (aa.bmbh in (" + self.DealWords(sle_zdbm.GetText()) + ")) ";
	    }
	    if (sle_wldw.GetText().length > 0) {
	     	sqlWhere += " and (aa.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_hz.RetrieveEx(sqlWhere);
	     //dw_hz.Retrieve();
	 }
     //#endregion

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

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

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

	     if (row > 0) {
	         tab_1.SelectTab(2);
	         var fplsh = dw_hz.GetItemString(row, "fplsh");
	         dw_mx_top.SetDataObject("da_fp_hzxz_top");
	         dw_mx_top.GetChild("kplx", dwc);
	         dwc.Retrieve("cw_pjlx");
	         dw_mx_top.GetChild("pjzl", dwc);
	         dwc.Retrieve("cw_pjzl");
	         dw_mx_top.GetChild("kpr", dwc);
	         dwc.Retrieve("%");
	         dw_mx_top.GetChild("jbr", dwc);
	         dwc.Retrieve("%");
	         dw_mx_top.GetChild("bmbh", dwc);
	         dwc.Retrieve("%", curYhbh, "all");
	         dw_mx_top.Retrieve(fplsh);
	         dw_mx.Retrieve(fplsh);
	         dw_mx_top.Modify("DataWindow.Readonly=yes");
	         dw_mx.Modify("DataWindow.Readonly=yes");
	     }
	    
	 }
	 //#endregion

	 //#region 开票
	 this.KaiPiao = function () {

	     dw_mx_top.SetDataObject("da_fp_hzxz_top");
	     dw_mx_top.GetChild("kplx", dwc);
	     dwc.Retrieve("cw_pjlx");
	     dw_mx_top.GetChild("pjzl", dwc);
	     dwc.Retrieve("cw_pjzl");
	     dw_mx_top.GetChild("kpr", dwc);
	     dwc.Retrieve("%");
	     dw_mx_top.GetChild("jbr", dwc);
	     dwc.Retrieve("%");
	     dw_mx_top.GetChild("bmbh", dwc);
	     dwc.Retrieve("%", curYhbh, "all");

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

	 //#region 退票
	 this.TuiPiao = function () {
	     var row = dw_hz.GetRow();

	     if (row <= 0) {
	         alert("请先选择一行数据！");
	         return;
	     }
	     var fplsh = dw_hz.GetItemString(row, "fplsh");
	     dw_mx_top.SetDataObject("da_fp_hztp_top");
	     dw_mx_top.GetChild("kplx", dwc);
	     dwc.Retrieve("cw_pjlx");
	     dw_mx_top.GetChild("pjzl", dwc);
	     dwc.Retrieve("cw_pjzl");
	     dw_mx_top.GetChild("kpr", dwc);
	     dwc.Retrieve("%");
	     dw_mx_top.GetChild("jbr", dwc);
	     dwc.Retrieve("%");
	     dw_mx_top.GetChild("tpr", dwc);
	     dwc.Retrieve("%");
	     dw_mx_top.GetChild("bmbh", dwc);
	     dwc.Retrieve("%", curYhbh, "all");

	     dw_hz.ReSelectRow(row);
	     dw_hz.RefreshDetails();
	     dw_mx_top.Retrieve(fplsh);
	     dw_mx.Retrieve(fplsh);
	     dw_mx_top.SetItem(dw_mx_top.GetRow(), "tprq", (new Date().getVarDate()));
	     dw_mx_top.SetItem(dw_mx_top.GetRow(), "tpr", curYhbh);
	     self.SetEditMode("TuiPiao");
	     EditFlag = "TuiPiao";

	 }
	 //#endregion

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

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

	 //#region this.SetEditMode
	 this.SetEditMode = function (newMode) {
	    
	     var bEditFlag = false;
	     if (newMode != "") {
	         bEditFlag = true;
	     }
	     dw_hz.SetEnabled(!bEditFlag);

	     tb_1.SetEnabled("btn_KaiPiao", !bEditFlag);
	     tb_1.SetEnabled("btn_Edit", !bEditFlag);
	     tb_1.SetEnabled("btn_TuiPiao", !bEditFlag);
	     tb_1.SetEnabled("btn_Audit", !bEditFlag);
	     tb_1.SetEnabled("btn_Query", !bEditFlag);
	     tb_1.SetEnabled("btn_Filter", !bEditFlag);
	     tb_1.SetEnabled("btn_Clear", !bEditFlag);

	     if (newMode != "TuiPiao") {
	         tb_1.SetEnabled("btn_InsertRow", bEditFlag);
	         tb_1.SetEnabled("btn_DeleteRow", bEditFlag);
	     } else {
	         tb_1.SetEnabled("btn_InsertRow", false);
	         tb_1.SetEnabled("btn_DeleteRow", false);
	     }

	     tb_1.SetEnabled("btn_Save", bEditFlag);
	     tb_1.SetEnabled("btn_Cancel", bEditFlag);

	     if (bEditFlag) {
	         tab_1.SelectTab(2);
	         dw_mx_top.Modify("DataWindow.Readonly=no");
	         dw_mx.Modify("DataWindow.Readonly=no");
	         dw_mx_top.SetFocus();
	     }
	     else {
	         dw_mx_top.Modify("DataWindow.Readonly=yes");
	         dw_mx.Modify("DataWindow.Readonly=yes");
	         dw_mx_top.SetFocus();
	     }
	 }
	 //#endregion     

	 //#region this.InsertRow
	 this.InsertRow = function () {
	   
	     dw_mx.ScrollToRow(dw_mx.InsertRow(0));
	     dw_mx.SetColumn("djbh");
	     dw_mx.SetFocus();
	     dw_mx.ScrollFirstPage();
	     dw_mx.ScrollToRow(dw_mx.RowCount());
	 }
	 //#endregion  

	 //#region this.DeleteRow
	 this.DeleteRow = function () {
	   
	     var row = dw_mx.GetRow();
	     if (row <= 0)
	         return;
	     var status = dw_mx.GetItemStatus(row, 0, DWBUFFER.Primary);

	     if (status == DWITEMSTATUS.DataModified || status == DWITEMSTATUS.NotModified) {
	         if (requestor.MessageBox("提示", "是否确认删除当前明细记录?", ICON.Question, BUTTON.YesNo, 2) == 2) {
	             dw_mx.SetFocus();
	             return;
	         }
	     }
	     dw_mx.DeleteRow(0);
	     dw_mx.SetFocus();

	 }
	 //#endregion  

	 //#region this.Cancel
	 this.Cancel = function () {
	     dw_hz.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_mx_top.Modify("DataWindow.Readonly=yes");
	     dw_mx.Modify("DataWindow.Readonly=yes");

	     if (dw_hz.RowCount() < 1) {
	         tb_1.SetEnabled("btn_KaiPiao",true);
	         tb_1.SetEnabled("btn_Edit", true);
	         tb_1.SetEnabled("btn_TuiPiao", true);
	         tb_1.SetEnabled("btn_Audit", true);
	         tb_1.SetEnabled("btn_Query", true);
	         tb_1.SetEnabled("btn_Filter", true);
	         tb_1.SetEnabled("btn_Clear", true);
	     } else {
	         self.Hz_RowFocusChanged(1);
	     }
	     dw_hz.SetEnabled(true);
	 }
	 //#endregion

	 //#region Save
	 this.Save = function () {
	     dw_mx_top.AcceptText();
	     dw_mx.AcceptText();

	     if ((dw_mx_top.ModifiedCount() + dw_mx.DeletedCount() + dw_mx.ModifiedCount()) <= 0) {
	         alert("没有数据需要保存!");
	         return;
	     }
	     dw_mx_top.SetItem(1, "fphm", dw_mx_top.GetItemString(1, "fphm"));

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

	         if (dw_mx.GetItemString(row, "djbh") == "" || dw_mx.GetItemNumber(row, "kpje") == 0) {
	             dw_mx.DeleteRow(row);
	         }
	     }
	     if (dw_mx.RowCount() == 0) {
	         alert("无明细记录!");
	         dw_mx.SetFocus();
	         return;
	     }
	     var curPzh = dw_mx_top.GetItemString(1, "fplsh");
	     for (row = 1; row <= dw_mx.RowCount(); row++) {
	         // 这个是为了有改变的值，无意思           
	         dw_mx.SetItem(row, "fplsh", curPzh);
	         dw_mx.SetItem(row, "djlsh", curPzh + "_" + self.formartStr(parseInt(row)));
	     }
	    
	     //进行数据数据校验
	     if (dw_mx_top.ModifiedCount() > 0 || dw_mx.GetItemString(dw_mx_top.GetRow(), "fplsh") == "") {
	         if (!dw_mx_top.CheckData("*", ""))
	             return;
	     }

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

	     var dw_mx_top_data = dw_mx_top.GetChanges();
	     var dw_mx_data = dw_mx.GetChanges();
	     if (dw_mx_top_data == "" && dw_mx_data == "")
	         return;

	     //#region 处理时间戳，【判断是否被占用】---------------begin--------------

//	     webReqTimestamp.Invoke("YW_TIMESTAMP_REC", "curPzh=" + dw_mx_top.GetItemString(dw_mx_top.GetRow(), "pzh") + "&curFlag=SAVE&curLAST_SJC=" + curSjc + "&times=" + new Date());
//	     if (webReqTimestamp.StatusCode() == 200) {
//	         var resmsg = webReqTimestamp.ResponseText();
//	         if (resmsg.split("#")[0] == "N") {
//	             requestor.MessageBox("提示", "此单据正在被其他人保存,您不能保存。");
//	             return;
//	         }
//	     }
//	     else {
//	         requestor.MessageBox("提示(时间戳)", webReqTimestamp.ResponseText(), ICON.StopSign);
//	     }
	     //#endregion 处理时间戳，【判断是否被占用】---------------end----------------
	     webReq.Invoke("FaPiao_Save", "dw_Master=" + dw_mx_top_data + "&dw_Detail=" + dw_mx_data + "&hzDwName=" + dw_mx_top.GetDataObject() + "&mxDwName=" + dw_mx.GetDataObject() + "&flag=" + EditFlag);

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

	         dw_mx_top.ResetUpdate();
	         dw_mx.ResetUpdate();
	       
	         //刷新 
	         dw_hz.Refresh();

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

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

	         tb_1.SetEnabled("btn_InsertRow", false);
	         tb_1.SetEnabled("btn_DeleteRow", false);
	         tb_1.SetEnabled("btn_Save", false);
	         tb_1.SetEnabled("btn_Cancel", false);
	         tb_1.SetEnabled("btn_KaiPiao", true);
	         tb_1.SetEnabled("btn_Edit", true);
	         tb_1.SetEnabled("btn_TuiPiao", true);
	         tb_1.SetEnabled("btn_Audit", true);
	         dw_hz.SetEnabled(true);
	         dw_mx_top.Modify("DataWindow.Readonly=yes");
	         dw_mx.Modify("DataWindow.Readonly=yes");

	     }
	     else {
	         requestor.MessageBox("提示", webReq.ResponseText(), ICON.StopSign);
	         dw_mx_top.SetFocus();
	     }
	 }
     //#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_hz.GetRow() < 1) {
	         alert("请先选择一笔数据进行审核");
	         return;
	     }
	     var fplsh = dw_hz.GetItemString(dw_hz.GetRow(), "fplsh");
	     if (fplsh.length < 1) {
	         alert("凭证号异常");
	         return;
	     }
	     if (!confirm("您确定要审核凭证号为[" + fplsh + "]的单据吗？")) {
	         return;
	     }
	     var curFhjb = dw_hz.GetItemString(dw_hz.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=" + fplsh + "&curShjb=" + curToFhjb);
	     if (webReq.StatusCode() == 200) {

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

	         //刷新
	         //dw_hz.Retrieve(curDjlx, curYhbh);
	         self.Query();
	         //找到相应的记录
	         var findRow = dw_hz.Find("fplsh='" + fplsh + "' ", 1, dw_hz.RowCount());
	         if (findRow > 0) {
	             dw_hz.ScrollToRow(findRow);
	         }
	         dw_hz.SetProcessing(false);
	         dw_hz.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_mx_keyDown 如果是在最后一行的金额列按回车，则自动增加一行

	 this.dw_mx_keyDown = function (keyCode, keyFlags) {

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

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

	             dw_mx.ScrollToRow(dw_mx.InsertRow(0));

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

	 }
	 //#endregion

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

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

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

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

	 }
	 //#endregion

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

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

	     }

	 }
	 //#endregion

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

	 this.dw_mx_top_doubleClicked = function (x, y, row, dwoName) {
	     if (dwoName == "bmbh") {
	         curW_OPEN_BMDA.SetDataWindow(dw_mx_top, "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_mx_top, "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_mx_top_editChanged  自动查供应商

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

	 //#region this.dw_mx_top_itemChanged  检查供应商数据完整性

	 this.dw_mx_top_itemChanged = function (row, dwoName, data) {

	     if (dwoName == "wldw") {
	         dw_mx_top.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_mx_top.SetItem(row, "wldw", dwc.GetItemString(findRow, "wldw"));
	         }
	       
	     }
	     if (dwoName == "bmbh") {
	         dw_mx_top.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_mx_top.SetItem(row, "bmbh", dwc.GetItemString(findRow, "bmbh"));
	         }
	     }
	     if (dwoName == "jbr") {
	         dw_mx_top.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_mx_top.SetItem(row, "jbr", dwc.GetItemString(findRow, "ygbh"));
	         }
	     }
	     if (dwoName == "tpr") {
	         dw_mx_top.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_mx_top.SetItem(row, "tpr", dwc.GetItemString(findRow, "ygbh"));
	         }
	     }
	 }
	 //#endregion 

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

	 this.dw_mx_top_keyDown = function (keyCode, keyFlags) {
	     if (keyCode == 13 && dw_mx_top.GetColumnName() == "bzxx") {
	         if (dw_mx_top.GetItemString(1, "fphm") == "") {

	             alert("请先填写发票号码!");
	             dw_mx_top.SetColumn("fphm");
	             dw_mx_top.SetFocus();
	             return;
	         }
	         if (dw_mx.GetItemString(1, "djlx") != "") {
	             dw_mx.ScrollToRow(dw_mx.InsertRow(0));
	             dw_mx.SetColumn("djlx");
	             dw_mx.SetFocus();
	         } else {
	             dw_mx.ScrollToRow(1);
	             dw_mx.SetColumn("djlx");
	             dw_mx.SetFocus();
	         }
	         return 1;
	     }
	 }
	 //#endregion 

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