Пример #1
0
        /// <summary>
        /// Gets a list of all products
        /// </summary>
        /// <returns>Returns all products and any active promotions</returns>
        public List<ProductDTO> GetProducts()
        {
            var result = db.Products.OrderBy(p => p.Description);

            List<ProductDTO> productsDTO = new List<ProductDTO>();

            foreach(Product product in result.ToList())
            {
                // get the product DTO, and only return promotions that have not expired
                ProductDTO productDTO = new ProductDTO
                {
                    Code = product.Code,
                    Description = product.Description,
                    ProductTypeCode = product.ProductTypeCode,
                    Price = product.Price,
                    Promotions = ConvertPromotionList(product.Promotions.Where(p => p.EndDate == null).ToList())
                };

                productsDTO.Add(productDTO);
            }

            return productsDTO;
        }
        private decimal GetPrice(ProductDTO product, ref int productItemCount, ref bool promotionApplied, OrderDTO order)
        {
            // set the price (including a discount)

            decimal price = product.Price;

            if (product.Promotions.Count > 0)
            {
                // assume the service only return 1 active promotion
                PromotionDTO promotion = product.Promotions.First();

                if (promotion.PromotionCode == Constants.PromotionType.Discount)
                {
                    // we reset the productItemCount once the promotion reaches the max apply to count
                    if (promotion.ApplyTo != null && productItemCount > promotion.Quantity + promotion.ApplyTo)
                        productItemCount = 1;

                    // set the price by the percentage discount
                    if (productItemCount > promotion.Quantity)
                    {
                        price = price * promotion.Amount;
                        promotionApplied = true;
                    }
                }
                else if (promotion.PromotionCode == Constants.PromotionType.Price && productItemCount == promotion.Quantity)
                {
                    // for a price by a number of items, we set the price on the final item in the group, and set
                    // all other items in the group to a price of 0

                    for (int i = order.OrderItems.Count - 1; i >= order.OrderItems.Count - promotion.Quantity + 1; i--)
                    {
                        OrderItemDTO orderItemPriceAdjustment = order.OrderItems.ToList()[i];
                        orderItemPriceAdjustment.Price = 0;
                        orderItemPriceAdjustment.PromotionApplied = true;
                    }

                    price = promotion.Amount;
                    productItemCount = 0;
                    promotionApplied = true;
                }

            }

            return price;
        }