protected void bt_Approve_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ForcastID"] != 0)
        {
            string wftitle = "经销商销量预估流程申请ID:";
            Save();
            if ((int)ViewState["ClientType"] == 3)
            {
                wftitle = "零售商销量预估流程申请ID:";
            }

            Org_StaffBLL        bll         = new Org_StaffBLL((int)Session["UserID"]);
            NameValueCollection dataobjects = new NameValueCollection();
            dataobjects.Add("ID", ViewState["ForcastID"].ToString());
            dataobjects.Add("OrganizeCity", bll.Model.OrganizeCity.ToString());
            dataobjects.Add("ApplyFee", SVM_ClassifyForcastBLL.GetForcastSumPrice((int)ViewState["ForcastID"]).ToString());
            int TaskID = EWF_TaskBLL.NewTask("SVM_ClassifyForcast_Approve", (int)Session["UserID"], wftitle + ViewState["ForcastID"].ToString(), "~/SubModule/SVM/ClassifyForcastDetail.aspx?ClientID=" + ViewState["ClientID"].ToString() + "&ForcastID=" + ViewState["ForcastID"].ToString(), dataobjects);
            if (TaskID > 0)
            {
                SVM_ClassifyForcastBLL.Submit((int)ViewState["ForcastID"], TaskID);
                new EWF_TaskBLL(TaskID).Start();        //直接启动流程
            }
            bt_Approve.Enabled = false;
            Response.Redirect("~/SubModule/SVM/ClassifyForcast.aspx");
        }
    }
    private void Save()
    {
        SVM_ClassifyForcastBLL bll       = new SVM_ClassifyForcastBLL((int)ViewState["ForcastID"]);
        DropDownList           ddl_Month = (DropDownList)UC_DetailView1.FindControl("SVM_ClassifyForcast_AccountMonth");

        bll.Model.AccountMonth = int.Parse(ddl_Month.SelectedValue);
        bll.Update();
        foreach (GridViewRow gr in gv_List.Rows)
        {
            int id = (int)gv_List.DataKeys[gr.RowIndex][0];
            SVM_ClassifyForcast_Detail item = bll.GetDetailModel(id);

            TextBox tbx_Amount = (TextBox)gr.FindControl("tbx_Amount");
            if (tbx_Amount != null && tbx_Amount.Enabled)
            {
                item.Amount = decimal.Parse(tbx_Amount.Text);
            }
            else
            {
                Label   lbl_Sales = (Label)gr.FindControl("lbl_Sales");
                TextBox tbx_Rate  = (TextBox)gr.FindControl("tbx_Rate");
                if (lbl_Sales != null && tbx_Rate != null)
                {
                    item.Rate = decimal.Parse(tbx_Rate.Text);
                }
                item.Amount = decimal.Parse(lbl_Sales.Text) * decimal.Parse(tbx_Rate.Text) / 100;
            }
            item.Amount = Math.Round(item.Amount, 1);
            item.Remark = ((TextBox)gr.FindControl("tbx_Remark")).Text;

            bll.UpdateDetail(item);
        }
        MessageBox.Show(this, "保存成功!");
        BindGrid();
    }
    private void BindData()
    {
        SVM_ClassifyForcastBLL bll = new SVM_ClassifyForcastBLL((int)ViewState["ForcastID"]);

        ViewState["ClientID"] = bll.Model.Client;
        ViewState["Month"]    = bll.Model.AccountMonth;
        CM_Client c = new CM_ClientBLL((int)ViewState["ClientID"]).Model;

        ViewState["ClientType"] = c.ClientType;

        UC_DetailView1.BindData(bll.Model);
        UC_DetailView1.SetControlsEnable(false);
        BindGrid();

        if (bll.Model.ApproveFlag == 1 || bll.Model.TaskID != 0)
        {
            gv_List.SetControlsEnable(false);
            bt_Approve.Visible             = false;
            bt_Save.Visible                = false;
            bt_Del.Visible                 = false;
            bt_DirectApprove.Visible       = false;
            cbx_UpdateAccountMonth.Visible = false;
            bt_Refresh.Visible             = false;
        }
    }
 protected void bt_Refresh_Click(object sender, EventArgs e)
 {
     if ((int)ViewState["ForcastID"] != 0)
     {
         SVM_ClassifyForcastBLL.RefreshAvgSales((int)ViewState["ForcastID"]);
         Response.Redirect("ClassifyForcastDetail.aspx?ForcastID=" + ViewState["ForcastID"].ToString());
     }
 }
    protected void btn_SalesForcast_Click(object sender, EventArgs e)
    {
        #region 已有分配单展示,没有则生成
        MCSTreeControl   tr_OrganizeCity = (MCSTreeControl)UC_DetailView1.FindControl("SVM_ClassifyForcast_OrganizeCity");
        MCSSelectControl select_Client   = (MCSSelectControl)UC_DetailView1.FindControl("SVM_ClassifyForcast_Client");
        DropDownList     ddl_Month       = (DropDownList)UC_DetailView1.FindControl("SVM_ClassifyForcast_AccountMonth");


        int id = SVM_ClassifyForcastBLL.Init(int.Parse(tr_OrganizeCity.SelectValue), int.Parse(ddl_Month.SelectedValue), int.Parse(select_Client.SelectValue == "" ? "0" : select_Client.SelectValue), (int)Session["UserID"]);

        Response.Redirect("ClassifyForcastDetail.aspx?ForcastID=" + id.ToString());
        #endregion
    }
 protected decimal GetForcastSumPrice(string ForcastID)
 {
     return(SVM_ClassifyForcastBLL.GetForcastSumPrice(int.Parse(ForcastID)));
 }
    private void BindGrid()
    {
        if (tr_list.Visible)
        {
            string condition = "1=1";

            #region 组织查询条件
            //管理片区及所有下属管理片区
            if (tr_OrganizeCity.SelectValue != "1")
            {
                Addr_OrganizeCityBLL orgcity = new Addr_OrganizeCityBLL(int.Parse(tr_OrganizeCity.SelectValue));
                string orgcitys = orgcity.GetAllChildNodeIDs();
                if (orgcitys != "")
                {
                    orgcitys += ",";
                }
                orgcitys += tr_OrganizeCity.SelectValue;

                condition += " AND SVM_ClassifyForcast.OrganizeCity IN (" + orgcitys + ")";
            }

            condition += " AND SVM_ClassifyForcast.AccountMonth BETWEEN " + ddl_BeginMonth.SelectedValue + " AND " + ddl_EndMonth.SelectedValue;

            if (rbl_ApproveFlag.SelectedValue != "0")
            {
                condition += " AND SVM_ClassifyForcast.ApproveFlag= " + rbl_ApproveFlag.SelectedValue;
            }

            if (select_Client.SelectValue != "")
            {
                condition += " AND SVM_ClassifyForcast.Client = " + select_Client.SelectValue;
            }
            else
            {
                condition += " AND CM_Client.ClientType=" + ViewState["ClientType"].ToString();

                if ((int)ViewState["ClientType"] == 2 && ViewState["DIClassify"] != null)
                {
                    condition += " AND MCS_SYS.dbo.UF_Spilt(CM_Client.ExtPropertys,'|',7)='" + ViewState["DIClassify"].ToString() + "'";
                }
            }
            #endregion

            gv_List.ConditionString = condition;
            gv_List.BindGrid();
        }
        else
        {
            int organizecity = int.Parse(tr_OrganizeCity.SelectValue);
            int client       = select_Client.SelectValue == "" ? 0 : int.Parse(select_Client.SelectValue);
            if (organizecity == 0)
            {
                organizecity = 1;
            }
            DataTable dt = SVM_ClassifyForcastBLL.GetSummary(organizecity, client, int.Parse(ddl_BeginMonth.SelectedValue), int.Parse(ddl_EndMonth.SelectedValue), int.Parse(ViewState["ClientType"].ToString()));

            DataRow dr = dt.NewRow();
            dr["Classify"]  = "合计";
            dr["SumAmount"] = dt.Compute("Sum(SumAmount)", "true");
            dt.Rows.Add(dr);
            gv_Summary.DataSource = dt;
            gv_Summary.DataBind();
        }
    }
    private void Save()
    {
        SVM_ClassifyForcastBLL bll = new SVM_ClassifyForcastBLL((int)ViewState["ForcastID"]);
        DropDownList ddl_Month = (DropDownList)UC_DetailView1.FindControl("SVM_ClassifyForcast_AccountMonth");
        bll.Model.AccountMonth = int.Parse(ddl_Month.SelectedValue);
        bll.Update();
        foreach (GridViewRow gr in gv_List.Rows)
        {
            int id = (int)gv_List.DataKeys[gr.RowIndex][0];
            SVM_ClassifyForcast_Detail item = bll.GetDetailModel(id);

            TextBox tbx_Amount = (TextBox)gr.FindControl("tbx_Amount");
            if (tbx_Amount != null && tbx_Amount.Enabled)
                item.Amount = decimal.Parse(tbx_Amount.Text);
            else
            {
                Label lbl_Sales = (Label)gr.FindControl("lbl_Sales");
                TextBox tbx_Rate = (TextBox)gr.FindControl("tbx_Rate");
                if (lbl_Sales != null && tbx_Rate != null)
                    item.Rate = decimal.Parse(tbx_Rate.Text);
                item.Amount = decimal.Parse(lbl_Sales.Text) * decimal.Parse(tbx_Rate.Text) / 100;
            }
            item.Amount = Math.Round(item.Amount, 1);
            item.Remark = ((TextBox)gr.FindControl("tbx_Remark")).Text;

            bll.UpdateDetail(item);
        }
        MessageBox.Show(this, "保存成功!");
        BindGrid();
    }
    private void BindData()
    {
        SVM_ClassifyForcastBLL bll = new SVM_ClassifyForcastBLL((int)ViewState["ForcastID"]);

        ViewState["ClientID"] = bll.Model.Client;
        ViewState["Month"] = bll.Model.AccountMonth;
        CM_Client c = new CM_ClientBLL((int)ViewState["ClientID"]).Model;
        ViewState["ClientType"] = c.ClientType;

        UC_DetailView1.BindData(bll.Model);
        UC_DetailView1.SetControlsEnable(false);
        BindGrid();

        if (bll.Model.ApproveFlag == 1 || bll.Model.TaskID != 0)
        {
            gv_List.SetControlsEnable(false);
            bt_Approve.Visible = false;
            bt_Save.Visible = false;
            bt_Del.Visible = false;
            bt_DirectApprove.Visible = false;
            cbx_UpdateAccountMonth.Visible = false;
            bt_Refresh.Visible = false;
        }
    }