Пример #1
0
        public IList <CartItem> GetCartItemsByProfileId(int profileId, bool autoRemovePhoneOrderItems, bool?isPharmaceutical = null)
        {
            var items = _cartItemRepository.Table
                        .Where(c => c.ProfileId == profileId)
                        .ToList()
                        .Select(c => _cartItemBuilder.Build(c));

            if (isPharmaceutical.HasValue)
            {
                items = items.Where(c => c.Product.IsPharmaceutical == isPharmaceutical.Value);
            }

            if (autoRemovePhoneOrderItems)
            {
                var phoneOrderItemIds = items.Where(x => x.Product.IsPhoneOrder == true).Select(x => x.Id);
                if (phoneOrderItemIds.Count() > 0)
                {
                    foreach (var phoneOrderItemId in phoneOrderItemIds)
                    {
                        _cartItemRepository.Delete(phoneOrderItemId);
                    }

                    items = items.Where(x => x.Product.IsPhoneOrder == false);
                }
            }

            var list = items.ToList();

            return(list);
        }
Пример #2
0
        /// <summary>
        /// Get cart item total (including tax) which is not on offer in GBP.
        /// </summary>
        private decimal GetOfferExcludedCartTotalInclTax(int profileId)
        {
            var total = _cartItemRepository.Table
                        .Where(i => i.ProfileId == profileId)
                        .ToList()
                        .Select(i => _cartItemBuilder.Build(i))
                        .Where(i => i.ProductPrice.OfferRuleId == 0)
                        .Select(i => i.ProductPrice.OfferPriceInclTax * i.Quantity)
                        .DefaultIfEmpty(0M)
                        .Sum();

            return(total);
        }
Пример #3
0
        public CartOffer ProcessCart(int profileId, string shippingCountryCode, string promoCode = "", int testOfferRuleId = 0)
        {
            promoCode = promoCode.ToLower().Trim();
            CartOffer cartOffer = new CartOffer();

            var cartRules = _offerBuilder.GetActiveOfferRulesByType(OfferRuleType.Cart);

            if (testOfferRuleId > 0)
            {
                var testRule = _offerBuilder.PrepareTestOfferRule(testOfferRuleId);
                cartRules = _offerBuilder.CloneActiveOfferRulesByType(OfferRuleType.Cart);
                cartRules.Remove(testRule);
                cartRules.Add(testRule);
                cartRules = cartRules.OrderBy(x => x.Priority).ToList();
            }

            if (cartRules.Count > 0)
            {
                // Get all items by profile ID
                var items = _cartItemRepository.Table
                            .Where(x => x.ProfileId == profileId)
                            .ToList()
                            .Select(x => _cartItemBuilder.Build(x))
                            .ToList();

                // Do a basket cleanup
                items = _cartUtility.RemoveFreeItemWithOffers(items, cartRules.Select(x => x.Id).ToArray());

                for (int i = 0; i < cartRules.Count; i++)
                {
                    // Check status and dates
                    // In theory, all inactive / expired rules will not be loaded from database
                    if ((cartRules[i].IsActive) &&
                        ((cartRules[i].StartDate.HasValue == false || cartRules[i].StartDate.Value.CompareTo(DateTime.Now) <= 0) &&
                         (cartRules[i].EndDate.HasValue == false || cartRules[i].EndDate.Value.CompareTo(DateTime.Now) >= 0)))
                    {
                        bool proceed = true;

                        if (profileId != 0 && !string.IsNullOrEmpty(promoCode) && promoCode.CompareTo(cartRules[i].PromoCode.ToLower()) == 0)
                        {
                            int orderQuantity = _cartUtility.GetNumberOfPaidOrders(promoCode, profileId);

                            // Check promocode and uses per customer
                            // value <= 0 will be considered as proceed
                            proceed = cartRules[i].UsesPerCustomer <= 0 || orderQuantity < cartRules[i].UsesPerCustomer;

                            if (cartRules[i].NewCustomerOnly && orderQuantity > 0)
                            {
                                proceed = false;
                            }
                        }

                        // Set html message from this offer
                        if (string.IsNullOrEmpty(cartRules[i].PromoCode) == false && cartRules[i].PromoCode.ToLower().CompareTo(promoCode) == 0)
                        {
                            cartOffer.Description = cartRules[i].HtmlMessage;
                        }

                        // If entered promocode is empty and promocode from offer is not empty, do not proceed
                        if (string.IsNullOrEmpty(cartRules[i].PromoCode) == false && string.IsNullOrEmpty(promoCode))
                        {
                            proceed = false;
                        }

                        bool conditionMatched = false;

                        if (proceed)
                        {
                            conditionMatched = _cartUtility.ProcessCartCondition(items, cartRules[i].Condition, cartRules[i].OfferedItemIncluded, cartRules[i].UseInitialPrice);

                            if (conditionMatched)
                            {
                                // If cart rule does not have promocode, proceed
                                // If cart rule does not match the given promocode, do not proceed
                                if (string.IsNullOrEmpty(cartRules[i].PromoCode) ||
                                    cartRules[i].PromoCode.ToLower().CompareTo(promoCode) == 0)
                                {
                                    // Run action here
                                    cartOffer = _cartUtility.ProcessCartOfferAction(profileId, items, shippingCountryCode, cartRules[i], cartRules[i].Action, cartOffer);
                                }
                            }

                            if (!cartRules[i].ProceedForNext)
                            {
                                break;
                            }
                        }
                    }
                }

                // Update all cart items in database at the end of the loop.
                foreach (var item in items)
                {
                    if (item.Id > 0)
                    {
                        _cartItemRepository.Update(item);
                    }
                    else
                    {
                        _cartItemRepository.Create(item);
                    }
                }
            }

            return(cartOffer);
        }