Example #1
0
        public void CanMakeFiveCentsDiscountOnPepsi()
        {
            // arrange
            var pepsiFiveCentsOff = new AmountDiscount {
                AmountOff = 0.05m, Product = Pepsi
            };

            _order.AddDiscount(pepsiFiveCentsOff);
            _order.ApplyDiscounts();

            // act
            var total = _order.CalculateTotal();

            // assert
            Assert.Equal(0.95m, total);
        }
 public AmountDiscount GetAmountDiscount(DiscountType disType)
 {
     AmountDiscount ad;
     if (discountLine.OfType<AmountDiscount>().Any(p => p.DiscountType == disType))
     {
         ad = discountLine.OfType<AmountDiscount>().FirstOrDefault(p => p.DiscountType == disType);
     }
     else
     {
         ad = new AmountDiscount();
         ad.DiscountType = disType;
         discountLine.Add(ad);
     }
     return ad;
 }
        public List<DiscountBase> GetDiscountSummary(List<OrderDiscountLineItem> productItems, Guid outletId)
        {
            discountLine = new List<DiscountBase>();
            var outlet = _costCentreService.GetById(outletId) as Outlet;
            if(outlet==null)
                throw new ArgumentException("Invalid Outlet Id");
            if (outlet.OutletProductPricingTier == null)
                throw new ArgumentException("Invalid Outlet Pricing Tier");
            ProductPricingTier tier = _productPricingTierService.GetById(outlet.OutletProductPricingTier.Id);
         
            decimal totalAmount = productItems.Sum(s => s.TotalPrice);

            #region Salevalue Discount
            SaleValueDiscount salevalueDiscount = _SaleValueDiscountService.GetCurrentDiscount(totalAmount,tier.Id);
            if (salevalueDiscount!=null)
            {
                decimal rate = salevalueDiscount.CurrentRate;
                decimal sd = rate*totalAmount;
                AmountDiscount ad = new AmountDiscount
                {
                    DiscountAmount = sd,
                    DiscountType = DiscountType.SaleValueDiscount,
                };
                AddDiscount(ad);
            }
            #endregion
            
            #region Product Discount
            foreach (OrderDiscountLineItem item in productItems)
            {
                try
                {
                    ProductDiscount pd = _productDiscount.GetProductDiscount(item.ProductId, tier.Id);
                    decimal rate = pd.CurrentDiscountRate(item.Quantity);
                    decimal issuedPd = rate * item.TotalPrice;
                    AmountDiscount ad = new AmountDiscount
                                            {
                                                DiscountAmount = issuedPd,
                                                DiscountType = DiscountType.ProductDiscount,
                                            };
                    AddDiscount(ad);
                }
                catch
                {
                }
            }
            #endregion

            #region Customer Discount
            if (outlet.DiscountGroup!=null)
            {
                foreach (OrderDiscountLineItem item in productItems)
                {
                    ProductGroupDiscount pgd = _productGroupDiscountService.GetCurrentCustomerDiscount(outlet.DiscountGroup.Id,item.ProductId,item.Quantity);
                    if (pgd != null)
                    {
                        decimal rate = pgd.CurrentDiscount();
                        decimal issuedPd = rate*item.TotalPrice;
                        if (issuedPd > 0)
                        {
                            AmountDiscount ad = new AmountDiscount
                            {
                                DiscountAmount = issuedPd,
                                DiscountType = DiscountType.GroupDiscount,
                            };
                            AddDiscount(ad);
                        }
                    }
                }

            }
            #endregion 

            #region Promotion
            foreach (OrderDiscountLineItem item in productItems)
            {
                PromotionDiscount pd = _promotionDiscountService.GetAll()
                    .Where(p => p.ProductRef.ProductId == item.ProductId).FirstOrDefault();
                if (pd != null)
                {
                    try
                    {
                        decimal rate = pd.CurrentDiscountRate;
                        decimal issuedPd = rate*item.TotalPrice;
                        if (issuedPd > 0)
                        {
                            AmountDiscount ad = new AmountDiscount
                            {
                                DiscountAmount = issuedPd,
                                DiscountType = DiscountType.PromotionDiscount,
                            };
                            AddDiscount(ad);
                        }
                        ProductRef pRef = pd.CurrentFreeOfChargeProduct;
                        decimal freequantity =  pd.CurrentFreeOfChargeQuantity;
                        if(pRef!= null && pRef.ProductId!=Guid.Empty)
                        {
                            ProductAsDiscount ad = new ProductAsDiscount
                            {
                                ProductId = pRef.ProductId,
                                DiscountType = DiscountType.PromotionDiscount,
                                Quantity = freequantity
                            };
                            AddDiscount(ad);
                        }
                       
                    }catch(Exception)
                    {
                    }
                }
            }
            #endregion

            #region Free of charge
            foreach (OrderDiscountLineItem item in productItems)
            {
                if(_freeOfChargeDiscountService.IsProductFreeOfCharge(item.ProductId))
                {
                    AmountDiscount ad = new AmountDiscount
                    {
                        DiscountAmount = item.TotalPrice,
                        DiscountType = DiscountType.FreeOfChargeDiscount,
                    };
                    AddDiscount(ad);
                }
            }

            #endregion

            #region Certain Value Certain Product
            CertainValueCertainProductDiscount certainvalueDiscount = _certainValueCertainProduct.GetByAmount(totalAmount);
            if (certainvalueDiscount != null)
            {
                try
                {
                    ProductRef certainProduct = certainvalueDiscount.CurrentProduct;
                    decimal certainProductQty = certainvalueDiscount.CurrentQuantity;
                    ProductAsDiscount ad = new ProductAsDiscount
                                               {
                                                   ProductId = certainProduct.ProductId,
                                                   DiscountType = DiscountType.CertainValueCertainProductDiscount,
                                                   Quantity = certainProductQty
                                               };
                    AddDiscount(ad);
                }catch(Exception)
                {
                    
                }

            }
            #endregion


            return discountLine;
        }