protected virtual void Initialize(Guid pageId, PageIdentity pageIdentity = PageIdentity.Unknown, bool promoteProducts = true) { this.currentUser = this.HttpContext.CurrentUser() ?? UserIdentityManager.GetActiveUser(this.Request, this.repository); this.ViewData["SettingsViewModel"] = this.settingsViewModel; this.ViewData["MainMenuViewModel"] = MenuViewModelFactory.CreateDefaultMenu(this.repository, pageId, currentUser, pageIdentity); this.ViewData["ShowLeftRailLogin"] = currentUser == null; //this.ViewData["MediaListViewModel"] = promoteProducts ? CategoryViewModelFactory.CreatePopularProducts(this.repository, null) : null; this.ViewData["HttpReferrer"] = this.Request.UrlReferrer != null?this.Request.UrlReferrer.ToString() : null; }
public ActionResult Order(OrderDetailsViewModel orderViewModel) { var user = UserIdentityManager.GetActiveUser(this.Request, repository); if (user == null || !user.IsAdmin || orderViewModel == null) { return(this.HttpNotFound()); } this.ViewData["MainMenuViewModel"] = MenuViewModelFactory.CreateAdminMenu(repository, ControlPanelPage.Orders); this.ViewData["SettingsViewModel"] = this.settingsViewModel; using (var context = new InstantStoreDataContext()) { var order = context.Orders.FirstOrDefault(x => x.Id == orderViewModel.Id); if (order == null) { return(this.HttpNotFound()); } if (order.Comment != orderViewModel.Description || order.Status != (int)orderViewModel.Status) { order.Comment = orderViewModel.Description; if (order.Status != (int)orderViewModel.Status) { order.Status = (int)orderViewModel.Status; context.OrderUpdates.InsertOnSubmit(new OrderUpdate { Status = (int)orderViewModel.Status, DateTime = DateTime.Now, Id = Guid.NewGuid(), OrderId = order.Id }); } var orderSubmitDate = order.OrderUpdates.FirstOrDefault(x => x.Status == (int)OrderStatus.Placed); context.SubmitChanges(); EmailManager.Send( order.User, this.repository, EmailType.EmailOrderHasBeenUpdated, new Dictionary <string, string> { { "%order.id%", order.Id.ToString() }, { "%order.user%", order.User.Name }, { "%order.date%", orderSubmitDate != null ? orderSubmitDate.DateTime.ToString("F", russianCulture) : string.Empty } }); } } return(this.RedirectToAction("Orders")); }
public ActionResult Offers(char t = 'a', int o = 0, int c = 50) { var user = UserIdentityManager.GetActiveUser(this.Request, repository); this.ViewData["MainMenuViewModel"] = MenuViewModelFactory.CreateAdminMenu(repository, ControlPanelPage.Offers); this.ViewData["SettingsViewModel"] = this.settingsViewModel; this.ViewData["OffersHeaderViewModel"] = new TabControlViewModel { Tabs = offerStatusMap.Select(key => CreateOfferStatusHeader(key, t)).ToList() }; bool showActive; if (!offerStatusMap.TryGetValue(t, out showActive)) { showActive = true; } using (var context = new InstantStoreDataContext()) { Func <Offer, bool> selector = showActive ? (Func <Offer, bool>)((Offer offer) => offer.IsActive) : (Func <Offer, bool>)((Offer offer) => !offer.IsActive); this.ViewData["OffersTableViewModel"] = new TableViewModel { Header = new List <TableCellViewModel> { new TableCellViewModel(StringResource.offerTableHeaderName), new TableCellViewModel(StringResource.offerTableHeaderThreshold), new TableCellViewModel(StringResource.offerTableHeaderDiscount), new TableCellViewModel(string.Empty) }, Rows = context.Offers .Where(selector) .OrderByDescending(offer => offer.Name) .Skip(o) .Take(c) .Select(ConvertOfferToTableRow) .ToList(), RowClickAction = new NavigationLink("Offer"), Pagination = new PaginationViewModel(c, o, context.Offers.Count(selector)) { Link = new NavigationLink("Offers", "Admin") { Parameters = new { t = t } } } }; } return(this.View()); }
public ActionResult CopyOrder(Guid id) { var user = UserIdentityManager.GetActiveUser(this.Request, this.repository); if (user == null) { return(this.HttpNotFound()); } this.repository.AddProductsFromOrder(id, user); return(this.RedirectToAction("Orders")); }
public ActionResult Orders(Guid?id, string a) { this.Initialize(Guid.Empty, PageIdentity.Cart); var user = UserIdentityManager.GetActiveUser(this.Request, this.repository); if (user == null) { return(this.HttpNotFound()); } if (a == "delete" && id != null) { repository.DeleteOrderProduct(id.Value); } return(this.View("Orders", new OrderDetailsViewModel(this.repository, user))); }
public ActionResult PlaceOrder(OrderDetailsViewModel viewModel) { var user = UserIdentityManager.GetActiveUser(this.Request, this.repository); if (user == null) { return(this.HttpNotFound()); } var order = this.repository.GetOrderById(viewModel.Id); if (order == null || order.Status != (int)OrderStatus.Active) { return(this.HttpNotFound()); } var orderProducts = this.repository.GetProductsForOrder(order.Id); if (orderProducts.Count != viewModel.Products.Count || !orderProducts.All(x => viewModel.Products.Any(y => y.Id == x.Key.Id))) { throw new ApplicationException("Cart contents is inconsistent."); } var orderProductsToRemove = new List <Guid>(); foreach (var orderProduct in orderProducts.Where(x => !viewModel.Products.Any(y => y.Id == x.Key.Id))) { this.repository.RemoveOrderProduct(orderProduct.Key.Id); } this.repository.SubmitOrder(order.Id); var orderSubmitDate = this.repository.GetStatusesForOrder(order.Id).FirstOrDefault(x => x.Status == (int)OrderStatus.Placed); EmailManager.Send( user, this.repository, EmailType.EmailOrderHasBeenPlaced, new Dictionary <string, string> { { "%order.id%", order.Id.ToString() }, { "%order.user%", user.Name }, { "%order.date%", orderSubmitDate != null ? orderSubmitDate.DateTime.ToString("F", new CultureInfo("ru-RU")) : string.Empty } }); return(this.RedirectToAction("Index", "Main")); }
public ActionResult Recalculate(OrderDetailsViewModel viewModel) { var user = UserIdentityManager.GetActiveUser(this.Request, this.repository); if (user == null) { repository.LogError(new ApplicationException("Current user is null."), DateTime.Now, "[Code] Recalculate", null, null, null, null); return(this.HttpNotFound()); } var order = this.repository.GetOrderById(viewModel.Id); if (order == null || order.Status != (int)OrderStatus.Active) { repository.LogError(new ApplicationException("There are no order associated. Order id: " + viewModel.Id.ToString()), DateTime.Now, "[Code] Recalculate", null, null, null, null); return(this.HttpNotFound()); } var orderProducts = this.repository.GetProductsForOrder(order.Id); if (orderProducts.Count != viewModel.Products.Count || !orderProducts.All(x => viewModel.Products.Any(y => y.Id == x.Key.Id))) { throw new ApplicationException("Cart contents is inconsistent."); } // update the cart items count. foreach (var orderProduct in viewModel.Products) { var orderProductPair = orderProducts.Where(x => x.Key.Id == orderProduct.Id).First(); var product = orderProductPair.Value; var orderItem = orderProductPair.Key; if (!product.IsAvailable) { continue; } orderItem.Count = orderProduct.Count; orderItem.Price = product.GetPriceForUser(user, this.repository.GetExchangeRates()); orderItem.PriceCurrencyId = user.DefaultCurrencyId.Value; this.repository.UpdateOrderProduct(orderItem); } return(this.Orders(null, null)); }
public ActionResult AddToCart(Guid id, int count = 1) { if (id == Guid.Empty) { return(this.HttpNotFound()); } var user = UserIdentityManager.GetActiveUser(this.Request, this.repository); if (user == null || !user.IsActivated) { return(this.HttpNotFound()); } this.repository.AddItemToCurrentOrder(user, id, count); return(this.Json(new { result = "success" })); }
public ActionResult DeleteOffer(Guid id) { var user = UserIdentityManager.GetActiveUser(this.Request, repository); if (user == null || !user.IsAdmin || id == Guid.Empty) { return(this.HttpNotFound()); } using (var context = new InstantStoreDataContext()) { var offer = context.Offers.FirstOrDefault(x => x.Id == id); if (offer != null) { context.Offers.DeleteOnSubmit(offer); context.SubmitChanges(); } } return(this.RedirectToAction("Offers")); }
public ActionResult Page(Guid?id, Guid?parentId, int c = 200, int o = 0) { if (id == null || id == Guid.Empty) { return(this.RedirectToAction("Index")); } this.Initialize(id.Value); Product product; var user = UserIdentityManager.GetActiveUser(this.Request, repository); this.ViewData["Id"] = id.Value; this.ViewData["IsAdmin"] = user != null && user.IsAdmin; var contentPage = this.repository.GetPageById(id.Value); if (contentPage != null) { return(this.ContentPage(contentPage, id.Value, user, c, o)); } else if ((product = repository.GetProductById(id.Value)) != null) { this.ViewData["BreadcrumbViewModel"] = MenuViewModelFactory.CreateBreadcrumb(repository, parentId); this.ViewData["NavigationMenuViewModel"] = MenuViewModelFactory.CreateNavigationMenu(repository, parentId, this.Request); //this.ViewData["MediaListViewModel"] = CategoryViewModelFactory.CreateSimilarProducts(repository, parentId); var productViewModel = new ProductViewModel(this.repository, id.Value, parentId, user); return(this.View("Product", productViewModel)); } else { return(this.HttpNotFound()); } }
public ActionResult Offer(OfferViewModel viewModel) { var user = UserIdentityManager.GetActiveUser(this.Request, repository); if (user == null || !user.IsAdmin) { return(this.HttpNotFound()); } if (this.ModelState.IsValid) { if (viewModel.Type == OfferDiscountType.Percent && viewModel.Discount >= 100) { this.ModelState.AddModelError(string.Empty, StringResource.offer_ErrorDiscountPercentRange); } else { using (var context = new InstantStoreDataContext()) { if (viewModel.Id == Guid.Empty) { context.Offers.InsertOnSubmit(new Offer { Id = Guid.NewGuid(), Name = viewModel.Name, IsActive = viewModel.IsActive, MultiApply = viewModel.IsMultiApply, Priority = viewModel.Priority, CurrencyId = viewModel.CurrencyId, DiscountType = (int)viewModel.Type, DiscountValue = viewModel.Discount, ThresholdPriceValue = viewModel.ThresholdPrice, Version = 1, VersionId = Guid.NewGuid() }); context.SubmitChanges(); } else { // check if offer is being used in any order. var offer = context.Offers.FirstOrDefault(o => o.VersionId == viewModel.Id); if (offer == null) { return(this.HttpNotFound()); } bool increaseVersion = context.Orders.Any(order => order.OfferId == viewModel.Id); if (increaseVersion) { context.Offers.InsertOnSubmit(new Offer { Id = offer.Id, Name = viewModel.Name, IsActive = viewModel.IsActive, MultiApply = viewModel.IsMultiApply, Priority = viewModel.Priority, CurrencyId = viewModel.CurrencyId, DiscountType = (int)viewModel.Type, DiscountValue = viewModel.Discount, ThresholdPriceValue = viewModel.ThresholdPrice, Version = offer.Version + 1, VersionId = Guid.NewGuid() }); } else { offer.Name = viewModel.Name; offer.IsActive = viewModel.IsActive; offer.MultiApply = viewModel.IsMultiApply; offer.Priority = viewModel.Priority; offer.CurrencyId = viewModel.CurrencyId; offer.DiscountType = (int)viewModel.Type; offer.DiscountValue = viewModel.Discount; offer.ThresholdPriceValue = viewModel.ThresholdPrice; } context.SubmitChanges(); } } return(this.RedirectToAction("Offers")); } } this.ViewData["MainMenuViewModel"] = MenuViewModelFactory.CreateAdminMenu(repository, ControlPanelPage.Offers); this.ViewData["SettingsViewModel"] = this.settingsViewModel; using (var context = new InstantStoreDataContext()) { this.ViewData["CurrencyList"] = context.Currencies.Select( currency => new SelectListItem { Text = currency.Text, Value = currency.Id.ToString() }).ToList(); } this.ViewData["DiscountTypeList"] = new[] { OfferDiscountType.Percent, OfferDiscountType.LumpSum }.Select( type => new SelectListItem { Text = type == OfferDiscountType.Percent ? StringResource.offerDetailsDiscountType_Percent : StringResource.offerDetailsDiscountType_LumpSum, Value = type.ToString() }).ToList(); return(this.View(viewModel)); }
public ActionResult Offer(Guid?id) { var user = UserIdentityManager.GetActiveUser(this.Request, repository); if (user == null || !user.IsAdmin) { return(this.HttpNotFound()); } this.ViewData["MainMenuViewModel"] = MenuViewModelFactory.CreateAdminMenu(repository, ControlPanelPage.Offers); this.ViewData["SettingsViewModel"] = this.settingsViewModel; OfferViewModel viewModel; using (var context = new InstantStoreDataContext()) { if (id != null && id != Guid.Empty) { var offer = context.Offers.FirstOrDefault(x => x.VersionId == id.Value); if (offer == null) { return(this.HttpNotFound()); } viewModel = new OfferViewModel { Id = offer.VersionId, IsActive = offer.IsActive, Name = offer.Name, IsMultiApply = offer.MultiApply, Priority = offer.Priority, Type = (OfferDiscountType)offer.DiscountType, CurrencyId = offer.CurrencyId, ThresholdPrice = offer.ThresholdPriceValue, Discount = offer.DiscountValue }; } else { viewModel = new OfferViewModel { IsActive = true }; } this.ViewData["CurrencyList"] = context.Currencies.Select( currency => new SelectListItem { Text = currency.Text, Value = currency.Id.ToString() }).ToList(); this.ViewData["DiscountTypeList"] = new[] { OfferDiscountType.Percent, OfferDiscountType.LumpSum }.Select( type => new SelectListItem { Text = type == OfferDiscountType.Percent ? StringResource.offerDetailsDiscountType_Percent : StringResource.offerDetailsDiscountType_LumpSum, Value = type.ToString() }).ToList(); } return(this.View(viewModel)); }