//
        // GET: /CostOrExpense/Add
        public ActionResult Add()
        {
            var viewModel = new CostOrExpenseViewModel();

            var accountList = SelectListItemExtension.PopulateDropdownList(_db.Accounts.ToList<Account>(), "AccountId", "AccountName").ToList();
            var costOrExpenseCategoryList = SelectListItemExtension.PopulateDropdownList(_db.CostOrExpenseCategories.ToList<CostOrExpenseCategory>(), "CostOrExpenseCategoryId", "CostOrExpenseCategoryName").ToList();

            viewModel.CostOrExpenseCategoryId = 0;
            viewModel.CreateDate = DateTime.Now;
            viewModel.ddlAccounts = accountList;
            viewModel.ddlCostOrExpenseCategories = costOrExpenseCategoryList;

            //return View();
            return PartialView("_AddOrEdit", viewModel);
        }
        //
        // GET: /CostOrExpense/Details/By ID
        public ActionResult Details(int id)
        {
            var errorViewModel = new ErrorViewModel();

            try
            {
                var costOrExpense = _db.CostOrExpenses.Find(id);
                if (costOrExpense != null)
                {
                    var viewModel = new CostOrExpenseViewModel() { CostOrExpenseId = costOrExpense.CostOrExpenseId, Amount = costOrExpense.Amount, CreateDate = costOrExpense.CreateDate, Remarks = costOrExpense.Remarks, AccountId = costOrExpense.AccountId, AccountName = costOrExpense.Account != null ? costOrExpense.Account.AccountName : "", CostOrExpenseCategoryId = costOrExpense.CostOrExpenseCategoryId, CostOrExpenseCategoryName = costOrExpense.CostOrExpenseCategory != null ? costOrExpense.CostOrExpenseCategory.CostOrExpenseCategoryName : "" };
                    return PartialView("_Details", viewModel);
                }

                errorViewModel = ExceptionHelper.ExceptionErrorMessageForNullObject();
            }
            catch (Exception ex)
            {
                errorViewModel = ExceptionHelper.ExceptionErrorMessageFormat(ex);
            }

            return PartialView("_ErrorPopup", errorViewModel);
        }
        public ActionResult Save(CostOrExpenseViewModel costOrExpenseViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //add
                    if (costOrExpenseViewModel.CostOrExpenseId == 0 && costOrExpenseViewModel.ActionName == "Add")
                    {
                        var model = new CostOrExpense() { CostOrExpenseId = costOrExpenseViewModel.CostOrExpenseId, Amount = costOrExpenseViewModel.Amount, CreateDate = costOrExpenseViewModel.CreateDate, Remarks = costOrExpenseViewModel.Remarks, AccountId = costOrExpenseViewModel.AccountId, CostOrExpenseCategoryId = costOrExpenseViewModel.CostOrExpenseCategoryId };
                        _db.CostOrExpenses.Add(model);
                    }
                    else if (costOrExpenseViewModel.ActionName == "Edit") //edit
                    {
                        CostOrExpense costOrExpense = _db.CostOrExpenses.Find(costOrExpenseViewModel.CostOrExpenseId);

                        if (costOrExpense != null)
                        {

                            costOrExpense.CostOrExpenseId = costOrExpenseViewModel.CostOrExpenseId;
                            costOrExpense.Amount = costOrExpenseViewModel.Amount;
                            costOrExpense.CreateDate = costOrExpenseViewModel.CreateDate;
                            costOrExpense.Remarks = costOrExpenseViewModel.Remarks;
                            costOrExpense.AccountId = costOrExpenseViewModel.AccountId;
                            costOrExpense.CostOrExpenseCategoryId = costOrExpenseViewModel.CostOrExpenseCategoryId;
                            _db.Entry(costOrExpense).State = EntityState.Modified;

                        }
                        else
                        {
                            return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.FalseString, MessageType.warning.ToString(), ExceptionHelper.ExceptionMessageForNullObject()));
                        }
                    }

                    _db.SaveChanges();

                    //return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.TrueString, MessageType.success.ToString(), "Saved Successfully."));
                    return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.TrueString, costOrExpenseViewModel.ActionName, MessageType.success.ToString(), "Saved Successfully."));

                }

                return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.TrueString, MessageType.success.ToString(), ExceptionHelper.ModelStateErrorFormat(ModelState)));
            }
            catch (Exception ex)
            {
                return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.TrueString, MessageType.success.ToString(), ExceptionHelper.ExceptionMessageFormat(ex)));
            }
        }
        //
        // GET: /CostOrExpense/Edit/By ID
        public ActionResult Edit(int id)
        {
            var errorViewModel = new ErrorViewModel();

            try
            {
                var costOrExpense = _db.CostOrExpenses.Find(id);
                if (costOrExpense != null)
                {
                    var accountList = SelectListItemExtension.PopulateDropdownList(_db.Accounts.ToList<Account>(), "AccountId", "AccountName", isEdit: true, selectedValue: costOrExpense.AccountId.ToString()).ToList();
                    var costOrExpenseCategoryList = SelectListItemExtension.PopulateDropdownList(_db.CostOrExpenseCategories.ToList<CostOrExpenseCategory>(), "CostOrExpenseCategoryId", "CostOrExpenseCategoryName", isEdit: true, selectedValue: costOrExpense.CostOrExpenseCategoryId.ToString()).ToList();

                    var viewModel = new CostOrExpenseViewModel() { CostOrExpenseId = costOrExpense.CostOrExpenseId, Amount = costOrExpense.Amount, CreateDate = costOrExpense.CreateDate, Remarks = costOrExpense.Remarks, AccountId = costOrExpense.AccountId, AccountName = costOrExpense.Account != null ? costOrExpense.Account.AccountName : "", ddlAccounts = accountList, CostOrExpenseCategoryId = costOrExpense.CostOrExpenseCategoryId, CostOrExpenseCategoryName = costOrExpense.CostOrExpenseCategory != null ? costOrExpense.CostOrExpenseCategory.CostOrExpenseCategoryName : "", ddlCostOrExpenseCategories = costOrExpenseCategoryList };

                    return PartialView("_AddOrEdit", viewModel);
                }

                errorViewModel = ExceptionHelper.ExceptionErrorMessageForNullObject();
            }
            catch (Exception ex)
            {
                errorViewModel = ExceptionHelper.ExceptionErrorMessageFormat(ex);
            }

            return PartialView("_ErrorPopup", errorViewModel);
        }