Example #1
0
        public ActionResult Restrictions()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageShippingSettings))
            {
                return(AccessDeniedView());
            }

            var model = new ShippingMethodRestrictionModel();

            var countries       = _countryService.GetAllCountries(true);
            var shippingMethods = _shippingService.GetAllShippingMethods();

            foreach (var country in countries)
            {
                model.AvailableCountries.Add(new CountryModel()
                {
                    Id   = country.Id,
                    Name = country.Name
                });
            }
            foreach (var sm in shippingMethods)
            {
                model.AvailableShippingMethods.Add(new ShippingMethodModel()
                {
                    Id   = sm.Id,
                    Name = sm.Name
                });
            }
            foreach (var country in countries)
            {
                foreach (var shippingMethod in shippingMethods)
                {
                    bool restricted = shippingMethod.CountryRestrictionExists(country.Id);
                    if (!model.Restricted.ContainsKey(country.Id))
                    {
                        model.Restricted[country.Id] = new Dictionary <int, bool>();
                    }
                    model.Restricted[country.Id][shippingMethod.Id] = restricted;
                }
            }

            return(View(model));
        }
Example #2
0
        /// <summary>
        /// Prepare shipping method restriction model
        /// </summary>
        /// <param name="model">Shipping method restriction model</param>
        /// <returns>Shipping method restriction model</returns>
        public virtual ShippingMethodRestrictionModel PrepareShippingMethodRestrictionModel(ShippingMethodRestrictionModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var countries = _countryService.GetAllCountries(showHidden: true);

            model.AvailableCountries = countries.Select(country => country.ToModel()).ToList();

            foreach (var shippingMethod in _shippingService.GetAllShippingMethods())
            {
                model.AvailableShippingMethods.Add(shippingMethod.ToModel());
                foreach (var country in countries)
                {
                    if (!model.Restricted.ContainsKey(country.Id))
                    {
                        model.Restricted[country.Id] = new Dictionary <int, bool>();
                    }

                    model.Restricted[country.Id][shippingMethod.Id] = shippingMethod.CountryRestrictionExists(country.Id);
                }
            }

            return(model);
        }
        public async Task <IActionResult> Restrictions()
        {
            var model = new ShippingMethodRestrictionModel();

            var countries = await _countryService.GetAllCountries(showHidden : true);

            var shippingMethods = await _shippingMethodService.GetAllShippingMethods();

            var customerRoles = await _customerService.GetAllCustomerRoles();

            foreach (var country in countries)
            {
                model.AvailableCountries.Add(new CountryModel {
                    Id   = country.Id,
                    Name = country.Name
                });
            }
            foreach (var sm in shippingMethods)
            {
                model.AvailableShippingMethods.Add(new ShippingMethodModel {
                    Id   = sm.Id,
                    Name = sm.Name
                });
            }
            foreach (var r in customerRoles)
            {
                model.AvailableCustomerRoles.Add(new CustomerRoleModel()
                {
                    Id = r.Id, Name = r.Name
                });
            }

            foreach (var country in countries)
            {
                foreach (var shippingMethod in shippingMethods)
                {
                    bool restricted = shippingMethod.CountryRestrictionExists(country.Id);
                    if (!model.Restricted.ContainsKey(country.Id))
                    {
                        model.Restricted[country.Id] = new Dictionary <string, bool>();
                    }
                    model.Restricted[country.Id][shippingMethod.Id] = restricted;
                }
            }

            foreach (var role in customerRoles)
            {
                foreach (var shippingMethod in shippingMethods)
                {
                    bool restricted = shippingMethod.CustomerRoleRestrictionExists(role.Id);
                    if (!model.RestictedRole.ContainsKey(role.Id))
                    {
                        model.RestictedRole[role.Id] = new Dictionary <string, bool>();
                    }
                    model.RestictedRole[role.Id][shippingMethod.Id] = restricted;
                }
            }


            return(View(model));
        }
Example #4
0
        /// <summary>
        /// Prepare shipping method restriction model
        /// </summary>
        /// <param name="model">Shipping method restriction model</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the shipping method restriction model
        /// </returns>
        public virtual async Task <ShippingMethodRestrictionModel> PrepareShippingMethodRestrictionModelAsync(ShippingMethodRestrictionModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var countries = await _countryService.GetAllCountriesAsync(showHidden : true);

            model.AvailableCountries = await countries.SelectAwait(async country =>
            {
                var countryModel            = country.ToModel <CountryModel>();
                countryModel.NumberOfStates = (await _stateProvinceService.GetStateProvincesByCountryIdAsync(country.Id))?.Count ?? 0;

                return(countryModel);
            }).ToListAsync();

            foreach (var shippingMethod in await _shippingService.GetAllShippingMethodsAsync())
            {
                model.AvailableShippingMethods.Add(shippingMethod.ToModel <ShippingMethodModel>());
                foreach (var country in countries)
                {
                    if (!model.Restricted.ContainsKey(country.Id))
                    {
                        model.Restricted[country.Id] = new Dictionary <int, bool>();
                    }

                    model.Restricted[country.Id][shippingMethod.Id] = await _shippingService.CountryRestrictionExistsAsync(shippingMethod, country.Id);
                }
            }

            return(model);
        }