private void AssertproductDiscountGroup(ProductGroupDiscount productDiscountGroupX ,ProductGroupDiscount productDiscountGroupY)
 {
     Assert.IsNotNull(productDiscountGroupX);
     Assert.AreEqual(productDiscountGroupX.EndDate, productDiscountGroupY.EndDate);
     Assert.AreEqual(productDiscountGroupX.DiscountRate, productDiscountGroupY.DiscountRate);
     Assert.AreEqual(productDiscountGroupX.EffectiveDate, productDiscountGroupY.EffectiveDate);
     Assert.AreEqual(productDiscountGroupX._Status, EntityStatus.Active);
 }
        public ProductGroupDiscount CreateProductGroupDiscount(DiscountGroup DiscountGroup,ProductRef ProductRef, decimal discountRate, DateTime effectiveDate, DateTime endDate, bool isQuantity,decimal quantity)
       {
           
           if (ProductRef.ProductId == Guid.Empty)
               throw new ArgumentException("Invalid product");
           if (DiscountGroup.Id == Guid.Empty)
               throw new ArgumentException("Invalid outlet");
           ProductGroupDiscount pgd = new ProductGroupDiscount(Guid.NewGuid())
           {
               GroupDiscount = new DiscountGroup (DiscountGroup.Id)
           };

            pgd.Product = new ProductRef {ProductId = ProductRef.ProductId};
            pgd.DiscountRate = discountRate;
            pgd.EffectiveDate = effectiveDate;
            pgd.EndDate = endDate;
            pgd.IsByQuantity = isQuantity;
            pgd.Quantity = quantity;
            pgd._Status = EntityStatus.New;
       
           return pgd;
        }
 public ProductGroupDiscountDTO Map(ProductGroupDiscount productGroupDiscount)
 {
     if (productGroupDiscount == null) return null;
     if (productGroupDiscount.GroupDiscount == null) return null;
     var item = new ProductGroupDiscountDTO()
                    {
                        DateCreated = productGroupDiscount._DateCreated,
                        DateLastUpdated = productGroupDiscount._DateLastUpdated,
                        StatusId = (int) productGroupDiscount._Status,
                        DiscountGroupMasterId = productGroupDiscount.GroupDiscount.Id,
                        MasterId = productGroupDiscount.Id,
                       
                                                 
                                                 DiscountRate = productGroupDiscount.DiscountRate,
                                                 EffectiveDate = productGroupDiscount.EffectiveDate,
                                                 EndDate = productGroupDiscount.EndDate,
                                                
                                                 ProductMasterId =productGroupDiscount.Product.ProductId
                           
                    };
     return item;
 }
       public ProductGroupDiscount MapProductGroupDiscount(ProductGroupDiscountDTO dto)
       {
           if (dto == null) return null;
           var productGroupDiscount = new ProductGroupDiscount(dto.MasterId)
                                          {
                                              GroupDiscount =
                                                  _discountGroupRepository.GetById(dto.DiscountGroupMasterId),
                                              _DateCreated = dto.DateCreated,
                                              _DateLastUpdated = dto.DateLastUpdated,
                                              _Status = (EntityStatus) dto.StatusId,
                                          };


           productGroupDiscount.DiscountRate = dto.DiscountRate;
           productGroupDiscount.EffectiveDate = dto.EffectiveDate;
           productGroupDiscount.EndDate = dto.EndDate;
           productGroupDiscount.Product = new ProductRef() {ProductId = dto.ProductMasterId};
                          
        
           return productGroupDiscount;
       }
 protected ValidationResultInfo Validate(ProductGroupDiscount productGroupDiscount)
 {
     return _productDiscountGroupRepository.Validate(productGroupDiscount);
 }
        protected ProductGroupDiscount Map(ProductDiscountGroupItemImport productDiscountGroupItemImport, List<string> mappingvalidationList)
        {
            var discountGroup = _discountGroupRepository.GetByCode(productDiscountGroupItemImport.ProductDiscountGroupCode);
            if (discountGroup==null)
            {
                mappingvalidationList.Add(string.Format((string) "Invalid Discount Group Code {0}", (object) productDiscountGroupItemImport.ProductDiscountGroupCode));
                return null;
            }
            if (!_productList.ContainsValue(productDiscountGroupItemImport.ProductCode.ToLower()))
            {
                mappingvalidationList.Add(string.Format((string) "Invalid Product Code {0}", (object) productDiscountGroupItemImport.ProductCode));
                return null;
            }
            var productId =_productList.FirstOrDefault(k => k.Value.ToLower() == productDiscountGroupItemImport.ProductCode.ToLower()).Key;


            tblProductDiscountGroup exists = Queryable.FirstOrDefault(_context.tblProductDiscountGroup, n => n.DiscountGroup == discountGroup.Id && n.Quantity == productDiscountGroupItemImport.Quantity && n.ProductRef == productId);


            Guid id = exists != null ? exists.id : Guid.NewGuid();

            var productGroupDiscount = new ProductGroupDiscount(id);

            productGroupDiscount.GroupDiscount = discountGroup; //Check this out
            productGroupDiscount.DiscountRate = productDiscountGroupItemImport.Rate;
            productGroupDiscount.EffectiveDate = productDiscountGroupItemImport.EffectiveDate;
            productGroupDiscount.EndDate = productDiscountGroupItemImport.EndDate;
            productGroupDiscount.Quantity = productDiscountGroupItemImport.Quantity;
            productGroupDiscount.Product = new ProductRef() { ProductId = productId };
            productGroupDiscount.IsByQuantity = productDiscountGroupItemImport.Quantity > 0;
            return productGroupDiscount;


        }
        ProductDiscountGroupViewModel Map(ProductGroupDiscount pgd)
        {
            var pd = _productDiscountGroupRepository.GetById(pgd.Id);

            var product = _productRepository.GetById(pd.Product.ProductId);
            if (product == null) return new ProductDiscountGroupViewModel();
            return new ProductDiscountGroupViewModel
                       {
                           EffectiveDate = pd.EffectiveDate,
                           EndDate = pd.EndDate,
                           discountRate = pd.DiscountRate,
                           DiscountGroup = pgd.GroupDiscount.Id,
                           DiscountGroupName = pd.GroupDiscount != null ? pd.GroupDiscount.Name : "",
                           Id = pgd.Id,
                           isActive = pgd._Status == EntityStatus.Active ? true : false,
                           Product = pd.Product.ProductId,
                           ProductName = product.Description,
                           Quantity = pd.Quantity,
                       };
        }
        private bool HasChanged(ProductGroupDiscount groupD)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                var dGroup = ctx.tblProductDiscountGroup.FirstOrDefault(p => p.id == groupD.Id);
                if (dGroup == null) return true;
                var productId = groupD.Product.ProductId;
                var discountValue = groupD.DiscountRate;

                var item = dGroup.tblProductDiscountGroupItem.OrderBy(p=>p.IM_DateLastUpdated).FirstOrDefault(p => p.ProductRef == productId);

                return item == null || item.DiscountRate.ToString("0.00") != discountValue.ToString("0.00");
            }
        }