public ActionResult Edit(int id)
        {
            #region Edit Props

            var vm              = new DiscountFormViewModel();
            var discountGroup   = _repo.GetDiscountGroup(id);
            var groupIdentifier = discountGroup.FirstOrDefault().GroupIdentifier;
            vm.PreviousDiscounts = discountGroup;
            vm.GroupIdentifier   = groupIdentifier;
            vm.Title             = discountGroup.FirstOrDefault().Title;
            vm.OfferId           = discountGroup.FirstOrDefault().OfferId;
            vm.DiscountType      = discountGroup.FirstOrDefault().DiscountType;
            vm.Amount            = discountGroup.FirstOrDefault().Amount;

            #endregion

            ViewBag.Offers        = _offerRepo.GetAll();
            ViewBag.Brands        = _brandRepo.GetAll();
            ViewBag.ProductGroups = _productGroupRepo.GetAll();
            ViewBag.Products      = _productRepo.GetAll();
            return(View(vm));
        }
        public ActionResult Create(DiscountFormViewModel newDiscount)
        {
            if (ModelState.IsValid)
            {
                var groupIdentifier = Guid.NewGuid().ToString();
                #region Adding Brands Discounts
                if (newDiscount.BrandIds != null)
                {
                    foreach (var item in newDiscount.BrandIds)
                    {
                        var discount = new Discount()
                        {
                            DiscountType    = newDiscount.DiscountType,
                            Amount          = newDiscount.Amount,
                            OfferId         = newDiscount.OfferId,
                            Title           = newDiscount.Title,
                            BrandId         = item,
                            GroupIdentifier = groupIdentifier
                        };
                        _repo.Add(discount);
                    }
                }
                #endregion
                #region Adding ProductGroups Discounts
                if (newDiscount.ProductGroupIds != null)
                {
                    foreach (var item in newDiscount.ProductGroupIds)
                    {
                        var discount = new Discount()
                        {
                            DiscountType    = newDiscount.DiscountType,
                            Amount          = newDiscount.Amount,
                            OfferId         = newDiscount.OfferId,
                            Title           = newDiscount.Title,
                            ProductGroupId  = item,
                            GroupIdentifier = groupIdentifier
                        };
                        _repo.Add(discount);
                    }
                }
                #endregion

                #region Adding Products Discounts
                if (newDiscount.ProductIds != null)
                {
                    foreach (var item in newDiscount.ProductIds)
                    {
                        var discount = new Discount()
                        {
                            DiscountType    = newDiscount.DiscountType,
                            Amount          = newDiscount.Amount,
                            OfferId         = newDiscount.OfferId,
                            Title           = newDiscount.Title,
                            ProductId       = item,
                            GroupIdentifier = groupIdentifier
                        };
                        _repo.Add(discount);
                    }
                }
                #endregion

                return(RedirectToAction("Index"));
            }
            ViewBag.Offers        = _offerRepo.GetAll();
            ViewBag.Brands        = _brandRepo.GetAll();
            ViewBag.ProductGroups = _productGroupRepo.GetAll();
            ViewBag.Products      = _productRepo.GetAll();
            return(View());
        }
        public string ValidateDuplicateDiscount(DiscountFormViewModel newDiscount, string groupIdentifier = null)
        {
            var discounts = _repo.GetAll();

            #region Check for Duplicate Brands
            if (newDiscount.BrandIds != null)
            {
                foreach (var item in newDiscount.BrandIds)
                {
                    if (discounts.Any(d => d.BrandId == item))
                    {
                        var brandName    = _brandRepo.GetAll().FirstOrDefault(b => b.Id == item).Name;
                        var discountName = discounts.FirstOrDefault(d => d.BrandId == item).Title;
                        if (groupIdentifier != null)
                        {
                            if (discounts.FirstOrDefault(d => d.BrandId == item).GroupIdentifier != groupIdentifier)
                            {
                                return($"برای برند {brandName} قبلا تخفیف ثبت شده ( {discountName} )");
                            }
                        }
                        else
                        {
                            return($"برای برند {brandName} قبلا تخفیف ثبت شده ( {discountName} )");
                        }
                    }
                }
            }
            #endregion
            #region Check for Duplicate ProductGroups
            if (newDiscount.ProductGroupIds != null)
            {
                foreach (var item in newDiscount.ProductGroupIds)
                {
                    if (discounts.Any(d => d.ProductGroupId == item))
                    {
                        var productGroupName = _productGroupRepo.GetAll().FirstOrDefault(b => b.Id == item).Title;
                        var discountName     = discounts.FirstOrDefault(d => d.ProductGroupId == item).Title;
                        if (groupIdentifier != null)
                        {
                            if (discounts.FirstOrDefault(d => d.ProductGroupId == item).GroupIdentifier != groupIdentifier)
                            {
                                return($"برای دسته {productGroupName} قبلا تخفیف ثبت شده ( {discountName} )");
                            }
                        }
                        else
                        {
                            return($"برای دسته {productGroupName} قبلا تخفیف ثبت شده ( {discountName} )");
                        }
                    }
                }
            }
            #endregion

            #region Check for Duplicate Products
            if (newDiscount.ProductIds != null)
            {
                foreach (var item in newDiscount.ProductIds)
                {
                    if (discounts.Any(d => d.ProductId == item))
                    {
                        var productName  = _productRepo.GetAll().FirstOrDefault(b => b.Id == item).Title;
                        var discountName = discounts.FirstOrDefault(d => d.ProductId == item).Title;
                        if (groupIdentifier != null)
                        {
                            if (discounts.FirstOrDefault(d => d.ProductId == item).GroupIdentifier != groupIdentifier)
                            {
                                return($"برای محصول {productName} قبلا تخفیف ثبت شده ( {discountName} )");
                            }
                        }
                        else
                        {
                            return($"برای محصول {productName} قبلا تخفیف ثبت شده ( {discountName} )");
                        }
                    }
                }
            }
            #endregion
            return("valid");
        }