public List <BudgetMenguru> GetBudgetMengurusWithTreeCalc(List <int> LstSegmentDetailIDs, ref bool CanEdit)
        {
            List <BudgetMenguru> BudgetData  = new List <BudgetMenguru>();
            List <List <int> >   lstIDs      = new List <List <int> >();
            List <List <int> >   resultcombi = new List <List <int> >();

            if (LstSegmentDetailIDs.Count > 0)
            {
                foreach (int id in LstSegmentDetailIDs)
                {
                    lstIDs.Add(new SegmentDetailsDAL().AllLeafDetails(id));
                }

                List <int> index  = new List <int>();
                List <int> count  = new List <int>();
                List <int> result = new List <int>();

                for (int i = 0; i < lstIDs.Count; i++)
                {
                    index.Add(0);
                    count.Add(lstIDs[i].Count);
                }

                while (index.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y) != count.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y))
                {
                    List <int> sdids = new List <int>();
                    for (int id = 0; id < index.Count; id++)
                    {
                        sdids.Add(lstIDs[id][index[id]]);
                    }

                    foreach (BudgetMenguru bm in GetBudgetMengurus(sdids))
                    {
                        BudgetData.Add(bm);
                    }

                    int i = index.Count - 1;
                    while (i >= 0 && index[i] < count[i])
                    {
                        index[i] = index[i] + 1;
                        i--;
                    }

                    if (index.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y) == count.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y))
                    {
                        continue;
                    }

                    for (int j = 0; j < index.Count; j++)
                    {
                        if (index[j] == count[j])
                        {
                            index[j] = 0;
                        }
                    }
                }
            }

            string s1 = lstIDs.Select(x => (x.Count) > 0 ? x[0] : 0).OrderBy(x => x).Select(x => x.ToString()).Aggregate((x, y) => x + y);
            string s2 = LstSegmentDetailIDs.OrderBy(x => x).Select(x => x.ToString()).Aggregate((x, y) => x + y);

            if (lstIDs.Where(x => x.Count > 1).Count() == 0)
            {
                if (s1 == s2)
                {
                    CanEdit = true;
                    return(BudgetData);
                }
            }

            CanEdit = false;
            return(BudgetData
                   .GroupBy(x => new
            {
                x.AccountCode,
                x.PeriodMengurusID,
                x.Status
            })
                   .Select(x => new BudgetMenguru()
            {
                BudgetMengurusID = x.Count(),
                PeriodMengurusID = x.Key.PeriodMengurusID,
                AccountCode = x.Key.AccountCode,
                Amount = x.Sum(y => y.Amount),
                Status = x.Key.Status,
                Remarks = string.Empty
            }).ToList());

            //else
            //{
            //    CanEdit = true;
            //    return BudgetData;
            //    //.Select(x => new BudgetMenguru()
            //    //{
            //    //    BudgetMengurusID = 1,
            //    //    PeriodMengurusID = x.PeriodMengurusID,
            //    //    AccountCode = x.AccountCode,
            //    //    Amount = x.Amount,
            //    //    Status = x.Status,
            //    //    Remarks = x.Remarks,
            //    //    CreatedBy = x.CreatedBy,
            //    //    CreatedTimeStamp = x.CreatedTimeStamp,
            //    //    ModifiedBy = x.ModifiedBy,
            //    //    ModifiedTimeStamp = x.ModifiedTimeStamp
            //    //}).ToList();
            //}
        }
        public List <BudgetMenguru> GetBudgetMengurusStatus(Dictionary <int, int> SegmentAndDetailPair, ref bool CanEdit)
        {
            List <BudgetMenguru> BudgetData = new List <BudgetMenguru>();

            List <int> segDtlIDs = new SegmentDetailsDAL().AllLeafDetails(SegmentAndDetailPair);
            List <int> budID     = db.JuncBgtMengurusSegDtls.Where(x => segDtlIDs.Contains(x.SegmentDetailID))
                                   .GroupBy(x => new { x.BudgetMengurusID })
                                   //.Select(x => new { BudgetMengurusID=x.Key.BudgetMengurusID, Count = x.Count() })
                                   .Where(x => x.Count() == SegmentAndDetailPair.Count)
                                   .Select(x => x.Key.BudgetMengurusID).Distinct().ToList();

            BudgetData = db.BudgetMengurus.Where(x => budID.Contains(x.BudgetMengurusID)).Select(x => x).ToList();

            //List<List<int>> lstIDs = new List<List<int>>();
            //CanEdit = true;

            ////if (SegmentAndDetailPair.Count != 0)
            ////{
            //    //foreach (int segid in db.Segments.Where(x => x.Status == "A").OrderBy(x => x.SegmentOrder).Select(x => x.SegmentID))
            //    //    lstIDs.Add(db.SegmentDetails.Where(x => x.ParentDetailID == 0 && x.Status == "A" && x.SegmentID == segid).Select(x => x.SegmentDetailID).ToList());

            //    foreach (KeyValuePair<int, int> pair in SegmentAndDetailPair)
            //    {
            //        if (pair.Value == 0)
            //            lstIDs.Add(db.SegmentDetails.Where(x => x.ParentDetailID == 0 && x.Status == "A" && x.SegmentID == pair.Key).Select(x => x.SegmentDetailID).ToList());
            //        else
            //            lstIDs.Add(new List<int>() { pair.Value });
            //    }

            //    List<int> index = new List<int>();
            //    List<int> count = new List<int>();
            //    List<int> result = new List<int>();

            //    for (int i = 0; i < lstIDs.Count; i++)
            //    {
            //        index.Add(0);
            //        count.Add(lstIDs[i].Count);
            //    }

            //    while (index.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y) != count.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y))
            //    {
            //        List<int> sdids = new List<int>();
            //        for (int id = 0; id < index.Count; id++)
            //            sdids.Add(lstIDs[id][index[id]]);

            //        bool flag = true;
            //        foreach (BudgetMenguru bm in GetBudgetMengurusWithTreeCalc(sdids,ref flag))
            //            BudgetData.Add(bm);

            //        CanEdit = CanEdit && flag;

            //        int i = index.Count - 1;
            //        while (i >= 0 && index[i] < count[i])
            //        {
            //            index[i] = index[i] + 1;
            //            i--;
            //        }

            //        if (index.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y) == count.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y))
            //            continue;

            //        for (int j = 0; j < index.Count; j++)
            //        {
            //            if (index[j] == count[j])
            //            {
            //                index[j] = 0;
            //            }
            //        }
            //    }
            ////}

            //if (lstIDs.Where(x => x.Count > 1).Count() > 0)
            //{
            //    CanEdit = false;
            //}

            return(BudgetData
                   .GroupBy(x => new
            {
                x.AccountCode,
                x.PeriodMengurusID,
                x.Status
            })
                   .Select(x => new BudgetMenguru()
            {
                BudgetMengurusID = x.Count(),
                PeriodMengurusID = x.Key.PeriodMengurusID,
                AccountCode = x.Key.AccountCode,
                Amount = x.Sum(y => y.Amount),
                Status = x.Key.Status,
                Remarks = string.Empty
            }).ToList());
        }