예제 #1
0
            /// <summary>
            /// Gets the promotions.
            /// </summary>
            /// <param name="isCheckoutSession">If set to <c>true</c> [is checkout session].</param>
            /// <returns>The service response containing the applicable promotions.</returns>
            public async Task <ActionResult> GetPromotions(bool isCheckoutSession)
            {
                EcommerceContext      ecommerceContext      = ServiceUtilities.GetEcommerceContext(this.HttpContext);
                CartOperationsHandler cartOperationsHandler = new CartOperationsHandler(ecommerceContext);

                SessionType sessionType = ServiceUtilities.GetSessionType(this.HttpContext, isCheckoutSession);
                string      cartId      = ServiceUtilities.GetCartIdFromRequestCookie(this.HttpContext, sessionType);

                CartPromotions cartPromotions = await cartOperationsHandler.GetPromotions(cartId);

                return(this.Json(cartPromotions));
            }
            /// <summary>
            /// Get the applicable promotions for the items in the cart.
            /// </summary>
            /// <param name="shoppingCartId">The shopping cart identifier.</param>
            /// <returns>The applicable promotions.</returns>
            public virtual async Task <CartPromotions> GetPromotions(string shoppingCartId)
            {
                if (string.IsNullOrWhiteSpace(shoppingCartId))
                {
                    throw new ArgumentNullException(nameof(shoppingCartId));
                }

                ManagerFactory managerFactory = Utilities.GetManagerFactory(this.EcommerceContext);
                ICartManager   cartManager    = managerFactory.GetManager <ICartManager>();
                CartPromotions cartPromotions = await cartManager.GetPromotions(shoppingCartId);

                return(cartPromotions);
            }
            /// <summary>
            /// Executes the workflow to fetch the promotions.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>The response.</returns>
            protected override GetPromotionsResponse Process(GetPromotionsRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.CartId, "request.CartId");

                // Get the current instance of the transaction from the database.
                SalesTransaction transaction = CartWorkflowHelper.LoadSalesTransaction(this.Context, request.CartId);

                if (transaction == null)
                {
                    return(new GetPromotionsResponse(null));
                }

                ThrowIf.Null(transaction, "transaction");

                // Calculate totals on the current instance of transaction.
                CartWorkflowHelper.Calculate(this.Context, transaction, CalculationModes.All);

                // The discount lines on this transaction are the discount lines that have been applied.
                SalesTransaction currentSalesTransaction = transaction.Clone <SalesTransaction>();

                Cart cart = CartWorkflowHelper.ConvertToCart(this.Context, currentSalesTransaction);

                CartWorkflowHelper.RemoveHistoricalTenderLines(cart);

                // The discount lines on the transaction are all available discount lines for the items.
                CartWorkflowHelper.LoadAllPeriodicDiscounts(this.Context, currentSalesTransaction);
                SalesTransaction tempSalesTransaction = transaction.Clone <SalesTransaction>();

                transaction = currentSalesTransaction.Clone <SalesTransaction>();

                Collection <string>            cartPromotionLines = new Collection <string>();
                Collection <CartLinePromotion> cartLinePromotions = new Collection <CartLinePromotion>();

                for (int i = 0; i < currentSalesTransaction.SalesLines.Count; i++)
                {
                    // Removing the applied discount lines, except multiple buy because a different discount level of the already applied multi buy discount can be promoted.
                    foreach (DiscountLine discountLine in currentSalesTransaction.SalesLines[i].DiscountLines)
                    {
                        tempSalesTransaction.SalesLines[i].DiscountLines.Remove(tempSalesTransaction.SalesLines[i].DiscountLines.Where(j => j.OfferId == discountLine.OfferId).SingleOrDefault());
                    }

                    // Removing the discounts that require coupon code.
                    // Removing the discount offers those were not applied (because of concurrency rules).
                    // Removing mix and match discounts (mix and match discounts are not shown as promotions).
                    List <DiscountLine> offerDiscountLines = tempSalesTransaction.SalesLines[i].DiscountLines.Where(j => (j.PeriodicDiscountType == PeriodicDiscountOfferType.Offer) || j.IsDiscountCodeRequired || (j.PeriodicDiscountType == PeriodicDiscountOfferType.MixAndMatch)).ToList();
                    foreach (DiscountLine discountLine in offerDiscountLines)
                    {
                        tempSalesTransaction.SalesLines[i].DiscountLines.Remove(discountLine);
                    }

                    PricingDataManager pricingDataManager = new PricingDataManager(this.Context);

                    // Quantity discounts.
                    // Finding all the quantity discounts that will be applied to the cart.
                    List <DiscountLine> quantityDiscountLines = tempSalesTransaction.SalesLines[i].DiscountLines.Where(j => j.PeriodicDiscountType == PeriodicDiscountOfferType.MultipleBuy).ToList();

                    // Get the multibuy discount lines for this multi buy discounts.
                    IEnumerable <QuantityDiscountLevel> multiBuyDiscountLines = pricingDataManager.GetMultipleBuyDiscountLinesByOfferIds(quantityDiscountLines.Select(j => j.OfferId));

                    foreach (DiscountLine discountLine in quantityDiscountLines)
                    {
                        GetQuantityPromotions(transaction, tempSalesTransaction, this.Context, i, discountLine, multiBuyDiscountLines);
                    }

                    // Threshhold Discounts.
                    // Finding all the threshold discounts that will be applied to the cart.
                    List <DiscountLine> thresholdDiscountLines = tempSalesTransaction.SalesLines[i].DiscountLines.Where(j => j.PeriodicDiscountType == PeriodicDiscountOfferType.Threshold).ToList();

                    // Get the tiers for this threshold discounts
                    IEnumerable <ThresholdDiscountTier> tiers = pricingDataManager.GetThresholdTiersByOfferIds(thresholdDiscountLines.Select(j => j.OfferId));

                    foreach (DiscountLine thresholdDiscount in thresholdDiscountLines)
                    {
                        GetThresholdDiscounts(transaction, tempSalesTransaction, this.Context, i, cartPromotionLines, thresholdDiscount, tiers);
                    }

                    IEnumerable <string> promotionsForCurrentLine = tempSalesTransaction.SalesLines[i].DiscountLines.Select(j => j.OfferName);
                    cartLinePromotions.Add(new CartLinePromotion(cart.CartLines[i].LineId, promotionsForCurrentLine));
                }

                CartPromotions cartPromotions = new CartPromotions(cartPromotionLines, cartLinePromotions);

                return(new GetPromotionsResponse(cartPromotions));
            }