public ActionResult Create(ViewProductPriceCreate model) { if (ModelState.IsValid) { ShopProduct product = db.ShopProducts.Include(p => p.ShopProductsPrices).Where(p => p.Id == model.Id).SingleOrDefault(); ShopProductsPrice price = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == product.Id && p.CurrentPrice).SingleOrDefault(); if (price != null) { price.CurrentPrice = false; db.Entry(price).State = EntityState.Modified; db.SaveChanges(); } price = new ShopProductsPrice { Price = decimal.Parse(model.Price), CurrentPrice = true, DateSet = DateTime.Now, ShopProduct = product }; db.ShopProductsPrices.Add(price); db.SaveChanges(); return(Redirect(model.URL)); //return RedirectToAction("Details", "Products", new { id = product.Id }); } return(View(model)); }
//========================================================== //========================================================== // GET: Order public ActionResult Index() { string currentUserId = HttpContext.User.Identity.GetUserId(); ApplicationUserManager userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(db)); ApplicationUser currentUser = userManager.FindById(currentUserId); if (!currentUser.EmailConfirmed) { return(RedirectToAction("Index", "Profile")); } ShoppingCart cart = db.ShoppingCarts .Include(c => c.ShoppingCartsProducts) .Where(c => c.UserId == currentUser.Id).SingleOrDefault(); if (cart != null && cart.ShoppingCartsProducts != null && cart.ShoppingCartsProducts.Count > 0) { ShopOrder order = new ShopOrder { DateCreate = DateTime.Now, ApplicationUser = currentUser }; foreach (ShoppingCartsProduct cartProduct in cart.ShoppingCartsProducts) { ShopOrderProduct orderProduct = new ShopOrderProduct { Quantity = cartProduct.Quantity, ShopProduct = cartProduct.ShopProduct }; ShopProductsPrice price = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == cartProduct.ShopProduct.Id && p.CurrentPrice).SingleOrDefault(); //cartProduct.ShopProduct.ShopProductsPrices.Where(p => p.CurrentPrice).SingleOrDefault(); if (price != null) { orderProduct.Price = price.Price; } order.ShopOrderProducts.Add(orderProduct); } db.ShopOrders.Add(order); db.ShoppingCartsProducts.RemoveRange(cart.ShoppingCartsProducts); db.SaveChanges(); return(View("Index")); } return(View("Error")); }
public ActionResult DeleteConfirmed(ViewProductPriceDelete model) { ShopProductsPrice price = db.ShopProductsPrices.Find(model.Id); if (price.CurrentPrice) { ShopProductsPrice currentPriceNew = db.ShopProductsPrices.Where(p => p.Id != price.Id).OrderByDescending(p => p.DateSet).FirstOrDefault(); if (currentPriceNew != null) { currentPriceNew.CurrentPrice = true; db.Entry(currentPriceNew).State = EntityState.Modified; db.SaveChanges(); } } RemovePrice(price); return(RedirectToAction("Details", "Products", new { id = model.ProductId })); }
//========================================================== //========================================================== public ActionResult Index() { ShoppingCart cart = ShoppingCartCreateOrLoad(); ViewShoppingCart modelCart = new ViewShoppingCart(); if (cart != null) { modelCart.Id = cart.Id; modelCart.UserId = cart.UserId; modelCart.CookieKey = cart.CookieKey; modelCart.CountProducts = 0; if (cart.ShoppingCartsProducts.Count > 0) { modelCart.Products = new List <ViewShoppingCartsProducts>(); foreach (ShoppingCartsProduct cartProduct in cart.ShoppingCartsProducts) { ViewShoppingCartsProducts modelProduct = new ViewShoppingCartsProducts { Id = cartProduct.Id, Alias = cartProduct.ShopProduct.Alias, Name = cartProduct.ShopProduct.Name, PhotoName = cartProduct.ShopProduct.PhotoName, Quantity = cartProduct.Quantity, DateAdded = cartProduct.DateAdded }; modelCart.CountProducts = modelCart.CountProducts + cartProduct.Quantity; ShopProductsPrice price = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == cartProduct.ShopProduct.Id && p.CurrentPrice).SingleOrDefault(); if (price != null) { modelProduct.PriceOne = price.Price; modelProduct.PriceTotal = Decimal.Multiply(modelProduct.Quantity, modelProduct.PriceOne); } modelCart.Products.Add(modelProduct); } modelCart.Products = modelCart.Products.OrderByDescending(m => m.DateAdded).ToList(); } } return(View(modelCart)); }
//========================================================== //========================================================== // вывод всех товаров с доп данными public ActionResult Index() { // все товары List <ShopProduct> products = db.ShopProducts .Include(p => p.ShopProductsBrand) .Include(p => p.ShopCategories) .Include(p => p.ShopProductsPrices) .ToList(); // созадем список отображения List <ViewProducts> models = new List <ViewProducts>(); foreach (ShopProduct product in products) { ViewProducts model = new ViewProducts { Id = product.Id, Name = product.Name, PhotoName = product.PhotoName, }; if (product.ShopProductsBrand != null) { model.Brand = product.ShopProductsBrand.Name; } if (product.ShopProductsPrices.Count > 0) { ShopProductsPrice price = product.ShopProductsPrices.Where(p => p.CurrentPrice).SingleOrDefault(); if (price != null) { model.Price = price.Price; } } model.ShopCategories = new List <string>(); foreach (ShopCategory category in product.ShopCategories) { model.ShopCategories.Add(category.Name); } models.Add(model); } return(View(models)); }
//========================================================== //========================================================== // GET: AdminPanel/Prices/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ShopProductsPrice price = db.ShopProductsPrices.Find(id); if (price == null) { return(HttpNotFound()); } ViewProductPriceEdit model = new ViewProductPriceEdit { Id = price.Id, Price = price.Price.ToString(), ProductId = price.ShopProduct.Id, CurrentPrice = price.CurrentPrice }; return(View(model)); }
public ActionResult Edit(ViewProductPriceEdit model) { if (ModelState.IsValid) { ShopProductsPrice price = db.ShopProductsPrices.Find(model.Id); if (model.CurrentPrice != price.CurrentPrice) { price.CurrentPrice = model.CurrentPrice; ShopProductsPrice ollCurrentPrice = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == model.ProductId && p.CurrentPrice == model.CurrentPrice).SingleOrDefault(); if (ollCurrentPrice != null) { ollCurrentPrice.CurrentPrice = !model.CurrentPrice; db.Entry(ollCurrentPrice).State = EntityState.Modified; db.SaveChanges(); } } price.Price = decimal.Parse(model.Price); db.Entry(price).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", "Products", new { id = model.ProductId })); } return(View(model)); }
public ActionResult Create(ViewProductCreate model) { for (int i = 0; i < model.SelectedCategoriesId.Length; i++) { ShopCategory category = db.ShopCategories.Find(model.SelectedCategoriesId[i]); model.CategoriesSelected.Add(category); } if (ModelState.IsValid) { ShopProduct product = new ShopProduct { Name = model.Name, Alias = Translit.TranslitString(model.Name), Description = model.Description, VendorCode = model.VendorCode, Weight = model.Weight.Value, Proteins = model.Proteins.Value, Fats = model.Fats.Value, Carbohydrates = model.Carbohydrates.Value, Kcal = model.Kcal.Value }; //брэнд if (model.SelectedBrandId.HasValue) { product.ShopProductsBrand = db.ShopProductsBrands.Find(model.SelectedBrandId); } // вкус if (model.SelectedTasteId.HasValue) { product.ShopProductsTaste = db.ShopProductsTastes.Find(model.SelectedTasteId); } // катгории product.ShopCategories = model.CategoriesSelected; if (model.PortionsWeight.HasValue) { product.PortionsWeight = model.PortionsWeight.Value; } if (model.PortionsCount.HasValue) { product.PortionsCount = model.PortionsCount.Value; } product.DateCreation = DateTime.Now; if (model.Price != null) { ShopProductsPrice price = new ShopProductsPrice { Price = decimal.Parse(model.Price), CurrentPrice = true, DateSet = product.DateCreation, }; product.ShopProductsPrices.Add(price); } //сохранение фото товра if (model.PhotoFile != null) { string fileName = Guid.NewGuid().ToString(); string dirPath = HttpContext.Server.MapPath("~/Content/Images/Shop/Products"); product.PhotoName = Image.Save( model.PhotoFile, dirPath, null, fileName); } db.ShopProducts.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } // все категории model.CategoriesAll = db.ShopCategories.OrderBy(c => c.Name).ToList(); // список вкусов List <ShopProductsTaste> tastes = db.ShopProductsTastes.OrderBy(c => c.Name).ToList(); model.TasteList = new SelectList(tastes, "Id", "Name"); // список брэндов List <ShopProductsBrand> brands = db.ShopProductsBrands.OrderBy(c => c.Name).ToList(); model.BrandList = new SelectList(brands, "Id", "Name"); return(View(model)); }
//========================================================== //========================================================== public ActionResult _ProductsList(ShopFilterResult filterResult) { List <ShopProduct> products = new List <ShopProduct>(); // категории if (filterResult.CategorySelectedId != null) { // поиск категории и дочерних категорий ShopCategory category = db.ShopCategories.Find(filterResult.CategorySelectedId); List <ShopCategory> categories = new List <ShopCategory>(); categories.AddRange(FindChildCategories(category)); foreach (ShopCategory item in categories) { List <ShopProduct> productsByCategory = db.ShopProducts .Include(p => p.ShopProductsBrand) .Include(p => p.ShopCategories) .Include(p => p.ShopProductsPrices) .ToList(); productsByCategory = productsByCategory.Where(p => p.ShopCategories.Contains(item)).ToList(); products.AddRange(productsByCategory); } } else { products = db.ShopProducts .Include(p => p.ShopProductsBrand) .Include(p => p.ShopCategories) .Include(p => p.ShopProductsPrices) .ToList(); } // брэнды if (filterResult.BrandSelectedId != null) { products = products.Where(p => p.ShopProductsBrand != null && p.ShopProductsBrand.Id == filterResult.BrandSelectedId).ToList(); } // вкусы if (filterResult.TasteSelectedId != null) { products = products.Where(p => p.ShopProductsTaste != null && p.ShopProductsTaste.Id == filterResult.TasteSelectedId).ToList(); } List <ViewProductCatalog> models = new List <ViewProductCatalog>(); foreach (ShopProduct prod in products) { ViewProductCatalog model = new ViewProductCatalog { Id = prod.Id, Alias = prod.Alias, Name = prod.Name, PhotoName = prod.PhotoName, DateCreation = prod.DateCreation, Quantity = 1 }; ShopProductsPrice price = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == prod.Id && p.CurrentPrice).SingleOrDefault(); if (price != null) { model.Price = price.Price; } if (prod.ShopProductsTaste != null) { model.Taste = prod.ShopProductsTaste.Name; } models.Add(model); } // сортировка if (filterResult.SortingSelectedId != null) { switch (filterResult.SortingSelectedId) { case 1: models = models.OrderBy(m => m.Price).ToList(); break; case 2: models = models.OrderByDescending(m => m.Price).ToList(); break; case 3: models = models.OrderBy(m => m.Name).ToList(); break; case 4: models = models.OrderByDescending(m => m.Name).ToList(); break; case 5: models = models.OrderByDescending(m => m.DateCreation).ToList(); break; case 6: models = models.OrderBy(m => m.DateCreation).ToList(); break; default: models = models.OrderByDescending(m => m.DateCreation).ToList(); break; } } else { models = models.OrderByDescending(m => m.DateCreation).ToList(); } // цена от if (filterResult.PriceFrom != null) { decimal priceFrom = decimal.Parse(filterResult.PriceFrom); models = models.Where(m => m.Price != 0 && m.Price > priceFrom).ToList(); } // цена до if (filterResult.PriceTo != null) { decimal priceTo = decimal.Parse(filterResult.PriceTo); models = models.Where(m => m.Price != 0 && m.Price < priceTo).ToList(); } return(PartialView(models)); }
//========================================================== //========================================================== public void RemovePrice(ShopProductsPrice price) { db.ShopProductsPrices.Remove(price); db.SaveChanges(); }