public ActionResult Edit(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                //No discount found with the specified id
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevDiscountType = discount.DiscountType;
                discount = model.ToEntity(discount);
                _discountService.UpdateDiscount(discount);

                //clean up old references (if changed) and update "HasDiscountsApplied" properties
                if (prevDiscountType == DiscountType.AssignedToCategories &&
                    discount.DiscountType != DiscountType.AssignedToCategories)
                {
                    //applied to categories
                    var categories = discount.AppliedToCategories.ToList();
                    discount.AppliedToCategories.Clear();
                    _discountService.UpdateDiscount(discount);
                    //update "HasDiscountsApplied" property
                    foreach (var category in categories)
                    {
                        _categoryService.UpdateHasDiscountsApplied(category);
                    }
                }
                if (prevDiscountType == DiscountType.AssignedToSkus &&
                    discount.DiscountType != DiscountType.AssignedToSkus)
                {
                    //applied to products
                    var products = discount.AppliedToProducts.ToList();
                    discount.AppliedToProducts.Clear();
                    _discountService.UpdateDiscount(discount);
                    //update "HasDiscountsApplied" property
                    foreach (var product in products)
                    {
                        _productService.UpdateHasDiscountsApplied(product);
                    }
                }

                //activity log
                _customerActivityService.InsertActivity("EditDiscount", _localizationService.GetResource("ActivityLog.EditDiscount"), discount.Name);

                NotifySuccess(_localizationService.GetResource("Admin.Promotions.Discounts.Updated"));
                return(continueEditing ? RedirectToAction("Edit", discount.Id) : RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            PrepareDiscountModel(model, discount);
            return(View(model));
        }
Exemple #2
0
        public ActionResult Create(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            if (ModelState.IsValid)
            {
                var discount = model.ToEntity();
                _discountService.InsertDiscount(discount);

                //activity log
                _customerActivityService.InsertActivity("AddNewDiscount", _localizationService.GetResource("ActivityLog.AddNewDiscount"), discount.Name);

                SuccessNotification(_localizationService.GetResource("Admin.Promotions.Discounts.Added"));

                if (continueEditing)
                {
                    //selected tab
                    SaveSelectedTabName();

                    return(RedirectToAction("Edit", new { id = discount.Id }));
                }
                return(RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            PrepareDiscountModel(model, null);
            return(View(model));
        }
        public virtual IActionResult Create(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            if (ModelState.IsValid)
            {
                var discount = model.ToEntity <Discount>();
                _discountService.InsertDiscount(discount);

                //activity log
                _customerActivityService.InsertActivity("AddNewDiscount",
                                                        string.Format(_localizationService.GetResource("ActivityLog.AddNewDiscount"), discount.Name), discount);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Promotions.Discounts.Added"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = discount.Id }));
            }

            //prepare model
            model = _discountModelFactory.PrepareDiscountModel(model, null, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
Exemple #4
0
        public ActionResult Edit(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                throw new ArgumentException("No discount found with the specified id");
            }
            if (ModelState.IsValid)
            {
                discount = model.ToEntity(discount);
                _discountService.UpdateDiscount(discount);

                //activity log
                _customerActivityService.InsertActivity("EditDiscount", _localizationService.GetResource("ActivityLog.EditDiscount"), discount.Name);

                SuccessNotification(_localizationService.GetResource("Admin.Promotions.Discounts.Updated"));
                return(continueEditing ? RedirectToAction("Edit", discount.Id) : RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            PrepareDiscountModel(model, discount);
            return(View(model));
        }
        public ActionResult Edit(DiscountModel model, bool continueEditing)
        {
            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevDiscountType = discount.DiscountType;

                discount = model.ToEntity(discount);

                // Add\remove assigned rule sets.
                _ruleStorage.ApplyRuleSetMappings(discount, model.SelectedRuleSetIds);

                _discountService.UpdateDiscount(discount);

                // Clean up old references (if changed) and update "HasDiscountsApplied" properties.
                if (prevDiscountType == DiscountType.AssignedToCategories && discount.DiscountType != DiscountType.AssignedToCategories)
                {
                    var categories = discount.AppliedToCategories.ToList();
                    discount.AppliedToCategories.Clear();
                    _discountService.UpdateDiscount(discount);

                    categories.Each(x => _categoryService.UpdateHasDiscountsApplied(x));
                }

                if (prevDiscountType == DiscountType.AssignedToManufacturers && discount.DiscountType != DiscountType.AssignedToManufacturers)
                {
                    var manufacturers = discount.AppliedToManufacturers.ToList();
                    discount.AppliedToManufacturers.Clear();
                    _discountService.UpdateDiscount(discount);

                    manufacturers.Each(x => _manufacturerService.UpdateHasDiscountsApplied(x));
                }

                if (prevDiscountType == DiscountType.AssignedToSkus && discount.DiscountType != DiscountType.AssignedToSkus)
                {
                    var products = discount.AppliedToProducts.ToList();
                    discount.AppliedToProducts.Clear();
                    _discountService.UpdateDiscount(discount);

                    products.Each(x => _productService.UpdateHasDiscountsApplied(x));
                }

                _customerActivityService.InsertActivity("EditDiscount", T("ActivityLog.EditDiscount"), discount.Name);

                NotifySuccess(T("Admin.Promotions.Discounts.Updated"));
                return(continueEditing ? RedirectToAction("Edit", discount.Id) : RedirectToAction("List"));
            }

            PrepareDiscountModel(model, discount);
            return(View(model));
        }
        public ActionResult Edit(DiscountModel model, bool continueEditing)
        {
            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevDiscountType = discount.DiscountType;
                discount = model.ToEntity(discount);
                _discountService.UpdateDiscount(discount);

                //clean up old references (if changed) and update "HasDiscountsApplied" properties
                if (prevDiscountType == DiscountType.AssignedToCategories && discount.DiscountType != DiscountType.AssignedToCategories)
                {
                    //applied to categories
                    var categories = discount.AppliedToCategories.ToList();
                    discount.AppliedToCategories.Clear();
                    _discountService.UpdateDiscount(discount);

                    categories.Each(x => _categoryService.UpdateHasDiscountsApplied(x));
                }

                if (prevDiscountType == DiscountType.AssignedToManufacturers && discount.DiscountType != DiscountType.AssignedToManufacturers)
                {
                    var manufacturers = discount.AppliedToManufacturers.ToList();
                    discount.AppliedToManufacturers.Clear();
                    _discountService.UpdateDiscount(discount);

                    manufacturers.Each(x => _manufacturerService.UpdateHasDiscountsApplied(x));
                }

                if (prevDiscountType == DiscountType.AssignedToSkus && discount.DiscountType != DiscountType.AssignedToSkus)
                {
                    //applied to products
                    var products = discount.AppliedToProducts.ToList();
                    discount.AppliedToProducts.Clear();
                    _discountService.UpdateDiscount(discount);

                    products.Each(x => _productService.UpdateHasDiscountsApplied(x));
                }

                //activity log
                _customerActivityService.InsertActivity("EditDiscount", _services.Localization.GetResource("ActivityLog.EditDiscount"), discount.Name);

                NotifySuccess(_services.Localization.GetResource("Admin.Promotions.Discounts.Updated"));
                return(continueEditing ? RedirectToAction("Edit", discount.Id) : RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            PrepareDiscountModel(model, discount);
            return(View(model));
        }
        public virtual Discount InsertDiscountModel(DiscountModel model)
        {
            var discount = model.ToEntity();

            _discountService.InsertDiscount(discount);

            //activity log
            _customerActivityService.InsertActivity("AddNewDiscount", discount.Id, _localizationService.GetResource("ActivityLog.AddNewDiscount"), discount.Name);
            return(discount);
        }
        public virtual async Task <Discount> InsertDiscountModel(DiscountModel model)
        {
            var discount = model.ToEntity(_dateTimeHelper);
            await _discountService.InsertDiscount(discount);

            //activity log
            await _customerActivityService.InsertActivity("AddNewDiscount", discount.Id, _localizationService.GetResource("ActivityLog.AddNewDiscount"), discount.Name);

            return(discount);
        }
        public virtual async Task <Discount> UpdateDiscountModel(Discount discount, DiscountModel model)
        {
            var prevDiscountType = discount.DiscountType;

            discount = model.ToEntity(discount, _dateTimeHelper);
            await _discountService.UpdateDiscount(discount);

            //clean up old references (if changed) and update "HasDiscountsApplied" properties
            if (prevDiscountType == DiscountType.AssignedToCategories &&
                discount.DiscountType != DiscountType.AssignedToCategories)
            {
                //applied to categories
                //_categoryService.
                var categories = await _categoryService.GetAllCategoriesByDiscount(discount.Id);

                //update "HasDiscountsApplied" property
                foreach (var category in categories)
                {
                    var item = category.AppliedDiscounts.Where(x => x == discount.Id).FirstOrDefault();
                    category.AppliedDiscounts.Remove(item);
                }
            }
            if (prevDiscountType == DiscountType.AssignedToManufacturers &&
                discount.DiscountType != DiscountType.AssignedToManufacturers)
            {
                //applied to manufacturers
                var manufacturers = await _manufacturerService.GetAllManufacturersByDiscount(discount.Id);

                foreach (var manufacturer in manufacturers)
                {
                    var item = manufacturer.AppliedDiscounts.Where(x => x == discount.Id).FirstOrDefault();
                    manufacturer.AppliedDiscounts.Remove(item);
                }
            }
            if (prevDiscountType == DiscountType.AssignedToSkus &&
                discount.DiscountType != DiscountType.AssignedToSkus)
            {
                //applied to products
                var products = await _productService.GetProductsByDiscount(discount.Id);

                foreach (var p in products)
                {
                    var item = p.AppliedDiscounts.Where(x => x == discount.Id).FirstOrDefault();
                    p.AppliedDiscounts.Remove(item);
                    await _productService.DeleteDiscount(item, p.Id);
                }
            }

            //activity log
            await _customerActivityService.InsertActivity("EditDiscount", discount.Id, _localizationService.GetResource("ActivityLog.EditDiscount"), discount.Name);

            return(discount);
        }
        public ActionResult Create(DiscountModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                var discount = model.ToEntity();
                _discountService.InsertDiscount(discount);

                //activity log
                _customerActivityService.InsertActivity("AddNewDiscount", _services.Localization.GetResource("ActivityLog.AddNewDiscount"), discount.Name);

                NotifySuccess(_services.Localization.GetResource("Admin.Promotions.Discounts.Added"));
                return(continueEditing ? RedirectToAction("Edit", new { id = discount.Id }) : RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            PrepareDiscountModel(model, null);
            return(View(model));
        }
        public ActionResult Create(DiscountModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                var discount = model.ToEntity();
                _discountService.InsertDiscount(discount);

                if (model.SelectedRuleSetIds?.Any() ?? false)
                {
                    _ruleStorage.ApplyRuleSetMappings(discount, model.SelectedRuleSetIds);

                    _discountService.UpdateDiscount(discount);
                }

                _customerActivityService.InsertActivity("AddNewDiscount", T("ActivityLog.AddNewDiscount"), discount.Name);

                NotifySuccess(T("Admin.Promotions.Discounts.Added"));
                return(continueEditing ? RedirectToAction("Edit", new { id = discount.Id }) : RedirectToAction("List"));
            }

            PrepareDiscountModel(model, null);
            return(View(model));
        }
        public virtual IActionResult Edit(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            //try to get a discount with the specified id
            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevDiscountType = discount.DiscountType;
                discount = model.ToEntity(discount);
                _discountService.UpdateDiscount(discount);

                //clean up old references (if changed) and update "HasDiscountsApplied" properties
                if (prevDiscountType == DiscountType.AssignedToCategories && discount.DiscountType != DiscountType.AssignedToCategories)
                {
                    //applied to categories
                    discount.DiscountCategoryMappings.Clear();
                    _discountService.UpdateDiscount(discount);
                }

                if (prevDiscountType == DiscountType.AssignedToManufacturers && discount.DiscountType != DiscountType.AssignedToManufacturers)
                {
                    //applied to manufacturers
                    discount.DiscountManufacturerMappings.Clear();
                    _discountService.UpdateDiscount(discount);
                }

                if (prevDiscountType == DiscountType.AssignedToSkus && discount.DiscountType != DiscountType.AssignedToSkus)
                {
                    //applied to products
                    var products = _discountService.GetProductsWithAppliedDiscount(discount.Id, true);

                    discount.DiscountProductMappings.Clear();
                    _discountService.UpdateDiscount(discount);

                    //update "HasDiscountsApplied" property
                    foreach (var p in products)
                    {
                        _productService.UpdateHasDiscountsApplied(p);
                    }
                }

                //activity log
                _customerActivityService.InsertActivity("EditDiscount",
                                                        string.Format(_localizationService.GetResource("ActivityLog.EditDiscount"), discount.Name), discount);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Promotions.Discounts.Updated"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = discount.Id }));
            }

            //prepare model
            model = _discountModelFactory.PrepareDiscountModel(model, discount, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
        /// <returns>A task that represents the asynchronous operation</returns>
        public virtual async Task <IActionResult> Edit(DiscountModel model, bool continueEditing)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            //try to get a discount with the specified id
            var discount = await _discountService.GetDiscountByIdAsync(model.Id);

            if (discount == null)
            {
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevDiscountType = discount.DiscountType;
                discount = model.ToEntity(discount);
                await _discountService.UpdateDiscountAsync(discount);

                //clean up old references (if changed)
                if (prevDiscountType != discount.DiscountType)
                {
                    switch (prevDiscountType)
                    {
                    case DiscountType.AssignedToSkus:
                        await _productService.ClearDiscountProductMappingAsync(discount);

                        break;

                    case DiscountType.AssignedToCategories:
                        await _categoryService.ClearDiscountCategoryMappingAsync(discount);

                        break;

                    case DiscountType.AssignedToManufacturers:
                        await _manufacturerService.ClearDiscountManufacturerMappingAsync(discount);

                        break;

                    default:
                        break;
                    }
                }

                //activity log
                await _customerActivityService.InsertActivityAsync("EditDiscount",
                                                                   string.Format(await _localizationService.GetResourceAsync("ActivityLog.EditDiscount"), discount.Name), discount);

                _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Promotions.Discounts.Updated"));

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = discount.Id }));
            }

            //prepare model
            model = await _discountModelFactory.PrepareDiscountModelAsync(model, discount, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }
Exemple #14
0
        public ActionResult Edit(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                //No discount found with the specified id
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                discount = model.ToEntity(discount);
                _discountService.UpdateDiscount(discount);

                //related Item Ids
                model.RelatedItemIds = model.RelatedItemIds == null ? "" : model.RelatedItemIds;
                var relatedItemIds = model.RelatedItemIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToList();
                if (relatedItemIds.Count > 0)
                {
                    switch (discount.DiscountType)
                    {
                    case DiscountType.AssignedToManufacturers:

                        //apply discount all manufacturers except related ones
                        if (model.ExludeRelatedItems)
                        {
                            var manufacturers = _manufacturerService.GetAllManufacturers();
                            relatedItemIds = manufacturers.Select(x => x.Id).Except(relatedItemIds).ToList();
                        }

                        List <Manufacturer> itemsToBoRemoved = new List <Manufacturer>();
                        foreach (var manufacturer in discount.AppliedToManufacturers)
                        {
                            if (relatedItemIds.Where(x => x == manufacturer.Id).Count() == 0)
                            {
                                itemsToBoRemoved.Add(manufacturer);
                            }
                        }
                        foreach (var item in itemsToBoRemoved)
                        {
                            discount.AppliedToManufacturers.Remove(item);
                        }

                        foreach (var id in relatedItemIds)
                        {
                            var manufacturer = _manufacturerService.GetManufacturerById(id);
                            if (manufacturer == null)
                            {
                                continue;
                            }
                            if (manufacturer.AppliedDiscounts.Where(d => d.Id == discount.Id).Count() == 0)
                            {
                                discount.AppliedToManufacturers.Add(manufacturer);
                            }
                            //manufacturer.AppliedDiscounts.Add(discount);
                        }
                        break;

                    case DiscountType.AssignedToSkus:

                        break;

                    case DiscountType.AssignedToCategories:

                        break;
                    }
                }

                _discountService.UpdateDiscount(discount);

                //activity log
                _customerActivityService.InsertActivity("EditDiscount", _localizationService.GetResource("ActivityLog.EditDiscount"), discount.Name);

                SuccessNotification(_localizationService.GetResource("Admin.Promotions.Discounts.Updated"));
                return(continueEditing ? RedirectToAction("Edit", discount.Id) : RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            PrepareDiscountModel(model, discount);
            return(View(model));
        }
        public ActionResult Edit(DiscountModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            var discount = _discountService.GetDiscountById(model.Id);

            if (discount == null)
            {
                //No discount found with the specified id
                return(RedirectToAction("List"));
            }

            if (ModelState.IsValid)
            {
                var prevDiscountType = discount.DiscountType;
                discount = model.ToEntity(discount);
                _discountService.UpdateDiscount(discount);

                //clean up old references (if changed) and update "HasDiscountsApplied" properties
                if (prevDiscountType == DiscountType.AssignedToCategories &&
                    discount.DiscountType != DiscountType.AssignedToCategories)
                {
                    //applied to categories
                    //_categoryService.
                    var categories = _categoryService.GetAllCategoriesByDiscount(discount.Id);

                    //update "HasDiscountsApplied" property
                    foreach (var category in categories)
                    {
                        var item = category.AppliedDiscounts.Where(x => x.Id == discount.Id).FirstOrDefault();
                        category.AppliedDiscounts.Remove(item);
                        //_discountService.UpdateDiscount(discount);
                        //_categoryService.Update(category);
                    }
                }
                if (prevDiscountType == DiscountType.AssignedToManufacturers &&
                    discount.DiscountType != DiscountType.AssignedToManufacturers)
                {
                    //applied to manufacturers
                    var manufacturers = _manufacturerService.GetAllManufacturersByDiscount(discount.Id);
                    foreach (var manufacturer in manufacturers)
                    {
                        var item = manufacturer.AppliedDiscounts.Where(x => x.Id == discount.Id).FirstOrDefault();
                        manufacturer.AppliedDiscounts.Remove(item);
                        //_manufacturerService.UpdateHasDiscountsApplied(manufacturer);
                    }
                }
                if (prevDiscountType == DiscountType.AssignedToSkus &&
                    discount.DiscountType != DiscountType.AssignedToSkus)
                {
                    //applied to products
                    //var products = discount.AppliedToProducts.ToList();
                    var products = _productService.GetProductsByDiscount(discount.Id);

                    foreach (var p in products)
                    {
                        var item = p.AppliedDiscounts.Where(x => x.Id == discount.Id).FirstOrDefault();
                        p.AppliedDiscounts.Remove(item);
                        _productService.DeleteDiscount(item, p.Id);
                        _productService.UpdateHasDiscountsApplied(p.Id);
                    }
                }

                //activity log
                _customerActivityService.InsertActivity("EditDiscount", discount.Id, _localizationService.GetResource("ActivityLog.EditDiscount"), discount.Name);

                SuccessNotification(_localizationService.GetResource("Admin.Promotions.Discounts.Updated"));

                if (continueEditing)
                {
                    //selected tab
                    SaveSelectedTabIndex();

                    return(RedirectToAction("Edit", new { id = discount.Id }));
                }
                return(RedirectToAction("List"));
            }

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