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

    var self = this;
    this.Instance = null;
    var requestor = new PBWindow();
    var st_gysmc = new StaticText(requestor);
    var cb_3 = new CommandButton(requestor);
    var sle_gys = new SingleLineEdit(requestor);
    var dp_RqEnd = new DatePicker(requestor);
    var dp_RqBegin = new DatePicker(requestor);
    var dw_list = 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 (st_gysmc == null) st_gysmc = new StaticText(requestor);
        st_gysmc.Attach(win.Control("st_gysmc"));
        if (cb_3 == null) cb_3 = new CommandButton(requestor);
        cb_3.Attach(win.Control("cb_3"));
        if (sle_gys == null) sle_gys = new SingleLineEdit(requestor);
        sle_gys.Attach(win.Control("sle_gys"));
        if (dp_RqEnd == null) dp_RqEnd = new DatePicker(requestor);
        dp_RqEnd.Attach(win.Control("dp_RqEnd"));
        if (dp_RqBegin == null) dp_RqBegin = new DatePicker(requestor);
        dp_RqBegin.Attach(win.Control("dp_RqBegin"));
        if (dw_list == null) dw_list = new DataWindow(requestor);
        dw_list.Attach(win.Control("dw_list"));
        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.Changegys) == "function")
            win.AttachEvent("cb_3", "Clicked", self.Changegys);

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

        if (typeof (self.print) == "function")
            win.AttachEvent("btn_2", "Clicked", self.print);

        if (typeof (self.Exit) == "function")
            win.AttachEvent("btn_3", "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();

        st_gysmc.Detach(); st_gysmc = null;
        cb_3.Detach(); cb_3 = null;
        sle_gys.Detach(); sle_gys = null;
        dp_RqEnd.Detach(); dp_RqEnd = null;
        dp_RqBegin.Detach(); dp_RqBegin = null;
        dw_list.Detach(); dw_list = null;
        tb_1.Detach(); tb_1 = null;
        requestor.Detach(); requestor = null;

        setTimeout(CollectGarbage, 100);
    }

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

    var curW_OPEN_BMDA_NEW = new OpenWindowQueryNewBase();
    var curW_OPEN_BMDA_URL = "OpenWindowQueryNewBase";
    var curPzh = "";
    var curBmbh = "";
    this.SetDataWindow = function (dw_1, selfs, curpzhs, curBmbhs) {

        curPzh = curpzhs;
        curBmbh = curBmbhs
    }

    this.PostOpen = function () {

       // alert(curPzh);
    }

    this.Changegys = function (x, y, row, dwoName) {
        dwoName = "W_XJZCPZ_GYSZL_gysbh";
        curW_OPEN_BMDA_NEW.SetDataWindow(W_XJZCPZ_GYSZL, "gysbh", "gysmc", "N", self,dwoName)  // 这个用来给子窗口Set当前数据窗口
        requestor.Open(encodeURI("/Service/LoadWindow.ashx?WinName=A8ERP.DangAn.Base." + curW_OPEN_BMDA_URL + "&treeDw=dw_da_gysda_fl_tree&listDw=dw_pub_list_gysda&treeRootLabel=供应商分类&treeData=flbh&treeLabel=flmc&treeParentData=fflbh&treeLeave=islast&selectStyle="), curW_OPEN_BMDA_NEW, null);
    }

    this.SetSleValue = function (difBh, val) {
        if ("''" == val)
            val = "";

        if ("W_XJZCPZ_GYSZL_gysbh" == difBh) {

            sle_gys.SetText(val.split("#")[0]);
            st_gysmc.SetText(val.split("#")[1]);

        }

    }

    this.Exit = function () {
        requestor.Close();

    }
    this.Query = function () {
        var beginDate = UtcDateFormatToString(dp_RqBegin.GetValue());
        var endDate = UtcDateFormatToString(dp_RqEnd.GetValue());
        var curGys = sle_gys.GetText();

        var curXsze = "";
        if (curGys.length < 1) {
            alert("供应商不能为空，请检查。");
            return;
        }
        var curGysmc = st_gysmc.GetText();
        var curSql = " SELECT ISNULL(SUM(LSJE),0)" +
                     " FROM   DBO.YW_POS_TZ TZ LEFT JOIN DA_SP_SPDA SPDA ON TZ.SPKH=SPDA.SPKH" +
                     " WHERE  BMBH='" + curBmbh + "'" +
                     " AND    SPDA.GYSBH=" + curGys + "" +
                     " AND    CONVERT(DATE,RQ,120) BETWEEN '" + beginDate + "' AND '" + endDate + "'";

        var curGlobalDwExecSql = window.external.Control("dw_sql");
        curGlobalDwExecSql.Retrieve(curSql);
        if (curGlobalDwExecSql.RowCount() > 0) {
            curXsze = curGlobalDwExecSql.GetItemString(1, "value");
        } else {
            curXsze = 0;
        }


        dw_list.Retrieve(curPzh);
        var curBuRow = 3 - dw_list.RowCount();

        for (var i = 1; i <= curBuRow; i++) {
            dw_list.ScrollToRow(dw_list.InsertRow());
        }

        var curGxkk = dw_list.GetItemNumber(1, "sumJe");
        var curSfje = (curXsze - curGxkk).toFixed(2);
        dw_list.SetItem(1, "xm", "销售收入： " + curXsze);
        dw_list.SetItem(2, "xm", "各项扣款： " + curGxkk);
        dw_list.SetItem(3, "xm", "实付金额： " + curSfje);

        dw_list.SetItem(3, "bmmc", dw_list.GetItemString(1, "bmmc"));
        dw_list.SetItem(3, "fkfs_1", dw_list.GetItemString(1, "fkfs_1"));
        dw_list.SetItem(3, "khr", dw_list.GetItemString(1, "khr"));
        dw_list.SetItem(3, "zhmc", dw_list.GetItemString(1, "zhmc"));
        dw_list.SetItem(3, "yhkh", dw_list.GetItemString(1, "yhkh"));
        dw_list.SetItem(3, "bzxx", dw_list.GetItemString(1, "bzxx"));

        dw_list.Modify("title.text='" + curGysmc + " 租赁专柜结算单'");
        dw_list.Modify("qzrq.text='" + beginDate + " 至 " + endDate + "'");
        dw_list.Modify("gys.text='" + curGysmc + "'");

        var totalJe = GetRound4_2(curSfje);
        if (totalJe > 0) {
            var strRMB = GetNumConvertCurrency(curSfje);
            if (strRMB.split(":").length > 0) {
                strRMB = strRMB.split(":")[1];
            }
            dw_list.Modify("dxhjje.text='" + strRMB + "'");
        } else if (totalJe < 0) {
            var strRMB = GetNumConvertCurrency(Math.abs(curSfje));
            if (strRMB.split(":").length > 0) {
                strRMB = strRMB.split(":")[1];
            }
            dw_list.Modify("dxhjje.text='负" + strRMB + "'");
        } else {
            dw_list.Modify("dxhjje.text='零'");
        }
    }
    this.print = function () {
        dw_list.PrintPreview();
    }
}