//
        // GET: /SaleOrIncome/Add
        public ActionResult Add()
        {
            var viewModel = new SaleOrIncomeViewModel();

            var accountList = SelectListItemExtension.PopulateDropdownList(_db.Accounts.ToList<Account>(), "AccountId", "AccountName").ToList();
            var saleOrIncomeCategoryList = SelectListItemExtension.PopulateDropdownList(_db.SaleOrIncomeCategories.ToList<SaleOrIncomeCategory>(), "SaleOrIncomeCategoryId", "SaleOrIncomeCategoryName").ToList();

            viewModel.SaleOrIncomeCategoryId = 0;
            viewModel.UnitPrice = 0;
            viewModel.Quantity = 1;
            viewModel.CreateDate = DateTime.Now;
            viewModel.ddlAccounts = accountList;
            viewModel.ddlSaleOrIncomeCategories = saleOrIncomeCategoryList;

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

            try
            {
                var saleOrIncome = _db.SaleOrIncomes.Find(id);
                if (saleOrIncome != null)
                {
                    var viewModel = new SaleOrIncomeViewModel() { SaleOrIncomeId = saleOrIncome.SaleOrIncomeId, UnitPrice = saleOrIncome.UnitPrice, Quantity = saleOrIncome.Quantity, ProcessCostRate = saleOrIncome.ProcessCostRate, ExtraCostAmount = saleOrIncome.ExtraCostAmount, CreateDate = saleOrIncome.CreateDate, Remarks = saleOrIncome.Remarks, AccountId = saleOrIncome.AccountId, AccountName = saleOrIncome.Account != null ? saleOrIncome.Account.AccountName : "", SaleOrIncomeCategoryId = saleOrIncome.SaleOrIncomeCategoryId, SaleOrIncomeCategoryName = saleOrIncome.SaleOrIncomeCategory != null ? saleOrIncome.SaleOrIncomeCategory.SaleOrIncomeCategoryName : "" };

                    return PartialView("_Details", viewModel);
                }

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

            return PartialView("_ErrorPopup", errorViewModel);
        }
        //
        // GET: /SaleOrIncome/Edit/By ID
        public ActionResult Edit(int id)
        {
            var errorViewModel = new ErrorViewModel();

            try
            {
                var saleOrIncome = _db.SaleOrIncomes.Find(id);
                if (saleOrIncome != null)
                {
                    var accountList = SelectListItemExtension.PopulateDropdownList(_db.Accounts.ToList<Account>(), "AccountId", "AccountName", isEdit: true, selectedValue: saleOrIncome.AccountId.ToString()).ToList();
                    var saleOrIncomeCategoryList = SelectListItemExtension.PopulateDropdownList(_db.SaleOrIncomeCategories.ToList<SaleOrIncomeCategory>(), "SaleOrIncomeCategoryId", "SaleOrIncomeCategoryName", isEdit: true, selectedValue: saleOrIncome.SaleOrIncomeCategoryId.ToString()).ToList();

                    var viewModel = new SaleOrIncomeViewModel() { SaleOrIncomeId = saleOrIncome.SaleOrIncomeId, UnitPrice = saleOrIncome.UnitPrice, Quantity = saleOrIncome.Quantity, ProcessCostRate = saleOrIncome.ProcessCostRate, ExtraCostAmount = saleOrIncome.ExtraCostAmount, CreateDate = saleOrIncome.CreateDate, Remarks = saleOrIncome.Remarks, AccountId = saleOrIncome.AccountId, AccountName = saleOrIncome.Account != null ? saleOrIncome.Account.AccountName : "", ddlAccounts = accountList, SaleOrIncomeCategoryId = saleOrIncome.SaleOrIncomeCategoryId, SaleOrIncomeCategoryName = saleOrIncome.SaleOrIncomeCategory != null ? saleOrIncome.SaleOrIncomeCategory.SaleOrIncomeCategoryName : "", ddlSaleOrIncomeCategories = saleOrIncomeCategoryList };

                    return PartialView("_AddOrEdit", viewModel);
                }

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

            return PartialView("_ErrorPopup", errorViewModel);
        }
        public ActionResult Save(SaleOrIncomeViewModel saleOrIncomeViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //add
                    if (saleOrIncomeViewModel.SaleOrIncomeId == 0 && saleOrIncomeViewModel.ActionName == "Add")
                    {
                        var model = new SaleOrIncome() { SaleOrIncomeId = saleOrIncomeViewModel.SaleOrIncomeId, UnitPrice = saleOrIncomeViewModel.UnitPrice, Quantity = saleOrIncomeViewModel.Quantity, ProcessCostRate = saleOrIncomeViewModel.ProcessCostRate, ExtraCostAmount = saleOrIncomeViewModel.ExtraCostAmount, CreateDate = saleOrIncomeViewModel.CreateDate, Remarks = saleOrIncomeViewModel.Remarks, AccountId = saleOrIncomeViewModel.AccountId, SaleOrIncomeCategoryId = saleOrIncomeViewModel.SaleOrIncomeCategoryId };
                        _db.SaleOrIncomes.Add(model);
                    }
                    else if (saleOrIncomeViewModel.ActionName == "Edit") //edit
                    {
                        SaleOrIncome saleOrIncome = _db.SaleOrIncomes.Find(saleOrIncomeViewModel.SaleOrIncomeId);

                        if (saleOrIncome != null)
                        {

                            saleOrIncome.SaleOrIncomeId = saleOrIncomeViewModel.SaleOrIncomeId;
                            saleOrIncome.UnitPrice = saleOrIncomeViewModel.UnitPrice;
                            saleOrIncome.Quantity = saleOrIncomeViewModel.Quantity;
                            saleOrIncome.ProcessCostRate = saleOrIncomeViewModel.ProcessCostRate;
                            saleOrIncome.ExtraCostAmount = saleOrIncomeViewModel.ExtraCostAmount;
                            saleOrIncome.CreateDate = saleOrIncomeViewModel.CreateDate;
                            saleOrIncome.Remarks = saleOrIncomeViewModel.Remarks;
                            saleOrIncome.AccountId = saleOrIncomeViewModel.AccountId;
                            saleOrIncome.SaleOrIncomeCategoryId = saleOrIncomeViewModel.SaleOrIncomeCategoryId;
                            _db.Entry(saleOrIncome).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, saleOrIncomeViewModel.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)));
            }
        }