private OtherItemViewModel PrepareViewModel(OtherItemModel item)
        {
            var categories = this.queryProcessor.Execute(new GetGroupedCategoriesQuery());

            return(new OtherItemViewModel {
                Item = item, CategoriesList = categories
            });
        }
        public ActionResult CreateUpdateOtherItem(Int32 id)
        {
            var otherItemModel = new OtherItemModel();

            otherItemModel.DatePurchase = DateTime.Now;

            if (id > 0)
            {
                var otherItem = FarmManagementEntities.OtherItems.Single(x => x.ItemId == id);
                otherItemModel = otherItem.ToType <OtherItem, OtherItemModel>();
            }

            return(PartialView("OtherItemPartial", otherItemModel));
        }
        public ActionResult CreateUpdateOtherItem(OtherItemModel otherItemModel)
        {
            if (!ModelState.IsValid)
            {
                return(ShowErrorMessage(GetModelErrors(ModelState)));
            }

            if (!CheckEmployeeHasBalance(otherItemModel.Price))
            {
                return(ShowErrorMessage(Constant.InsufficientBalance));
            }

            var otherItem = new OtherItem();

            if (otherItemModel.Id > 0)
            {
                otherItem = FarmManagementEntities.OtherItems.Single(x => x.ItemId == otherItemModel.Id);
            }

            otherItem.FarmId    = otherItemModel.FarmId;
            otherItem.AccountId = otherItemModel.AccountId;

            otherItem.TitleExpense = otherItemModel.TitleExpense;
            otherItem.Description  = otherItemModel.Description;
            otherItem.DatePurchase = otherItemModel.DatePurchase;

            if (otherItemModel.Id == 0)
            {
                otherItem.Price = otherItemModel.Price;

                otherItem.InsertDate = DateTime.Now;
                otherItem.UserId     = otherItemModel.UserId;
                FarmManagementEntities.OtherItems.Add(otherItem);

                ManageEmployeeBalance(otherItemModel.Price);
            }
            else
            {
                otherItem.UpdateDate = DateTime.Now;
            }

            FarmManagementEntities.SaveChanges();

            var message = string.Format(Constant.SuccessMessage, otherItemModel.Id > 0 ? "updated" : "added");

            return(ShowSuccessMessage(message));
        }