protected void bt_Submit_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            bt_OK_Click(null, null);

            FNA_BudgetExtraApplyBLL bll = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]);

            #region 发起工作流
            NameValueCollection dataobjects = new NameValueCollection();
            dataobjects.Add("ID", ViewState["ID"].ToString());
            dataobjects.Add("OrganizeCity", bll.Model.OrganizeCity.ToString());
            dataobjects.Add("AccountMonth", bll.Model.AccountMonth.ToString());
            dataobjects.Add("ExtraAmount", bll.Model.ExtraAmount.ToString("0.##"));
            dataobjects.Add("FeeType", bll.Model.FeeType.ToString());
            dataobjects.Add("ExtraType", bll.Model["ExtraType"]);
            dataobjects.Add("SupportOrganizeCity", bll.Model["SupportOrganizeCity"]);

            #region 组合审批任务主题
            string title = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OrganizeCity", bll.Model.OrganizeCity) + " 申请扩增预算额度,申请扩增额度:" + bll.Model.ExtraAmount.ToString("0.##");
            #endregion

            int TaskID = EWF_TaskBLL.NewTask("FNA_BudgetExtraApplyFlow", (int)Session["UserID"], title, "~/SubModule/FNA/Budget/BudgetExtraApplyDetail.aspx?ID=" + ViewState["ID"].ToString(), dataobjects);
            if (TaskID <= 0)
            {
                MessageBox.Show(this, "对不起,工作流发起失败,请与管理员联系!");
                return;
            }
            bll.Submit((int)Session["UserID"], TaskID);
            new EWF_TaskBLL(TaskID).Start();        //直接启动流程
            #endregion

            MessageBox.ShowAndRedirect(this, "预算扩增提交成功!", Page.ResolveClientUrl("~/SubModule/EWF/TaskDetail.aspx?TaskID=" + TaskID.ToString()));
        }
    }
    protected void bt_SaveAdjust_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            decimal adjustamount     = 0;
            TextBox tbx_AdjustAmount = (TextBox)pl_detail.FindControl("FNA_BudgetExtraApply_AdjustAmount");
            if (tbx_AdjustAmount != null)
            {
                decimal.TryParse(tbx_AdjustAmount.Text, out adjustamount);
            }

            FNA_BudgetExtraApplyBLL _bll = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]);
            if (_bll.Model.ExtraAmount < adjustamount)
            {
                MessageBox.Show(this, "扣减金额不能大于申请金额!");
                return;
            }
            decimal OldAdjustCost = _bll.Model.AdjustAmount;
            _bll.Model.AdjustAmount = adjustamount;
            _bll.Model.UpdateStaff  = (int)Session["UserID"];
            _bll.Update();
            FNA_BudgetExtraApplyBLL.UpdateAdjustRecord(_bll.Model.ID, (int)Session["UserID"], _bll.Model.FeeType, OldAdjustCost.ToString("0.##"), _bll.Model.AdjustAmount.ToString("0.##"), _bll.Model["AdjustReason"]);
            BindData();
            MessageBox.Show(this, "保存扣减金额成功!");
        }
    }
    private void BindData()
    {
        FNA_BudgetExtraApply m = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]).Model;

        if (m != null)
        {
            pl_detail.BindData(m);

            if (m.ApproveTask > 0 || m.ApproveFlag == 1 || m.InsertStaff != (int)Session["UserID"])
            {
                lb_ApproveAmount.Text = (m.ExtraAmount - m.AdjustAmount).ToString("0.##");
                pl_detail.SetControlsEnable(false);
                bt_OK.Visible     = false;
                bt_Submit.Visible = false;

                //允许调整审批金额
                if (m.ApproveTask > 0 && m.ApproveFlag == 2 &&
                    Request.QueryString["Decision"] != null && Request.QueryString["Decision"] == "Y")
                {
                    TextBox tbx_AdjustAmount = (TextBox)pl_detail.FindControl("FNA_BudgetExtraApply_AdjustAmount");
                    tbx_AdjustAmount.Enabled = true;
                    bt_SaveAdjust.Visible    = true;
                }
            }

            if (!string.IsNullOrEmpty(m.ApproveTask.ToString()) && m.ApproveTask != 0)
            {
                bt_Submit.Visible = false;
            }

            if (m.ApproveFlag == 2)
            {
                tbl_BudgetInfo.Visible = true;
                BindBudget(m.AccountMonth, m.OrganizeCity, m.FeeType);
            }

            #region 获取当月该费用类型累计已批复扩增金额
            //lb_SumExtraInfo.Text = new Addr_OrganizeCityBLL(m.OrganizeCity).Model.Name +
            //    ":<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetExtraAmount(m.AccountMonth, m.OrganizeCity, m.FeeType, false).ToString("0.##") + "</b></font>  ";

            //int staffcity = new Org_StaffBLL((int)Session["UserID"]).Model.OrganizeCity;
            //if (staffcity == 0) staffcity = 1;
            //if (staffcity != 1)
            //{
            //    staffcity = TreeTableBLL.GetSuperIDByLevel("MCS_SYS.dbo.Addr_OrganizeCity", m.OrganizeCity, new Addr_OrganizeCityBLL(staffcity).Model.Level);
            //}

            //lb_SumExtraInfo.Text += new Addr_OrganizeCityBLL(staffcity).Model.Name +
            //    ":<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetExtraAmount(m.AccountMonth, staffcity, m.FeeType, true).ToString("0.##") + "</b></font>";
            #endregion

            #region 显示各扩增类型已批复扩增金额
            DropDownList ddl_ExtraType = (DropDownList)pl_detail.FindControl("FNA_BudgetExtraApply_ExtraType");
            foreach (ListItem item in ddl_ExtraType.Items)
            {
                if (item.Value != "0")
                {
                    lb_SumExtraInfo1.Text += item.Text + "总计:<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetModelList("").Where(p => p.ApproveFlag == 1 && p.AccountMonth == m.AccountMonth && p.OrganizeCity == m.OrganizeCity && p.FeeType == m.FeeType && p["ExtraType"] == item.Value).Select(p => p.ExtraAmount - p.AdjustAmount).Sum().ToString() + "</b></font>  ";
                }
            }
            int staffcity = new Org_StaffBLL((int)Session["UserID"]).Model.OrganizeCity;
            if (staffcity == 0)
            {
                staffcity = 1;
            }
            if (staffcity != 1)
            {
                staffcity = TreeTableBLL.GetSuperIDByLevel("MCS_SYS.dbo.Addr_OrganizeCity", m.OrganizeCity, new Addr_OrganizeCityBLL(staffcity).Model.Level);
            }
            foreach (ListItem item in ddl_ExtraType.Items)
            {
                if (item.Value != "0")
                {
                    lb_SumExtraInfo2.Text += item.Text + "总计:<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetModelList(" MCS_SYS.dbo.UF_IsChildOrganizeCity(" + staffcity.ToString() + ",OrganizeCity)=0").Where(p => p.ApproveFlag == 1 && p.AccountMonth == m.AccountMonth && p.OrganizeCity == m.OrganizeCity && p.FeeType == m.FeeType && p["ExtraType"] == item.Value).Select(p => p.ExtraAmount - p.AdjustAmount).Sum().ToString() + "</b></font>  ";
                }
            }
            #endregion
        }
    }
    protected void bt_OK_Click(object sender, EventArgs e)
    {
        FNA_BudgetExtraApplyBLL _bll;

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]);
        }
        else
        {
            //新增
            _bll = new FNA_BudgetExtraApplyBLL();
            _bll.Model.AccountMonth = AC_AccountMonthBLL.GetCurrentMonth();
        }
        pl_detail.GetData(_bll.Model);

        //_bll.Model["SheetCode"] = FNA_BudgetExtraApplyBLL.GenerateSheetCode(_bll.Model.OrganizeCity);在存储过程中实现
        #region 判断必填项
        if (_bll.Model.OrganizeCity == 0)
        {
            MessageBox.Show(this, "必须正确选择管理片区!");
            return;
        }

        if (_bll.Model.FeeType == 0)
        {
            MessageBox.Show(this, "必须正确选择费用类型!");
            return;
        }
        if (_bll.Model["ExtraType"] == "0")
        {
            MessageBox.Show(this, "必须正确选择扩增费用类别!");
            return;
        }
        #endregion

        #region 判断上级直至部经理手中有无预算
        decimal sumbudget         = 0;
        Addr_OrganizeCityBLL city = new Addr_OrganizeCityBLL(_bll.Model.OrganizeCity);
        DataTable            dt   = city.GetFullPath();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Addr_OrganizeCity c = new Addr_OrganizeCityBLL((int)dt.Rows[i]["ID"]).Model;
            if (c == null || c.ID == 1 || c.ID == _bll.Model.OrganizeCity || c.Level == 1)
            {
                continue;                                                                                  //忽略总部
            }
            sumbudget += FNA_BudgetBLL.GetUsableAmount(_bll.Model.AccountMonth, c.ID, _bll.Model.FeeType);
        }

        if (sumbudget > _bll.Model.ExtraAmount)
        {
            MessageBox.Show(this, "对不起,您的上级领导预算余额大于您要扩增的金额,请与领导沟通申请要求分配预算给您,而无需提交扩增预算流程!");
            return;
        }
        #endregion

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];
            if (_bll.Update() == 0)
            {
                if (sender != null)
                {
                    MessageBox.ShowAndRedirect(this, "修改成功!", "BudgetExtraApplyDetail.aspx?ID=" + ViewState["ID"].ToString());
                }
            }
        }
        else
        {
            //新增

            _bll.Model.ApproveFlag = 2;
            _bll.Model.InsertStaff = (int)Session["UserID"];
            ViewState["ID"]        = _bll.Add();
            bt_Submit.Visible      = true;
            if ((int)ViewState["ID"] > 0)
            {
                if (sender != null)
                {
                    MessageBox.ShowAndRedirect(this, "新增成功!", "BudgetExtraApplyDetail.aspx?ID=" + ViewState["ID"].ToString());
                }
            }
        }
    }
Beispiel #5
0
    private void BindGrid()
    {
        int month        = int.Parse(ddl_Month.SelectedValue);
        int organizecity = int.Parse(tr_OrganizeCity.SelectValue);
        int level        = int.Parse(ddl_Level.SelectedValue);
        int feetype      = int.Parse(ddl_FeeType.SelectedValue);
        int state        = int.Parse(ddl_State.SelectedValue);
        int flag         = int.Parse(ddl_Flag.SelectedValue);

        if (MCSTabControl1.SelectedIndex == 0)
        {
            #region 显示汇总单数据源
            Dictionary_Data dicFeeType = null;
            if (feetype > 0)
            {
                dicFeeType = DictionaryBLL.GetDicCollections("FNA_FeeType")[feetype.ToString()];
            }
            DataTable dtSummary_Sub;
            DataTable dtSummary = FNA_FeeApplyBLL.GetSummaryTotal(month, organizecity, level, feetype, state, flag, int.Parse(Session["UserID"].ToString()));
            if (dtSummary.Rows.Count == 0)
            {
                gv_List.DataBind();
                return;
            }
            else
            {
                dtSummary_Sub = FNA_FeeApplyBLL.GetSummaryTotal_Sub(month, organizecity, level, feetype, state, flag, int.Parse(Session["UserID"].ToString()));
            }
            #region 矩阵化数据表,扩展表数据列
            dtSummary.Columns.Add(new DataColumn("ID", Type.GetType("System.Int32")));
            if (level < 10)
            {
                foreach (DataRow row in dtSummary.Rows)
                {
                    row["ID"] = row["OrganizeCity"];
                }
                dtSummary = MatrixTable.Matrix(dtSummary, new string[] { "ID", "管理片区名称" },
                                               new string[] { "FeeTypeName", "AccountTitleName" }, "ApplyCost", true, true);
            }
            else
            {
                if (level == 10)
                {
                    //按经销商查看
                    #region 将经销商的ID赋至表ID列
                    foreach (DataRow row in dtSummary.Rows)
                    {
                        row["ID"]    = row["经销商ID"] == DBNull.Value ? 0 : row["经销商ID"];
                        row["经销商名称"] = row["经销商名称"] == DBNull.Value ? "无" : row["经销商名称"];
                    }
                    #endregion

                    dtSummary = MatrixTable.Matrix(dtSummary, new string[] { "ID", "管理片区名称", "经销商名称" },
                                                   new string[] { "FeeTypeName", "AccountTitleName" }, "ApplyCost", true, true);
                }
                else if (level == 20)
                {
                    //按门店查看
                    #region 将门店的ID赋至表ID列
                    foreach (DataRow row in dtSummary.Rows)
                    {
                        row["ID"]   = row["客户ID"] == DBNull.Value ? 0 : row["客户ID"];
                        row["客户名称"] = row["客户名称"] == DBNull.Value ? "无" : row["客户名称"];
                    }
                    #endregion

                    dtSummary = MatrixTable.Matrix(dtSummary, new string[] { "ID", "管理片区名称", "客户名称" },
                                                   new string[] { "FeeTypeName", "AccountTitleName" }, "ApplyCost", true, true);
                }
            }
            dtSummary = MatrixTable.ColumnSummaryTotal(dtSummary, new int[] { 1 }, new string[] { "sales" });
            dtSummary.Columns["合计"].ColumnName = "费用合计";

            dtSummary.Columns.Add("本月费用合计", Type.GetType("System.Decimal"));
            dtSummary.Columns.Add("预计销量", Type.GetType("System.Decimal"));
            dtSummary.Columns.Add("上月销量", Type.GetType("System.Decimal"));
            dtSummary.Columns.Add("平均销量", Type.GetType("System.Decimal"));
            dtSummary.Columns.Add("预计费点", Type.GetType("System.String"));

            if (dicFeeType != null && dicFeeType.Description == "BudgetControl" && level < 10)
            {
                dtSummary.Columns.Add("预算总额", Type.GetType("System.Decimal"));
                dtSummary.Columns.Add("其中扩增额", Type.GetType("System.Decimal"));
                dtSummary.Columns.Add("预算余额", Type.GetType("System.Decimal"));
                dtSummary.Columns.Add("终结费用", Type.GetType("System.Decimal"));
            }

            if (dicFeeType != null && dicFeeType.Description == "FeeRateControl")
            {
                dtSummary.Columns.Add("上月余额", Type.GetType("System.Decimal"));
            }

            #endregion

            decimal sumTotalVolume = 0, sumAvgVolume = 0, sumSalesForcast = 0, sumHappenApplyCost = 0;
            decimal sumTotalBudget = 0, sumExtBudget = 0, sumPreMonthBudgetBalance = 0, sumUsableAmount = 0, sumCancelCost = 0;

            int premonth = month - 1;
            if (premonth >= AC_AccountMonthBLL.GetCurrentMonth())
            {
                premonth = AC_AccountMonthBLL.GetCurrentMonth() - 1;
            }

            foreach (DataRow row in dtSummary.Rows)
            {
                int id = 0;

                if (int.TryParse(row["ID"].ToString(), out id) && id > 0)
                {
                    string filter       = "ID=" + id;
                    bool   includechild = false;
                    if (level < 10 && new Addr_OrganizeCityBLL(id).Model.Level >= level)
                    {
                        includechild = true;
                    }

                    #region 计算销量数据

                    decimal   happenApplyCost = 0, forcast = 0, preSales = 0, aVGSales = 0, feeRate = 0;
                    DataRow[] drows = dtSummary_Sub.Select(filter);
                    if (drows.Length > 0)
                    {
                        decimal.TryParse(drows[0]["HappenApplyCost"].ToString(), out happenApplyCost);
                        decimal.TryParse(drows[0]["Forcast"].ToString(), out forcast);
                        decimal.TryParse(drows[0]["PreSales"].ToString(), out preSales);
                        decimal.TryParse(drows[0]["AVGSales"].ToString(), out aVGSales);
                        decimal.TryParse(drows[0]["FeeRate"].ToString(), out feeRate);
                    }
                    row["本月费用合计"] = happenApplyCost.ToString("0.##");
                    row["上月销量"]   = preSales.ToString("0.##");
                    row["平均销量"]   = aVGSales.ToString("0.##");
                    row["预计销量"]   = forcast.ToString("0.##");
                    row["预计费点"]   = feeRate.ToString("0.##%");

                    sumHappenApplyCost += (decimal)row["本月费用合计"];
                    sumTotalVolume     += (decimal)row["上月销量"];
                    sumAvgVolume       += (decimal)row["平均销量"];
                    sumSalesForcast    += (decimal)row["预计销量"];
                    #endregion

                    #region 预算总额及余额
                    if (dicFeeType != null && dicFeeType.Description == "BudgetControl" && level < 10)
                    {
                        row["预算总额"]     = (FNA_BudgetBLL.GetSumBudgetAmount(month, id, feetype, includechild) + FNA_BudgetBLL.GetSumBudgetAmount(month, id, 0, includechild));
                        sumTotalBudget += (decimal)row["预算总额"];

                        row["其中扩增额"]  = (FNA_BudgetExtraApplyBLL.GetExtraAmount(month, id, feetype, includechild) + FNA_BudgetExtraApplyBLL.GetExtraAmount(month, id, 0, includechild));
                        sumExtBudget += (decimal)row["其中扩增额"];

                        row["预算余额"]      = (FNA_BudgetBLL.GetUsableAmount(month, id, feetype, includechild) + FNA_BudgetBLL.GetUsableAmount(month, id, 0, includechild));
                        sumUsableAmount += (decimal)row["预算余额"];

                        row["终结费用"]    = (FNA_FeeApplyBLL.GetCancelCost(month, id, feetype, includechild) + FNA_FeeApplyBLL.GetCancelCost(month, id, 0, includechild));
                        sumCancelCost += (decimal)row["终结费用"];
                    }
                    #endregion

                    //#region 计算预计费点
                    //if (dicFeeType != null && dicFeeType.Description == "FeeRateControl" && level < 10)
                    //{
                    //    row["上月余额"] = FNA_BudgetBLL.GetUsableAmount(month - 1, id, feetype, includechild);
                    //    sumPreMonthBudgetBalance += (decimal)row["上月余额"];

                    //    if ((decimal)row["预计销量"] != 0)
                    //        row["预计费点"] = (((decimal)row["费用合计"] - (decimal)row["上月余额"]) / (decimal)row["预计销量"]).ToString("0.00%");
                    //}
                    //else if ((decimal)row["预计销量"] != 0)
                    //    row["预计费点"] = ((decimal)row["费用合计"] / (decimal)row["预计销量"]).ToString("0.00%");
                    //#endregion
                }

                #region 求合计行
                if (id == 0)
                {
                    row["本月费用合计"] = sumHappenApplyCost.ToString("0.##");
                    row["上月销量"]   = sumTotalVolume.ToString("0.##");
                    row["平均销量"]   = sumAvgVolume.ToString("0.##");
                    row["预计销量"]   = sumSalesForcast.ToString("0.##");

                    #region 预算总额及余额
                    if (dicFeeType != null && dicFeeType.Description == "BudgetControl" && level < 10)
                    {
                        row["预算总额"]  = sumTotalBudget.ToString("0.##");
                        row["其中扩增额"] = sumExtBudget.ToString("0.##");
                        row["预算余额"]  = sumUsableAmount.ToString("0.##");
                        row["终结费用"]  = sumCancelCost.ToString("0.##");
                    }
                    #endregion

                    #region 预计费点
                    if (sumSalesForcast != 0)
                    {
                        if (dicFeeType != null && dicFeeType.Description == "FeeRateControl" && level < 10)
                        {
                            row["上月余额"] = sumPreMonthBudgetBalance;
                            row["预计费点"] = (((decimal)row["费用合计"] - sumPreMonthBudgetBalance) / sumSalesForcast).ToString("0.##%");
                        }
                        else
                        {
                            row["预计费点"] = ((decimal)row["费用合计"] / sumSalesForcast).ToString("0.##%");
                        }
                    }
                    #endregion
                }
                #endregion
            }


            gv_List.DataSource = dtSummary;
            gv_List.DataBind();

            if (dtSummary.Columns.Count >= 24)
            {
                gv_List.Width = new Unit(dtSummary.Columns.Count * 55);
            }
            else
            {
                gv_List.Width = new Unit(100, UnitType.Percentage);
            }

            MatrixTable.GridViewMatric(gv_List);

            if (level == 20)
            {
                //按终端门店显示时,将上下行相同管理片区合并
                MatrixTable.GridViewMergSampeValueRow(gv_List, 0);
                MatrixTable.GridViewMergSampeValueRow(gv_List, 1);
            }

            #region 是否可以批量审批
            if (state != 1 || level >= 10)
            {
                gv_List.Columns[0].ItemStyle.Width = new Unit(1);
                foreach (GridViewRow row in gv_List.Rows)
                {
                    row.FindControl("bt_Approved").Visible   = false;
                    row.FindControl("bt_UnApproved").Visible = false;
                }
            }
            else
            {
                gv_List.Columns[0].ItemStyle.Width = new Unit(68);
            }
            #endregion

            #endregion
        }
        else
        {
            string condition = "1=1";

            #region 组织明细记录的查询条件
            //管理片区及所有下属管理片区
            if (tr_OrganizeCity.SelectValue == "0")
            {
                #region 绑定用户可管辖的管理片区
                Org_StaffBLL staff = new Org_StaffBLL((int)Session["UserID"]);
                tr_OrganizeCity.DataSource = staff.GetStaffOrganizeCity();

                if (tr_OrganizeCity.DataSource.Select("ID = 1").Length > 0)
                {
                    tr_OrganizeCity.RootValue   = "0";
                    tr_OrganizeCity.SelectValue = "1";
                }
                else
                {
                    tr_OrganizeCity.RootValue   = new Addr_OrganizeCityBLL(staff.Model.OrganizeCity).Model.SuperID.ToString();
                    tr_OrganizeCity.SelectValue = staff.Model.OrganizeCity.ToString();
                }
                if ((int)ViewState["OrganizeCity"] > 0)
                {
                    tr_OrganizeCity.SelectValue = ViewState["OrganizeCity"].ToString();
                }
                #endregion
            }
            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 FNA_FeeApply.OrganizeCity IN (" + orgcitys + ")";
            }

            //会计月条件
            condition += " AND FNA_FeeApply.AccountMonth = " + ddl_Month.SelectedValue;

            //费用类型
            if (ddl_FeeType.SelectedValue != "0")
            {
                condition += " AND FNA_FeeApply.FeeType = " + ddl_FeeType.SelectedValue;
            }

            int accounttile = 0;
            int.TryParse(tr_AccountTitle.SelectValue, out accounttile);
            decimal _cost = 0;
            decimal.TryParse(tbx_ApplyCost.Text, out _cost);

            //费用科目
            if (accounttile > 1)
            {
                DataTable dt  = TreeTableBLL.GetAllChildByNodes("MCS_PUB.dbo.AC_AccountTitle", "ID", "SuperID", accounttile.ToString());
                string    ids = "";
                foreach (DataRow dr in dt.Rows)
                {
                    ids += dr["ID"].ToString() + ",";
                }
                ids += accounttile.ToString();

                if (_cost == 0)
                {
                    condition += " AND FNA_FeeApply.ID IN (SELECT ApplyID FROM MCS_FNA.dbo.FNA_FeeApplyDetail WHERE AccountTitle IN(" + ids + ") AND FNA_FeeApplyDetail.ApplyID=FNA_FeeApply.ID)";
                }
                else
                {
                    condition += " AND FNA_FeeApply.ID IN (SELECT ApplyID FROM MCS_FNA.dbo.FNA_FeeApplyDetail WHERE AccountTitle IN(" + ids + ") AND (ApplyCost+AdjustCost)" + ddl_ApplyCostOP.SelectedValue + "  " + _cost.ToString() + " AND FNA_FeeApplyDetail.ApplyID=FNA_FeeApply.ID)";
                }
            }
            else if (_cost != 0)//金额判断
            {
                condition += " AND FNA_FeeApply.ID IN (SELECT ApplyID FROM MCS_FNA.dbo.FNA_FeeApplyDetail WHERE (ApplyCost+AdjustCost)" + ddl_ApplyCostOP.SelectedValue + "  " + _cost.ToString() + " AND FNA_FeeApplyDetail.ApplyID=FNA_FeeApply.ID)";
            }

            //审批状态
            if (ddl_State.SelectedValue == "0")
            {
                condition += " AND FNA_FeeApply.State IN (2,3) ";
            }
            else if (ddl_State.SelectedValue == "1")
            {
                condition +=
                    @" AND FNA_FeeApply.State = 2 AND FNA_FeeApply.ApproveTask IN 
(SELECT EWF_Task_Job.Task FROM  MCS_EWF.dbo.EWF_Task_JobDecision INNER JOIN
    MCS_EWF.dbo.EWF_Task_Job ON EWF_Task_JobDecision.Job = EWF_Task_Job.ID 
WHERE EWF_Task_JobDecision.RecipientStaff=" + Session["UserID"].ToString() + @" AND
    EWF_Task_JobDecision.DecisionResult=1 and EWF_Task_Job.Status=3)";
            }
            else if (ddl_State.SelectedValue == "2")
            {
                condition += " AND FNA_FeeApply.State = 3 ";
            }
            else if (ddl_State.SelectedValue == "3")
            {
                AC_AccountMonth m = new AC_AccountMonthBLL(month).Model;
                condition +=
                    @" AND FNA_FeeApply.State IN (2,3) AND FNA_FeeApply.ApproveTask IN 
(SELECT EWF_Task_Job.Task FROM  MCS_EWF.dbo.EWF_Task_JobDecision INNER JOIN
	MCS_EWF.dbo.EWF_Task_Job ON EWF_Task_JobDecision.Job = EWF_Task_Job.ID 
WHERE EWF_Task_JobDecision.RecipientStaff=" + Session["UserID"].ToString() + @" AND
	EWF_Task_JobDecision.DecisionResult IN(2,5,6) AND 
	EWF_Task_JobDecision.DecisionTime BETWEEN DATEADD(month,-1,'"     + m.BeginDate.ToString("yyyy-MM-dd") + @"') AND 
		DATEADD(month,3,'"         + m.BeginDate.ToString("yyyy-MM-dd") + @"'))";
            }
            #endregion

            gv_ListDetail.ConditionString = condition;
            gv_ListDetail.BindGrid();

            btn_Approve.Visible   = state == 1;
            btn_UnApprove.Visible = state == 1;
            gv_ListDetail.Columns[gv_ListDetail.Columns.Count - 1].Visible = state == 1;
        }
    }
    protected void bt_OK_Click(object sender, EventArgs e)
    {
        FNA_BudgetExtraApplyBLL _bll;
        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]);
        }
        else
        {
            //新增
            _bll = new FNA_BudgetExtraApplyBLL();
            _bll.Model.AccountMonth = AC_AccountMonthBLL.GetCurrentMonth();
        }
        pl_detail.GetData(_bll.Model);

        //_bll.Model["SheetCode"] = FNA_BudgetExtraApplyBLL.GenerateSheetCode(_bll.Model.OrganizeCity);在存储过程中实现
        #region 判断必填项
        if (_bll.Model.OrganizeCity == 0)
        {
            MessageBox.Show(this, "必须正确选择管理片区!");
            return;
        }

        if (_bll.Model.FeeType == 0)
        {
            MessageBox.Show(this, "必须正确选择费用类型!");
            return;
        }
        if (_bll.Model["ExtraType"] == "0")
        {
            MessageBox.Show(this, "必须正确选择扩增费用类别!");
            return;
        }
        #endregion

        #region 判断上级直至部经理手中有无预算
        decimal sumbudget = 0;
        Addr_OrganizeCityBLL city = new Addr_OrganizeCityBLL(_bll.Model.OrganizeCity);
        DataTable dt = city.GetFullPath();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Addr_OrganizeCity c = new Addr_OrganizeCityBLL((int)dt.Rows[i]["ID"]).Model;
            if (c == null || c.ID == 1 || c.ID == _bll.Model.OrganizeCity || c.Level == 1) continue;       //忽略总部

            sumbudget += FNA_BudgetBLL.GetUsableAmount(_bll.Model.AccountMonth, c.ID, _bll.Model.FeeType);
        }

        if (sumbudget > _bll.Model.ExtraAmount)
        {
            MessageBox.Show(this, "对不起,您的上级领导预算余额大于您要扩增的金额,请与领导沟通申请要求分配预算给您,而无需提交扩增预算流程!");
            return;
        }
        #endregion

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];
            if (_bll.Update() == 0)
            {
                if (sender != null) MessageBox.ShowAndRedirect(this, "修改成功!", "BudgetExtraApplyDetail.aspx?ID=" + ViewState["ID"].ToString());
            }
        }
        else
        {
            //新增

            _bll.Model.ApproveFlag = 2;
            _bll.Model.InsertStaff = (int)Session["UserID"];
            ViewState["ID"] = _bll.Add();
            bt_Submit.Visible = true;
            if ((int)ViewState["ID"] > 0)
            {
                if (sender != null) MessageBox.ShowAndRedirect(this, "新增成功!", "BudgetExtraApplyDetail.aspx?ID=" + ViewState["ID"].ToString());
            }
        }
    }
    private void BindData()
    {
        FNA_BudgetExtraApply m = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]).Model;
        if (m != null)
        {
            pl_detail.BindData(m);

            if (m.ApproveTask > 0 || m.ApproveFlag == 1 || m.InsertStaff != (int)Session["UserID"])
            {
                lb_ApproveAmount.Text = (m.ExtraAmount - m.AdjustAmount).ToString("0.##");
                pl_detail.SetControlsEnable(false);
                bt_OK.Visible = false;
                bt_Submit.Visible = false;

                //允许调整审批金额
                if (m.ApproveTask > 0 && m.ApproveFlag == 2 &&
                    Request.QueryString["Decision"] != null && Request.QueryString["Decision"] == "Y")
                {
                    TextBox tbx_AdjustAmount = (TextBox)pl_detail.FindControl("FNA_BudgetExtraApply_AdjustAmount");
                    tbx_AdjustAmount.Enabled = true;
                    bt_SaveAdjust.Visible = true;
                }
            }

            if (!string.IsNullOrEmpty(m.ApproveTask.ToString()) && m.ApproveTask != 0)
            {
                bt_Submit.Visible = false;
            }

            if (m.ApproveFlag == 2)
            {
                tbl_BudgetInfo.Visible = true;
                BindBudget(m.AccountMonth, m.OrganizeCity, m.FeeType);
            }

            #region 获取当月该费用类型累计已批复扩增金额
            //lb_SumExtraInfo.Text = new Addr_OrganizeCityBLL(m.OrganizeCity).Model.Name +
            //    ":<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetExtraAmount(m.AccountMonth, m.OrganizeCity, m.FeeType, false).ToString("0.##") + "</b></font>  ";

            //int staffcity = new Org_StaffBLL((int)Session["UserID"]).Model.OrganizeCity;
            //if (staffcity == 0) staffcity = 1;
            //if (staffcity != 1)
            //{
            //    staffcity = TreeTableBLL.GetSuperIDByLevel("MCS_SYS.dbo.Addr_OrganizeCity", m.OrganizeCity, new Addr_OrganizeCityBLL(staffcity).Model.Level);
            //}

            //lb_SumExtraInfo.Text += new Addr_OrganizeCityBLL(staffcity).Model.Name +
            //    ":<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetExtraAmount(m.AccountMonth, staffcity, m.FeeType, true).ToString("0.##") + "</b></font>";
            #endregion

            #region 显示各扩增类型已批复扩增金额
            DropDownList ddl_ExtraType = (DropDownList)pl_detail.FindControl("FNA_BudgetExtraApply_ExtraType");
            foreach (ListItem item in ddl_ExtraType.Items)
            {
                if (item .Value!="0")
                lb_SumExtraInfo1.Text += item.Text + "总计:<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetModelList("").Where(p => p.ApproveFlag == 1 && p.AccountMonth == m.AccountMonth && p.OrganizeCity == m.OrganizeCity && p.FeeType == m.FeeType && p["ExtraType"] == item.Value).Select(p => p.ExtraAmount - p.AdjustAmount).Sum().ToString() + "</b></font>  ";
            }
            int staffcity = new Org_StaffBLL((int)Session["UserID"]).Model.OrganizeCity;
            if (staffcity == 0) staffcity = 1;
            if (staffcity != 1)
            staffcity = TreeTableBLL.GetSuperIDByLevel("MCS_SYS.dbo.Addr_OrganizeCity", m.OrganizeCity, new Addr_OrganizeCityBLL(staffcity).Model.Level);
            foreach (ListItem item in ddl_ExtraType.Items)
            {
                if (item.Value != "0")
                lb_SumExtraInfo2.Text += item.Text + "总计:<b><font color=red>" + FNA_BudgetExtraApplyBLL.GetModelList(" MCS_SYS.dbo.UF_IsChildOrganizeCity(" + staffcity.ToString() + ",OrganizeCity)=0").Where(p => p.ApproveFlag == 1 && p.AccountMonth == m.AccountMonth && p.OrganizeCity == m.OrganizeCity && p.FeeType == m.FeeType && p["ExtraType"] == item.Value).Select(p => p.ExtraAmount - p.AdjustAmount).Sum().ToString() + "</b></font>  ";
            }
            #endregion
        }
    }
    protected void bt_Submit_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            bt_OK_Click(null, null);

            FNA_BudgetExtraApplyBLL bll = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]);

            #region 发起工作流
            NameValueCollection dataobjects = new NameValueCollection();
            dataobjects.Add("ID", ViewState["ID"].ToString());
            dataobjects.Add("OrganizeCity", bll.Model.OrganizeCity.ToString());
            dataobjects.Add("AccountMonth", bll.Model.AccountMonth.ToString());
            dataobjects.Add("ExtraAmount", bll.Model.ExtraAmount.ToString("0.##"));
            dataobjects.Add("FeeType", bll.Model.FeeType.ToString());
            dataobjects.Add("ExtraType", bll.Model["ExtraType"]);
            dataobjects.Add("SupportOrganizeCity", bll.Model["SupportOrganizeCity"]);

            #region 组合审批任务主题
            string title = TreeTableBLL.GetFullPathName("MCS_SYS.dbo.Addr_OrganizeCity", bll.Model.OrganizeCity) + " 申请扩增预算额度,申请扩增额度:" + bll.Model.ExtraAmount.ToString("0.##");
            #endregion

            int TaskID = EWF_TaskBLL.NewTask("FNA_BudgetExtraApplyFlow", (int)Session["UserID"], title, "~/SubModule/FNA/Budget/BudgetExtraApplyDetail.aspx?ID=" + ViewState["ID"].ToString(), dataobjects);
            if (TaskID <= 0)
            {
                MessageBox.Show(this, "对不起,工作流发起失败,请与管理员联系!");
                return;
            }
            bll.Submit((int)Session["UserID"], TaskID);
            new EWF_TaskBLL(TaskID).Start();        //直接启动流程
            #endregion

            MessageBox.ShowAndRedirect(this, "预算扩增提交成功!", Page.ResolveClientUrl("~/SubModule/EWF/TaskDetail.aspx?TaskID=" + TaskID.ToString()));
        }
    }
    protected void bt_SaveAdjust_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            decimal adjustamount = 0;
            TextBox tbx_AdjustAmount = (TextBox)pl_detail.FindControl("FNA_BudgetExtraApply_AdjustAmount");
            if (tbx_AdjustAmount != null) decimal.TryParse(tbx_AdjustAmount.Text, out adjustamount);

            FNA_BudgetExtraApplyBLL _bll = new FNA_BudgetExtraApplyBLL((int)ViewState["ID"]);
            if (_bll.Model.ExtraAmount < adjustamount)
            {
                MessageBox.Show(this, "扣减金额不能大于申请金额!");
                return;
            }
            decimal OldAdjustCost = _bll.Model.AdjustAmount;
            _bll.Model.AdjustAmount = adjustamount;
            _bll.Model.UpdateStaff = (int)Session["UserID"];
            _bll.Update();
            FNA_BudgetExtraApplyBLL.UpdateAdjustRecord(_bll.Model.ID, (int)Session["UserID"], _bll.Model.FeeType, OldAdjustCost.ToString("0.##"), _bll.Model.AdjustAmount.ToString("0.##"), _bll.Model["AdjustReason"]);
            BindData();
            MessageBox.Show(this, "保存扣减金额成功!");
        }
    }