Exemple #1
0
        /// <summary>
        /// Get discount validation result
        /// </summary>
        /// <param name="requirements">Collection of discount requirement</param>
        /// <param name="groupInteractionType">Interaction type within the group of requirements</param>
        /// <param name="customer">Customer</param>
        /// <param name="errors">Errors</param>
        /// <returns>True if result is valid; otherwise false</returns>
        protected bool GetValidationResult(IEnumerable <DiscountRequirement> requirements,
                                           RequirementGroupInteractionType groupInteractionType, Customer customer, List <string> errors)
        {
            var result = false;

            foreach (var requirement in requirements)
            {
                if (requirement.IsGroup)
                {
                    var childRequirements = GetDiscountRequirementsByParent(requirement);
                    //get child requirements for the group
                    var interactionType = requirement.InteractionType ?? RequirementGroupInteractionType.And;
                    result = GetValidationResult(childRequirements, interactionType, customer, errors);
                }
                else
                {
                    //or try to get validation result for the requirement
                    var requirementRulePlugin = _discountPluginManager
                                                .LoadPluginBySystemName(requirement.DiscountRequirementRuleSystemName, customer, _storeContext.CurrentStore.Id);
                    if (requirementRulePlugin == null)
                    {
                        continue;
                    }

                    var ruleResult = requirementRulePlugin.CheckRequirement(new DiscountRequirementValidationRequest
                    {
                        DiscountRequirementId = requirement.Id,
                        Customer = customer,
                        Store    = _storeContext.CurrentStore
                    });

                    //add validation error
                    if (!ruleResult.IsValid)
                    {
                        var userError = !string.IsNullOrEmpty(ruleResult.UserError)
                            ? ruleResult.UserError
                            : _localizationService.GetResource("ShoppingCart.Discount.CannotBeUsed");
                        errors.Add(userError);
                    }

                    result = ruleResult.IsValid;
                }

                //all requirements must be met, so return false
                if (!result && groupInteractionType == RequirementGroupInteractionType.And)
                {
                    return(false);
                }

                //any of requirements must be met, so return true
                if (result && groupInteractionType == RequirementGroupInteractionType.Or)
                {
                    return(true);
                }
            }

            return(result);
        }
        /// <summary>
        /// Prepare discount requirement rule models
        /// </summary>
        /// <param name="requirements">Collection of discount requirements</param>
        /// <param name="discount">Discount</param>
        /// <param name="groupInteractionType">Interaction type within the group of requirements</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the list of discount requirement rule models
        /// </returns>
        public virtual async Task <IList <DiscountRequirementRuleModel> > PrepareDiscountRequirementRuleModelsAsync
            (ICollection <DiscountRequirement> requirements, Discount discount, RequirementGroupInteractionType groupInteractionType)
        {
            if (requirements == null)
            {
                throw new ArgumentNullException(nameof(requirements));
            }

            if (discount == null)
            {
                throw new ArgumentNullException(nameof(discount));
            }

            var lastRequirement = requirements.LastOrDefault();

            return(await requirements.SelectAwait(async requirement =>
            {
                //set common properties
                var requirementModel = new DiscountRequirementRuleModel
                {
                    DiscountRequirementId = requirement.Id,
                    ParentId = requirement.ParentId,
                    IsGroup = requirement.IsGroup,
                    RuleName = requirement.DiscountRequirementRuleSystemName,
                    IsLastInGroup = lastRequirement == null || lastRequirement.Id == requirement.Id,
                    InteractionType = groupInteractionType.ToString().ToUpper()
                };

                var interactionType = requirement.InteractionType ?? RequirementGroupInteractionType.And;
                requirementModel.AvailableInteractionTypes = await interactionType.ToSelectListAsync();

                if (requirement.IsGroup)
                {
                    //get child requirements for the group
                    var childRequirements = await _discountService.GetDiscountRequirementsByParentAsync(requirement);

                    requirementModel.ChildRequirements = await PrepareDiscountRequirementRuleModelsAsync(childRequirements, discount, interactionType);

                    return requirementModel;
                }

                //or try to get name and configuration URL for the requirement
                var requirementRule = await _discountPluginManager.LoadPluginBySystemNameAsync(requirement.DiscountRequirementRuleSystemName);
                if (requirementRule == null)
                {
                    return null;
                }

                requirementModel.RuleName = requirementRule.PluginDescriptor.FriendlyName;
                requirementModel
                .ConfigurationUrl = requirementRule.GetConfigurationUrl(discount.Id, requirement.Id);

                return requirementModel;
            }).ToListAsync());
        }
Exemple #3
0
        protected IList <DiscountModel.DiscountRequirementMetaInfo> GetReqirements(IEnumerable <DiscountRequirement> requirements,
                                                                                   RequirementGroupInteractionType groupInteractionType, Discount discount)
        {
            var lastRequirement = requirements.LastOrDefault();

            return(requirements.Select(requirement =>
            {
                //set common properties
                var requirementModel = new DiscountModel.DiscountRequirementMetaInfo
                {
                    DiscountRequirementId = requirement.Id,
                    ParentId = requirement.ParentId,
                    IsGroup = requirement.IsGroup,
                    RuleName = requirement.DiscountRequirementRuleSystemName,
                    IsLastInGroup = lastRequirement == null || lastRequirement.Id == requirement.Id,
                    InteractionTypeId = (int)groupInteractionType,
                };

                var interactionType = requirement.InteractionType.HasValue
                    ? requirement.InteractionType.Value : RequirementGroupInteractionType.And;
                requirementModel.AvailableInteractionTypes = interactionType.ToSelectList(true);

                if (requirement.IsGroup)
                {
                    //get child requirements for the group
                    requirementModel.ChildRequirements = GetReqirements(requirement.ChildRequirements, interactionType, discount);
                    return requirementModel;
                }

                //or try to get name and configuration URL for the requirement
                var requirementRule = _discountService.LoadDiscountRequirementRuleBySystemName(requirement.DiscountRequirementRuleSystemName);
                if (requirementRule == null)
                {
                    return null;
                }

                requirementModel.RuleName = requirementRule.PluginDescriptor.FriendlyName;
                requirementModel.ConfigurationUrl = GetRequirementUrlInternal(requirementRule, discount, requirement.Id);

                return requirementModel;
            }).ToList());
        }
Exemple #4
0
        /// <summary>
        /// Get discount validation result
        /// </summary>
        /// <param name="requirements">Collection of discount requirement</param>
        /// <param name="groupInteractionType">Interaction type within the group of requirements</param>
        /// <param name="customer">Customer</param>
        /// <param name="errors">Errors</param>
        /// <returns>True if result is valid; otherwise false</returns>
        protected bool GetValidationResult(IEnumerable <DiscountRequirementForCaching> requirements,
                                           RequirementGroupInteractionType groupInteractionType, Customer customer, List <string> errors)
        {
            var result = false;

            foreach (var requirement in requirements)
            {
                if (requirement.IsGroup)
                {
                    //get child requirements for the group
                    var interactionType = requirement.InteractionType ?? RequirementGroupInteractionType.And;
                    result = GetValidationResult(requirement.ChildRequirements, interactionType, customer, errors);
                }
                else
                {
                    //or try to get validation result for the requirement
                    var requirementRulePlugin = LoadDiscountRequirementRuleBySystemName(requirement.SystemName);
                    if (requirementRulePlugin == null)
                    {
                        continue;
                    }

                    if (!_pluginFinder.AuthorizedForUser(requirementRulePlugin.PluginDescriptor, customer))
                    {
                        continue;
                    }

                    if (!_pluginFinder.AuthenticateStore(requirementRulePlugin.PluginDescriptor, _storeContext.CurrentStore.Id))
                    {
                        continue;
                    }

                    var ruleResult = requirementRulePlugin.CheckRequirement(new DiscountRequirementValidationRequest
                    {
                        DiscountRequirementId = requirement.Id,
                        Customer = customer,
                        Store    = _storeContext.CurrentStore
                    });

                    //add validation error
                    if (!ruleResult.IsValid)
                    {
                        errors.Add(ruleResult.UserError);
                    }

                    result = ruleResult.IsValid;
                }

                //all requirements must be met, so return false
                if (!result && groupInteractionType == RequirementGroupInteractionType.And)
                {
                    return(false);
                }

                //any of requirements must be met, so return true
                if (result && groupInteractionType == RequirementGroupInteractionType.Or)
                {
                    return(true);
                }
            }

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Prepare discount requirement rule models
        /// </summary>
        /// <param name="requirements">Collection of discount requirements</param>
        /// <param name="discount">Discount</param>
        /// <param name="groupInteractionType">Interaction type within the group of requirements</param>
        /// <returns>List of discount requirement rule models</returns>
        public virtual IList <DiscountRequirementRuleModel> PrepareDiscountRequirementRuleModels(ICollection <DiscountRequirement> requirements,
                                                                                                 Discount discount, RequirementGroupInteractionType groupInteractionType)
        {
            if (requirements == null)
            {
                throw new ArgumentNullException(nameof(requirements));
            }

            if (discount == null)
            {
                throw new ArgumentNullException(nameof(discount));
            }

            var lastRequirement = requirements.LastOrDefault();

            return(requirements.Select(requirement =>
            {
                //set common properties
                var requirementModel = new DiscountRequirementRuleModel
                {
                    DiscountRequirementId = requirement.Id,
                    ParentId = requirement.ParentId,
                    IsGroup = requirement.IsGroup,
                    RuleName = requirement.DiscountRequirementRuleSystemName,
                    IsLastInGroup = lastRequirement == null || lastRequirement.Id == requirement.Id,
                    InteractionTypeId = (int)groupInteractionType
                };

                var interactionType = requirement.InteractionType ?? RequirementGroupInteractionType.And;
                requirementModel.AvailableInteractionTypes = interactionType.ToSelectList();

                if (requirement.IsGroup)
                {
                    //get child requirements for the group
                    requirementModel
                    .ChildRequirements = PrepareDiscountRequirementRuleModels(requirement.ChildRequirements, discount, interactionType);

                    return requirementModel;
                }

                //or try to get name and configuration URL for the requirement
                var requirementRule = _discountService.LoadDiscountRequirementRuleBySystemName(requirement.DiscountRequirementRuleSystemName);
                if (requirementRule == null)
                {
                    return null;
                }

                requirementModel.RuleName = requirementRule.PluginDescriptor.FriendlyName;
                requirementModel
                .ConfigurationUrl = $"{_webHelper.GetStoreLocation()}{requirementRule.GetConfigurationUrl(discount.Id, requirement.Id)}";

                return requirementModel;
            }).ToList());
        }