Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OfferItem"/> class for the specified product.
        /// </summary>
        /// <param name="draftItem"><see cref="OfferDraftItem"/> from which the offer item is created.</param>
        public OfferItem(OfferDraftItem draftItem)
        {
            if (draftItem == null)
                throw new ArgumentNullException("draftItem");

            _product = draftItem.Product;
            _quantity = draftItem.Quantity;
            _discount = draftItem.Discount;
            _productPrice = _product.Price;
            _discountAmount = draftItem.DiscountAmount;
            _itemTotal = draftItem.ItemTotal;
            _itemSubtotal = draftItem.ItemSubtotal;
        }
Example #2
0
 /// <summary>
 /// Adds a new item to the offer.
 /// </summary>
 /// <param name="item">An item to add to the offer.</param>
 public virtual void AddItem(OfferDraftItem item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     if (item.OfferDraft != null)
     {
         throw new ArgumentException("Item is already contained in a draft.");
     }
     item.OfferDraft = this;
     _items.Add(item);
 }
Example #3
0
        /// <summary>
        /// Removes the specified item from the offer.
        /// </summary>
        /// <param name="item">An item to remove from the offer.</param>
        public virtual void RemoveItem(OfferDraftItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (!Equals(item.OfferDraft, this))
            {
                throw new ArgumentException("Item is not part of current draft.", "item");
            }

            item.OfferDraft = null;
            _items.Remove(item);
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OfferItem"/> class for the specified product.
        /// </summary>
        /// <param name="draftItem"><see cref="OfferDraftItem"/> from which the offer item is created.</param>
        public OfferItem(OfferDraftItem draftItem)
        {
            if (draftItem == null)
            {
                throw new ArgumentNullException("draftItem");
            }

            _product = draftItem.Product;

            _quantity       = draftItem.Quantity;
            _discount       = draftItem.Discount;
            _productPrice   = _product.Price;
            _discountAmount = draftItem.DiscountAmount;
            _itemTotal      = draftItem.ItemTotal;
            _itemSubtotal   = draftItem.ItemSubtotal;
        }
Example #5
0
        /// <summary>
        /// Creates a new draft from given offer.
        /// </summary>
        /// <returns>A new <see cref="OfferDraft"/> instance.</returns>
        public static OfferDraft CreateFromOffer(Offer sourceOffer)
        {
            if (sourceOffer == null)
            {
                throw new ArgumentNullException("sourceOffer");
            }

            var draft = new OfferDraft(sourceOffer);

            foreach (var item in sourceOffer.Items)
            {
                var draftItem = new OfferDraftItem(item.Product, item.Quantity);
                draft.AddItem(draftItem);
                draft.ChangeItemDiscount(draftItem, item.Discount);
            }

            return(draft);
        }
Example #6
0
        /// <summary>
        /// Changes discount for given item.
        /// </summary>
        /// <param name="item">Item for which to change discount.</param>
        /// <param name="discount">Discount for the item.</param>
        public virtual void ChangeItemDiscount(OfferDraftItem item, int discount)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (!Items.Contains(item))
            {
                throw new ArgumentException("Item is not part of current draft.", "item");
            }

            // Dummy business rule - at most 5 items can have an additional discount.
            if ((discount > 0) && (item.Discount == 0))
            {
                var itemsWithDiscount = Items.Count(x => x.Discount > 0);
                if (itemsWithDiscount >= 5)
                {
                    throw new InvalidOperationException("At most 5 items can have a special discount.");
                }
            }
            item.Discount = discount;
        }
Example #7
0
        /// <summary>
        /// Removes the specified item from the offer.
        /// </summary>
        /// <param name="item">An item to remove from the offer.</param>
        public virtual void RemoveItem(OfferDraftItem item)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (!Equals(item.OfferDraft, this))
                throw new ArgumentException("Item is not part of current draft.", "item");

            item.OfferDraft = null;
            _items.Remove(item);
        }
Example #8
0
        /// <summary>
        /// Changes discount for given item.
        /// </summary>
        /// <param name="item">Item for which to change discount.</param>
        /// <param name="discount">Discount for the item.</param>
        public virtual void ChangeItemDiscount(OfferDraftItem item, int discount)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (!Items.Contains(item))
                throw new ArgumentException("Item is not part of current draft.", "item");

            // Dummy business rule - at most 5 items can have an additional discount.
            if ((discount > 0) && (item.Discount == 0))
            {
                var itemsWithDiscount = Items.Count(x => x.Discount > 0);
                if (itemsWithDiscount >= 5)
                    throw new InvalidOperationException("At most 5 items can have a special discount.");
            }
            item.Discount = discount;
        }
Example #9
0
 /// <summary>
 /// Adds a new item to the offer.
 /// </summary>
 /// <param name="item">An item to add to the offer.</param>
 public virtual void AddItem(OfferDraftItem item)
 {
     if (item == null)
         throw new ArgumentNullException("item");
     if (item.OfferDraft != null)
         throw new ArgumentException("Item is already contained in a draft.");
     item.OfferDraft = this;
     _items.Add(item);
 }
Example #10
0
        /// <summary>
        /// Creates a new draft from given offer.
        /// </summary>
        /// <returns>A new <see cref="OfferDraft"/> instance.</returns>
        public static OfferDraft CreateFromOffer(Offer sourceOffer)
        {
            if (sourceOffer == null)
                throw new ArgumentNullException("sourceOffer");

            var draft = new OfferDraft(sourceOffer);
            foreach (var item in sourceOffer.Items)
            {
                var draftItem = new OfferDraftItem(item.Product, item.Quantity);
                draft.AddItem(draftItem);
                draft.ChangeItemDiscount(draftItem, item.Discount);
            }

            return draft;
        }