public static BG_BudgetAllocation AddBG_BudgetAllocation(BG_BudgetAllocation bG_BudgetAllocation)
        {
            string sql =
                "INSERT BG_BudgetAllocation (DepID, PIID, BAAMon, SuppMon, BAAYear)" +
                "VALUES (@DepID, @PIID, @BAAMon, @SuppMon, @BAAYear)";

            sql += " ; SELECT @@IDENTITY";

            try
            {
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@DepID", bG_BudgetAllocation.DepID),
                    new SqlParameter("@PIID", bG_BudgetAllocation.PIID),
                    new SqlParameter("@BAAMon", bG_BudgetAllocation.BAAMon),
                    new SqlParameter("@SuppMon", bG_BudgetAllocation.SuppMon),
                    new SqlParameter("@BAAYear", bG_BudgetAllocation.BAAYear)
                };

                string IdStr = DBUnity.ExecuteScalar(CommandType.Text, sql, para);
                int newId = Convert.ToInt32(IdStr);
                return GetBG_BudgetAllocationByBAAID(newId);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        public static BG_BudgetAllocation GetMonDT(int year, int PIID, int depID)
        {
            string sql = "SELECT * FROM BG_BudgetAllocation WHERE PIID = @PIID and DepID = @DepID and BAAYear=@BAAYear";

            try
            {
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@DepID", depID),
                    new SqlParameter("@PIID", PIID),
                    new SqlParameter("@BAAYear", year)
                };
                DataTable dt = DBUnity.AdapterToTab(sql, para);

                if (dt.Rows.Count > 0)
                {
                    BG_BudgetAllocation bG_BudgetAllocation = new BG_BudgetAllocation();

                    bG_BudgetAllocation.BAAID = dt.Rows[0]["BAAID"] == DBNull.Value ? 0 : (int)dt.Rows[0]["BAAID"];
                    bG_BudgetAllocation.DepID = dt.Rows[0]["DepID"] == DBNull.Value ? 0 : (int)dt.Rows[0]["DepID"];
                    bG_BudgetAllocation.PIID = dt.Rows[0]["PIID"] == DBNull.Value ? 0 : (int)dt.Rows[0]["PIID"];
                    bG_BudgetAllocation.BAAMon = dt.Rows[0]["BAAMon"] == DBNull.Value ? 0 : (decimal)dt.Rows[0]["BAAMon"];
                    bG_BudgetAllocation.SuppMon = dt.Rows[0]["SuppMon"] == DBNull.Value ? 0 : (decimal)dt.Rows[0]["SuppMon"];
                    bG_BudgetAllocation.BAAYear = dt.Rows[0]["BAAYear"] == DBNull.Value ? 0 : (int)dt.Rows[0]["BAAYear"];

                    return bG_BudgetAllocation;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
 public static bool ModifyBG_BudgetAllocation(BG_BudgetAllocation bG_BudgetAllocation)
 {
     return BG_BudgetAllocationService.ModifyBG_BudgetAllocation(bG_BudgetAllocation);
 }
 public static bool DeleteBG_BudgetAllocation(BG_BudgetAllocation bG_BudgetAllocation)
 {
     return BG_BudgetAllocationService.DeleteBG_BudgetAllocation(bG_BudgetAllocation);
 }
 public static BG_BudgetAllocation AddBG_BudgetAllocation(BG_BudgetAllocation bG_BudgetAllocation)
 {
     return BG_BudgetAllocationService.AddBG_BudgetAllocation(bG_BudgetAllocation);
 }
 public static bool DeleteBG_BudgetAllocation(BG_BudgetAllocation bG_BudgetAllocation)
 {
     return DeleteBG_BudgetAllocationByBAAID( bG_BudgetAllocation.BAAID );
 }
        public static bool ModifyBG_BudgetAllocation(BG_BudgetAllocation bG_BudgetAllocation)
        {
            string sql =
                "UPDATE BG_BudgetAllocation " +
                "SET " +
                    "DepID = @DepID, " +
                    "PIID = @PIID, " +
                    "BAAMon = @BAAMon, " +
                    "SuppMon = @SuppMon, " +
                    "BAAYear = @BAAYear " +
                "WHERE BAAID = @BAAID";

            try
            {
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@BAAID", bG_BudgetAllocation.BAAID),
                    new SqlParameter("@DepID", bG_BudgetAllocation.DepID),
                    new SqlParameter("@PIID", bG_BudgetAllocation.PIID),
                    new SqlParameter("@BAAMon", bG_BudgetAllocation.BAAMon),
                    new SqlParameter("@SuppMon", bG_BudgetAllocation.SuppMon),
                    new SqlParameter("@BAAYear", bG_BudgetAllocation.BAAYear)
                };

                int t = DBUnity.ExecuteNonQuery(CommandType.Text, sql, para);
                if(t>0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
    //protected void GetSelect_DirectClick(object sender, DirectEventArgs e)
    //{
    //    string Strincome = Hidselector.Value.ToString();
    //    string[] income = Strincome.Split(',');
    //    DataTable dt = null;
    //    dt.Columns.Add();
    //    for (int i = 0; i < income.Count(); i++)
    //    {
    //    }
    //}
    protected void RemoteEdit(object sender, RemoteEditEventArgs e)
    {
        if (ParToDecimal.ParToDel(HidSupp.Text.ToString()) < 0 || ParToDecimal.ParToDel(HidBAA.Text.ToString()) < 0)
        {
            if (ParToDecimal.ParToDel(HidSupp.Text) < 0)
            {
                X.Msg.Alert("提示", "超过追加余额,请核实后修改数据!").Show(); return;
            }
            if (ParToDecimal.ParToDel(HidBAA.Text) < 0)
            {
                X.Msg.Alert("提示", "超过总余额,请核实后修改数据!").Show(); return;
            }

        }
        e.Accept = true;
        string[] idStrArr = HidSlist.Text.ToString().TrimEnd('&').Split('&');
        List<int> slist = new List<int>();
        foreach (string item in idStrArr)
        {
            int IntId = common.IntSafeConvert(item);
            if (!slist.Contains(IntId))
            {
                slist.Add(IntId);
            }
        }
        int piid = common.IntSafeConvert(e.NodeID);
        RowChanges rc = e.Changes[0];
        //if (rc.Value<float>() <= 0 && rc.OldValue<float>() <= 0)
        //{
        //    X.Msg.Alert("提示", "填写数字不能为0").Show();
        //    return;
        //}
        if (e.Json == null)
        {
            return;
        }

        if (!slist.Contains(piid))
        {
            X.Msg.Alert("提示", "此处不可填写,请填写到正确位置").Show();
            return;
        }
        else
        {
            int year = common.IntSafeConvert(CurrentYear);
            decimal baamon = 0;
            decimal suppmon = 0;
            BG_BudgetAllocation bg = BG_BudgetAllocationLogic.GetMonDT(year, piid, depid);
            if (bg == null)
            {
                BG_BudgetAllocation bgadd = new BG_BudgetAllocation();
                foreach (RowChanges change in e.Changes)
                {
                    if (change.Field == "BAAMon" && change.IsDirty<float>())
                    {
                        bgadd.BAAMon = ParToDecimal.ParToDel(change.Value<float>().ToString());
                        baamon = ParToDecimal.ParToDel(HidBAA.Text.ToString()) - bgadd.BAAMon;
                        if (baamon < 0)
                        {
                            X.Msg.Alert("提示", "超过总余额,请核实后修改数据!").Show();
                            rc.SetValue(rc.OldValue<float>());
                            return;
                        }

                    }
                    else if (change.Field == "SuppMon" && change.IsDirty<float>())
                    {
                        bgadd.SuppMon = ParToDecimal.ParToDel(change.Value<float>().ToString());
                        suppmon = ParToDecimal.ParToDel(HidSupp.Text) - bgadd.SuppMon;
                        if (suppmon < 0)
                        {
                            X.Msg.Alert("提示", "超过追加余额,请核实后修改数据!").Show();
                            rc.SetValue(rc.OldValue<float>());
                            return;
                        }
                    }
                }
                bgadd.PIID = piid;
                bgadd.BAAYear = year;
                bgadd.DepID = depid;

                if (BG_BudgetAllocationManager.AddBG_BudgetAllocation(bgadd).BAAID>0)
                {
                    BG_BudgetAllocationHis bgBudgetAllocationHis   =new BG_BudgetAllocationHis();
                    bgBudgetAllocationHis.BAAID                    = bgadd.BAAID;
                    bgBudgetAllocationHis.NewBAAMon                = bgadd.BAAMon;
                    bgBudgetAllocationHis.Crtime                   = DateTime.Now;
                    bgBudgetAllocationHis.DepID                    = bgadd.DepID;
                    bgBudgetAllocationHis.PIID                     = bgadd.PIID;
                    bgBudgetAllocationHis.NewSuppMon               = bgadd.SuppMon;
                    bgBudgetAllocationHis.UserOp                   = UserName;
                    bgBudgetAllocationHis.OldBAAMon = rc.Field == "BAAMon" && rc.IsDirty<float>()
                        ? (decimal)rc.OldValue<float>()
                        : bgadd.BAAMon;
                    bgBudgetAllocationHis.OldSuppMon = rc.Field == "SuppMon" && rc.IsDirty<float>()
                        ? (decimal)rc.OldValue<float>()
                        : bgadd.SuppMon;
                    bgBudgetAllocationHis.AddBAAMon  = bgBudgetAllocationHis.NewBAAMon - bgBudgetAllocationHis.OldBAAMon;
                    bgBudgetAllocationHis.AddSuppMon = bgBudgetAllocationHis.NewSuppMon - bgBudgetAllocationHis.OldSuppMon;
                    bgBudgetAllocationHis.DepName = BG_DepartmentManager.GetBG_DepartmentByDepID(bgadd.DepID).DepName;
                    BG_BudgetAllocationHisManager.AddBG_BudgetAllocationHis(bgBudgetAllocationHis);
                }
                DtDataBind();

                //if (baamon < 0 || suppmon < 0)
                //{
                //    if (baamon > 0)
                //    {
                //        X.Msg.Alert("提示", "超过追加余额,请核实后修改数据!").Show();
                //        rc.SetValue(rc.OldValue<float>());
                //    }
                //    else if (suppmon > 0)
                //    {
                //        X.Msg.Alert("提示", "超过总余额,请核实后修改数据!").Show();
                //        rc.SetValue(rc.OldValue<float>());
                //    }
                //    else
                //    {
                //        X.Msg.Alert("提示", "追加余额或总余额不足,请核实后修改数据!").Show();
                //        rc.SetValue(rc.OldValue<float>());
                //    }

                //}
                //else
                //{
                //    BG_BudgetAllocationManager.AddBG_BudgetAllocation(bgadd);
                //    DtDataBind();
                //}
            }
            else
            {

                decimal Mon = BG_MonPayPlanGenerateLogic.GetMonPayYear(piid, depid, year);
                decimal UseMon = BG_ApplyReimburLogic.GetARUseMon(piid, depid, year);
                //if (ParToDecimal.ParToDel(rc.OldValue<float>().ToString()) - ParToDecimal.ParToDel(rc.Value<float>().ToString())-(Mon - UseMon) > 0)
                //{
                    if (Mon>0)
                    {
                        X.Msg.Alert("提示", "分配金额小于期初分配金额,会影响月度用款申请及预算执行!").Show();
                    }
                    foreach (RowChanges change in e.Changes)
                    {
                        if (change.Field == "BAAMon" && change.IsDirty<float>())
                        {
                            bg.BAAMon = ParToDecimal.ParToDel(change.Value<float>().ToString());
                            baamon = ParToDecimal.ParToDel(HidBAA.Text.ToString()) + ParToDecimal.ParToDel(change.OldValue<float>().ToString()) - bg.BAAMon;
                            if (baamon < 0)
                            {
                                X.Msg.Alert("提示", "超过总余额,请核实后修改数据!").Show();
                                rc.SetValue(rc.OldValue<float>());
                                return;
                            }
                        }
                        else if (change.Field == "SuppMon" && change.IsDirty<float>())
                        {
                            bg.SuppMon = ParToDecimal.ParToDel(change.Value<float>().ToString());
                            suppmon = ParToDecimal.ParToDel(HidSupp.Text.ToString()) + ParToDecimal.ParToDel(change.OldValue<float>().ToString()) - bg.SuppMon;
                            if (suppmon < 0)
                            {
                                X.Msg.Alert("提示", "超过追加余额,请核实后修改数据!").Show();
                                rc.SetValue(rc.OldValue<float>());
                                return;
                            }
                        }
                    }
                //}
                if (BG_BudgetAllocationManager.ModifyBG_BudgetAllocation(bg))
                {
                    BG_BudgetAllocationHis bgBudgetAllocationHis=new BG_BudgetAllocationHis();
                    bgBudgetAllocationHis.BAAID                    = bg.BAAID;
                    bgBudgetAllocationHis.NewBAAMon                = bg.BAAMon;
                    bgBudgetAllocationHis.Crtime                   = DateTime.Now;
                    bgBudgetAllocationHis.DepID                    = bg.DepID;
                    bgBudgetAllocationHis.PIID                     = bg.PIID;
                    bgBudgetAllocationHis.NewSuppMon               = bg.SuppMon;
                    bgBudgetAllocationHis.UserOp                   = UserName;
                    bgBudgetAllocationHis.OldBAAMon = rc.Field == "BAAMon" && rc.IsDirty<float>()
                        ? (decimal)rc.OldValue<float>()
                        : bg.BAAMon;
                    bgBudgetAllocationHis.OldSuppMon = rc.Field == "SuppMon" && rc.IsDirty<float>()
                        ? (decimal)rc.OldValue<float>()
                        : bg.SuppMon;
                    bgBudgetAllocationHis.AddBAAMon  = bgBudgetAllocationHis.NewBAAMon - bgBudgetAllocationHis.OldBAAMon;
                    bgBudgetAllocationHis.AddSuppMon = bgBudgetAllocationHis.NewSuppMon - bgBudgetAllocationHis.OldSuppMon;
                    bgBudgetAllocationHis.DepName = BG_DepartmentManager.GetBG_DepartmentByDepID(bg.DepID).DepName;
                    BG_BudgetAllocationHisManager.AddBG_BudgetAllocationHis(bgBudgetAllocationHis);
                }
                DtDataBind();
                //if (baamon < 0 || suppmon < 0)
                //{
                //    if (baamon > 0)
                //    {
                //        X.Msg.Alert("提示", "超过追加余额,请核实后修改数据!").Show();
                //        rc.SetValue(rc.OldValue<float>());
                //    }
                //    else if (suppmon > 0)
                //    {
                //        X.Msg.Alert("提示", "超过总余额,请核实后修改数据!").Show();
                //        rc.SetValue(rc.OldValue<float>());
                //    }
                //    else
                //    {
                //        X.Msg.Alert("提示", "追加余额或总余额不足,请核实后修改数据!").Show();
                //        rc.SetValue(rc.OldValue<float>());
                //    }

                //}
                //else
                //{
                //    BG_BudgetAllocationManager.ModifyBG_BudgetAllocation(bg);
                //    DtDataBind();
                //}

                #region 8-8   修改后总分配额不能少于已经审核通过的金额
                //if (Mon - ParToDecimal.ParToDel(rc.OldValue<float>().ToString()) + ParToDecimal.ParToDel(rc.Value<float>().ToString()) - UseMon < 0)
                //{
                //    X.Msg.Alert("提示", "修改后总分配额不能少于已经审核通过的金额").Show();
                //    rc.SetValue(rc.OldValue<float>());
                //}
                //else
                //{
                //    foreach (RowChanges change in e.Changes)
                //    {
                //        if (change.Field == "BAAMon" && change.IsDirty<float>())
                //        {
                //            bg.BAAMon = ParToDecimal.ParToDel(change.Value<float>().ToString());
                //            baamon = ParToDecimal.ParToDel(HidBAA.Text.ToString()) + ParToDecimal.ParToDel(change.OldValue<float>().ToString()) - bg.BAAMon;

                //        }
                //        else if (change.Field == "SuppMon" && change.IsDirty<float>())
                //        {
                //            bg.SuppMon = ParToDecimal.ParToDel(change.Value<float>().ToString());
                //            suppmon = ParToDecimal.ParToDel(HidSupp.Text.ToString()) + ParToDecimal.ParToDel(change.OldValue<float>().ToString()) - bg.SuppMon;

                //        }
                //    }
                //    if (baamon < 0 || suppmon < 0)
                //    {
                //        if (baamon > 0)
                //        {
                //            X.Msg.Alert("提示", "超过追加余额,请核实后修改数据!").Show();
                //            rc.SetValue(rc.OldValue<float>());
                //        }
                //        else if (suppmon > 0)
                //        {
                //            X.Msg.Alert("提示", "超过总余额,请核实后修改数据!").Show();
                //            rc.SetValue(rc.OldValue<float>());
                //        }
                //        else
                //        {
                //            X.Msg.Alert("提示", "追加余额或总余额不足,请核实后修改数据!").Show();
                //            rc.SetValue(rc.OldValue<float>());
                //        }

                //    }
                //    else
                //    {
                //        BG_BudgetAllocationManager.ModifyBG_BudgetAllocation(bg);
                //        DtDataBind();
                //    }
                //}
                #endregion
            }
        }
        HidBind();
        TPPayIncome.Listeners.AddScript("GetTotal();");
    }