Ejemplo n.º 1
0
        public static void RemoveDuplicatePromotionUsages(int CustomerId)
        {
            PromotionsData.EntityContextDataContext db = PromotionsData.DataContextProvider.Current;

            //Grab a list of all promos for this customer that are applied to the current cart (IE not complete)
            var promoUsages = from pu in db.PromotionUsages
                              join p in db.Promotions on pu.PromotionId equals p.Id
                              join pli in db.PromotionLineItems on pu.Id equals pli.PromotionUsageId into pui
                              from pli in pui.DefaultIfEmpty()
                              join sc in db.ShoppingCarts on pli.shoppingCartRecordId equals sc.ShoppingCartRecID into sci
                              from sc in sci.DefaultIfEmpty()
                              where pu.CustomerId == CustomerId && !pu.Complete

                              select new
            {
                promoUsage      = pu
                , promo         = p
                , promoLineItem = pli
                , scItem        = sc
            };

            //Get all the promousages that are duplicates (should only have on promousage per promo on a customers cart, can have multiple complete but should only have one un-complete per promo)
            var groupedPromoUsages = from pu in promoUsages
                                     group pu by new { pu.promoUsage.Complete, pu.promoUsage.CustomerId, pu.promoUsage.PromotionId } into pug
            where pug.Count() > 1

            select new
            {
                pug.Key.CustomerId
                , pug.Key.PromotionId
                , pug.Key.Complete
            };

            if (!groupedPromoUsages.Any())
            {
                return;
            }

            //reduce our list of promo usages down to just those that have duplicates
            promoUsages = from pu in promoUsages
                          join pug in groupedPromoUsages on new { pu.promoUsage.CustomerId, pu.promoUsage.PromotionId, pu.promoUsage.Complete } equals new { pug.CustomerId, pug.PromotionId, pug.Complete }
            select pu;

            foreach (var promoGroup in groupedPromoUsages)
            {
                //we will keep the first dupe we find, doesn't matter which
                var promoToSave = promoUsages.FirstOrDefault(pu => pu.promoUsage.PromotionId == promoGroup.PromotionId);

                //select all the duplicate records
                var promoUsageToDelete = promoUsages.Where(pu => pu.promoUsage.PromotionId == promoGroup.PromotionId && pu.promoUsage.Id != promoToSave.promoUsage.Id);
                //delete any duplicate free gift items
                var giftCarts = promoUsageToDelete.Where(pu => pu.promoLineItem.isAGift).Select(pu => pu.scItem).Where(sc => sc != null);
                db.ShoppingCarts.DeleteAllOnSubmit(giftCarts);
                //delete the duplicate promo usages
                db.PromotionUsages.DeleteAllOnSubmit(promoUsageToDelete.Select(pu => pu.promoUsage));
            }
            db.SubmitChanges();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method clears out any unused promotions for an order.  Unused promotions are an artifact of promotions that include
        /// shipping discounts.  They stay applied in order to support switching of shipping methods without the promotion falling
        /// off in the checkout process.
        /// </summary>
        /// <param name="orderNumber"></param>
        public static void RemoveUnusedPromotionsForOrder(int orderNumber)
        {
            PromotionsData.EntityContextDataContext db = PromotionsData.DataContextProvider.Current;

            var promoUsages = from pu in db.PromotionUsages
                              where pu.OrderId == orderNumber && pu.Complete && pu.DiscountAmount == decimal.Zero
                              select pu;

            db.PromotionUsages.DeleteAllOnSubmit(promoUsages);
            db.SubmitChanges();
        }
Ejemplo n.º 3
0
        public static void TransferPromotionsOnUserLogin(int currentCustomerId, int newCustomerId)
        {
            PromotionsData.EntityContextDataContext db = PromotionsData.DataContextProvider.Current;
            var promotionUsages = db.PromotionUsages.Where(pu => pu.CustomerId == currentCustomerId);

            foreach (PromotionsData.PromotionUsage promoUsage in promotionUsages)
            {
                promoUsage.CustomerId = newCustomerId;
            }

            db.SubmitChanges();
        }
Ejemplo n.º 4
0
        /// <summary>
        ///  Creates a new data context to the database of the specified version of adnsf.
        /// </summary>
        /// <param name="connectionString">Connection string to use when creating the context.</param>
        /// <returns>A new data context pointing to the specified adnsf database.</returns>
        /// <exception cref="ArgumentNullException" />
        /// /// <exception cref="InvalidOperationException" />
        public static DataContext CreateDataContext(String connectionString)
        {
            if (String.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("connectionString", "Connection string cannot be null or empty.");
            }

            DataContext dataContext = null;

            try
            {
                dataContext = new EntityContextDataContext(connectionString);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException("An error occurred trying to create a data context for an ADNSF8.0.1.2 database.", exception);
            }

            return(dataContext);
        }
Ejemplo n.º 5
0
        public static void ClearPromotionUsages(int customerId, string promotionCode, bool removeAutoAssigned)
        {
            PromotionsData.EntityContextDataContext db = PromotionsData.DataContextProvider.Current;

            var promotionUsages = from pu in db.PromotionUsages
                                  join p in db.Promotions on pu.PromotionId equals p.Id
                                  join pli in db.PromotionLineItems on pu.Id equals pli.PromotionUsageId into pui
                                  from pli in pui.DefaultIfEmpty()
                                  join sc in db.ShoppingCarts on pli.shoppingCartRecordId equals sc.ShoppingCartRecID into sci
                                  from sc in sci.DefaultIfEmpty()
                                  where pu.CustomerId == customerId && !pu.Complete

                                  select new
            {
                promoUsage      = pu
                , promo         = p
                , promoLineItem = pli
                , scItem        = sc
            };

            if (!string.IsNullOrEmpty(promotionCode))
            {
                promotionUsages = promotionUsages.Where(puli => puli.promo.Code == promotionCode);
            }

            var giftCarts = promotionUsages.Where(puli => puli.promoLineItem.isAGift).Select(puli => puli.scItem).Where(sc => sc != null);

            db.ShoppingCarts.DeleteAllOnSubmit(giftCarts);
            db.PromotionUsages.DeleteAllOnSubmit(promotionUsages.Select(puli => puli.promoUsage));

            //Only add auto assigned promos to the removed list if they are intentionally removed by user
            if (removeAutoAssigned)
            {
                foreach (var promoUsage in promotionUsages.Where(puli => puli.promo.AutoAssigned))
                {
                    PromotionManager.AddAutoAssignedPromotionToRemovedList(promoUsage.promoUsage.CustomerId, promoUsage.promo.Id);
                }
            }
            db.SubmitChanges();
        }
        public override IDiscountResult ApplyPromotion(IPromotionUsage promotionUsage, IRuleContext ruleContext, IDiscountContext discountContext, Func <IDiscountResult> resultFactory)
        {
            Data.EntityContextDataContext context = new Data.EntityContextDataContext();
            IDiscountResult       retVal          = resultFactory();
            Decimal               totalDiscount   = Decimal.Zero;
            List <DiscountedItem> discountedItems = new List <DiscountedItem>();
            Int32 quantity             = 1;
            Int32 contextQuantity      = discountContext.DiscountableItems.Sum(s => s.Quantity);
            Int32 shoppingCartRecordId = 0;

            if (MatchQuantities && contextQuantity > 0)
            {
                quantity = discountContext.DiscountableItems.Sum(s => s.Quantity);
            }

            foreach (Int32 productId in GiftProductIds)
            {
                Data.ShoppingCart cart = null;

                //Try to find an existing free gift in the promotionlineitem table for this promousage
                PromotionLineItem giftItem = context.PromotionLineItems.FirstOrDefault(pli => pli.productId == productId && pli.isAGift && pli.PromotionUsageId == promotionUsage.Id);

                //Try to grab the shopping cart item for the promolineitem
                if (giftItem != null)
                {
                    cart = context.ShoppingCarts.FirstOrDefault(sc => sc.ShoppingCartRecID == giftItem.shoppingCartRecordId);
                }

                //Add the free item to the shoppingcart if it doesn't already exist
                if (cart == null)
                {
                    int variantId = context.ProductVariants.FirstOrDefault(pv => pv.ProductID == productId && pv.IsDefault == 1).VariantID;
                    if (ruleContext.AddItemToCart != null)
                    {
                        shoppingCartRecordId = ruleContext.AddItemToCart(productId, variantId, quantity);
                    }
                    cart = context.ShoppingCarts.FirstOrDefault(sc => sc.ShoppingCartRecID == shoppingCartRecordId);
                }
                else
                {
                    //Make sure our quantities match up.
                    cart.Quantity = quantity;
                    context.SubmitChanges();
                }

                if (cart != null)
                {
                    DiscountedItem discountedItem = new DiscountedItem();

                    //We store the original price of the item in the promotionlineitem table, we want to use that original price if we already have a promo line item
                    discountedItem.CartPrice            = giftItem == null ? (decimal)cart.ProductPrice : giftItem.cartPrice;
                    discountedItem.IsAGift              = true;
                    discountedItem.ProductId            = productId;
                    discountedItem.Quantity             = cart.Quantity;
                    discountedItem.ShoppingCartRecordId = cart.ShoppingCartRecID;
                    discountedItem.Sku            = cart.ProductSKU;
                    discountedItem.Subtotal       = (decimal)cart.ProductPrice * cart.Quantity;
                    discountedItem.VariantId      = cart.VariantID;
                    discountedItem.PromotionUsage = promotionUsage;

                    decimal discount = -(discountedItem.CartPrice) * (GiftDiscountPercentage * .01m);
                    //Make sure our price won't go negative
                    discount = Math.Abs(discount) > discountedItem.CartPrice ? -(discountedItem.CartPrice) : discount;

                    //The discount is already baked into the cart price we will record the discount in gift amount instead
                    discountedItem.DiscountAmount = 0.0M;
                    discountedItem.GiftAmount     = discount * cart.Quantity;
                    totalDiscount += discountedItem.GiftAmount;
                    discountedItem.DiscountPercentage = GiftDiscountPercentage;

                    Data.ContextController.TrackLineItemDiscount(discountedItem);

                    discountedItems.Add(discountedItem);
                    cart.ProductPrice        = discountedItem.CartPrice + discount;
                    cart.IsGift              = true;
                    cart.CustomerEntersPrice = 1;
                    context.SubmitChanges();
                }
            }
            retVal.DiscountedItems  = discountedItems;
            retVal.DiscountType     = DiscountType.Fixed;
            retVal.GiftProductTotal = totalDiscount;
            retVal.SequenceType     = (PromotionSequence)SequenceNumber;

            return(retVal);
        }