public virtual IActionResult TierPriceCreatePopup(TierPriceModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCategories))
            {
                return(AccessDeniedView());
            }

            //try to get a category with the specified id
            var category = _categoryService.GetCategoryById(model.CategoryId)
                           ?? throw new ArgumentException("No category found with the specified id");


            if (ModelState.IsValid)
            {
                //fill entity from model
                var tierPrice = model.ToEntity <TierPrice>();
                tierPrice.ProductId      = 1;
                tierPrice.CategoryId     = category.Id;
                tierPrice.CustomerRoleId = model.CustomerRoleId > 0 ? model.CustomerRoleId : (int?)null;

                _tierpriceService.InsertTierPrice(tierPrice);
                ViewBag.RefreshPage = true;

                return(View(model));
            }

            //prepare model
            model = _tirePriceModelFactory.PrepareTierPriceModel(model, category, null, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
        public virtual IActionResult TierPriceEditPopup(TierPriceModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            {
                return(AccessDeniedView());
            }

            //try to get a tier price with the specified id
            var tierPrice = _productService.GetTierPriceById(model.Id);

            if (tierPrice == null)
            {
                return(RedirectToAction("List", "Product"));
            }

            //try to get a product with the specified id
            var product = _productService.GetProductById(tierPrice.ProductId)
                          ?? throw new ArgumentException("No product found with the specified id");

            //a vendor should have access only to his products
            if (_workContext.CurrentVendor != null && product.VendorId != _workContext.CurrentVendor.Id)
            {
                return(RedirectToAction("List", "Product"));
            }

            if (ModelState.IsValid)
            {
                //fill entity from model
                tierPrice = model.ToEntity(tierPrice);
                tierPrice.CustomerRoleId = model.CustomerRoleId > 0 ? model.CustomerRoleId : (int?)null;
                _productService.UpdateTierPrice(tierPrice);

                ViewBag.RefreshPage = true;

                return(View(model));
            }

            //prepare model
            model = _productModelFactory.PrepareTierPriceModel(model, product, tierPrice, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }