public ActionResult NewOpeningBalance(sdtoViewOpeningBalance ViewOpeningBalance)
        {
            if (ModelState.IsValid)
            {
                var OpeningBalances = ViewOpeningBalance.OpeningBalances;
                if (OpeningBalances != null)
                {
                    for (int i = 0; i < OpeningBalances.Count(); i++)
                    {
                        var CurrentOpeningBalance = OpeningBalances[i];
                        //var prevCreditOpeningBalance = opnBalance.CreditOpeningBalance;
                        //var prevDebitOpeningBalance = opnBalance.DebitOpeningBalance;

                        //var opnBalance = db.OpeningBalance.Find(CurrentOpeningBalance.OpeningBalanceId);

                        //opnBalance.ScheduleId = CurrentOpeningBalance.ScheduleId;
                        //opnBalance.AccountHeadId = CurrentOpeningBalance.AccountHeadId;
                        //opnBalance.CreditOpeningBalance = CurrentOpeningBalance.CreditOpeningBalance;
                        //opnBalance.DebitOpeningBalance = CurrentOpeningBalance.DebitOpeningBalance;
                        //opnBalance.ModifiedBy = CurrentUserSession.UserId;
                        //opnBalance.ModifiedOn = DateTime.Now;
                        CurrentOpeningBalance.ClosingBalance = CurrentOpeningBalance.ClosingBalance - (CurrentOpeningBalance.PrevCreditOpeningBalance + CurrentOpeningBalance.PrevDebitOpeningBalance) + (CurrentOpeningBalance.CreditOpeningBalance + CurrentOpeningBalance.DebitOpeningBalance);

                        if (CurrentOpeningBalance.CreditOpeningBalance > 0 && CurrentOpeningBalance.DebitOpeningBalance > 0)
                        {
                            ModelState.AddModelError("", "Please enter either credit or debit opening balance");
                            LoadSelectListOpeningBalance(0, 0, 0);
                            return View(ViewOpeningBalance);
                        }

                        //opnBalance.CreatedOn = DateTime.Now;
                        //sdtoUser session = UtilityHelper.UserSession.GetSession(UtilityHelper.UserSession.LoggedInUser) as sdtoUser;
                        //if (session != null)
                        //    opnBalance.CreatedBy = session.UserID;
                        //opnBalance.IsDeleted = false;

                        //db.Entry(opnBalance).State = EntityState.Modified;

                        db.Database.ExecuteSqlCommand("Update OpeningBalance set CreditOpeningBalance = @CreditOpeningBalance, DebitOpeningBalance = @DebitOpeningBalance, ClosingBalance = @ClosingBalance, ModifiedBy = @ModifiedBy, ModifiedOn = GetDate() where OpeningBalanceId = @OpeningBalanceId"
                            , new System.Data.SqlClient.SqlParameter("@OpeningBalanceId", CurrentOpeningBalance.OpeningBalanceId)
                            , new System.Data.SqlClient.SqlParameter("@CreditOpeningBalance", CurrentOpeningBalance.CreditOpeningBalance)
                            , new System.Data.SqlClient.SqlParameter("@DebitOpeningBalance", CurrentOpeningBalance.DebitOpeningBalance)
                            , new System.Data.SqlClient.SqlParameter("@ClosingBalance", CurrentOpeningBalance.ClosingBalance)
                            , new System.Data.SqlClient.SqlParameter("@ModifiedBy", CurrentUserSession.UserId)
                            );
                    }
                    db.SaveChanges();

                    if (User.Identity.IsAuthenticated)
                        return RedirectToAction("OpeningBalances");
                    else
                        return RedirectToAction("Login", "Login");
                }
            }

            LoadSelectListOpeningBalance(0, 0, 0);
            return View(ViewOpeningBalance);
        }
        public ActionResult NewOpeningBalance(long? ScheduleId, string AccountName)
        {
            LoadSelectListOpeningBalance(0, 0, 0);
            var openingBalance = db.OpeningBalance.Include(x => x.AccountHead).Include(x => x.Schedule).Include(x => x.FinancialPeriod).Where(x => x.IsDeleted == false && x.FinancialPeriod.IsCurrentYear == true && (x.ScheduleId == ((ScheduleId == null || ScheduleId.Value == 0) ? x.ScheduleId : ScheduleId)) && x.AccountHead.AccountName.Contains(AccountName + "")).Select(x => new sdtoOpeningBalance() { OpeningBalanceId = x.OpeningBalanceId, ClosingBalance = x.ClosingBalance, CreditOpeningBalance = x.CreditOpeningBalance, DebitOpeningBalance = x.DebitOpeningBalance, AccountHeadId = x.AccountHeadId, ScheduleId = x.ScheduleId, PrevCreditOpeningBalance = x.CreditOpeningBalance, PrevDebitOpeningBalance = x.DebitOpeningBalance, FinancialYearId = x.FinancialYearId }).ToList();

            sdtoViewOpeningBalance openingBalances = new sdtoViewOpeningBalance() { ScheduleId = ScheduleId == null ? 0 : ScheduleId.Value, FilterAccountHead = AccountName, OpeningBalances = openingBalance };

            return View(openingBalances);
        }