//get singleton object
 public static PromotionStore GetStore()
 {
     if (_promotionStore == null)
     {
         _promotionStore            = new PromotionStore();
         _promotionStore.SKUItems   = new List <SKUItem>();
         _promotionStore.Promotions = new List <Promotion>();
     }
     return(_promotionStore);
 }
Exemple #2
0
        /// <summary>
        /// Processing of Promotions on cart ietms, and set the calculated Value to Total value property
        /// </summary>
        public void Process()
        {
            AppliedPromotions = new List <Promotion>();
            PromotionStore          promotionStore = PromotionStore.GetStore();
            List <Promotion>        lstPromotions  = promotionStore.GetPromotions();
            List <OrderSKUQuantity> tempOrderSKUs  = SKUs;

            TotalValue = 0;
            //llop through all promotions
            foreach (var item in lstPromotions)
            {
                decimal min = decimal.MaxValue;
                // Checking all Promotion SKUs are available for calculation or not by campring Qunatities of Order & Promotion, Status of OrderSKU(Promo Applied, Not applied)
                if (item.promotionSKUs.All(promoSKU => tempOrderSKUs.Exists(orderSku => !orderSku.PromoApplied && orderSku.Quantity > 0 &&
                                                                            orderSku.SKUId.ToLower() == promoSKU.SKUId.ToLower() && orderSku.Quantity >= promoSKU.Quantity)))
                {
                    // Counting the combinations of Promo SKU's in Order SKU's
                    foreach (var promoSKU in item.promotionSKUs)
                    {
                        min = Math.Min(min, tempOrderSKUs.Where(orderSku => orderSku.SKUId.ToLower() == promoSKU.SKUId.ToLower() && !orderSku.PromoApplied)
                                       .Min(orderSku => (int)orderSku.Quantity / (int)promoSKU.Quantity));
                    }
                    //Adding applied Promotion Values to Cart Value.
                    TotalValue += item.PromoValue * min;
                    //applying the unit Price of remained Quantity after Promotion is applied, set the PromoApplied flag to not to process same SKU, and also Quantity set to 0.
                    tempOrderSKUs.ForEach(orderSku =>
                    {
                        item.promotionSKUs.ForEach(promoSku =>
                        {
                            if (orderSku.SKUId.ToLower() == promoSku.SKUId.ToLower() && !orderSku.PromoApplied)
                            {
                                //TotalValue += promotionStore.GetSKUItem(orderSku.SKUId).UnitPrice * (orderSku.Quantity % (promoSku.Quantity * min));
                                orderSku.Quantity    -= promoSku.Quantity * min;
                                orderSku.PromoApplied = true;
                            }
                        });
                    });
                    AppliedPromotions.Add(item);
                }
            }
            //Applying the unit price of SKU's which have Quantity.
            tempOrderSKUs.ForEach(obj =>
            {
                if (obj.Quantity != 0)
                {
                    TotalValue += promotionStore.GetSKUItem(obj.SKUId).UnitPrice *obj.Quantity;
                }
            });
        }