Esempio n. 1
0
        public virtual async Task <double> CalculateAttributeOptionPriceForUserAsync(int optionId, TblUsers user)
        {
            var option = await FindOptionByIdAsync(optionId);

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

            var result = option.Price;

            if (result <= 0)
            {
                return(0);
            }

            var alreadyPurchased = user?.Invoices.Where(p => p.Status == InvoiceStatus.Paid)
                                   .SelectMany(p => p.InvoiceDetails).Where(p => p.ItemType == InvoiceDetailsItemType.ProductAttributeOption &&
                                                                            p.ItemId == optionId).ToList();

            if (alreadyPurchased != null &&
                alreadyPurchased.OrderByDescending(p => p.PurchaseExpiration).Any(p =>
                                                                                  DateTime.Now <= p.PurchaseExpiration))
            {
                result = option.RenewalPrice;
            }

            if (user?.UserGroup != null &&
                user.SubscriptionExpireDate > DateTime.Now)
            {
                var userGroupsDiscount = _productDiscountsForUserGroupsService
                                         .FindProductDiscounts(option.ProductCheckoutAttribute.ProductId)?.ToList()
                                         .Where(p =>
                                                p.ApplyDiscountToProductAttributes).ToList();
                if (userGroupsDiscount != null && userGroupsDiscount.Any())
                {
                    var discount =
                        userGroupsDiscount.FirstOrDefault(p => p.UserGroupId == user.UserGroupId);
                    if (discount?.DiscountPercent > 0)
                    {
                        result = result - ((result * discount.DiscountPercent) / 100);
                        if (result < 0)
                        {
                            result = 0;
                        }
                    }
                    else
                    {
                        discount =
                            userGroupsDiscount
                            .Where(p => p.ApplyDiscountToHigherUserGroups &&
                                   p.UserGroup.GroupPriority <= user.UserGroup.GroupPriority)
                            .OrderByDescending(p => p.UserGroup.GroupPriority)
                            .FirstOrDefault();
                        if (discount?.DiscountPercent > 0)
                        {
                            result = result - ((result * discount.DiscountPercent) / 100);
                            if (result < 0)
                            {
                                result = 0;
                            }
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
        public virtual double CalculateProductPriceForUser(TblProducts product, TblUsers user)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            var result = product.Price;

            if (result <= 0)
            {
                return(0);
            }

            var alreadyPurchased = user?.Invoices?.Where(p => p.Status == InvoiceStatus.Paid)
                                   .SelectMany(p => p.InvoiceDetails).Where(p => p.ItemType == InvoiceDetailsItemType.Product &&
                                                                            p.ItemId == product.Id).ToList();

            if (alreadyPurchased != null &&
                alreadyPurchased.OrderByDescending(p => p.PurchaseExpiration).Any(p =>
                                                                                  DateTime.Now <= p.PurchaseExpiration))
            {
                result = product.RenewalPrice;
            }

            if (user?.UserGroup != null &&
                user.SubscriptionExpireDate > DateTime.Now)
            {
                var discountsForUserGroup = _productDiscountsForUserGroupsService.FindProductDiscounts(product.Id)?.ToList();
                if (discountsForUserGroup != null &&
                    discountsForUserGroup.Any())
                {
                    var discount =
                        discountsForUserGroup.FirstOrDefault(p => p.UserGroupId == user.UserGroupId);
                    if (discount?.DiscountPercent > 0)
                    {
                        result = result - (result * discount.DiscountPercent) / 100;
                        if (result < 0)
                        {
                            result = 0;
                        }
                    }
                    else
                    {
                        discount =
                            discountsForUserGroup
                            .Where(p => p.ApplyDiscountToHigherUserGroups &&
                                   p.UserGroup.GroupPriority <= user.UserGroup.GroupPriority)
                            .OrderByDescending(p => p.UserGroup.GroupPriority)
                            .FirstOrDefault();
                        if (discount?.DiscountPercent > 0)
                        {
                            result = result - ((result * discount.DiscountPercent) / 100);
                            if (result < 0)
                            {
                                result = 0;
                            }
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        protected virtual List <Tuple <TblUserGroups, string> > GenerateUserGroupDiscountsDescription(TblProducts product, TblUsers user)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            var result = new List <Tuple <TblUserGroups, string> >();

            var fromGroup = user?.UserGroupId != null ? user.UserGroup.GroupPriority : int.MinValue;
            var discounts = _productDiscountsForUserGroupsService.FindProductDiscounts(product.Id);

            foreach (var discount in discounts.Where(p => p.UserGroup.GroupPriority >= fromGroup))
            {
                var price     = product.Price - (discount.DiscountPercent * product.Price) / 100;
                var groupName = discount.UserGroup.GetLocalized(p => p.GroupName);

                if (price <= 0)
                {
                    //Free
                    if (discount.ApplyDiscountToHigherUserGroups)
                    {
                        result.Add(new Tuple <TblUserGroups, string>(discount.UserGroup, string.Format(
                                                                         _localizationService.GetResource("FreeForUserGroupsOrHigher"),
                                                                         groupName,
                                                                         discount.UserGroup.Id,
                                                                         discount.UserGroup.GroupPriority,
                                                                         product.Id,
                                                                         product.Slug)));
                    }
                    else
                    {
                        result.Add(new Tuple <TblUserGroups, string>(discount.UserGroup, string.Format(
                                                                         _localizationService.GetResource("FreeForUserGroups"),
                                                                         groupName,
                                                                         discount.UserGroup.Id,
                                                                         discount.UserGroup.GroupPriority,
                                                                         product.Id,
                                                                         product.Slug)));
                    }
                }
                else
                {
                    if (discount.ApplyDiscountToHigherUserGroups)
                    {
                        result.Add(new Tuple <TblUserGroups, string>(discount.UserGroup, string.Format(
                                                                         _localizationService.GetResource("DiscountForUserGroupsOrHigher"),
                                                                         discount.DiscountPercent,
                                                                         groupName,
                                                                         price.ExchangeCurrencyStr(),
                                                                         discount.UserGroup.Id,
                                                                         discount.UserGroup.GroupPriority,
                                                                         product.Id,
                                                                         product.Slug)));
                    }
                    else
                    {
                        result.Add(new Tuple <TblUserGroups, string>(discount.UserGroup, string.Format(
                                                                         _localizationService.GetResource("DiscountForUserGroups"),
                                                                         discount.DiscountPercent,
                                                                         groupName,
                                                                         price.ExchangeCurrencyStr(),
                                                                         discount.UserGroup.Id,
                                                                         discount.UserGroup.GroupPriority,
                                                                         product.Id,
                                                                         product.Slug)));
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
        protected virtual string GenerateUserGroupDiscountsDescription(TblProducts product, TblUsers user)
        {
            string result = "";

            var fromGroup = user?.UserGroupId != null ? user.UserGroup.GroupPriority : int.MinValue;
            var discounts = _productDiscountsForUserGroupsService.FindProductDiscounts(product.Id);

            foreach (var discount in discounts.Where(p => p.UserGroup.GroupPriority >= fromGroup))
            {
                var price     = product.Price - (discount.DiscountPercent * product.Price) / 100;
                var groupName = discount.UserGroup.GetLocalized(p => p.GroupName);

                if (price <= 0)
                {
                    //Free
                    if (discount.ApplyDiscountToHigherUserGroups)
                    {
                        result += string.Format(
                            _localizationService.GetResource("FreeForUserGroupsOrHigher"),
                            groupName,
                            discount.UserGroup.Id,
                            discount.UserGroup.GroupPriority,
                            product.Id,
                            product.Slug) + "<br/>";
                    }
                    else
                    {
                        result += string.Format(
                            _localizationService.GetResource("FreeForUserGroups"),
                            groupName,
                            discount.UserGroup.Id,
                            discount.UserGroup.GroupPriority,
                            product.Id,
                            product.Slug) + "<br/>";
                    }
                }
                else
                {
                    if (discount.ApplyDiscountToHigherUserGroups)
                    {
                        result += string.Format(
                            _localizationService.GetResource("DiscountForUserGroupsOrHigher"),
                            discount.DiscountPercent,
                            groupName,
                            price.ExchangeCurrencyStr(),
                            discount.UserGroup.Id,
                            discount.UserGroup.GroupPriority,
                            product.Id,
                            product.Slug) + "<br/>";
                    }
                    else
                    {
                        result += string.Format(
                            _localizationService.GetResource("DiscountForUserGroups"),
                            discount.DiscountPercent,
                            groupName,
                            price.ExchangeCurrencyStr(),
                            discount.UserGroup.Id,
                            discount.UserGroup.GroupPriority,
                            product.Id,
                            product.Slug) + "<br/>";
                    }
                }
            }

            return(result);
        }