public PromotionDiscount CreateFreeOfChargeDiscount(ProductRef parentProduct, Guid? freeOfChargeProduct, int parentProductQuantity, int? freeProductQuantity, DateTime effectiveDate, decimal DiscountRate, DateTime endDate)
  {
      
      if (parentProduct.ProductId == Guid.Empty)
          throw new ArgumentException("Invalid product");
      //if (freeOfChargeProduct.ProductId == 0)
      //    throw new ArgumentException("Invalid product");
      PromotionDiscount foc = new PromotionDiscount(Guid.NewGuid())
      {
          ProductRef = new ProductRef { ProductId=parentProduct.ProductId }
      };
      foc.PromotionDiscountItems.Add(new PromotionDiscount.PromotionDiscountItem(Guid.NewGuid())
      {
          ParentProductQuantity = parentProductQuantity,
          FreeOfChargeQuantity = freeProductQuantity.Value,
          FreeOfChargeProduct =freeOfChargeProduct==null ?null: new ProductRef { ProductId=freeOfChargeProduct.Value},
          EffectiveDate=effectiveDate,
          EndDate = endDate,
          DiscountRate=DiscountRate,
          _Status = EntityStatus.New, 
       });
      return foc;
  }
  private void AssertPromotionDiscount(PromotionDiscount promotionDiscountX, PromotionDiscount PromotionDiscountY)
  {
      var pdX = promotionDiscountX.PromotionDiscountItems.FirstOrDefault();
      var pdY = PromotionDiscountY.PromotionDiscountItems.FirstOrDefault();
      Assert.IsNotNull(promotionDiscountX);
      Assert.AreEqual(promotionDiscountX.Id.ToString(), PromotionDiscountY.Id.ToString());
      Assert.AreEqual(promotionDiscountX.ProductRef.ProductId.ToString(), PromotionDiscountY.ProductRef.ProductId.ToString());
      if (pdX != null && pdY != null)
      {
          Assert.AreEqual(pdX.EffectiveDate, pdY.EffectiveDate);
          Assert.AreEqual(pdX.DiscountRate, pdY.DiscountRate);
          Assert.AreEqual(promotionDiscountX._Status, PromotionDiscountY._Status); 
      }
         
 
  }
 PromotionDiscountViewModel Map(PromotionDiscount foc)
 {
     PromotionDiscountViewModel pdvm = new PromotionDiscountViewModel();
     var discountItem = foc.PromotionDiscountItems.FirstOrDefault();
     pdvm.EffectiveDate = discountItem != null ? discountItem.EffectiveDate : DateTime.Parse("01-jan-1900");
     pdvm.EndDate = discountItem != null ? discountItem.EndDate : DateTime.Parse("01-jan-1900");
     pdvm.FreeOfChargeProductQuantity = discountItem != null ? discountItem.FreeOfChargeQuantity : 0;
     pdvm.FreeProduct = (discountItem != null && discountItem.FreeOfChargeProduct != null)
         ? discountItem.FreeOfChargeProduct.ProductId : Guid.Empty;
     pdvm.Product = foc.ProductRef.ProductId;
     pdvm.ParentProductQuantity = discountItem != null ? discountItem.ParentProductQuantity : 0;
     var currentFreeOfChargeProduct = discountItem != null && discountItem.FreeOfChargeProduct != null
         ? discountItem.FreeOfChargeProduct : null;
     if (currentFreeOfChargeProduct != null)
     {
         if (discountItem.FreeOfChargeProduct.ProductId != Guid.Empty)
         {
             pdvm.FreeProductName = discountItem.FreeOfChargeProduct == null
                 ? "None" 
                 : _productRepository.GetById(discountItem.FreeOfChargeProduct.ProductId).Description;
         }
     }
     pdvm.ProductName = _productRepository.GetById(foc.ProductRef.ProductId).Description;
     pdvm.Id = foc.Id;
     pdvm.isActive = foc._Status == EntityStatus.Active ? true : false;
     pdvm.DiscountRate = discountItem != null ? discountItem.DiscountRate : 0;
     return pdvm;
 }
 public PromotionDiscountDTO Map(PromotionDiscount promotion)
 {
     if (promotion == null) return null;
     var item = new PromotionDiscountDTO()
                    {
                        DateCreated = promotion._DateCreated,
                        DateLastUpdated = promotion._DateLastUpdated,
                        StatusId = (int) promotion._Status,
                        MasterId = promotion.Id,
                        ProductMasterId = promotion.ProductRef.ProductId,
                        PromotionDiscountItems = promotion.PromotionDiscountItems
                            .Select(s => new PromotionDiscountItemDTO
                                             {
                                                 DateCreated = s._DateCreated,
                                                 DateLastUpdated = s._DateLastUpdated,
                                                 StatusId = (int) s._Status,
                                                 EffectiveDate = s.EffectiveDate,
                                                 EndDate = s.EndDate,
                                                 FreeQuantity = s.FreeOfChargeQuantity,
                                                 ParentQuantity = s.ParentProductQuantity,
                                                 MasterId = s.Id,
                                                 ProductMasterId =
                                                     s.FreeOfChargeProduct == null
                                                         ? Guid.Empty
                                                         : s.FreeOfChargeProduct.ProductId,
                                                 DiscountRate = s.DiscountRate
                                             }).ToList()
                    };
     return item;
 }
        private bool HasPromotionDiscountChanged(PromotionDiscount entity)
        {
           
                var repo = ObjectFactory.GetInstance<IPromotionDiscountRepository>();
                var promo = repo.GetById(entity.Id);
                if (promo == null) return true;
                if (promo.CurrentDiscountRate != entity.CurrentDiscountRate ||
                    promo.CurrentFreeOfChargeProduct != entity.CurrentFreeOfChargeProduct ||
                    promo.CurrentFreeOfChargeQuantity != entity.CurrentFreeOfChargeQuantity ||
                    promo.CurrentParentProductQuantity != entity.CurrentParentProductQuantity) return true;
            return false;

        }