public bool CloseCurrentAccYear()
        {
            string currentFinancialYear = GetCurrentFinancialYear();
            IList<ProjectHead> allProjectHeads = _projectHeadRepository.GetAll().ToList();
            //IList<Project> allProjects = (IList<Project>)_projectHeadRepository.GetAll().Distinct();
            IList<string> processed = new List<string>();
            foreach (ProjectHead projectHead in allProjectHeads)
            {
                string projectName = projectHead.Project.Name;
                if (processed.Contains(projectName))
                    continue;

                processed.Add(projectName);

                if (projectHead.Head.HeadType.Equals("Capital", StringComparison.OrdinalIgnoreCase))
                {
                    SortedDictionary<string, double> closingBalances = GetAllClosingBalances(projectHead.Project);
                    for (int i = 0; i < closingBalances.Count; i++)
                    {
                        string headName = closingBalances.Keys.ToArray()[i];
                        ProjectHead actualProjectHead = _projectHeadRepository.GetSingle(ph => ph.Project.Name == projectName && ph.Head.Name == headName);
                        OpeningBalance closingBalance = new OpeningBalance
                        {
                            Balance = closingBalances.Values.ToArray()[i],
                            FinancialYear = currentFinancialYear,
                            Date = DateTime.Today,
                            IsActive = Convert.ToInt32(currentFinancialYear) < DateTime.Now.Year ? false : true,
                            Description = "closing",
                            ProjectHead = actualProjectHead
                        };

                        if (!InsertOrUpdateOpeningBalance(closingBalance, false))
                            return false;
                    }
                }
            }

            //string currentFinancialYear = GetCurrentFinancialYear();
            //IList<OpeningBalance> allOpeningBalances = _openingBalanceRepository.GetAll().ToList();
            //allOpeningBalances = allOpeningBalances.Where(ob => ob.FinancialYear == currentFinancialYear && ob.Description.Equals("opening", StringComparison.OrdinalIgnoreCase)).ToList();
            //foreach (OpeningBalance openingBalance in allOpeningBalances)
            //{
            //    // TODO: closing balance is wrong here. It should be calculated from records.
            //    OpeningBalance closingBalance = new OpeningBalance
            //    {
            //        Balance = openingBalance.Balance,
            //        FinancialYear = currentFinancialYear,
            //        Date = DateTime.Today,
            //        IsActive = Convert.ToInt32(currentFinancialYear) < DateTime.Now.Year ? false : true,
            //        Description = "closing",
            //        ProjectHead = openingBalance.ProjectHead
            //    };

            //    if (!InsertOrUpdateOpeningBalance(closingBalance, false))
            //        return false;
            //}

            return true;
        }
        private bool InsertOrUpdateOpeningBalance(OpeningBalance openingBalance, bool update)
        {
            if (update)
                _openingBalanceRepository.Update(openingBalance);
            else
                _openingBalanceRepository.Insert(openingBalance);

            if (_openingBalanceRepository.Save() > 0)
            {
                InvokeManagerEvent(new BLLEventArgs { EventType = EventType.Success, MessageKey = "NewBudgetSavedSuccessfully", Parameters = new Dictionary<string, string> { { "BudgetYear", openingBalance.Date.Year.ToString() } } });
                return true;
            }

            InvokeManagerEvent(EventType.Error, "BudgetUpdatedFailed");
            return false;
        }
        public bool ImportBalancesFromLastYear()
        {
            string currentFinancialYear = GetCurrentFinancialYear();
            string lastAccYear = GetLastFinancialYear();
            IList<OpeningBalance> allClosingBalances = _openingBalanceRepository.Get(ob => ob.FinancialYear == lastAccYear && ob.Description.Equals("closing", StringComparison.OrdinalIgnoreCase)).ToList();

            if (allClosingBalances == null || allClosingBalances.Count == 0)
                return false;

            foreach (OpeningBalance closingBalance in allClosingBalances)
            {
                OpeningBalance openingBalance = new OpeningBalance
                {
                    Balance = closingBalance.Balance,
                    FinancialYear = currentFinancialYear,
                    Date = DateTime.Today,
                    IsActive = Convert.ToInt32(currentFinancialYear) < DateTime.Now.Year ? false : true,
                    Description = "opening",
                    ProjectHead = closingBalance.ProjectHead
                };

                if (!InsertOrUpdateOpeningBalance(openingBalance, false))
                    return false;
            }

            return true;
        }
        public bool Set(Project project, Head head, double amount)
        {
            if (project == null)
            {
                InvokeManagerEvent(EventType.Error, "NoProjectSelected");
                return false;
            }
            if (head == null)
            {
                InvokeManagerEvent(EventType.Error, "NoHeadSelected");
                return false;
            }
            if (amount == 0)
            {
                // TODO: This will not work right now, think through. But not a big problem.
                InvokeManagerEvent(EventType.Warning, "ZeroDepreciationProvidedForFixedAsset");
            }

            string projectName = project.Name;
            string headName = head.Name;

            ProjectHead projectHead = _projectHeadRepository.GetSingle(ph => ph.Head.Name == headName && ph.Project.Name == projectName);

            bool update = false;
            //if (projectHead != null && projectHead.Budgets != null)
            if (projectHead != null && projectHead.OpeningBalances != null)
            {
                string currentFinancialYear = GetCurrentFinancialYear();
                OpeningBalance openingBalance = projectHead.OpeningBalances.SingleOrDefault(b => b.FinancialYear == currentFinancialYear);
                if (openingBalance != null)
                {
                    openingBalance.Balance = amount;
                    openingBalance.FinancialYear = currentFinancialYear;
                    openingBalance.Date = DateTime.Today;
                    openingBalance.IsActive = Convert.ToInt32(currentFinancialYear) < DateTime.Now.Year ? false : true;
                    openingBalance.Description = "opening";

                    update = true;
                }
                else
                {
                    openingBalance = new OpeningBalance
                    {
                        Balance = amount,
                        FinancialYear = currentFinancialYear,
                        Date = DateTime.Today,
                        IsActive = Convert.ToInt32(currentFinancialYear) < DateTime.Now.Year ? false : true,
                        Description = "opening",
                        ProjectHead = projectHead
                    };
                }

                return InsertOrUpdateOpeningBalance(openingBalance, update);
            }
            else
                return false;

            //return true;
        }
 private OpeningBalanceGridRow GetOpeningBalaceDridRow(OpeningBalance openingBalance)
 {
     return new OpeningBalanceGridRow
     {
         HeadName = openingBalance.ProjectHead.Head.Name,
         CurrentYearBalance = openingBalance.Balance,
     };
 }