public ActionResult Create(ExpenseViewModel expenseViewModel) { bool isSuperAdmin = User.IsInAnyRoles("SuperAdmin"); var organisationId = UserOrganisationId; var centreId = UserCentreId; var personnelId = UserPersonnelId; var currentMonth = DateTime.UtcNow.Month; var currentYear = DateTime.UtcNow.Year; var isCashAvailable = expenseViewModel.AvailablePettyCash > expenseViewModel.Expense.DebitAmount; var expenseHeadLimit = NidanBusinessService.RetrieveExpenseHeadLimits(organisationId, centreId, e => e.CentreId == centreId && e.ExpenseMonth == currentMonth && e.ExpenseYear == currentYear && e.ExpenseHeaderId == expenseViewModel.Expense.ExpenseHeaderId).Items.FirstOrDefault(); expenseViewModel.ExpenseHeaders = new SelectList(NidanBusinessService.RetrieveExpenseHeaders(organisationId, e => true).Items.ToList()); expenseViewModel.Projects = new SelectList(NidanBusinessService.RetrieveProjects(organisationId, e => e.CentreId == centreId).Items.ToList()); if (expenseHeadLimit != null) { var limitAmount = expenseHeadLimit.LimitAmount; var monthlyExpenseByExpenseHeader = NidanBusinessService.RetrieveExpenses(organisationId, centreId, e => e.CentreId == centreId && e.ExpenseGeneratedDate.Month == currentMonth && e.ExpenseHeaderId == expenseViewModel.Expense.ExpenseHeaderId); var totalMonthlyExpenseByExpenseHeader = monthlyExpenseByExpenseHeader.Items.Sum(e => e.DebitAmount); var balanceLimit = limitAmount - totalMonthlyExpenseByExpenseHeader; var isExpenseLimitExceed = expenseViewModel.Expense.DebitAmount > balanceLimit; expenseViewModel.ExpenseHeaders = new SelectList(NidanBusinessService.RetrieveExpenseHeaders(organisationId, e => true).Items.ToList()); if (isExpenseLimitExceed) { ModelState.AddModelError("", String.Format("Limit is exceeded for {0} Expense Head", monthlyExpenseByExpenseHeader.Items.FirstOrDefault()?.ExpenseHeader.Name)); return(View(expenseViewModel)); } } if (!isCashAvailable) { ModelState.AddModelError("", String.Format("Insufficient cash, available petty cash is {0}", expenseViewModel.AvailablePettyCash)); return(View(expenseViewModel)); } if (ModelState.IsValid) { expenseViewModel.Expense.OrganisationId = organisationId; expenseViewModel.Expense.CentreId = centreId; expenseViewModel.Expense.PersonnelId = personnelId; expenseViewModel.Expense.CreatedBy = personnelId; expenseViewModel.Expense.PaymentModeId = (int)PaymentMode.Cash; expenseViewModel.Expense.CreatedBy = personnelId; expenseViewModel.Expense = NidanBusinessService.CreateExpense(organisationId, centreId, expenseViewModel.Expense, expenseViewModel.SelectedProjectIds); return(RedirectToAction("Index")); } expenseViewModel.ExpenseHeaders = new SelectList(NidanBusinessService.RetrieveExpenseHeaders(organisationId, e => true).Items.ToList()); expenseViewModel.Projects = new SelectList(NidanBusinessService.RetrieveProjects(organisationId, e => true).Items.ToList()); return(View(expenseViewModel)); }